All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kr.pe.kwonnam.freemarkerdynamicqlbuilder.DynamicQueryImpl Maven / Gradle / Ivy

package kr.pe.kwonnam.freemarkerdynamicqlbuilder;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DynamicQueryImpl implements DynamicQuery {
    private String queryString;
    private List queryParameters;

    public DynamicQueryImpl(String queryString, List queryParameters) {
        this.queryString = queryString;

        if (queryParameters == null) {
            this.queryParameters = new ArrayList();
        } else {
            this.queryParameters = queryParameters;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getQueryString() {
        return queryString;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List getQueryParameters() {
        return queryParameters;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object[] getQueryParameterArray() {
        return queryParameters.toArray(new Object[queryParameters.size()]);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void bindParameters(PreparedStatement preparedStatement) throws SQLException {
        if (preparedStatement == null) {
            throw new IllegalArgumentException("preparedStatement must not be null.");
        }

        for (int parameterIndex = 1; parameterIndex <= queryParameters.size(); parameterIndex++) {
            preparedStatement.setObject(parameterIndex, queryParameters.get(parameterIndex - 1));
        }
    }

    @Override
    public String toString() {
        return "DynamicQueryImpl{" +
                "queryString='" + queryString + '\'' +
                ", queryParameters=" + queryParameters +
                '}';
    }
}