2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.repository;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.UUID;
31 import java.util.stream.Collectors;
32 import javax.persistence.EntityManager;
33 import javax.persistence.PersistenceContext;
34 import lombok.AllArgsConstructor;
35 import lombok.extern.slf4j.Slf4j;
36 import org.springframework.stereotype.Component;
37 import org.springframework.transaction.annotation.Transactional;
43 public class TempTableCreator {
46 private EntityManager entityManager;
49 * Create a uniquely named temporary table.
51 * @param prefix prefix for the table name (so you can recognize it)
52 * @param sqlData data to insert (strings only) the inner List present a row of data
53 * @param columnNames column names (in same order as data in rows in sqlData)
54 * @return a unique temporary table name with given prefix
56 public String createTemporaryTable(final String prefix,
57 final Collection<List<String>> sqlData,
58 final String... columnNames) {
59 final String tempTableName = prefix + UUID.randomUUID().toString().replace("-", "");
60 final StringBuilder sqlStringBuilder = new StringBuilder("CREATE TEMPORARY TABLE ");
61 sqlStringBuilder.append(tempTableName);
62 defineColumns(sqlStringBuilder, columnNames);
63 insertData(sqlStringBuilder, tempTableName, columnNames, sqlData);
64 entityManager.createNativeQuery(sqlStringBuilder.toString()).executeUpdate();
69 * Create a uniquely named temporary table with a single column.
71 * @param prefix prefix for the table name (so you can recognize it)
72 * @param sqlData data to insert (strings only); each entry is a single row of data
73 * @param columnName column name
74 * @return a unique temporary table name with given prefix
76 public String createTemporaryTable(final String prefix,
77 final Collection<String> sqlData,
78 final String columnName) {
79 final Collection<List<String>> tableData = new ArrayList<>(sqlData.size());
80 for (final String entry : sqlData) {
81 tableData.add(Collections.singletonList(entry));
83 return createTemporaryTable(prefix, tableData, columnName);
86 private static void defineColumns(final StringBuilder sqlStringBuilder, final String[] columnNames) {
87 sqlStringBuilder.append('(');
88 final Iterator<String> it = Arrays.stream(columnNames).iterator();
89 while (it.hasNext()) {
90 final String columnName = it.next();
91 sqlStringBuilder.append(" ");
92 sqlStringBuilder.append(columnName);
93 sqlStringBuilder.append(" varchar NOT NULL");
95 sqlStringBuilder.append(",");
98 sqlStringBuilder.append(");");
101 private static void insertData(final StringBuilder sqlStringBuilder,
102 final String tempTableName,
103 final String[] columnNames,
104 final Collection<List<String>> sqlData) {
105 final Collection<String> sqlInserts = new HashSet<>(sqlData.size());
106 for (final Collection<String> rowValues : sqlData) {
107 final Collection<String> escapedValues =
108 rowValues.stream().map(it -> escapeSingleQuotesByDoublingThem(it)).collect(Collectors.toList());
109 sqlInserts.add("('" + String.join("','", escapedValues) + "')");
111 sqlStringBuilder.append("INSERT INTO ");
112 sqlStringBuilder.append(tempTableName);
113 sqlStringBuilder.append(" (");
114 sqlStringBuilder.append(String.join(",", columnNames));
115 sqlStringBuilder.append(") VALUES ");
116 sqlStringBuilder.append(String.join(",", sqlInserts));
117 sqlStringBuilder.append(";");
120 private static String escapeSingleQuotesByDoublingThem(final String value) {
121 return value.replace("'", "''");