package com.avaje.ebeaninternal.api; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import com.avaje.ebeaninternal.server.querydefn.NaturalKeyBindParam; /** * Parameters used for binding to a statement. *
* Used by FindByNativeSql and UpdateSql to support ordered and named * parameters. Note that you can use either ordered OR named parameters. *
*/ public class BindParams implements Serializable { private static final long serialVersionUID = 4541081933302086285L; private ArrayList positionedParameters = new ArrayList(); private HashMap* This is to handle binding collections to in clauses. The number * of values in the collection effects the query (number of bind values) * and so must be taken into account when calculating the query hash. *
*/ public int getQueryPlanHash() { return queryPlanHash; } /** * Set an encryption key as a bind value. ** Needs special treatment as the value should not be included in a log. *
*/ public Param setEncryptionKey(String name, Object value) { Param p = getParam(name); p.setEncryptionKey(value); return p; } /** * Register the named parameter as an Out parameter. */ public void registerOut(String name, int outType) { Param p = getParam(name); p.setOutType(outType); } /** * Return the Parameter for a given position. */ public Param getParameter(int position) { // Used to read Out value by CallableSql return getParam(position); } /** * Return the named parameter. */ public Param getParameter(String name) { return getParam(name); } /** * Return the values of ordered parameters. */ public List positionedParameters() { return positionedParameters; } /** * Set the sql with named parameters replaced with place holder ?. */ public void setPreparedSql(String preparedSql) { this.preparedSql = preparedSql; } /** * Return the sql with ? place holders (named parameters have been processed * and ordered). */ public String getPreparedSql() { return preparedSql; } /** * The bind parameters in the correct binding order. ** This is the result of converting sql with named parameters * into sql with ? and ordered parameters. *
*/ public static final class OrderedList { final List paramList; final StringBuilder preparedSql; public OrderedList() { this(new ArrayList()); } public OrderedList(List paramList) { this.paramList = paramList; this.preparedSql = new StringBuilder(); } /** * Add a parameter in the correct binding order. */ public void add(Param param) { paramList.add(param); } /** * Return the number of bind parameters in this list. */ public int size() { return paramList.size(); } /** * Returns the ordered list of bind parameters. */ public List list() { return paramList; } /** * Append parsedSql that has named parameters converted into ?. */ public void appendSql(String parsedSql) { preparedSql.append(parsedSql); } public String getPreparedSql() { return preparedSql.toString(); } } /** * A In Out capable parameter for the CallableStatement. */ public static final class Param implements Serializable { private static final long serialVersionUID = 1L; private boolean encryptionKey; private boolean isInParam; private boolean isOutParam; private int type; private Object inValue; private Object outValue; private int textLocation; /** * Construct a Parameter. */ public Param() { } /** * Create a deep copy of the Param. */ public Param copy() { Param copy = new Param(); copy.isInParam = isInParam; copy.isOutParam = isOutParam; copy.type = type; copy.inValue = inValue; copy.outValue = outValue; return copy; } public int hashCode() { int hc = getClass().hashCode(); hc = hc * 31 + (isInParam ? 0 : 1); hc = hc * 31 + (isOutParam ? 0 : 1); hc = hc * 31 + (type); hc = hc * 31 + (inValue == null ? 0 : inValue.hashCode()); return hc; } public boolean equals(Object o) { if (o == null) { return false; } if (o == this) { return true; } if (o instanceof Param) { return hashCode() == o.hashCode(); } return false; } /** * Return true if this is an In parameter that needs to be bound before * execution. */ public boolean isInParam() { return isInParam; } /** * Return true if this is an out parameter that needs to be registered * before execution. */ public boolean isOutParam() { return isOutParam; } /** * Return the jdbc type of this parameter. Used for registering Out * parameters and setting NULL In parameters. */ public int getType() { return type; } /** * Set the Out parameter type. */ public void setOutType(int type) { this.type = type; this.isOutParam = true; } /** * Set the In value. */ public void setInValue(Object in) { this.inValue = in; this.isInParam = true; } /** * Set an encryption key (which can not be logged). */ public void setEncryptionKey(Object in) { this.inValue = in; this.isInParam = true; this.encryptionKey = true; } /** * Specify that the In parameter is NULL and the specific type that it * is. */ public void setInNullType(int type) { this.type = type; this.inValue = null; this.isInParam = true; } /** * Return the OUT value that was retrieved. This value is set after * CallableStatement was executed. */ public Object getOutValue() { return outValue; } /** * Return the In value. If this is null, then the type should be used to * specify the type of the null. */ public Object getInValue() { return inValue; } /** * Set the OUT value returned by a CallableStatement after it has * executed. */ public void setOutValue(Object out) { this.outValue = out; } /** * Return the location this parameter was found in the sql text. */ public int getTextLocation() { return textLocation; } /** * Set the location in the sql text this parameter was located. This is * used to control order for named parameters. */ public void setTextLocation(int textLocation) { this.textLocation = textLocation; } /** * If true do not include this value in a transaction log. */ public boolean isEncryptionKey() { return encryptionKey; } } }