mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
Fix for #206 - Reuse SqlUpdate when binding a list that can vary in size
This commit is contained in:
1 parent
216806074c
commit
6ecb77dcc2
10 files changed
+406
-233
No files matched your search
@@ -93,6 +93,11 @@ public final class DefaultSqlUpdate implements Serializable, SpiSqlUpdate {
|
||||
*/
|
||||
private final String sql;
|
||||
|
||||
/**
|
||||
* The actual sql with named parameters converted.
|
||||
*/
|
||||
private String generatedSql;
|
||||
|
||||
/**
|
||||
* Some descriptive text that can be put into the transaction log.
|
||||
*/
|
||||
@@ -170,7 +175,16 @@ public final class DefaultSqlUpdate implements Serializable, SpiSqlUpdate {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
public String getGeneratedSql() {
|
||||
return generatedSql;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGeneratedSql(String generatedSql) {
|
||||
this.generatedSql = generatedSql;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ public class Binder {
|
||||
|
||||
ArrayList<BindValues.Value> list = bindValues.values();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
BindValues.Value bindValue = (BindValues.Value) list.get(i);
|
||||
BindValues.Value bindValue = list.get(i);
|
||||
if (bindValue.isComment()) {
|
||||
if (bindBuf != null) {
|
||||
bindBuf.append(bindValue.getName());
|
||||
@@ -226,14 +226,12 @@ public class Binder {
|
||||
try {
|
||||
switch (dataType) {
|
||||
case java.sql.Types.BOOLEAN:
|
||||
Boolean bo = (Boolean) data;
|
||||
b.setBoolean(bo.booleanValue());
|
||||
b.setBoolean((Boolean) data);
|
||||
break;
|
||||
case java.sql.Types.BIT:
|
||||
// Types.BIT should map to Java Boolean
|
||||
Boolean bitBool = (Boolean) data;
|
||||
b.setBoolean(bitBool.booleanValue());
|
||||
break;
|
||||
case java.sql.Types.BIT:
|
||||
// Types.BIT should map to Java Boolean
|
||||
b.setBoolean((Boolean) data);
|
||||
break;
|
||||
|
||||
case java.sql.Types.VARCHAR:
|
||||
b.setString((String) data);
|
||||
@@ -244,32 +242,32 @@ public class Binder {
|
||||
break;
|
||||
|
||||
case java.sql.Types.TINYINT:
|
||||
b.setByte(((Byte) data).byteValue());
|
||||
b.setByte((Byte) data);
|
||||
break;
|
||||
|
||||
case java.sql.Types.SMALLINT:
|
||||
b.setShort(((Short) data).shortValue());
|
||||
b.setShort((Short) data);
|
||||
break;
|
||||
|
||||
case java.sql.Types.INTEGER:
|
||||
b.setInt(((Integer) data).intValue());
|
||||
b.setInt((Integer) data);
|
||||
break;
|
||||
|
||||
case java.sql.Types.BIGINT:
|
||||
b.setLong(((Long) data).longValue());
|
||||
b.setLong((Long) data);
|
||||
break;
|
||||
|
||||
case java.sql.Types.REAL:
|
||||
b.setFloat(((Float) data).floatValue());
|
||||
b.setFloat((Float) data);
|
||||
break;
|
||||
|
||||
case java.sql.Types.FLOAT:
|
||||
// DB Float in theory maps to Java Double type
|
||||
b.setDouble(((Double) data).doubleValue());
|
||||
b.setDouble((Double) data);
|
||||
break;
|
||||
|
||||
case java.sql.Types.DOUBLE:
|
||||
b.setDouble(((Double) data).doubleValue());
|
||||
b.setDouble((Double) data);
|
||||
break;
|
||||
|
||||
case java.sql.Types.NUMERIC:
|
||||
|
||||
@@ -98,6 +98,7 @@ public class ExeUpdateSql {
|
||||
|
||||
// process named parameters if required
|
||||
sql = BindParamsParser.parse(bindParams, sql);
|
||||
updateSql.setGeneratedSql(sql);
|
||||
|
||||
boolean logSql = request.isLogSql();
|
||||
|
||||
|
||||
@@ -18,205 +18,201 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
*/
|
||||
public class BindParamsParser {
|
||||
|
||||
public static final String ENCRYPTKEY_PREFIX = "encryptkey_";
|
||||
public static final String ENCRYPTKEY_GAP = "___";
|
||||
|
||||
public static final String ENCRYPTKEY_PREFIX = "encryptkey_";
|
||||
public static final String ENCRYPTKEY_GAP = "___";
|
||||
private static final int ENCRYPTKEY_PREFIX_LEN = ENCRYPTKEY_PREFIX.length();
|
||||
private static final int ENCRYPTKEY_GAP_LEN = ENCRYPTKEY_GAP.length();
|
||||
|
||||
private static final int ENCRYPTKEY_PREFIX_LEN = ENCRYPTKEY_PREFIX.length();
|
||||
private static final int ENCRYPTKEY_GAP_LEN = ENCRYPTKEY_GAP.length();
|
||||
/**
|
||||
* Used to parse sql looking for named parameters.
|
||||
*/
|
||||
private static final String quote = "'";
|
||||
|
||||
/**
|
||||
* Used to parse sql looking for named parameters.
|
||||
*/
|
||||
private static final String quote = "'";
|
||||
/**
|
||||
* Used to parse sql looking for named parameters.
|
||||
*/
|
||||
private static final String colon = ":";
|
||||
|
||||
/**
|
||||
* Used to parse sql looking for named parameters.
|
||||
*/
|
||||
private static final String colon = ":";
|
||||
private final BindParams params;
|
||||
private final String sql;
|
||||
|
||||
private final BindParams params;
|
||||
private final String sql;
|
||||
private final BeanDescriptor<?> beanDescriptor;
|
||||
|
||||
private final BeanDescriptor<?> beanDescriptor;
|
||||
public static String parse(BindParams params, String sql) {
|
||||
return parse(params, sql, null);
|
||||
}
|
||||
|
||||
public static String parse(BindParams params, String sql) {
|
||||
return parse(params, sql, null);
|
||||
}
|
||||
|
||||
public static String parse(BindParams params, String sql, BeanDescriptor<?> beanDescriptor) {
|
||||
return new BindParamsParser(params, sql, beanDescriptor).parseSql();
|
||||
public static String parse(BindParams params, String sql, BeanDescriptor<?> beanDescriptor) {
|
||||
return new BindParamsParser(params, sql, beanDescriptor).parseSql();
|
||||
}
|
||||
|
||||
public static OrderedList parseNamedParams(BindParams params, String sql) {
|
||||
return new BindParamsParser(params, sql, null).parseSqlNamedParams();
|
||||
}
|
||||
|
||||
private BindParamsParser(BindParams params, String sql, BeanDescriptor<?> beanDescriptor) {
|
||||
this.params = params;
|
||||
this.sql = sql;
|
||||
this.beanDescriptor = beanDescriptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for parsing having clauses with named parameters.
|
||||
* <p>
|
||||
* The issue here is that BindParams contains named parameters for
|
||||
* both where and having clauses. BindParams.positionedParameters is
|
||||
* used for the where and the OrderedList for the having.
|
||||
* </p>
|
||||
*/
|
||||
private OrderedList parseSqlNamedParams() {
|
||||
OrderedList orderedList = new OrderedList();
|
||||
parseNamedParams(orderedList);
|
||||
return orderedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the sql changed named parameters to positioned parameters if required.
|
||||
* <p>
|
||||
* The sql is used when named parameters are used.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is used in most cases of named parameters. The case it is NOT used for is
|
||||
* named parameters in a having clause. In this case some of the named parameters
|
||||
* could be for a where clause and some for the having clause.
|
||||
* </p>
|
||||
*/
|
||||
private String parseSql() {
|
||||
|
||||
if (params.isSameBindHash()) {
|
||||
String preparedSql = params.getPreparedSql();
|
||||
if (preparedSql != null && preparedSql.length() > 0) {
|
||||
// the sql has already been parsed and positionedParameters are set in order
|
||||
return preparedSql;
|
||||
}
|
||||
}
|
||||
|
||||
public static OrderedList parseNamedParams(BindParams params, String sql) {
|
||||
return new BindParamsParser(params, sql, null).parseSqlNamedParams();
|
||||
String preparedSql;
|
||||
if (params.requiresNamedParamsPrepare()) {
|
||||
// convert named parameters into ordered list
|
||||
OrderedList orderedList = params.createOrderedList();
|
||||
parseNamedParams(orderedList);
|
||||
preparedSql = orderedList.getPreparedSql();
|
||||
} else {
|
||||
preparedSql = sql;
|
||||
}
|
||||
|
||||
private BindParamsParser(BindParams params, String sql, BeanDescriptor<?> beanDescriptor) {
|
||||
this.params = params;
|
||||
this.sql = sql;
|
||||
this.beanDescriptor = beanDescriptor;
|
||||
params.setPreparedSql(preparedSql);
|
||||
return preparedSql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Named parameters need to be parsed and replaced with ?.
|
||||
*/
|
||||
private void parseNamedParams(OrderedList orderedList) {
|
||||
|
||||
parseNamedParams(0, orderedList);
|
||||
}
|
||||
|
||||
private void parseNamedParams(int startPos, OrderedList orderedList) {
|
||||
|
||||
if (sql == null) {
|
||||
throw new PersistenceException("query does not contain any named bind parameters?");
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for parsing having clauses with named parameters.
|
||||
* <p>
|
||||
* The issue here is that BindParams contains named parameters for
|
||||
* both where and having clauses. BindParams.positionedParameters is
|
||||
* used for the where and the OrderedList for the having.
|
||||
* </p>
|
||||
*/
|
||||
private OrderedList parseSqlNamedParams() {
|
||||
OrderedList orderedList = new OrderedList();
|
||||
parseNamedParams(orderedList);
|
||||
return orderedList;
|
||||
if (startPos > sql.length()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the sql changed named parameters to positioned parameters if required.
|
||||
* <p>
|
||||
* The sql is used when named parameters are used.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is used in most cases of named parameters. The case it is NOT used for is
|
||||
* named parameters in a having clause. In this case some of the named parameters
|
||||
* could be for a where clause and some for the having clause.
|
||||
* </p>
|
||||
*/
|
||||
private String parseSql() {
|
||||
|
||||
String preparedSql = params.getPreparedSql();
|
||||
if (preparedSql != null && preparedSql.length() > 0){
|
||||
// the sql has already been parsed and
|
||||
// positionedParameters are set in order
|
||||
return preparedSql;
|
||||
}
|
||||
|
||||
String prepardSql;
|
||||
if (params.requiresNamedParamsPrepare()) {
|
||||
OrderedList orderedList = new OrderedList(params.positionedParameters());
|
||||
|
||||
parseNamedParams(orderedList);
|
||||
prepardSql = orderedList.getPreparedSql();
|
||||
|
||||
// search for quotes and named params... in order...
|
||||
int beginQuotePos = sql.indexOf(quote, startPos);
|
||||
int nameParamStart = sql.indexOf(colon, startPos);
|
||||
if (beginQuotePos > 0 && beginQuotePos < nameParamStart) {
|
||||
// the quote precedes the named parameter...
|
||||
// find and add up to the end quote
|
||||
int endQuotePos = sql.indexOf(quote, beginQuotePos + 1);
|
||||
String sub = sql.substring(startPos, endQuotePos + 1);
|
||||
orderedList.appendSql(sub);
|
||||
|
||||
// start again after the end quote
|
||||
parseNamedParams(endQuotePos + 1, orderedList);
|
||||
|
||||
} else {
|
||||
if (nameParamStart < 0) {
|
||||
// no more params, add the rest
|
||||
String sub = sql.substring(startPos, sql.length());
|
||||
orderedList.appendSql(sub);
|
||||
|
||||
} else {
|
||||
// find the end of the parameter name
|
||||
int endOfParam = nameParamStart + 1;
|
||||
do {
|
||||
char c = sql.charAt(endOfParam);
|
||||
if (c != '_' && !Character.isLetterOrDigit(c)) {
|
||||
break;
|
||||
}
|
||||
endOfParam++;
|
||||
} while (endOfParam < sql.length());
|
||||
|
||||
// add the named parameter value to bindList
|
||||
String paramName = sql.substring(nameParamStart + 1, endOfParam);
|
||||
|
||||
Param param;
|
||||
if (paramName.startsWith(ENCRYPTKEY_PREFIX)) {
|
||||
param = addEncryptKeyParam(paramName);
|
||||
} else {
|
||||
prepardSql = sql;
|
||||
}
|
||||
params.setPreparedSql(prepardSql);
|
||||
return prepardSql;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Named parameters need to be parsed and replaced with ?.
|
||||
*/
|
||||
private void parseNamedParams(OrderedList orderedList) {
|
||||
|
||||
parseNamedParams(0, orderedList);
|
||||
}
|
||||
|
||||
private void parseNamedParams(int startPos, OrderedList orderedList) {
|
||||
|
||||
if (sql == null){
|
||||
throw new PersistenceException("query does not contain any named bind parameters?");
|
||||
}
|
||||
if (startPos > sql.length()) {
|
||||
return;
|
||||
param = params.getParameter(paramName);
|
||||
}
|
||||
|
||||
// search for quotes and named params... in order...
|
||||
int beginQuotePos = sql.indexOf(quote, startPos);
|
||||
int nameParamStart = sql.indexOf(colon, startPos);
|
||||
if (beginQuotePos > 0 && beginQuotePos < nameParamStart) {
|
||||
// the quote precedes the named parameter...
|
||||
// find and add up to the end quote
|
||||
int endQuotePos = sql.indexOf(quote, beginQuotePos + 1);
|
||||
String sub = sql.substring(startPos, endQuotePos + 1);
|
||||
orderedList.appendSql(sub);
|
||||
if (param == null) {
|
||||
String msg = "Bind value is not set or null for [" + paramName + "] in [" + sql + "]";
|
||||
throw new PersistenceException(msg);
|
||||
}
|
||||
|
||||
// start again after the end quote
|
||||
parseNamedParams(endQuotePos + 1, orderedList);
|
||||
String sub = sql.substring(startPos, nameParamStart);
|
||||
orderedList.appendSql(sub);
|
||||
|
||||
} else {
|
||||
if (nameParamStart < 0) {
|
||||
// no more params, add the rest
|
||||
String sub = sql.substring(startPos, sql.length());
|
||||
orderedList.appendSql(sub);
|
||||
|
||||
} else {
|
||||
// find the end of the parameter name
|
||||
int endOfParam = nameParamStart + 1;
|
||||
do {
|
||||
char c = sql.charAt(endOfParam);
|
||||
if (c != '_' && !Character.isLetterOrDigit(c)) {
|
||||
break;
|
||||
}
|
||||
endOfParam++;
|
||||
} while (endOfParam < sql.length());
|
||||
|
||||
// add the named parameter value to bindList
|
||||
String paramName = sql.substring(nameParamStart + 1, endOfParam);
|
||||
|
||||
Param param;
|
||||
if (paramName.startsWith(ENCRYPTKEY_PREFIX)){
|
||||
param = addEncryptKeyParam(paramName);
|
||||
} else {
|
||||
param = params.getParameter(paramName);
|
||||
}
|
||||
|
||||
if (param == null) {
|
||||
String msg = "Bind value is not set or null for [" + paramName
|
||||
+ "] in [" + sql+ "]";
|
||||
throw new PersistenceException(msg);
|
||||
}
|
||||
|
||||
String sub = sql.substring(startPos, nameParamStart);
|
||||
orderedList.appendSql(sub);
|
||||
|
||||
// check if inValue is a Collection type...
|
||||
Object inValue = param.getInValue();
|
||||
if (inValue != null && inValue instanceof Collection<?>){
|
||||
// Chop up Collection parameter into a number
|
||||
// of individual parameters and add each one individually
|
||||
Collection<?> collection = (Collection<?>)inValue;
|
||||
int c = 0;
|
||||
for (Object elVal : collection) {
|
||||
if (++c > 1){
|
||||
orderedList.appendSql(",");
|
||||
}
|
||||
orderedList.appendSql("?");
|
||||
BindParams.Param elParam = new BindParams.Param();
|
||||
elParam.setInValue(elVal);
|
||||
orderedList.add(elParam);
|
||||
}
|
||||
|
||||
} else {
|
||||
// its a normal scalar value parameter...
|
||||
orderedList.add(param);
|
||||
orderedList.appendSql("?");
|
||||
}
|
||||
|
||||
// continue on after the end of the parameter
|
||||
parseNamedParams(endOfParam, orderedList);
|
||||
// check if inValue is a Collection type...
|
||||
Object inValue = param.getInValue();
|
||||
if (inValue != null && inValue instanceof Collection<?>) {
|
||||
// Chop up Collection parameter into a number
|
||||
// of individual parameters and add each one individually
|
||||
Collection<?> collection = (Collection<?>) inValue;
|
||||
int c = 0;
|
||||
for (Object elVal : collection) {
|
||||
if (++c > 1) {
|
||||
orderedList.appendSql(",");
|
||||
}
|
||||
}
|
||||
}
|
||||
orderedList.appendSql("?");
|
||||
BindParams.Param elParam = new BindParams.Param();
|
||||
elParam.setInValue(elVal);
|
||||
orderedList.add(elParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an encryption key bind parameter.
|
||||
*/
|
||||
private Param addEncryptKeyParam(String keyNamedParam) {
|
||||
|
||||
|
||||
int pos = keyNamedParam.indexOf(ENCRYPTKEY_GAP, ENCRYPTKEY_PREFIX_LEN);
|
||||
|
||||
String tableName = keyNamedParam.substring(ENCRYPTKEY_PREFIX_LEN, pos);
|
||||
String columnName = keyNamedParam.substring(pos+ENCRYPTKEY_GAP_LEN);
|
||||
|
||||
EncryptKey key = beanDescriptor.getEncryptKey(tableName, columnName);
|
||||
String strKey = key.getStringValue();
|
||||
|
||||
return params.setEncryptionKey(keyNamedParam, strKey);
|
||||
} else {
|
||||
// its a normal scalar value parameter...
|
||||
orderedList.add(param);
|
||||
orderedList.appendSql("?");
|
||||
}
|
||||
|
||||
// continue on after the end of the parameter
|
||||
parseNamedParams(endOfParam, orderedList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an encryption key bind parameter.
|
||||
*/
|
||||
private Param addEncryptKeyParam(String keyNamedParam) {
|
||||
|
||||
int pos = keyNamedParam.indexOf(ENCRYPTKEY_GAP, ENCRYPTKEY_PREFIX_LEN);
|
||||
|
||||
String tableName = keyNamedParam.substring(ENCRYPTKEY_PREFIX_LEN, pos);
|
||||
String columnName = keyNamedParam.substring(pos + ENCRYPTKEY_GAP_LEN);
|
||||
|
||||
EncryptKey key = beanDescriptor.getEncryptKey(tableName, columnName);
|
||||
String strKey = key.getStringValue();
|
||||
|
||||
return params.setEncryptionKey(keyNamedParam, strKey);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user