Revize c20f287b
Přidáno uživatelem Michal Linha před téměř 4 roky(ů)
src/main/java/vldc/aswi/service/SqlQueryManagerImpl.java | ||
---|---|---|
4 | 4 |
import org.springframework.beans.factory.annotation.Autowired; |
5 | 5 |
import org.springframework.stereotype.Service; |
6 | 6 |
import vldc.aswi.database.DatabaseInterface; |
7 |
import vldc.aswi.domain.Assembly; |
|
8 | 7 |
import vldc.aswi.domain.Configuration; |
9 | 8 |
import vldc.aswi.domain.Function; |
10 | 9 |
import vldc.aswi.domain.parameter.ParameterInConfiguration; |
... | ... | |
14 | 13 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
15 | 14 |
import vldc.aswi.utils.Converter; |
16 | 15 |
|
17 |
import javax.sql.DataSource; |
|
18 | 16 |
import java.util.*; |
19 | 17 |
import java.util.concurrent.TimeUnit; |
20 | 18 |
|
... | ... | |
23 | 21 |
*/ |
24 | 22 |
@Service |
25 | 23 |
@Slf4j |
26 |
public class SqlQueryManagerImpl implements SqlQueryManager{ |
|
24 |
public class SqlQueryManagerImpl implements SqlQueryManager {
|
|
27 | 25 |
|
28 |
/** Autowired databaseInterface component. */ |
|
26 |
/** |
|
27 |
* Autowired databaseInterface component. |
|
28 |
*/ |
|
29 | 29 |
@Autowired |
30 | 30 |
private DatabaseInterface databaseInterface; |
31 | 31 |
|
32 | 32 |
/** |
33 | 33 |
* Get list of contingencyTableRow. |
34 |
* |
|
34 | 35 |
* @param configuration - configuration from which table will be created. |
35 | 36 |
* @return List of contingencyTableRow. |
36 | 37 |
*/ |
37 | 38 |
@Override |
38 | 39 |
public List<ContingencyTableRow> getContingencyTableRow(Configuration configuration) { |
39 | 40 |
long startTime = System.nanoTime(); |
41 |
|
|
40 | 42 |
Map<String, TableColumn> tableColumns = this.databaseInterface.executeQueryAndReturnTableColumns( |
41 |
configuration.getAssembly().getSQLQuery()
|
|
43 |
generateFullSQLQuery(configuration.getAssembly().getSQLQuery(), configuration.getParametersInConfiguration())
|
|
42 | 44 |
); |
45 |
|
|
43 | 46 |
long stopTime = System.nanoTime(); |
44 | 47 |
System.out.println("Select:" + TimeUnit.MILLISECONDS.convert((stopTime - startTime), TimeUnit.NANOSECONDS)); |
45 | 48 |
List<NameUserName> rowNames = new ArrayList<>(); |
... | ... | |
49 | 52 |
// sort parameters in configuration to have them in order set by user |
50 | 53 |
Collections.sort(configuration.getParametersInConfiguration()); |
51 | 54 |
|
52 |
// TODO: 28.05.2020 map would be better |
|
53 |
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) { |
|
54 |
if (parameterInConfiguration.getLocation() != null && parameterInConfiguration.getLocation().getName().equals("Řádek")) { |
|
55 |
rowNames.add(new NameUserName(parameterInConfiguration.getParameter().getNameOfSelect().toUpperCase(), |
|
56 |
parameterInConfiguration.getColumnName())); |
|
57 |
} |
|
58 |
else if (parameterInConfiguration.getLocation() != null && parameterInConfiguration.getLocation().getName().equals("Sloupec")) { |
|
59 |
columnNames.add(new NameUserName(parameterInConfiguration.getParameter().getNameOfSelect().toUpperCase(), |
|
60 |
parameterInConfiguration.getColumnName())); |
|
61 |
} |
|
62 |
else if(parameterInConfiguration.getLocation() != null && parameterInConfiguration.getLocation().getName().equals("Hodnota")) { |
|
63 |
for (Function function : parameterInConfiguration.getFunctions()) { |
|
64 |
valueFunctions.add(new ValueFunction(parameterInConfiguration.getParameter().getNameOfSelect().toUpperCase(), |
|
65 |
function.getName().toUpperCase())); |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
55 |
initializeContingencyTableDataLists(rowNames, columnNames, valueFunctions, configuration.getParametersInConfiguration()); |
|
69 | 56 |
|
70 |
Converter converter = new Converter(); |
|
71 |
return converter.convertTableColumnsToContingencyTableRows(tableColumns, rowNames, columnNames, valueFunctions);
|
|
57 |
Converter converter = new Converter(tableColumns, rowNames, columnNames, valueFunctions);
|
|
58 |
return converter.convertTableColumnsToContingencyTableRows(); |
|
72 | 59 |
} |
73 | 60 |
|
74 | 61 |
/** |
75 | 62 |
* Validate given SQL query. |
63 |
* |
|
76 | 64 |
* @param sqlQuery - Validated SQL query. |
77 | 65 |
* @return true if SQL query is ok, false if not. |
78 | 66 |
*/ |
... | ... | |
83 | 71 |
|
84 | 72 |
/** |
85 | 73 |
* Get list of table column names from SQL query. |
74 |
* |
|
86 | 75 |
* @param sqlQuery - SQL query. |
87 | 76 |
* @return List of table column names. |
88 | 77 |
*/ |
89 | 78 |
public List<String> getNameOfColumnsFromQuery(String sqlQuery) { |
90 | 79 |
return this.databaseInterface.getNameOfColumnsFromQuery(sqlQuery); |
91 | 80 |
} |
81 |
|
|
82 |
private void initializeContingencyTableDataLists(List<NameUserName> rowNames, List<NameUserName> columnNames, |
|
83 |
List<ValueFunction> valueFunctions, List<ParameterInConfiguration> parametersInConfiguration) { |
|
84 |
for (ParameterInConfiguration parameterInConfiguration : parametersInConfiguration) { |
|
85 |
if (parameterInConfiguration.getLocation() != null && parameterInConfiguration.getLocation().getName().equals("Řádek")) { |
|
86 |
rowNames.add(new NameUserName(parameterInConfiguration.getParameter().getNameOfSelect().toUpperCase(), |
|
87 |
parameterInConfiguration.getColumnName())); |
|
88 |
} else if (parameterInConfiguration.getLocation() != null && parameterInConfiguration.getLocation().getName().equals("Sloupec")) { |
|
89 |
columnNames.add(new NameUserName(parameterInConfiguration.getParameter().getNameOfSelect().toUpperCase(), |
|
90 |
parameterInConfiguration.getColumnName())); |
|
91 |
} else if (parameterInConfiguration.getLocation() != null && parameterInConfiguration.getLocation().getName().equals("Hodnota")) { |
|
92 |
for (Function function : parameterInConfiguration.getFunctions()) { |
|
93 |
valueFunctions.add(new ValueFunction(parameterInConfiguration.getParameter().getNameOfSelect().toUpperCase(), |
|
94 |
parameterInConfiguration.getParameter().getName(), function.getName().toUpperCase(), |
|
95 |
parameterInConfiguration.getParameter().getParameterType().getName())); |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
private String generateFullSQLQuery(String basicSQLQuery, List<ParameterInConfiguration> parametersInConfiguration) { |
|
102 |
StringBuilder finalSQLQuery = new StringBuilder("SELECT * FROM ("); |
|
103 |
finalSQLQuery.append(basicSQLQuery); |
|
104 |
finalSQLQuery.append(")"); |
|
105 |
|
|
106 |
boolean isWhereDefined = false; |
|
107 |
|
|
108 |
for (ParameterInConfiguration parameterInConfiguration : parametersInConfiguration) { |
|
109 |
if (parameterInConfiguration.getOperator() != null && !parameterInConfiguration.getOperator().getName().equals("zadny") && |
|
110 |
parameterInConfiguration.getOperatorValue() != null && !parameterInConfiguration.getOperatorValue().equals("")) { |
|
111 |
String[] values = parameterInConfiguration.getOperatorValue().split(";"); |
|
112 |
if (!isWhereDefined) { |
|
113 |
finalSQLQuery.append(" WHERE "); |
|
114 |
isWhereDefined = true; |
|
115 |
} else { |
|
116 |
finalSQLQuery.append(" AND "); |
|
117 |
} |
|
118 |
|
|
119 |
finalSQLQuery.append(parameterInConfiguration.getParameter().getNameOfSelect()); |
|
120 |
finalSQLQuery.append(" "); |
|
121 |
finalSQLQuery.append(parameterInConfiguration.getOperator().getName()); |
|
122 |
finalSQLQuery.append(" "); |
|
123 |
switch (parameterInConfiguration.getOperator().getName()) { |
|
124 |
case "BETWEEN": |
|
125 |
case "NOT BETWEEN": |
|
126 |
finalSQLQuery.append(betweenNotBetween(values, parameterInConfiguration.getParameter().getParameterType().getName())); |
|
127 |
break; |
|
128 |
case "IN": |
|
129 |
case "NOT IN": |
|
130 |
finalSQLQuery.append(inNotIn(values, parameterInConfiguration.getParameter().getParameterType().getName())); |
|
131 |
break; |
|
132 |
case "LIKE": |
|
133 |
case "NOT LIKE": |
|
134 |
finalSQLQuery.append(likeNotLike(values, parameterInConfiguration.getParameter().getParameterType().getName())); |
|
135 |
break; |
|
136 |
default: |
|
137 |
finalSQLQuery.append(otherOperators(values, parameterInConfiguration.getParameter().getParameterType().getName())); |
|
138 |
break; |
|
139 |
} |
|
140 |
|
|
141 |
} |
|
142 |
} |
|
143 |
|
|
144 |
return finalSQLQuery.toString(); |
|
145 |
} |
|
146 |
|
|
147 |
private String likeNotLike(String[] values, String type) { |
|
148 |
StringBuilder SQLQueryPart = new StringBuilder(); |
|
149 |
addQuotes(type, SQLQueryPart); |
|
150 |
SQLQueryPart.append(values[0]); |
|
151 |
addQuotes(type, SQLQueryPart); |
|
152 |
|
|
153 |
return SQLQueryPart.toString(); |
|
154 |
} |
|
155 |
|
|
156 |
private String inNotIn(String[] values, String type) { |
|
157 |
StringBuilder SQLQueryPart = new StringBuilder(); |
|
158 |
SQLQueryPart.append("("); |
|
159 |
for (int i = 0; i < values.length; i++) { |
|
160 |
addQuotes(type, SQLQueryPart); |
|
161 |
SQLQueryPart.append(values[i]); |
|
162 |
addQuotes(type, SQLQueryPart); |
|
163 |
if (i < values.length - 1) { |
|
164 |
SQLQueryPart.append(","); |
|
165 |
} |
|
166 |
} |
|
167 |
SQLQueryPart.append(")"); |
|
168 |
|
|
169 |
return SQLQueryPart.toString(); |
|
170 |
} |
|
171 |
|
|
172 |
private String betweenNotBetween(String[] values, String type) { |
|
173 |
StringBuilder SQLQueryPart = new StringBuilder(); |
|
174 |
|
|
175 |
addQuotes(type, SQLQueryPart); |
|
176 |
SQLQueryPart.append(values[0]); |
|
177 |
addQuotes(type, SQLQueryPart); |
|
178 |
|
|
179 |
SQLQueryPart.append(" AND "); |
|
180 |
|
|
181 |
addQuotes(type, SQLQueryPart); |
|
182 |
SQLQueryPart.append(values[1]); |
|
183 |
addQuotes(type, SQLQueryPart); |
|
184 |
|
|
185 |
return SQLQueryPart.toString(); |
|
186 |
} |
|
187 |
|
|
188 |
private String otherOperators(String[] values, String type) { |
|
189 |
StringBuilder SQLQueryPart = new StringBuilder(); |
|
190 |
|
|
191 |
addQuotes(type, SQLQueryPart); |
|
192 |
SQLQueryPart.append(values[0]); |
|
193 |
addQuotes(type, SQLQueryPart); |
|
194 |
|
|
195 |
return SQLQueryPart.toString(); |
|
196 |
} |
|
197 |
|
|
198 |
private void addQuotes(String type, StringBuilder SQLQueryPart) { |
|
199 |
if (type.equals("Text") || type.equals("Datum")) { |
|
200 |
SQLQueryPart.append("'"); |
|
201 |
} |
|
202 |
} |
|
92 | 203 |
} |
Také k dispozici: Unified diff
re #8113 re #7885 all functions implemented, contingency table generation improved; re #8154 value filtering added; no comments