mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
EQL - parsing IN expressions
This commit is contained in:
@@ -1,35 +1,54 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.event.BeanQueryRequest;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
|
||||
import com.avaje.ebeaninternal.api.SpiExpression;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
class InExpression extends AbstractExpression {
|
||||
|
||||
private final boolean not;
|
||||
|
||||
private final Object[] values;
|
||||
private final Collection<?> sourceValues;
|
||||
|
||||
InExpression(String propertyName, Collection<?> coll, boolean not) {
|
||||
private Object[] bindValues;
|
||||
|
||||
InExpression(String propertyName, Collection<?> sourceValues, boolean not) {
|
||||
super(propertyName);
|
||||
this.values = coll.toArray(new Object[coll.size()]);
|
||||
this.sourceValues = sourceValues;
|
||||
this.not = not;
|
||||
}
|
||||
|
||||
InExpression(String propertyName, Object[] array, boolean not) {
|
||||
super(propertyName);
|
||||
this.values = array;
|
||||
this.sourceValues = Arrays.asList(array);
|
||||
this.not = not;
|
||||
}
|
||||
|
||||
private Object[] values() {
|
||||
List<Object> vals = new ArrayList<Object>();
|
||||
for (Object sourceValue : sourceValues) {
|
||||
NamedParamHelp.valueAdd(vals, sourceValue);
|
||||
}
|
||||
return vals.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareExpression(BeanQueryRequest<?> request) {
|
||||
bindValues = values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDocQuery(DocQueryContext context) throws IOException {
|
||||
context.writeIn(propName, values, not);
|
||||
context.writeIn(propName, values(), not);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,13 +59,13 @@ class InExpression extends AbstractExpression {
|
||||
prop = null;
|
||||
}
|
||||
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
for (int i = 0; i < bindValues.length; i++) {
|
||||
if (prop == null) {
|
||||
request.addBindValue(values[i]);
|
||||
request.addBindValue(bindValues[i]);
|
||||
|
||||
} else {
|
||||
// extract the id values from the bean
|
||||
Object[] ids = prop.getAssocIdValues((EntityBean) values[i]);
|
||||
Object[] ids = prop.getAssocIdValues((EntityBean) bindValues[i]);
|
||||
if (ids != null) {
|
||||
for (int j = 0; j < ids.length; j++) {
|
||||
request.addBindValue(ids[j]);
|
||||
@@ -59,7 +78,7 @@ class InExpression extends AbstractExpression {
|
||||
@Override
|
||||
public void addSql(SpiExpressionRequest request) {
|
||||
|
||||
if (values.length == 0) {
|
||||
if (bindValues.length == 0) {
|
||||
String expr = not ? "1=1" : "1=0";
|
||||
request.append(expr);
|
||||
return;
|
||||
@@ -72,7 +91,7 @@ class InExpression extends AbstractExpression {
|
||||
|
||||
if (prop != null) {
|
||||
request.append(prop.getAssocIdInExpr(propName));
|
||||
String inClause = prop.getAssocIdInValueExpr(values.length);
|
||||
String inClause = prop.getAssocIdInValueExpr(bindValues.length);
|
||||
request.append(inClause);
|
||||
|
||||
} else {
|
||||
@@ -81,7 +100,7 @@ class InExpression extends AbstractExpression {
|
||||
request.append(" not");
|
||||
}
|
||||
request.append(" in (?");
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
for (int i = 1; i < bindValues.length; i++) {
|
||||
request.append(", ").append("?");
|
||||
}
|
||||
|
||||
@@ -94,15 +113,15 @@ class InExpression extends AbstractExpression {
|
||||
*/
|
||||
@Override
|
||||
public void queryPlanHash(HashQueryPlanBuilder builder) {
|
||||
builder.add(InExpression.class).add(propName).add(values.length).add(not);
|
||||
builder.bind(values.length);
|
||||
builder.add(InExpression.class).add(propName).add(bindValues.length).add(not);
|
||||
builder.bind(bindValues.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int queryBindHash() {
|
||||
int hc = 31;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
hc = 31 * hc + values[i].hashCode();
|
||||
for (int i = 0; i < bindValues.length; i++) {
|
||||
hc = 31 * hc + bindValues[i].hashCode();
|
||||
}
|
||||
return hc;
|
||||
}
|
||||
@@ -116,17 +135,17 @@ class InExpression extends AbstractExpression {
|
||||
InExpression that = (InExpression) other;
|
||||
return propName.equals(that.propName)
|
||||
&& not == that.not
|
||||
&& values.length == that.values.length;
|
||||
&& bindValues.length == that.bindValues.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameByBind(SpiExpression other) {
|
||||
InExpression that = (InExpression) other;
|
||||
if (this.values.length != that.values.length) {
|
||||
if (this.bindValues.length != that.bindValues.length) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (!values[i].equals(that.values[i])) {
|
||||
for (int i = 0; i < bindValues.length; i++) {
|
||||
if (!bindValues[i].equals(that.bindValues[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiNamedParam;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Helper for evaluating named parameters.
|
||||
*/
|
||||
@@ -25,4 +28,16 @@ class NamedParamHelp {
|
||||
return (value == null) ? null : value.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the potentially named parameter(s) to the values.
|
||||
*/
|
||||
public static void valueAdd(List<Object> values, Object sourceValue) {
|
||||
|
||||
Object value = value(sourceValue);
|
||||
if (value instanceof Collection) {
|
||||
values.addAll((Collection)value);
|
||||
} else {
|
||||
values.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.avaje.ebeaninternal.server.grammer;
|
||||
import com.avaje.ebean.Expression;
|
||||
import com.avaje.ebean.ExpressionList;
|
||||
import com.avaje.ebean.LikeType;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.grammer.antlr.EQLBaseListener;
|
||||
import com.avaje.ebeaninternal.server.grammer.antlr.EQLLexer;
|
||||
@@ -13,6 +12,9 @@ import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class EqlAdapter<T> extends EQLBaseListener {
|
||||
|
||||
private static final OperatorMapping operatorMapping = new OperatorMapping();
|
||||
@@ -27,6 +29,10 @@ class EqlAdapter<T> extends EQLBaseListener {
|
||||
|
||||
private boolean textMode;
|
||||
|
||||
private List<Object> inValues;
|
||||
|
||||
private String inPropertyName;
|
||||
|
||||
public EqlAdapter(SpiQuery<T> query) {
|
||||
this.query = query;
|
||||
this.helper = new EqlAdapterHelper(this);
|
||||
@@ -100,7 +106,25 @@ class EqlAdapter<T> extends EQLBaseListener {
|
||||
|
||||
@Override
|
||||
public void enterIn_expression(EQLParser.In_expressionContext ctx) {
|
||||
this.inValues = new ArrayList<Object>();
|
||||
this.inPropertyName = getLeftHandSidePath(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterIn_value(EQLParser.In_valueContext ctx) {
|
||||
int childCount = ctx.getChildCount();
|
||||
for (int i = 0; i < childCount; i++) {
|
||||
ParseTree child = ctx.getChild(i);
|
||||
String text = child.getText();
|
||||
if (!text.equals("(") && !text.equals(")")) {
|
||||
inValues.add(helper.bind(text));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitIn_expression(EQLParser.In_expressionContext ctx) {
|
||||
helper.addIn(inPropertyName, inValues);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.avaje.ebean.ExpressionList;
|
||||
import com.avaje.ebean.LikeType;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
class EqlAdapterHelper {
|
||||
|
||||
@@ -40,6 +41,10 @@ class EqlAdapterHelper {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void addIn(String path, List<Object> inValues) {
|
||||
peekExprList().in(path, inValues);
|
||||
}
|
||||
|
||||
protected void addExpression(String path, EqlOperator op, String value) {
|
||||
|
||||
@@ -99,7 +104,7 @@ class EqlAdapterHelper {
|
||||
peekExprList().add(owner.like(caseInsensitive, likeType, path, bindValue));
|
||||
}
|
||||
|
||||
private Object bind(String value) {
|
||||
protected Object bind(String value) {
|
||||
ValueType valueType = getValueType(value);
|
||||
return getBindValue(valueType, value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user