1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Tue Dec 24 01:42:24 2013 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Tue Dec 24 11:54:58 2013 +0100
1.3 @@ -133,29 +133,4 @@
1.4 Arrays.fill(array, ch);
1.5 return new String(array);
1.6 }
1.7 -
1.8 - /**
1.9 - * @param original any text
1.10 - * @return escaped text that can be used as part of an regular expression, matches the original
1.11 - * text
1.12 - */
1.13 - public static String escapeRegEx(String original) {
1.14 - StringBuilder escaped = new StringBuilder(original.length() * 3);
1.15 -
1.16 - escaped.append("\\Q"); // start quotation
1.17 - for (int i = 0; i < original.length(); i++) {
1.18 - char ch = original.charAt(i);
1.19 - if (ch == 'E' && i > 0 && original.charAt(i - 1) == '\\') {
1.20 - escaped.append(ch); // this unintentionally ends quotation
1.21 - escaped.append("\\\\E"); // insert escaped \E (eaten before)
1.22 - escaped.append("\\Q"); // re-start quotation
1.23 - } else {
1.24 - escaped.append(ch);
1.25 - }
1.26 -
1.27 - }
1.28 - escaped.append("\\E"); // end quotation
1.29 -
1.30 - return escaped.toString();
1.31 - }
1.32 }
2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java Tue Dec 24 01:42:24 2013 +0100
2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java Tue Dec 24 11:54:58 2013 +0100
2.3 @@ -18,13 +18,14 @@
2.4 package info.globalcode.sql.dk;
2.5
2.6 import static info.globalcode.sql.dk.Functions.notNull;
2.7 -import static info.globalcode.sql.dk.Functions.escapeRegEx;
2.8 import static info.globalcode.sql.dk.Functions.findByName;
2.9 import java.sql.Connection;
2.10 import java.sql.PreparedStatement;
2.11 import java.sql.SQLException;
2.12 import java.util.ArrayList;
2.13 import java.util.List;
2.14 +import java.util.logging.Level;
2.15 +import java.util.logging.Logger;
2.16 import java.util.regex.Matcher;
2.17 import java.util.regex.Pattern;
2.18
2.19 @@ -34,7 +35,7 @@
2.20 */
2.21 public class SQLCommandNamed extends SQLCommand {
2.22
2.23 - private static final String PROBLEM_MARK = "<OMG>";
2.24 + private static final Logger log = Logger.getLogger(SQLCommandNamed.class.getName());
2.25 private String namePrefix;
2.26 private String nameSuffix;
2.27 private List<NamedParameter> parameters;
2.28 @@ -54,6 +55,7 @@
2.29 public PreparedStatement prepareStatement(Connection c) throws SQLException {
2.30 buildPattern();
2.31 placeParametersAndUpdateQuery();
2.32 + logPossiblyMissingParameters();
2.33 return c.prepareStatement(updatedQuery.toString());
2.34 }
2.35
2.36 @@ -72,16 +74,16 @@
2.37 private void buildPattern() {
2.38 StringBuilder patternString = new StringBuilder();
2.39
2.40 - patternString.append(escapeRegEx(namePrefix));
2.41 + patternString.append(Pattern.quote(namePrefix));
2.42 patternString.append("(");
2.43 for (int i = 0; i < parameters.size(); i++) {
2.44 - patternString.append(escapeRegEx(parameters.get(i).getName()));
2.45 - if (i < parameters.size()) {
2.46 + patternString.append(Pattern.quote(parameters.get(i).getName()));
2.47 + if (i < parameters.size() - 1) {
2.48 patternString.append("|");
2.49 }
2.50 }
2.51 patternString.append(")");
2.52 - patternString.append(escapeRegEx(nameSuffix));
2.53 + patternString.append(Pattern.quote(nameSuffix));
2.54
2.55 pattern = Pattern.compile(patternString.toString());
2.56 }
2.57 @@ -95,13 +97,6 @@
2.58 String name = m.group(1);
2.59
2.60 updatedQuery.append(originalQuery.substring(lastPosition, m.start()));
2.61 -
2.62 - if (name.isEmpty()) {
2.63 - updatedQuery.append(PROBLEM_MARK);
2.64 - updatedQuery.append(originalQuery.substring(m.end(), originalQuery.length()));
2.65 - throw new SQLException("Named parameter (near " + PROBLEM_MARK + ") is not defined: „" + updatedQuery + "“");
2.66 - }
2.67 -
2.68 updatedQuery.append("?");
2.69
2.70 parametersUsed.add(findByName(parameters, name));
2.71 @@ -117,6 +112,17 @@
2.72 }
2.73 }
2.74
2.75 + private void logPossiblyMissingParameters() {
2.76 + Pattern p = Pattern.compile(Pattern.quote(namePrefix) + ".*?" + Pattern.quote(nameSuffix));
2.77 + Matcher m = p.matcher(updatedQuery);
2.78 + int lastPosition = 0;
2.79 + while (m.find(lastPosition)) {
2.80 +
2.81 + log.log(Level.WARNING, "Possibly missing parameter: {0}", m.group());
2.82 + lastPosition = m.end();
2.83 + }
2.84 + }
2.85 +
2.86 @Override
2.87 public List<NamedParameter> getParameters() {
2.88 return parameters;
3.1 --- a/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java Tue Dec 24 01:42:24 2013 +0100
3.2 +++ b/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java Tue Dec 24 11:54:58 2013 +0100
3.3 @@ -31,7 +31,7 @@
3.4 @Test
3.5 public void testEscapeRegEx() {
3.6 for (String original : new String[]{"abcd", "1234", "xxx", "\\Eescape\\Q", "\\Qescape\\E", "abc\\Eescape\\Qdef.", ".", ""}) {
3.7 - String patternString = Functions.escapeRegEx(original);
3.8 + String patternString = Pattern.quote(original);
3.9 System.out.println(original + " → " + patternString);
3.10
3.11 Pattern pattern = Pattern.compile(patternString);