|
1 /** |
|
2 * SQL-DK |
|
3 * Copyright © 2013 František Kučera (frantovo.cz) |
|
4 * |
|
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. |
|
9 * |
|
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. |
|
14 * |
|
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/>. |
|
17 */ |
|
18 package info.globalcode.sql.dk; |
|
19 |
|
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; |
|
31 |
|
32 /** |
|
33 * Has named parameters. |
|
34 * |
|
35 * @author Ing. František Kučera (frantovo.cz) |
|
36 */ |
|
37 public class SQLCommandNamed extends SQLCommand { |
|
38 |
|
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; |
|
47 |
|
48 public SQLCommandNamed(String query, List<NamedParameter> parameters, String namePrefix, String nameSuffix) { |
|
49 super(query); |
|
50 this.updatedQuery = new StringBuilder(query.length()); |
|
51 this.parameters = parameters; |
|
52 this.namePrefix = namePrefix; |
|
53 this.nameSuffix = nameSuffix; |
|
54 } |
|
55 |
|
56 @Override |
|
57 public PreparedStatement prepareStatement(Connection c) throws SQLException { |
|
58 return getSQLCommandNumbered().prepareStatement(c); |
|
59 } |
|
60 |
|
61 @Override |
|
62 public void parametrize(PreparedStatement ps) throws SQLException { |
|
63 getSQLCommandNumbered().parametrize(ps); |
|
64 } |
|
65 |
|
66 private void prepare() throws SQLException { |
|
67 try { |
|
68 buildPattern(); |
|
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); |
|
73 } |
|
74 } |
|
75 |
|
76 /** |
|
77 * @return SQL command with named parameters converted to SQL command with numbered parameters |
|
78 */ |
|
79 public SQLCommandNumbered getSQLCommandNumbered() throws SQLException { |
|
80 if (numbered == null) { |
|
81 prepare(); |
|
82 numbered = new SQLCommandNumbered(updatedQuery.toString(), parametersUsed); |
|
83 } |
|
84 |
|
85 return numbered; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Builds a regexp pattern that matches all parameter names (with prefix/suffix) and which has |
|
90 * one group: parameter name (without prefix/suffix) |
|
91 */ |
|
92 private void buildPattern() throws PatternSyntaxException { |
|
93 StringBuilder patternString = new StringBuilder(); |
|
94 |
|
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("|"); |
|
101 } |
|
102 } |
|
103 patternString.append(")"); |
|
104 patternString.append(nameSuffix); |
|
105 |
|
106 pattern = Pattern.compile(patternString.toString()); |
|
107 } |
|
108 |
|
109 private void placeParametersAndUpdateQuery() { |
|
110 final String originalQuery = getQuery(); |
|
111 Matcher m = pattern.matcher(originalQuery); |
|
112 |
|
113 int lastPosition = 0; |
|
114 while (m.find(lastPosition)) { |
|
115 String name = m.group("paramName"); |
|
116 |
|
117 updatedQuery.append(originalQuery.substring(lastPosition, m.start())); |
|
118 updatedQuery.append("?"); |
|
119 |
|
120 parametersUsed.add(findByName(parameters, name)); |
|
121 |
|
122 lastPosition = m.end(); |
|
123 } |
|
124 updatedQuery.append(originalQuery.substring(lastPosition, originalQuery.length())); |
|
125 |
|
126 for (NamedParameter definedParameter : parameters) { |
|
127 if (findByName(parametersUsed, definedParameter.getName()) == null) { |
|
128 /** |
|
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. |
|
131 */ |
|
132 log.log(Level.WARNING, "Parameter „{0}“ is defined but not used in the query: „{1}“", new Object[]{definedParameter.getName(), originalQuery}); |
|
133 } |
|
134 } |
|
135 } |
|
136 |
|
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)) { |
|
142 /** |
|
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. |
|
145 */ |
|
146 log.log(Level.WARNING, "Possibly missing parameter „{0}“ in the query: „{1}“", new Object[]{m.group("paramName"), getQuery()}); |
|
147 lastPosition = m.end(); |
|
148 } |
|
149 } |
|
150 |
|
151 @Override |
|
152 public List<NamedParameter> getParameters() { |
|
153 return parameters; |
|
154 } |
|
155 } |