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.Arrays;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.UUID;
29 import java.util.stream.Collectors;
30 import javax.persistence.EntityManager;
31 import javax.persistence.PersistenceContext;
32 import lombok.AllArgsConstructor;
33 import lombok.extern.slf4j.Slf4j;
34 import org.springframework.stereotype.Component;
35 import org.springframework.transaction.annotation.Transactional;
41 public class TempTableCreator {
44 private EntityManager entityManager;
47 * Create a uniquely named temporary table.
49 * @param prefix prefix for the table name (so you can recognize it)
50 * @param sqlData data to insert (strings only) the inner List present a row of data
51 * @param columnNames column names (in same order as data in rows in sqlData)
52 * @return a unique temporary table name with given prefix
54 public String createTemporaryTable(final String prefix,
55 final Collection<List<String>> sqlData,
56 final String... columnNames) {
57 final String tempTableName = prefix + UUID.randomUUID().toString().replace("-", "");
58 final StringBuilder sqlStringBuilder = new StringBuilder("CREATE TEMPORARY TABLE ");
59 sqlStringBuilder.append(tempTableName);
60 defineColumns(sqlStringBuilder, columnNames);
61 sqlStringBuilder.append(" ON COMMIT DROP;");
62 insertData(sqlStringBuilder, tempTableName, columnNames, sqlData);
63 entityManager.createNativeQuery(sqlStringBuilder.toString()).executeUpdate();
67 private static void defineColumns(final StringBuilder sqlStringBuilder, final String[] columnNames) {
68 sqlStringBuilder.append('(');
69 final Iterator<String> it = Arrays.stream(columnNames).iterator();
70 while (it.hasNext()) {
71 final String columnName = it.next();
72 sqlStringBuilder.append(" ");
73 sqlStringBuilder.append(columnName);
74 sqlStringBuilder.append(" varchar NOT NULL");
76 sqlStringBuilder.append(",");
79 sqlStringBuilder.append(")");
82 private static void insertData(final StringBuilder sqlStringBuilder,
83 final String tempTableName,
84 final String[] columnNames,
85 final Collection<List<String>> sqlData) {
86 final Collection<String> sqlInserts = new HashSet<>(sqlData.size());
87 for (final Collection<String> rowValues : sqlData) {
88 final Collection<String> escapedValues =
89 rowValues.stream().map(it -> escapeSingleQuotesByDoublingThem(it)).collect(Collectors.toList());
90 sqlInserts.add("('" + String.join("','", escapedValues) + "')");
92 sqlStringBuilder.append("INSERT INTO ");
93 sqlStringBuilder.append(tempTableName);
94 sqlStringBuilder.append(" (");
95 sqlStringBuilder.append(String.join(",", columnNames));
96 sqlStringBuilder.append(") VALUES ");
97 sqlStringBuilder.append(String.join(",", sqlInserts));
98 sqlStringBuilder.append(";");
101 private static String escapeSingleQuotesByDoublingThem(final String value) {
102 return value.replace("'", "''");