franta-hg@16
|
1 |
/**
|
franta-hg@16
|
2 |
* SQL-DK
|
franta-hg@16
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@16
|
4 |
*
|
franta-hg@16
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@16
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@16
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@16
|
8 |
* (at your option) any later version.
|
franta-hg@16
|
9 |
*
|
franta-hg@16
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@16
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@16
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@16
|
13 |
* GNU General Public License for more details.
|
franta-hg@16
|
14 |
*
|
franta-hg@16
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@16
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@16
|
17 |
*/
|
franta-hg@1
|
18 |
package info.globalcode.sql.dk;
|
franta-hg@1
|
19 |
|
franta-hg@49
|
20 |
import static info.globalcode.sql.dk.Functions.notNull;
|
franta-hg@49
|
21 |
import static info.globalcode.sql.dk.Functions.escapeRegEx;
|
franta-hg@49
|
22 |
import static info.globalcode.sql.dk.Functions.findByName;
|
franta-hg@49
|
23 |
import java.sql.Connection;
|
franta-hg@1
|
24 |
import java.sql.PreparedStatement;
|
franta-hg@34
|
25 |
import java.sql.SQLException;
|
franta-hg@49
|
26 |
import java.util.ArrayList;
|
franta-hg@34
|
27 |
import java.util.List;
|
franta-hg@49
|
28 |
import java.util.regex.Matcher;
|
franta-hg@49
|
29 |
import java.util.regex.Pattern;
|
franta-hg@1
|
30 |
|
franta-hg@1
|
31 |
/**
|
franta-hg@1
|
32 |
*
|
franta-hg@1
|
33 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
34 |
*/
|
franta-hg@1
|
35 |
public class SQLCommandNamed extends SQLCommand {
|
franta-hg@1
|
36 |
|
franta-hg@49
|
37 |
private static final String PROBLEM_MARK = "<OMG>";
|
franta-hg@49
|
38 |
private String namePrefix;
|
franta-hg@49
|
39 |
private String nameSuffix;
|
franta-hg@34
|
40 |
private List<NamedParameter> parameters;
|
franta-hg@49
|
41 |
private List<NamedParameter> parametersUsed = new ArrayList<>();
|
franta-hg@49
|
42 |
private StringBuilder updatedQuery;
|
franta-hg@49
|
43 |
private Pattern pattern;
|
franta-hg@34
|
44 |
|
franta-hg@49
|
45 |
public SQLCommandNamed(String query, List<NamedParameter> parameters, String namePrefix, String nameSuffix) {
|
franta-hg@37
|
46 |
super(query);
|
franta-hg@49
|
47 |
this.updatedQuery = new StringBuilder(query.length());
|
franta-hg@34
|
48 |
this.parameters = parameters;
|
franta-hg@49
|
49 |
this.namePrefix = namePrefix;
|
franta-hg@49
|
50 |
this.nameSuffix = nameSuffix;
|
franta-hg@49
|
51 |
}
|
franta-hg@49
|
52 |
|
franta-hg@49
|
53 |
@Override
|
franta-hg@49
|
54 |
public PreparedStatement prepareStatement(Connection c) throws SQLException {
|
franta-hg@49
|
55 |
buildPattern();
|
franta-hg@49
|
56 |
placeParametersAndUpdateQuery();
|
franta-hg@49
|
57 |
return c.prepareStatement(updatedQuery.toString());
|
franta-hg@34
|
58 |
}
|
franta-hg@34
|
59 |
|
franta-hg@1
|
60 |
@Override
|
franta-hg@34
|
61 |
public void parametrize(PreparedStatement ps) throws SQLException {
|
franta-hg@49
|
62 |
int i = 1;
|
franta-hg@49
|
63 |
for (Parameter p : notNull(parametersUsed)) {
|
franta-hg@49
|
64 |
ps.setObject(i++, p.getValue(), p.getType());
|
franta-hg@49
|
65 |
}
|
franta-hg@49
|
66 |
}
|
franta-hg@49
|
67 |
|
franta-hg@49
|
68 |
/**
|
franta-hg@49
|
69 |
* Builds a regexp pattern that matches all parameter names (with prefix/suffix) and which has
|
franta-hg@49
|
70 |
* one group: parameter name (without prefix/suffix)
|
franta-hg@49
|
71 |
*/
|
franta-hg@49
|
72 |
private void buildPattern() {
|
franta-hg@49
|
73 |
StringBuilder patternString = new StringBuilder();
|
franta-hg@49
|
74 |
|
franta-hg@49
|
75 |
patternString.append(escapeRegEx(namePrefix));
|
franta-hg@49
|
76 |
patternString.append("(");
|
franta-hg@49
|
77 |
for (int i = 0; i < parameters.size(); i++) {
|
franta-hg@49
|
78 |
patternString.append(escapeRegEx(parameters.get(i).getName()));
|
franta-hg@49
|
79 |
if (i < parameters.size()) {
|
franta-hg@49
|
80 |
patternString.append("|");
|
franta-hg@49
|
81 |
}
|
franta-hg@49
|
82 |
}
|
franta-hg@49
|
83 |
patternString.append(")");
|
franta-hg@49
|
84 |
patternString.append(escapeRegEx(nameSuffix));
|
franta-hg@49
|
85 |
|
franta-hg@49
|
86 |
pattern = Pattern.compile(patternString.toString());
|
franta-hg@49
|
87 |
}
|
franta-hg@49
|
88 |
|
franta-hg@49
|
89 |
private void placeParametersAndUpdateQuery() throws SQLException {
|
franta-hg@49
|
90 |
final String originalQuery = getQuery();
|
franta-hg@49
|
91 |
Matcher m = pattern.matcher(originalQuery);
|
franta-hg@49
|
92 |
|
franta-hg@49
|
93 |
int lastPosition = 0;
|
franta-hg@49
|
94 |
while (m.find(lastPosition)) {
|
franta-hg@49
|
95 |
String name = m.group(1);
|
franta-hg@49
|
96 |
|
franta-hg@49
|
97 |
updatedQuery.append(originalQuery.substring(lastPosition, m.start()));
|
franta-hg@49
|
98 |
|
franta-hg@49
|
99 |
if (name.isEmpty()) {
|
franta-hg@49
|
100 |
updatedQuery.append(PROBLEM_MARK);
|
franta-hg@49
|
101 |
updatedQuery.append(originalQuery.substring(m.end(), originalQuery.length()));
|
franta-hg@49
|
102 |
throw new SQLException("Named parameter (near " + PROBLEM_MARK + ") is not defined: " + updatedQuery);
|
franta-hg@49
|
103 |
}
|
franta-hg@49
|
104 |
|
franta-hg@49
|
105 |
updatedQuery.append("?");
|
franta-hg@49
|
106 |
|
franta-hg@49
|
107 |
parametersUsed.add(findByName(parameters, name));
|
franta-hg@49
|
108 |
|
franta-hg@49
|
109 |
lastPosition = m.end();
|
franta-hg@49
|
110 |
}
|
franta-hg@49
|
111 |
updatedQuery.append(originalQuery.substring(lastPosition, originalQuery.length()));
|
franta-hg@49
|
112 |
|
franta-hg@49
|
113 |
for (NamedParameter definedParameter : parameters) {
|
franta-hg@49
|
114 |
if (findByName(parametersUsed, definedParameter.getName()) == null) {
|
franta-hg@49
|
115 |
throw new SQLException("Parameter " + definedParameter.getName() + " is defined but not used in the query.");
|
franta-hg@49
|
116 |
}
|
franta-hg@49
|
117 |
}
|
franta-hg@1
|
118 |
}
|
franta-hg@34
|
119 |
|
franta-hg@34
|
120 |
@Override
|
franta-hg@34
|
121 |
public List<NamedParameter> getParameters() {
|
franta-hg@34
|
122 |
return parameters;
|
franta-hg@34
|
123 |
}
|
franta-hg@1
|
124 |
}
|