#1186 - ENH: Support using TableValues / Array for select prop in (?) queries

This commit is contained in:
rob bygrave committed 2017-10-31 02:53:20 +13:00
1 parent 89dac3820b
commit 6e62b72fb9
43 files changed
+756 -131

No files matched your search

@@ -5,6 +5,7 @@ import io.ebeaninternal.api.BindParams;
import io.ebeaninternal.server.core.DbExpressionHandler;
import io.ebeaninternal.server.core.Message;
import io.ebeaninternal.server.core.timezone.DataTimeZone;
import io.ebeaninternal.server.persist.platform.MultiValueBind;
import io.ebeaninternal.server.type.DataBind;
import io.ebeaninternal.server.type.ScalarType;
import io.ebeaninternal.server.type.TypeManager;
@@ -19,6 +20,7 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
@@ -38,17 +40,20 @@ public class Binder {
private final DataTimeZone dataTimeZone;
private final MultiValueBind multiValueBind;
/**
* Set the PreparedStatement with which to bind variables to.
*/
public Binder(TypeManager typeManager, int asOfBindCount, boolean asOfStandardsBased,
DbExpressionHandler dbExpressionHandler, DataTimeZone dataTimeZone) {
DbExpressionHandler dbExpressionHandler, DataTimeZone dataTimeZone, MultiValueBind multiValueBind) {
this.typeManager = typeManager;
this.asOfBindCount = asOfBindCount;
this.asOfStandardsBased = asOfStandardsBased;
this.dbExpressionHandler = dbExpressionHandler;
this.dataTimeZone = dataTimeZone;
this.multiValueBind = multiValueBind;
}
/**
@@ -168,6 +173,26 @@ public class Binder {
}
}
/**
* Return true if MultiValue binding is supported for the given type.
*/
public boolean isMultiValueSupported(Class<?> cls) {
try {
ScalarType<?> scalarType = getScalarType(cls);
return multiValueBind.isTypeSupported(scalarType.getJdbcType());
} catch (PersistenceException e) {
return false;
}
}
private ScalarType<?> getScalarType(Class<?> clazz) {
ScalarType<?> type = typeManager.getScalarType(clazz);
if (type == null) {
throw new PersistenceException("No ScalarType registered for " + clazz);
}
return type;
}
/**
* Bind an Object with unknown data type.
*/
@@ -178,15 +203,19 @@ public class Binder {
bindObject(dataBind, null, Types.OTHER);
return null;
} else if (value instanceof MultiValueWrapper) {
MultiValueWrapper wrapper = (MultiValueWrapper) value;
Collection<?> values = wrapper.getValues();
ScalarType<?> type = getScalarType(wrapper.getType());
int dbType = type.getJdbcType();
// let the multiValueBind decide what to do with the value
multiValueBind.bindMultiValues(dataBind, values, type, one -> bindObject(dataBind, one, dbType));
return values;
} else {
ScalarType<?> type = typeManager.getScalarType(value.getClass());
if (type == null) {
// the type is not registered with the TypeManager.
String msg = "No ScalarType registered for " + value.getClass();
throw new PersistenceException(msg);
} else if (!type.isJdbcNative()) {
ScalarType<?> type = getScalarType(value.getClass());
if (!type.isJdbcNative()) {
// convert to a JDBC native type
value = type.toJdbcType(value);
}
@@ -197,6 +226,14 @@ public class Binder {
}
}
/**
* Return the SQL in clause taking into account Multi-value support.
*/
public String getInExpression(Object[] bindValues) {
ScalarType<?> type = getScalarType(bindValues[0].getClass());
return multiValueBind.getInExpression(type, bindValues.length);
}
/**
* bind a single value.
* <p>
@@ -0,0 +1,40 @@
package io.ebeaninternal.server.persist;
import java.util.Collection;
/**
* Wraps the multi values that are used for "property in (...)" queries
* @author Roland Praml, FOCONIS AG
*/
public class MultiValueWrapper {
private final Collection<?> values;
private Class<?> type;
public MultiValueWrapper(Collection<?> values) {
this.values = values;
this.type = values.iterator().next().getClass();
}
public Collection<?> getValues() {
return values;
}
public Class<?> getType() {
return type;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Array[" + values.size() + "]={");
for (Object value : values) {
sb.append(value).append(',');
if (sb.length() > 50) {
sb.append("...}");
return sb.toString();
}
}
sb.setLength(sb.length() - 1);
sb.append('}');
return sb.toString();
}
}
@@ -0,0 +1,93 @@
package io.ebeaninternal.server.persist.platform;
import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebeaninternal.server.type.DataBind;
import io.ebeaninternal.server.type.ScalarType;
import java.sql.SQLException;
import java.util.Collection;
import static java.sql.Types.BIGINT;
import static java.sql.Types.BIT;
import static java.sql.Types.BOOLEAN;
import static java.sql.Types.CHAR;
import static java.sql.Types.DATE;
import static java.sql.Types.DECIMAL;
import static java.sql.Types.DOUBLE;
import static java.sql.Types.FLOAT;
import static java.sql.Types.INTEGER;
import static java.sql.Types.NCHAR;
import static java.sql.Types.NUMERIC;
import static java.sql.Types.NVARCHAR;
import static java.sql.Types.REAL;
import static java.sql.Types.SMALLINT;
import static java.sql.Types.TIMESTAMP;
import static java.sql.Types.TIMESTAMP_WITH_TIMEZONE;
import static java.sql.Types.TIME_WITH_TIMEZONE;
import static java.sql.Types.TINYINT;
import static java.sql.Types.VARCHAR;
/**
* Base MultiValueBind for platform specific support.
*/
abstract class AbstractMultiValueBind extends MultiValueBind {
@Override
public boolean isSupported() {
return true;
}
@Override
public boolean isTypeSupported(int jdbcType) {
return getArrayType(jdbcType) != null;
}
@Override
public void bindMultiValues(DataBind dataBind, Collection<?> values, ScalarType<?> type, BindOne bindOne) throws SQLException {
String arrayType = getArrayType(type.getJdbcType());
if (arrayType == null) {
super.bindMultiValues(dataBind, values, type, bindOne);
} else {
dataBind.setArray(arrayType, toArray(values, type));
}
}
protected String getArrayType(int dbType) {
switch(dbType) {
case TINYINT:
case SMALLINT:
case INTEGER:
case BIGINT:
case DECIMAL: // TODO: we have no info about precision here
case NUMERIC:
return "bigint";
case REAL:
case FLOAT:
case DOUBLE:
return "float";
case BIT:
case BOOLEAN:
return "bit";
case DATE:
return "date";
case TIMESTAMP:
case TIME_WITH_TIMEZONE:
case TIMESTAMP_WITH_TIMEZONE:
return "timestamp";
//case LONGVARCHAR:
//case CLOB:
case CHAR:
case VARCHAR:
//case LONGNVARCHAR:
//case NCLOB:
case NCHAR:
case NVARCHAR:
return "varchar";
case ExtraDbTypes.UUID: // Db Native UUID
return "varchar";
default:
return null;
}
}
}
@@ -0,0 +1,23 @@
package io.ebeaninternal.server.persist.platform;
import io.ebeaninternal.server.type.ScalarType;
/**
* Multi value binder that uses SqlServers Table-value parameters
* @author Roland Praml, FOCONIS AG
*
*/
public class H2MultiValueBind extends AbstractMultiValueBind {
@Override
public String getInExpression(ScalarType<?> type, int size) {
String arrayType = getArrayType(type.getJdbcType());
if (arrayType == null) {
return super.getInExpression(type, size);
} else {
StringBuilder sb = new StringBuilder(50);
sb.append(" in (select * from table(x ").append(arrayType).append(" = ?)) ");
return sb.toString();
}
}
}
@@ -0,0 +1,67 @@
package io.ebeaninternal.server.persist.platform;
import io.ebeaninternal.server.type.DataBind;
import io.ebeaninternal.server.type.ScalarType;
import java.sql.SQLException;
import java.util.Collection;
/**
* Default implementation for multi value help.
*/
public class MultiValueBind {
@FunctionalInterface
public interface BindOne {
void bind(Object value) throws SQLException;
}
protected Object[] toArray(Collection<?> values, ScalarType<?> type) {
Object[] array = new Object[values.size()];
int i = 0;
for (Object value : values) {
array[i++] = type.toJdbcType(value);
}
return array;
}
/**
* Defaults to not supported and using a bind value per element.
*/
public boolean isSupported() {
return false;
}
/**
* Defaults to not supported and using a bind value per element.
*/
public boolean isTypeSupported(int jdbcType) {
return false;
}
/**
* Default for multi values. They are appended one by one.
*/
public void bindMultiValues(DataBind dataBind, Collection<?> values, ScalarType<?> type, BindOne bindOne) throws SQLException {
for (Object value : values) {
if (!type.isJdbcNative()) {
value = type.toJdbcType(value);
}
bindOne.bind(value);
}
}
/**
* Appends the 'in' expression to the request. Must add leading & trailing space!
*/
public String getInExpression(ScalarType<?> type, int size) {
StringBuilder sb = new StringBuilder();
sb.append(" in (?");
for (int i = 1; i < size; i++) {
sb.append(", ").append("?");
}
sb.append(" ) ");
return sb.toString();
}
}
@@ -0,0 +1,34 @@
package io.ebeaninternal.server.persist.platform;
import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebeaninternal.server.type.ScalarType;
/**
* Multi value binder that uses Postgres Array and unnest.
*
* @author Roland Praml, FOCONIS AG
*/
public class PostgresMultiValueBind extends AbstractMultiValueBind {
@Override
public String getInExpression(ScalarType<?> type, int size) {
int dbType = type.getJdbcType();
if (dbType == ExtraDbTypes.UUID) {
return " in (select(unnest(?))::uuid) ";
}
String arrayType = getArrayType(dbType);
if (arrayType == null) {
return super.getInExpression(type, size);
} else {
return " in (select(unnest(?))) ";
}
}
@Override
protected String getArrayType(int dbType) {
if (dbType == ExtraDbTypes.UUID) {
return "uuid";
}
return super.getArrayType(dbType);
}
}