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