convert named parameters to numbered v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 08 Jan 2014 12:44:18 +0100
branchv_0
changeset 1431336bb9a4499
parent 142 da1e38386d84
child 144 d273d7c6dc0c
convert named parameters to numbered
java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java
java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java	Tue Jan 07 21:54:59 2014 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java	Wed Jan 08 12:44:18 2014 +0100
     1.3 @@ -17,7 +17,6 @@
     1.4   */
     1.5  package info.globalcode.sql.dk;
     1.6  
     1.7 -import static info.globalcode.sql.dk.Functions.notNull;
     1.8  import static info.globalcode.sql.dk.Functions.findByName;
     1.9  import java.sql.Connection;
    1.10  import java.sql.PreparedStatement;
    1.11 @@ -43,6 +42,7 @@
    1.12  	private List<NamedParameter> parametersUsed = new ArrayList<>();
    1.13  	private StringBuilder updatedQuery;
    1.14  	private Pattern pattern;
    1.15 +	private SQLCommandNumbered numbered;
    1.16  
    1.17  	public SQLCommandNamed(String query, List<NamedParameter> parameters, String namePrefix, String nameSuffix) {
    1.18  		super(query);
    1.19 @@ -54,6 +54,15 @@
    1.20  
    1.21  	@Override
    1.22  	public PreparedStatement prepareStatement(Connection c) throws SQLException {
    1.23 +		return getSQLCommandNumbered().prepareStatement(c);
    1.24 +	}
    1.25 +
    1.26 +	@Override
    1.27 +	public void parametrize(PreparedStatement ps) throws SQLException {
    1.28 +		getSQLCommandNumbered().parametrize(ps);
    1.29 +	}
    1.30 +
    1.31 +	private void prepare() throws SQLException {
    1.32  		try {
    1.33  			buildPattern();
    1.34  			placeParametersAndUpdateQuery();
    1.35 @@ -61,22 +70,25 @@
    1.36  		} catch (PatternSyntaxException e) {
    1.37  			throw new SQLException("Name prefix „" + namePrefix + "“ or suffix „" + nameSuffix + "“ contain a wrong regular expression. " + e.getLocalizedMessage(), e);
    1.38  		}
    1.39 -		return c.prepareStatement(updatedQuery.toString());
    1.40  	}
    1.41  
    1.42 -	@Override
    1.43 -	public void parametrize(PreparedStatement ps) throws SQLException {
    1.44 -		int i = 1;
    1.45 -		for (Parameter p : notNull(parametersUsed)) {
    1.46 -			ps.setObject(i++, p.getValue(), p.getType().getCode());
    1.47 +	/**
    1.48 +	 * @return SQL command with named parameters converted to SQL command with numbered parameters
    1.49 +	 */
    1.50 +	public SQLCommandNumbered getSQLCommandNumbered() throws SQLException {
    1.51 +		if (numbered == null) {
    1.52 +			prepare();
    1.53 +			numbered = new SQLCommandNumbered(updatedQuery.toString(), parametersUsed);
    1.54  		}
    1.55 +
    1.56 +		return numbered;
    1.57  	}
    1.58  
    1.59  	/**
    1.60  	 * Builds a regexp pattern that matches all parameter names (with prefix/suffix) and which has
    1.61  	 * one group: parameter name (without prefix/suffix)
    1.62  	 */
    1.63 -	private void buildPattern() {
    1.64 +	private void buildPattern() throws PatternSyntaxException {
    1.65  		StringBuilder patternString = new StringBuilder();
    1.66  
    1.67  		patternString.append(namePrefix);
    1.68 @@ -93,7 +105,7 @@
    1.69  		pattern = Pattern.compile(patternString.toString());
    1.70  	}
    1.71  
    1.72 -	private void placeParametersAndUpdateQuery() throws SQLException {
    1.73 +	private void placeParametersAndUpdateQuery() {
    1.74  		final String originalQuery = getQuery();
    1.75  		Matcher m = pattern.matcher(originalQuery);
    1.76  
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java	Tue Jan 07 21:54:59 2014 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNumbered.java	Wed Jan 08 12:44:18 2014 +0100
     2.3 @@ -28,9 +28,9 @@
     2.4   */
     2.5  public class SQLCommandNumbered extends SQLCommand {
     2.6  
     2.7 -	private List<Parameter> parameters;
     2.8 +	private List<? extends Parameter> parameters;
     2.9  
    2.10 -	public SQLCommandNumbered(String query, List<Parameter> parameters) {
    2.11 +	public SQLCommandNumbered(String query, List<? extends Parameter> parameters) {
    2.12  		super(query);
    2.13  		this.parameters = parameters;
    2.14  	}
    2.15 @@ -44,7 +44,7 @@
    2.16  	}
    2.17  
    2.18  	@Override
    2.19 -	public List<Parameter> getParameters() {
    2.20 +	public List<? extends Parameter> getParameters() {
    2.21  		return parameters;
    2.22  	}
    2.23  }