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.findByName;
21 import java.sql.Connection;
22 import java.sql.PreparedStatement;
23 import java.sql.SQLException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import java.util.regex.PatternSyntaxException;
33 * Has named parameters.
35 * @author Ing. František Kučera (frantovo.cz)
37 public class SQLCommandNamed extends SQLCommand {
39 private static final Logger log = Logger.getLogger(SQLCommandNamed.class.getName());
40 private String namePrefix;
41 private String nameSuffix;
42 private List<NamedParameter> parameters;
43 private List<NamedParameter> parametersUsed = new ArrayList<>();
44 private StringBuilder updatedQuery;
45 private Pattern pattern;
46 private SQLCommandNumbered numbered;
48 public SQLCommandNamed(String query, List<NamedParameter> parameters, String namePrefix, String nameSuffix) {
50 this.updatedQuery = new StringBuilder(query.length());
51 this.parameters = parameters;
52 this.namePrefix = namePrefix;
53 this.nameSuffix = nameSuffix;
57 public PreparedStatement prepareStatement(Connection c) throws SQLException {
58 return getSQLCommandNumbered().prepareStatement(c);
62 public void parametrize(PreparedStatement ps) throws SQLException {
63 getSQLCommandNumbered().parametrize(ps);
66 private void prepare() throws SQLException {
69 placeParametersAndUpdateQuery();
70 logPossiblyMissingParameters();
71 } catch (PatternSyntaxException e) {
72 throw new SQLException("Name prefix „" + namePrefix + "“ or suffix „" + nameSuffix + "“ contain a wrong regular expression. " + e.getLocalizedMessage(), e);
77 * @return SQL command with named parameters converted to SQL command with numbered parameters
79 public SQLCommandNumbered getSQLCommandNumbered() throws SQLException {
80 if (numbered == null) {
82 numbered = new SQLCommandNumbered(updatedQuery.toString(), parametersUsed);
89 * Builds a regexp pattern that matches all parameter names (with prefix/suffix) and which has
90 * one group: parameter name (without prefix/suffix)
92 private void buildPattern() throws PatternSyntaxException {
93 StringBuilder patternString = new StringBuilder();
95 patternString.append(namePrefix);
96 patternString.append("(?<paramName>");
97 for (int i = 0; i < parameters.size(); i++) {
98 patternString.append(Pattern.quote(parameters.get(i).getName()));
99 if (i < parameters.size() - 1) {
100 patternString.append("|");
103 patternString.append(")");
104 patternString.append(nameSuffix);
106 pattern = Pattern.compile(patternString.toString());
109 private void placeParametersAndUpdateQuery() {
110 final String originalQuery = getQuery();
111 Matcher m = pattern.matcher(originalQuery);
113 int lastPosition = 0;
114 while (m.find(lastPosition)) {
115 String name = m.group("paramName");
117 updatedQuery.append(originalQuery.substring(lastPosition, m.start()));
118 updatedQuery.append("?");
120 parametersUsed.add(findByName(parameters, name));
122 lastPosition = m.end();
124 updatedQuery.append(originalQuery.substring(lastPosition, originalQuery.length()));
126 for (NamedParameter definedParameter : parameters) {
127 if (findByName(parametersUsed, definedParameter.getName()) == null) {
129 * User can have predefined set of parameters and use them with different SQL
130 * queries that use only subset of these parameters → just warning, not exception.
132 log.log(Level.WARNING, "Parameter „{0}“ is defined but not used in the query: „{1}“", new Object[]{definedParameter.getName(), originalQuery});
137 private void logPossiblyMissingParameters() {
138 Pattern p = Pattern.compile(namePrefix + "(?<paramName>.+?)" + nameSuffix);
139 Matcher m = p.matcher(updatedQuery);
140 int lastPosition = 0;
141 while (m.find(lastPosition)) {
143 * We have not parsed and understood the SQL query; the parameter-like looking string
144 * could be inside a literal part of the query → just warning, not exception.
146 log.log(Level.WARNING, "Possibly missing parameter „{0}“ in the query: „{1}“", new Object[]{m.group("paramName"), getQuery()});
147 lastPosition = m.end();
152 public List<NamedParameter> getParameters() {