named parameters: prefix/suffix are now regular expressions v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2013 14:36:14 +0100
branchv_0
changeset 5453020d0bd2e4
parent 53 eb30ad93ca8b
child 55 f5ed7c4efacc
named parameters: prefix/suffix are now regular expressions
java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
java/sql-dk/src/info/globalcode/sql/dk/Functions.java
java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Tue Dec 24 14:23:22 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Tue Dec 24 14:36:14 2013 +0100
     1.3 @@ -33,7 +33,7 @@
     1.4  public class CLIOptions {
     1.5  
     1.6  	public static final String DEFAULT_NAME_PREFIX = ":";
     1.7 -	public static final String DEFAULT_NAME_SUFFIX = "";
     1.8 +	public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
     1.9  	private String sql;
    1.10  	private String databaseName;
    1.11  	private String databaseNameToTest;
    1.12 @@ -172,18 +172,30 @@
    1.13  		namedParameters.add(p);
    1.14  	}
    1.15  
    1.16 +	/**
    1.17 +	 * @return regular expression describing the name prefix
    1.18 +	 */
    1.19  	public String getNamePrefix() {
    1.20  		return namePrefix;
    1.21  	}
    1.22  
    1.23 +	/**
    1.24 +	 * @see #getNamePrefix()
    1.25 +	 */
    1.26  	public void setNamePrefix(String namePrefix) {
    1.27  		this.namePrefix = namePrefix;
    1.28  	}
    1.29  
    1.30 +	/**
    1.31 +	 * @return regular expression describing the name prefix
    1.32 +	 */
    1.33  	public String getNameSuffix() {
    1.34  		return nameSuffix;
    1.35  	}
    1.36  
    1.37 +	/**
    1.38 +	 * @see #getNameSuffix()
    1.39 +	 */
    1.40  	public void setNameSuffix(String nameSuffix) {
    1.41  		this.nameSuffix = nameSuffix;
    1.42  	}
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Tue Dec 24 14:23:22 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Tue Dec 24 14:36:14 2013 +0100
     2.3 @@ -94,7 +94,7 @@
     2.4  
     2.5  	public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
     2.6  		for (T element : collection) {
     2.7 -			if (equalz(element.getName(), name)) {
     2.8 +			if (element != null && equalz(element.getName(), name)) {
     2.9  				return element;
    2.10  			}
    2.11  		}
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java	Tue Dec 24 14:23:22 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java	Tue Dec 24 14:36:14 2013 +0100
     3.3 @@ -74,8 +74,8 @@
     3.4  	private void buildPattern() {
     3.5  		StringBuilder patternString = new StringBuilder();
     3.6  
     3.7 -		patternString.append(Pattern.quote(namePrefix));
     3.8 -		patternString.append("(");
     3.9 +		patternString.append(namePrefix);
    3.10 +		patternString.append("(?<paramName>");
    3.11  		for (int i = 0; i < parameters.size(); i++) {
    3.12  			patternString.append(Pattern.quote(parameters.get(i).getName()));
    3.13  			if (i < parameters.size() - 1) {
    3.14 @@ -83,7 +83,7 @@
    3.15  			}
    3.16  		}
    3.17  		patternString.append(")");
    3.18 -		patternString.append(Pattern.quote(nameSuffix));
    3.19 +		patternString.append(nameSuffix);
    3.20  
    3.21  		pattern = Pattern.compile(patternString.toString());
    3.22  	}
    3.23 @@ -94,7 +94,7 @@
    3.24  
    3.25  		int lastPosition = 0;
    3.26  		while (m.find(lastPosition)) {
    3.27 -			String name = m.group(1);
    3.28 +			String name = m.group("paramName");
    3.29  
    3.30  			updatedQuery.append(originalQuery.substring(lastPosition, m.start()));
    3.31  			updatedQuery.append("?");
    3.32 @@ -107,18 +107,25 @@
    3.33  
    3.34  		for (NamedParameter definedParameter : parameters) {
    3.35  			if (findByName(parametersUsed, definedParameter.getName()) == null) {
    3.36 -				throw new SQLException("Parameter „" + definedParameter.getName() + "“ is defined but not used in the query: „" + originalQuery + "“");
    3.37 +				/**
    3.38 +				 * User can have predefined set of parameters and use them with different SQL
    3.39 +				 * queries that use only subset of these parameters → just warning, not exception.
    3.40 +				 */
    3.41 +				log.log(Level.WARNING, "Parameter „{0}“ is defined but not used in the query: „{1}“", new Object[]{definedParameter.getName(), originalQuery});
    3.42  			}
    3.43  		}
    3.44  	}
    3.45  
    3.46  	private void logPossiblyMissingParameters() {
    3.47 -		Pattern p = Pattern.compile(Pattern.quote(namePrefix) + ".*?" + Pattern.quote(nameSuffix));
    3.48 +		Pattern p = Pattern.compile(namePrefix + "(?<paramName>.*?)" + nameSuffix);
    3.49  		Matcher m = p.matcher(updatedQuery);
    3.50  		int lastPosition = 0;
    3.51  		while (m.find(lastPosition)) {
    3.52 -
    3.53 -			log.log(Level.WARNING, "Possibly missing parameter: {0}", m.group());
    3.54 +			/**
    3.55 +			 * We have not parsed and understood the SQL query; the parameter-like looking string
    3.56 +			 * could be inside a literal part of the query → just warning, not exception.
    3.57 +			 */
    3.58 +			log.log(Level.WARNING, "Possibly missing parameter „{0}“ in the query: „{1}“", new Object[]{m.group("paramName"), getQuery()});
    3.59  			lastPosition = m.end();
    3.60  		}
    3.61  	}