/*
 * Drizzle JDBC
 *
 * Copyright (C) 2009 Marcus Eriksson (krummas@gmail.com)
 * All rights reserved.
 *
 * Use and distribution licensed under the BSD license.
 */

package org.drizzle.jdbc.internal.common.query;

import static org.drizzle.jdbc.internal.common.Utils.countChars;
import org.drizzle.jdbc.internal.common.query.parameters.ParameterHolder;
import org.drizzle.jdbc.internal.common.QueryException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 .
 * User: marcuse
 * Date: Feb 18, 2009
 * Time: 10:13:42 PM

 */
public class DrizzleParameterizedQuery implements ParameterizedQuery {
    private final static Logger log = Logger.getLogger(DrizzleParameterizedQuery.class.toString());
    private List<ParameterHolder> parameters;
    private final int paramCount;
    private final String query;

    public DrizzleParameterizedQuery(String query) {
        this.query=query;
        this.paramCount=countChars(query,'?');
        log.finest("Found "+paramCount+" questionmarks");
        parameters=new ArrayList<ParameterHolder>(paramCount);
    }
    
    public DrizzleParameterizedQuery(ParameterizedQuery query) {
        this.query=query.getQuery();
        this.paramCount=query.getParamCount();
        parameters=new ArrayList<ParameterHolder>(paramCount);
        log.finest("Copying an existing parameterized query");
    }

    public void setParameter(int position, ParameterHolder parameter) throws IllegalParameterException {
        log.finest("Setting parameter "+parameter.toString()+" at position "+position);
        if(position>=0 && position<paramCount)
            this.parameters.add(position,parameter);
        else
            throw new IllegalParameterException("No '?' on that position");
    }

    public void clearParameters() {
        this.parameters.clear();
    }

    public int length() {
        int length = query.length() - paramCount; // remove the ?s
        for(ParameterHolder param : parameters)
            length+=param.length();
        return length;
    }

    public void writeTo(OutputStream os) throws IOException, QueryException {
        if(paramCount != this.parameters.size())
            throw new QueryException("You need to set exactly "+paramCount+" parameters on the prepared statement");
        StringReader strReader = new StringReader(query);
        int ch;
        int paramCounter=0;
        while((ch=strReader.read())!=-1) {
            if(ch=='?') {
                parameters.get(paramCounter++).writeTo(os);
            } else {
                os.write(ch);
            }
        }
    }

    public String getQuery() {
        return query;
    }

    public int getParamCount() {
        return paramCount;
    }
}

@@ -441,25 +441,23 @@
 va.util.
-ArrayList
+HashMap
 ;%0Aimport
@@ -467,20 +467,19 @@
 va.util.
-List
+Map
 ;%0Aimport
@@ -761,21 +761,29 @@
 private 
-List%3C
+Map%3CInteger, 
 Paramete
@@ -1073,34 +1073,41 @@
 ameters=new 
-ArrayList%3C
+HashMap%3CInteger, 
 ParameterHol
@@ -1107,34 +1107,24 @@
 eterHolder%3E(
-paramCount
 );%0A    %7D%0A   
@@ -1301,18 +1301,25 @@
 new 
-ArrayList%3C
+HashMap%3CInteger, 
 Para
@@ -1331,26 +1331,16 @@
 Holder%3E(
-paramCount
 );%0A     
@@ -1643,16 +1643,18 @@
 amCount)
+ %7B
 %0A       
@@ -1650,37 +1650,32 @@
 ) %7B%0A            
-this.
 parameters.add(p
@@ -1673,11 +1673,11 @@
 ers.
-add
+put
 (pos
@@ -1682,16 +1682,17 @@
 osition,
+ 
 paramete
@@ -1702,16 +1702,18 @@
 %0A       
+ %7D
  else%0A  
@@ -1962,16 +1962,17 @@
  the ?s%0A
+%0A
         
@@ -1975,16 +1975,35 @@
     for(
+Map.Entry%3CInteger, 
 Paramete
@@ -2005,24 +2005,25 @@
 ameterHolder
+%3E
  param : par
@@ -2029,16 +2029,27 @@
 rameters
+.entrySet()
 )%0A      
@@ -2068,16 +2068,27 @@
 +=param.
+getValue().
 length()
@@ -2085,24 +2085,25 @@
 ).length();%0A
+%0A
         retu

Fix Bug #380204 Setting a specific parameter multiple times does not replace the previous value