mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-17 22:56:28 +00:00
#1186 - ENH: Support using TableValues / Array for select prop in (?) queries
This commit is contained in:
1 parent
89dac3820b
commit
6e62b72fb9
43 files changed
+756
-131
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user