tabular formatter: new option 'separateBy' to print horizontal separator on each change of given column
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk;
19 import static info.globalcode.sql.dk.Functions.findByName;
20 import java.sql.Connection;
21 import java.sql.PreparedStatement;
22 import java.sql.SQLException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import java.util.regex.PatternSyntaxException;
32 * Has named parameters.
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;
45 private SQLCommandNumbered numbered;
47 public SQLCommandNamed(String query, List<NamedParameter> parameters, String namePrefix, String nameSuffix) {
49 this.updatedQuery = new StringBuilder(query.length());
50 this.parameters = parameters;
51 this.namePrefix = namePrefix;
52 this.nameSuffix = nameSuffix;
56 public PreparedStatement prepareStatement(Connection c) throws SQLException {
57 return getSQLCommandNumbered().prepareStatement(c);
61 public void parametrize(PreparedStatement ps) throws SQLException {
62 getSQLCommandNumbered().parametrize(ps);
65 private void prepare() throws SQLException {
68 placeParametersAndUpdateQuery();
69 logPossiblyMissingParameters();
70 } catch (PatternSyntaxException e) {
71 throw new SQLException("Name prefix „" + namePrefix + "“ or suffix „" + nameSuffix + "“ contain a wrong regular expression. " + e.getLocalizedMessage(), e);
76 * @return SQL command with named parameters converted to SQL command with numbered parameters
78 public SQLCommandNumbered getSQLCommandNumbered() throws SQLException {
79 if (numbered == null) {
81 numbered = new SQLCommandNumbered(updatedQuery.toString(), parametersUsed);
88 * Builds a regexp pattern that matches all parameter names (with prefix/suffix) and which has
89 * one group: parameter name (without prefix/suffix)
91 private void buildPattern() throws PatternSyntaxException {
92 StringBuilder patternString = new StringBuilder();
94 patternString.append(namePrefix);
95 patternString.append("(?<paramName>");
96 for (int i = 0; i < parameters.size(); i++) {
97 patternString.append(Pattern.quote(parameters.get(i).getName()));
98 if (i < parameters.size() - 1) {
99 patternString.append("|");
102 patternString.append(")");
103 patternString.append(nameSuffix);
105 pattern = Pattern.compile(patternString.toString());
108 private void placeParametersAndUpdateQuery() {
109 final String originalQuery = getQuery();
110 Matcher m = pattern.matcher(originalQuery);
112 int lastPosition = 0;
113 while (m.find(lastPosition)) {
114 String name = m.group("paramName");
116 updatedQuery.append(originalQuery.substring(lastPosition, m.start()));
117 updatedQuery.append("?");
119 parametersUsed.add(findByName(parameters, name));
121 lastPosition = m.end();
123 updatedQuery.append(originalQuery.substring(lastPosition, originalQuery.length()));
125 for (NamedParameter definedParameter : parameters) {
126 if (findByName(parametersUsed, definedParameter.getName()) == null) {
128 * User can have predefined set of parameters and use them with different SQL
129 * queries that use only subset of these parameters → just warning, not exception.
131 log.log(Level.WARNING, "Parameter „{0}“ is defined but not used in the query: „{1}“", new Object[]{definedParameter.getName(), originalQuery});
136 private void logPossiblyMissingParameters() {
137 Pattern p = Pattern.compile(namePrefix + "(?<paramName>.+?)" + nameSuffix);
138 Matcher m = p.matcher(updatedQuery);
139 int lastPosition = 0;
140 while (m.find(lastPosition)) {
142 * We have not parsed and understood the SQL query; the parameter-like looking string
143 * could be inside a literal part of the query → just warning, not exception.
145 log.log(Level.WARNING, "Possibly missing parameter „{0}“ in the query: „{1}“", new Object[]{m.group("paramName"), getQuery()});
146 lastPosition = m.end();
151 public List<NamedParameter> getParameters() {