mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
#695 - ENH: Add @DbArray for storing scalar types (UUID, Long, String) to Postgres ARRAY type
This commit is contained in:
1 parent
2bb39b02d0
commit
b197d8a637
53 files changed
+1102
-229
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
|
||||
import com.avaje.ebeaninternal.api.SpiExpression;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Contains expression for ARRAY type.
|
||||
*/
|
||||
public class ArrayContainsExpression extends AbstractExpression {
|
||||
|
||||
private final boolean contains;
|
||||
|
||||
private final Object[] values;
|
||||
|
||||
protected ArrayContainsExpression(String propName, boolean contains, Object... values) {
|
||||
super(propName);
|
||||
this.contains = contains;
|
||||
this.values = values;
|
||||
if (values == null || values.length == 0) {
|
||||
throw new IllegalArgumentException("values must not be null or empty");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDocQuery(DocQueryContext context) throws IOException {
|
||||
|
||||
if (values.length == 1) {
|
||||
context.writeEqualTo(propName, values[0]);
|
||||
} else {
|
||||
if (contains) {
|
||||
context.startBoolMust();
|
||||
} else {
|
||||
context.startBoolMustNot();
|
||||
}
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
context.writeEqualTo(propName, values[i]);
|
||||
}
|
||||
context.endBool();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queryPlanHash(HashQueryPlanBuilder builder) {
|
||||
builder.add(ArrayContainsExpression.class).add(propName).add(contains);
|
||||
builder.bind(values.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int queryBindHash() {
|
||||
int hc = values[0].hashCode();
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
hc = hc * 31 + values[i].hashCode();
|
||||
}
|
||||
return hc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameByPlan(SpiExpression other) {
|
||||
if (!(other instanceof ArrayContainsExpression)) {
|
||||
return false;
|
||||
}
|
||||
ArrayContainsExpression that = (ArrayContainsExpression) other;
|
||||
return this.propName.equals(that.propName)
|
||||
&& this.contains == that.contains
|
||||
&& this.values.length == that.values.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameByBind(SpiExpression other) {
|
||||
ArrayContainsExpression that = (ArrayContainsExpression) other;
|
||||
for (int i = 0; i < this.values.length; i++) {
|
||||
if (!this.values[i].equals(that.values[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSql(SpiExpressionRequest request) {
|
||||
request.getDbPlatformHandler().arrayContains(request, propName, contains, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBindValues(SpiExpressionRequest request) {
|
||||
for (Object value : values) {
|
||||
request.addBindValue(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
|
||||
import com.avaje.ebeaninternal.api.SpiExpression;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* IsEmpty expression for ARRAY type.
|
||||
*/
|
||||
public class ArrayIsEmptyExpression extends AbstractExpression {
|
||||
|
||||
private final boolean empty;
|
||||
|
||||
protected ArrayIsEmptyExpression(String propName, boolean empty) {
|
||||
super(propName);
|
||||
this.empty = empty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDocQuery(DocQueryContext context) throws IOException {
|
||||
context.writeExists(!empty, propName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queryPlanHash(HashQueryPlanBuilder builder) {
|
||||
builder.add(ArrayIsEmptyExpression.class).add(propName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int queryBindHash() {
|
||||
return empty ? 0 : 31;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameByPlan(SpiExpression other) {
|
||||
if (!(other instanceof ArrayIsEmptyExpression)) {
|
||||
return false;
|
||||
}
|
||||
ArrayIsEmptyExpression that = (ArrayIsEmptyExpression) other;
|
||||
return this.propName.equals(that.propName)
|
||||
&& this.empty == that.empty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameByBind(SpiExpression other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSql(SpiExpressionRequest request) {
|
||||
request.getDbPlatformHandler().arrayIsEmpty(request, propName, empty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBindValues(SpiExpressionRequest request) {
|
||||
// nothing to bind
|
||||
}
|
||||
}
|
||||
@@ -106,6 +106,26 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
|
||||
return new JsonPathExpression(propertyName, path, lowerValue, upperValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression arrayContains(String propertyName, Object... values) {
|
||||
return new ArrayContainsExpression(propertyName, true, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression arrayNotContains(String propertyName, Object... values) {
|
||||
return new ArrayContainsExpression(propertyName, false, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression arrayIsEmpty(String propertyName) {
|
||||
return new ArrayIsEmptyExpression(propertyName, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression arrayIsNotEmpty(String propertyName) {
|
||||
return new ArrayIsEmptyExpression(propertyName, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Equal To - property equal to the given value.
|
||||
*/
|
||||
|
||||
@@ -826,6 +826,30 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> arrayContains(String propertyName, Object... elementValue) {
|
||||
add(expr.arrayContains(propertyName, elementValue));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> arrayNotContains(String propertyName, Object... values) {
|
||||
add(expr.arrayNotContains(propertyName, values));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> arrayIsEmpty(String propertyName) {
|
||||
add(expr.arrayIsEmpty(propertyName));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> arrayIsNotEmpty(String propertyName) {
|
||||
add(expr.arrayIsNotEmpty(propertyName));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> raw(String raw, Object value) {
|
||||
add(expr.raw(raw, value));
|
||||
|
||||
+3
-3
@@ -6,7 +6,7 @@ import java.util.List;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionList;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.server.core.JsonExpressionHandler;
|
||||
import com.avaje.ebeaninternal.server.core.DbExpressionHandler;
|
||||
import com.avaje.ebeaninternal.server.core.SpiOrmQueryRequest;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.DeployParser;
|
||||
@@ -73,8 +73,8 @@ public class DefaultExpressionRequest implements SpiExpressionRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonExpressionHandler getJsonHandler() {
|
||||
return binder.getJsonExpressionHandler();
|
||||
public DbExpressionHandler getDbPlatformHandler() {
|
||||
return binder.getDbExpressionHandler();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -108,7 +108,7 @@ class JsonPathExpression extends AbstractExpression {
|
||||
public void addSql(SpiExpressionRequest request) {
|
||||
|
||||
// Use DB specific expression handling (Postgres and Oracle supported)
|
||||
request.getJsonHandler().addSql(request, propName, path, operator, value);
|
||||
request.getDbPlatformHandler().json(request, propName, path, operator, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -462,6 +462,26 @@ class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expression
|
||||
return exprList.jsonBetween(propertyName, path, lowerValue, upperValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> arrayContains(String propertyName, Object... values) {
|
||||
return exprList.arrayContains(propertyName, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> arrayNotContains(String propertyName, Object... values) {
|
||||
return exprList.arrayNotContains(propertyName, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> arrayIsEmpty(String propertyName) {
|
||||
return exprList.arrayIsEmpty(propertyName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> arrayIsNotEmpty(String propertyName) {
|
||||
return exprList.arrayIsNotEmpty(propertyName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> ge(String propertyName, Object value) {
|
||||
return exprList.ge(propertyName, value);
|
||||
|
||||
Reference in new issue
Block a user