# HG changeset patch # User František Kučera # Date 1387882498 -3600 # Node ID 6730214fab41cfa2c6ca3e12a89f08ba2e2f4e33 # Parent 074b81e5fa7c10009684d2ecbffe681452b09eb7 log warning: Possibly missing parameters diff -r 074b81e5fa7c -r 6730214fab41 java/sql-dk/src/info/globalcode/sql/dk/Functions.java --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Tue Dec 24 01:42:24 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Tue Dec 24 11:54:58 2013 +0100 @@ -133,29 +133,4 @@ Arrays.fill(array, ch); return new String(array); } - - /** - * @param original any text - * @return escaped text that can be used as part of an regular expression, matches the original - * text - */ - public static String escapeRegEx(String original) { - StringBuilder escaped = new StringBuilder(original.length() * 3); - - escaped.append("\\Q"); // start quotation - for (int i = 0; i < original.length(); i++) { - char ch = original.charAt(i); - if (ch == 'E' && i > 0 && original.charAt(i - 1) == '\\') { - escaped.append(ch); // this unintentionally ends quotation - escaped.append("\\\\E"); // insert escaped \E (eaten before) - escaped.append("\\Q"); // re-start quotation - } else { - escaped.append(ch); - } - - } - escaped.append("\\E"); // end quotation - - return escaped.toString(); - } } diff -r 074b81e5fa7c -r 6730214fab41 java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java --- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java Tue Dec 24 01:42:24 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java Tue Dec 24 11:54:58 2013 +0100 @@ -18,13 +18,14 @@ package info.globalcode.sql.dk; import static info.globalcode.sql.dk.Functions.notNull; -import static info.globalcode.sql.dk.Functions.escapeRegEx; import static info.globalcode.sql.dk.Functions.findByName; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -34,7 +35,7 @@ */ public class SQLCommandNamed extends SQLCommand { - private static final String PROBLEM_MARK = ""; + private static final Logger log = Logger.getLogger(SQLCommandNamed.class.getName()); private String namePrefix; private String nameSuffix; private List parameters; @@ -54,6 +55,7 @@ public PreparedStatement prepareStatement(Connection c) throws SQLException { buildPattern(); placeParametersAndUpdateQuery(); + logPossiblyMissingParameters(); return c.prepareStatement(updatedQuery.toString()); } @@ -72,16 +74,16 @@ private void buildPattern() { StringBuilder patternString = new StringBuilder(); - patternString.append(escapeRegEx(namePrefix)); + patternString.append(Pattern.quote(namePrefix)); patternString.append("("); for (int i = 0; i < parameters.size(); i++) { - patternString.append(escapeRegEx(parameters.get(i).getName())); - if (i < parameters.size()) { + patternString.append(Pattern.quote(parameters.get(i).getName())); + if (i < parameters.size() - 1) { patternString.append("|"); } } patternString.append(")"); - patternString.append(escapeRegEx(nameSuffix)); + patternString.append(Pattern.quote(nameSuffix)); pattern = Pattern.compile(patternString.toString()); } @@ -95,13 +97,6 @@ String name = m.group(1); updatedQuery.append(originalQuery.substring(lastPosition, m.start())); - - if (name.isEmpty()) { - updatedQuery.append(PROBLEM_MARK); - updatedQuery.append(originalQuery.substring(m.end(), originalQuery.length())); - throw new SQLException("Named parameter (near " + PROBLEM_MARK + ") is not defined: „" + updatedQuery + "“"); - } - updatedQuery.append("?"); parametersUsed.add(findByName(parameters, name)); @@ -117,6 +112,17 @@ } } + private void logPossiblyMissingParameters() { + Pattern p = Pattern.compile(Pattern.quote(namePrefix) + ".*?" + Pattern.quote(nameSuffix)); + Matcher m = p.matcher(updatedQuery); + int lastPosition = 0; + while (m.find(lastPosition)) { + + log.log(Level.WARNING, "Possibly missing parameter: {0}", m.group()); + lastPosition = m.end(); + } + } + @Override public List getParameters() { return parameters; diff -r 074b81e5fa7c -r 6730214fab41 java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java --- a/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java Tue Dec 24 01:42:24 2013 +0100 +++ b/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java Tue Dec 24 11:54:58 2013 +0100 @@ -31,7 +31,7 @@ @Test public void testEscapeRegEx() { for (String original : new String[]{"abcd", "1234", "xxx", "\\Eescape\\Q", "\\Qescape\\E", "abc\\Eescape\\Qdef.", ".", ""}) { - String patternString = Functions.escapeRegEx(original); + String patternString = Pattern.quote(original); System.out.println(original + " → " + patternString); Pattern pattern = Pattern.compile(patternString);