3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import static info.globalcode.sql.dk.Functions.notNull;
21 import static info.globalcode.sql.dk.Functions.findByName;
22 import java.sql.Connection;
23 import java.sql.PreparedStatement;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
34 * @author Ing. František Kučera (frantovo.cz)
36 public class SQLCommandNamed extends SQLCommand {
38 private static final Logger log = Logger.getLogger(SQLCommandNamed.class.getName());
39 private String namePrefix;
40 private String nameSuffix;
41 private List<NamedParameter> parameters;
42 private List<NamedParameter> parametersUsed = new ArrayList<>();
43 private StringBuilder updatedQuery;
44 private Pattern pattern;
46 public SQLCommandNamed(String query, List<NamedParameter> parameters, String namePrefix, String nameSuffix) {
48 this.updatedQuery = new StringBuilder(query.length());
49 this.parameters = parameters;
50 this.namePrefix = namePrefix;
51 this.nameSuffix = nameSuffix;
55 public PreparedStatement prepareStatement(Connection c) throws SQLException {
57 placeParametersAndUpdateQuery();
58 logPossiblyMissingParameters();
59 return c.prepareStatement(updatedQuery.toString());
63 public void parametrize(PreparedStatement ps) throws SQLException {
65 for (Parameter p : notNull(parametersUsed)) {
66 ps.setObject(i++, p.getValue(), p.getType());
71 * Builds a regexp pattern that matches all parameter names (with prefix/suffix) and which has
72 * one group: parameter name (without prefix/suffix)
74 private void buildPattern() {
75 StringBuilder patternString = new StringBuilder();
77 patternString.append(Pattern.quote(namePrefix));
78 patternString.append("(");
79 for (int i = 0; i < parameters.size(); i++) {
80 patternString.append(Pattern.quote(parameters.get(i).getName()));
81 if (i < parameters.size() - 1) {
82 patternString.append("|");
85 patternString.append(")");
86 patternString.append(Pattern.quote(nameSuffix));
88 pattern = Pattern.compile(patternString.toString());
91 private void placeParametersAndUpdateQuery() throws SQLException {
92 final String originalQuery = getQuery();
93 Matcher m = pattern.matcher(originalQuery);
96 while (m.find(lastPosition)) {
97 String name = m.group(1);
99 updatedQuery.append(originalQuery.substring(lastPosition, m.start()));
100 updatedQuery.append("?");
102 parametersUsed.add(findByName(parameters, name));
104 lastPosition = m.end();
106 updatedQuery.append(originalQuery.substring(lastPosition, originalQuery.length()));
108 for (NamedParameter definedParameter : parameters) {
109 if (findByName(parametersUsed, definedParameter.getName()) == null) {
110 throw new SQLException("Parameter „" + definedParameter.getName() + "“ is defined but not used in the query: „" + originalQuery + "“");
115 private void logPossiblyMissingParameters() {
116 Pattern p = Pattern.compile(Pattern.quote(namePrefix) + ".*?" + Pattern.quote(nameSuffix));
117 Matcher m = p.matcher(updatedQuery);
118 int lastPosition = 0;
119 while (m.find(lastPosition)) {
121 log.log(Level.WARNING, "Possibly missing parameter: {0}", m.group());
122 lastPosition = m.end();
127 public List<NamedParameter> getParameters() {