/**
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.informantproject.plugin.jdbc;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Used by JdbcAspect to capture and mirror the state of prepared statements since the underlying
 * {@link PreparedStatement} values cannot be inspected after they have been set.
 * 
 * TODO does this need to be thread safe? Need to research JDBC spec, can one thread create a
 * PreparedStatement and set some parameters into it, and then have another thread execute it (with
 * those previously set parameters), if there is nothing that says no, then may need to make this
 * thread safe
 * 
 * @author Trask Stalnaker
 * @since 0.5
 */
class PreparedStatementMirror extends StatementMirror {

    private final String sql;
    private List<Object> parameters;
    private Collection<List<Object>> batchedParameters;

    public PreparedStatementMirror(String sql) {
        this.sql = sql;
        parameters = new ArrayList<Object>();
    }

    public void addBatch() {
        // synchronization isn't an issue here as this method is called only by the monitored thread
        if (batchedParameters == null) {
            batchedParameters = new ConcurrentLinkedQueue<List<Object>>();
        }
        batchedParameters.add(parameters);
        parameters = new ArrayList<Object>(parameters.size());
    }

    public Collection<List<Object>> getBatchedParametersCopy() {
        // batched parameters cannot be changed after calling addBatch(),
        // so it is safe to not copy the inner list
        return new ArrayList<List<Object>>(batchedParameters);
    }

    public boolean isUsingBatchedParameters() {
        return batchedParameters != null;
    }

    public List<Object> getParametersCopy() {
        return new ArrayList<Object>(parameters);
    }

    public String getSql() {
        return sql;
    }

    // remember parameterIndex starts at 1 not 0
    public void setParameterValue(int parameterIndex, Object object) {
        if (parameterIndex == parameters.size() + 1) {
            // common path
            parameters.add(object);
        } else if (parameterIndex < parameters.size() + 1) {
            // overwrite existing value
            parameters.set(parameterIndex, object);
        } else {
            // expand list with nulls
            for (int i = parameters.size() + 1; i < parameterIndex; i++) {
                parameters.add(null);
            }
            parameters.add(object);
        }
    }

    @Override
    public void clearBatch() {
        parameters.clear();
        batchedParameters.clear();
    }
}

@@ -2932,16 +2932,20 @@
 terIndex
+ - 1
 , object

Fix index off by one issue