mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-17 22:56:28 +00:00
#1348 - ENH: Add support for bitwiseAnd / bitwiseAny / bitwiseAll .. expressions for use with integer/long "Flags" properties
This commit is contained in:
1 parent
4bc1f2ede4
commit
602a35d0b6
29 files changed
+652
-52
No files matched your search
@@ -0,0 +1,60 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.BitwiseOp;
|
||||
|
||||
/**
|
||||
* Not supported JSON or ARRAY expression handler.
|
||||
*/
|
||||
abstract class BaseDbExpression implements DbExpressionHandler {
|
||||
|
||||
private final String concatOperator;
|
||||
|
||||
BaseDbExpression(String concatOperator) {
|
||||
this.concatOperator = concatOperator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConcatOperator() {
|
||||
return concatOperator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bitwise(SpiExpressionRequest request, String propName, BitwiseOp operator, long flags, String compare, long match) {
|
||||
|
||||
String bitOp = bitOp(operator);
|
||||
request.append("(").append(propName).append(" ").append(bitOp).append(" ? ").append(compare).append(" ?)");
|
||||
}
|
||||
|
||||
private String bitOp(BitwiseOp operator) {
|
||||
switch (operator) {
|
||||
case ANY:
|
||||
case AND:
|
||||
case ALL:
|
||||
return "&";
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown Bitwise operator " + operator + " not handled?");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Common alternative where the bitwise operation is a function (specifically bitand is used - H2 and Oracle).
|
||||
*/
|
||||
protected void bitwiseFunction(SpiExpressionRequest request, String propName, BitwiseOp operator, String compare) {
|
||||
|
||||
String funcName = functionName(operator);
|
||||
request.append(funcName).append("(").append(propName).append(", ?) ").append(compare).append(" ?");
|
||||
}
|
||||
|
||||
protected String functionName(BitwiseOp operator) {
|
||||
switch (operator) {
|
||||
case AND:
|
||||
case ANY:
|
||||
case ALL:
|
||||
return "bitand";
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown Bitwise operator " + operator + " not handled?");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.Op;
|
||||
|
||||
/**
|
||||
* Not supported JSON or ARRAY expression handler.
|
||||
*/
|
||||
public class BasicDbExpression extends BaseDbExpression {
|
||||
|
||||
BasicDbExpression(String concatOperator) {
|
||||
super(concatOperator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void json(SpiExpressionRequest request, String propName, String path, Op operator, Object value) {
|
||||
throw new RuntimeException("JSON expressions only supported on Postgres and Oracle");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayContains(SpiExpressionRequest request, String propName, boolean contains, Object... values) {
|
||||
throw new RuntimeException("ARRAY expressions only supported on Postgres");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayIsEmpty(SpiExpressionRequest request, String propName, boolean empty) {
|
||||
throw new RuntimeException("ARRAY expressions only supported on Postgres");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.BitwiseOp;
|
||||
import io.ebeaninternal.server.expression.Op;
|
||||
|
||||
/**
|
||||
* Adds the db platform specific json expression.
|
||||
*/
|
||||
public interface DbExpressionHandler {
|
||||
|
||||
/**
|
||||
* Return the DB concat operator (Usually SQL standard "||").
|
||||
*/
|
||||
String getConcatOperator();
|
||||
|
||||
/**
|
||||
* Write the db platform specific json expression.
|
||||
*/
|
||||
void json(SpiExpressionRequest request, String propName, String path, Op operator, Object value);
|
||||
|
||||
/**
|
||||
* Add SQL for ARRAY CONTAINS expression.
|
||||
*/
|
||||
void arrayContains(SpiExpressionRequest request, String propName, boolean contains, Object... values);
|
||||
|
||||
/**
|
||||
* Add SQL for ARRAY IS EMPTY expression.
|
||||
*/
|
||||
void arrayIsEmpty(SpiExpressionRequest request, String propName, boolean empty);
|
||||
|
||||
/**
|
||||
* Add the bitwise expression.
|
||||
*/
|
||||
void bitwise(SpiExpressionRequest request, String propName, BitwiseOp operator, long flags, String compare, long match);
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
|
||||
|
||||
public class DbExpressionHandlerFactory {
|
||||
|
||||
/**
|
||||
* Create and return the appropriate platform specific handing of expressions.
|
||||
*/
|
||||
public static DbExpressionHandler from(DatabasePlatform databasePlatform) {
|
||||
|
||||
Platform platform = databasePlatform.getPlatform();
|
||||
String concatOperator = databasePlatform.getConcatOperator();
|
||||
switch (platform) {
|
||||
case H2:
|
||||
return new H2DbExpression(concatOperator);
|
||||
case POSTGRES:
|
||||
return new PostgresDbExpression(concatOperator);
|
||||
case MYSQL:
|
||||
return new MySqlDbExpression(concatOperator);
|
||||
case ORACLE:
|
||||
return new OracleDbExpression(concatOperator);
|
||||
case SQLSERVER:
|
||||
return new SqlServerDbExpression(concatOperator);
|
||||
default:
|
||||
return new BasicDbExpression(concatOperator);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.BitwiseOp;
|
||||
|
||||
/**
|
||||
* H2 handling of platform specific expressions.
|
||||
*/
|
||||
class H2DbExpression extends BasicDbExpression {
|
||||
|
||||
H2DbExpression(String concatOperator) {
|
||||
super(concatOperator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bitwise(SpiExpressionRequest request, String propName, BitwiseOp operator, long flags, String compare, long match) {
|
||||
|
||||
bitwiseFunction(request, propName, operator, compare);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
/**
|
||||
* MySql specific handling of platform specific expressions.
|
||||
*/
|
||||
class MySqlDbExpression extends BasicDbExpression {
|
||||
|
||||
MySqlDbExpression(String concatOperator) {
|
||||
super(concatOperator);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.BitwiseOp;
|
||||
import io.ebeaninternal.server.expression.Op;
|
||||
|
||||
/**
|
||||
* Oracle handling of platform specific expressions. ARRAY expressions not supported.
|
||||
*/
|
||||
public class OracleDbExpression extends BaseDbExpression {
|
||||
|
||||
OracleDbExpression(String concatOperator) {
|
||||
super(concatOperator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void json(SpiExpressionRequest request, String propName, String path, Op operator, Object value) {
|
||||
|
||||
if (operator == Op.EXISTS) {
|
||||
request.append("json_exists(").append(propName).append(", '$.").append(path).append("')");
|
||||
} else if (operator == Op.NOT_EXISTS) {
|
||||
request.append("not json_exists(").append(propName).append(", '$.").append(path).append("')");
|
||||
} else {
|
||||
request.append("json_value(").append(propName).append(", '$.").append(path).append("')");
|
||||
request.append(operator.bind());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bitwise(SpiExpressionRequest request, String propName, BitwiseOp operator, long flags, String compare, long match) {
|
||||
bitwiseFunction(request, propName, operator, compare);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayContains(SpiExpressionRequest request, String propName, boolean contains, Object... values) {
|
||||
throw new IllegalStateException("ARRAY expressions not supported on Oracle");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayIsEmpty(SpiExpressionRequest request, String propName, boolean empty) {
|
||||
throw new IllegalStateException("ARRAY expressions not supported on Oracle");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
/**
|
||||
* Helper for determining type casting for JSON and ARRAY expressions.
|
||||
*/
|
||||
public class PostgresCast {
|
||||
|
||||
/**
|
||||
* Postgres CAST the type if necessary.
|
||||
* <p>
|
||||
* This is generally necessary for JSON expressions as text values always returned from the json operators used.
|
||||
* </p>
|
||||
*/
|
||||
protected static String cast(Object value) {
|
||||
return cast(value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Postgres CAST the type if necessary additionally specify if DB ARRAY is used.
|
||||
*/
|
||||
protected static String cast(Object value, boolean asArray) {
|
||||
|
||||
if (value == null) {
|
||||
// for exists and not-exists expressions
|
||||
return "";
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return asArray ? "::integer[]" : "::integer";
|
||||
}
|
||||
if (value instanceof Long) {
|
||||
return asArray ? "::bigint[]" : "::bigint";
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return asArray ? "::decimal[]" : "::decimal";
|
||||
}
|
||||
if (value instanceof Boolean) {
|
||||
return asArray ? "::boolean[]" : "::boolean";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.Op;
|
||||
|
||||
/**
|
||||
* Postgres JSON and ARRAY expression handler
|
||||
*/
|
||||
public class PostgresDbExpression extends BaseDbExpression {
|
||||
|
||||
PostgresDbExpression(String concatOperator) {
|
||||
super(concatOperator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void json(SpiExpressionRequest request, String propName, String path, Op operator, Object value) {
|
||||
|
||||
StringBuilder sb = new StringBuilder(50);
|
||||
String[] paths = path.split("\\.");
|
||||
if (paths.length == 1) {
|
||||
// (t0.content ->> 'title') = 'Some value'
|
||||
sb.append("(").append(propName).append(" ->> '").append(path).append("')");
|
||||
|
||||
} else {
|
||||
// (t0.content #>> '{path,inner}') = 'Some value'
|
||||
sb.append("(").append(propName).append(" #>> '{");
|
||||
for (int i = 0; i < paths.length; i++) {
|
||||
if (i > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(paths[i]);
|
||||
}
|
||||
sb.append("}')");
|
||||
}
|
||||
|
||||
request.append(sb.toString());
|
||||
request.append(PostgresCast.cast(value));
|
||||
request.append(operator.bind());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayContains(SpiExpressionRequest request, String propName, boolean contains, Object... values) {
|
||||
|
||||
if (!contains) {
|
||||
request.append("not (");
|
||||
}
|
||||
request.append(propName).append(" @> array[?");
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
request.append(",?");
|
||||
}
|
||||
request.append("]");
|
||||
request.append(PostgresCast.cast(values[0], true));
|
||||
if (!contains) {
|
||||
request.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayIsEmpty(SpiExpressionRequest request, String propName, boolean empty) {
|
||||
|
||||
request.append("coalesce(cardinality(").append(propName).append("),0)");
|
||||
if (empty) {
|
||||
request.append(" = 0");
|
||||
} else {
|
||||
request.append(" <> 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.Op;
|
||||
|
||||
/**
|
||||
* Microsoft SQL Server JSON. ARRAY expressions not supported.
|
||||
*/
|
||||
public class SqlServerDbExpression extends BaseDbExpression {
|
||||
|
||||
SqlServerDbExpression(String concatOperator) {
|
||||
super(concatOperator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void json(final SpiExpressionRequest request, final String propName,
|
||||
final String path, final Op operator, final Object value) {
|
||||
request.append("json_value(").append(propName).append(", '$.").append(path).append("')");
|
||||
request.append(operator.bind());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayContains(final SpiExpressionRequest request, final String propName,
|
||||
final boolean contains, final Object... values) {
|
||||
throw new RuntimeException("ARRAY expressions not supported on Microsoft SQL Server");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayIsEmpty(final SpiExpressionRequest request, final String propName, final boolean empty) {
|
||||
throw new RuntimeException("ARRAY expressions not supported on Microsoft SQL Server");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user