java/sql-dk/src/info/globalcode/sql/dk/SQLCommandNamed.java
branchv_0
changeset 143 1336bb9a4499
parent 78 d98f33d91553
child 155 eb3676c6929b
     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