mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
EQL Named parameter support for Like and ieq
This commit is contained in:
1 parent
1a1c432c2a
commit
84b37fb014
10 files changed
+123
-88
No files matched your search
@@ -0,0 +1,32 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
/**
|
||||
* Abstract expression that helps with named parameter use.
|
||||
*/
|
||||
public abstract class AbstractValueExpression extends AbstractExpression {
|
||||
|
||||
protected final Object bindValue;
|
||||
|
||||
/**
|
||||
* Construct with property name and potential named parameter.
|
||||
*/
|
||||
protected AbstractValueExpression(String propName, Object bindValue) {
|
||||
super(propName);
|
||||
this.bindValue = bindValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bind value taking into account named parameters.
|
||||
*/
|
||||
protected Object value() {
|
||||
return NamedParamHelp.value(bindValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the String bind value taking into account named parameters.
|
||||
*/
|
||||
protected String strValue() {
|
||||
return NamedParamHelp.valueAsString(bindValue);
|
||||
}
|
||||
|
||||
}
|
||||
+13
-9
@@ -7,18 +7,22 @@ import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class CaseInsensitiveEqualExpression extends AbstractExpression {
|
||||
class CaseInsensitiveEqualExpression extends AbstractValueExpression {
|
||||
|
||||
private final String value;
|
||||
CaseInsensitiveEqualExpression(String propertyName, Object value) {
|
||||
super(propertyName, value);
|
||||
}
|
||||
|
||||
CaseInsensitiveEqualExpression(String propertyName, String value) {
|
||||
super(propertyName);
|
||||
this.value = value.toLowerCase();
|
||||
/**
|
||||
* Return the bind value taking into account named parameters.
|
||||
*/
|
||||
private String val() {
|
||||
return strValue().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDocQuery(DocQueryContext context) throws IOException {
|
||||
context.writeIEqualTo(propName, value);
|
||||
context.writeIEqualTo(propName, val());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,7 +35,7 @@ class CaseInsensitiveEqualExpression extends AbstractExpression {
|
||||
request.addBindEncryptKey(encryptKey);
|
||||
}
|
||||
|
||||
request.addBindValue(value);
|
||||
request.addBindValue(val());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -54,7 +58,7 @@ class CaseInsensitiveEqualExpression extends AbstractExpression {
|
||||
|
||||
@Override
|
||||
public int queryBindHash() {
|
||||
return value.hashCode();
|
||||
return val().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,6 +74,6 @@ class CaseInsensitiveEqualExpression extends AbstractExpression {
|
||||
@Override
|
||||
public boolean isSameByBind(SpiExpression other) {
|
||||
CaseInsensitiveEqualExpression that = (CaseInsensitiveEqualExpression) other;
|
||||
return value.equals(that.value);
|
||||
return val().equals(that.val());
|
||||
}
|
||||
}
|
||||
+9
-3
@@ -157,6 +157,13 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
|
||||
return new CaseInsensitiveEqualExpression(propertyName, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create for named parameter use (and without support for equalsWithNullAsNoop).
|
||||
*/
|
||||
public Expression ieqObject(String propertyName, Object value) {
|
||||
return new CaseInsensitiveEqualExpression(propertyName, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Between - property between the two given values.
|
||||
*/
|
||||
@@ -252,9 +259,8 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression like(LikeType likeType, String propertyName, Object value) {
|
||||
|
||||
return new LikeExpression(propertyName, value, true, LikeType.RAW);
|
||||
public Expression like(String propertyName, Object value, boolean caseInsensitive, LikeType likeType) {
|
||||
return new LikeExpression(propertyName, value, caseInsensitive, likeType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,43 +4,25 @@ import com.avaje.ebean.LikeType;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
|
||||
import com.avaje.ebeaninternal.api.SpiExpression;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.api.SpiNamedParam;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class LikeExpression extends AbstractExpression {
|
||||
|
||||
private final Object value;
|
||||
class LikeExpression extends AbstractValueExpression {
|
||||
|
||||
private final boolean caseInsensitive;
|
||||
|
||||
private final LikeType type;
|
||||
|
||||
LikeExpression(String propertyName, Object value, boolean caseInsensitive, LikeType type) {
|
||||
super(propertyName);
|
||||
super(propertyName, value);
|
||||
this.caseInsensitive = caseInsensitive;
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bind value taking into account named parameters.
|
||||
*/
|
||||
private Object value() {
|
||||
if (value instanceof SpiNamedParam) {
|
||||
return ((SpiNamedParam)value).getValue();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private String val() {
|
||||
return value().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDocQuery(DocQueryContext context) throws IOException {
|
||||
context.writeLike(propName, val(), type, caseInsensitive);
|
||||
context.writeLike(propName, strValue(), type, caseInsensitive);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -53,7 +35,7 @@ class LikeExpression extends AbstractExpression {
|
||||
request.addBindEncryptKey(encryptKey);
|
||||
}
|
||||
|
||||
String bindValue = getValue(val(), caseInsensitive, type);
|
||||
String bindValue = getValue(strValue(), caseInsensitive, type);
|
||||
request.addBindValue(bindValue);
|
||||
}
|
||||
|
||||
@@ -89,7 +71,7 @@ class LikeExpression extends AbstractExpression {
|
||||
|
||||
@Override
|
||||
public int queryBindHash() {
|
||||
return val().hashCode();
|
||||
return strValue().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -107,7 +89,7 @@ class LikeExpression extends AbstractExpression {
|
||||
@Override
|
||||
public boolean isSameByBind(SpiExpression other) {
|
||||
LikeExpression that = (LikeExpression) other;
|
||||
return val().equals(that.val());
|
||||
return strValue().equals(that.strValue());
|
||||
}
|
||||
|
||||
private static String getValue(String value, boolean caseInsensitive, LikeType type) {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiNamedParam;
|
||||
|
||||
/**
|
||||
* Helper for evaluating named parameters.
|
||||
*/
|
||||
class NamedParamHelp {
|
||||
|
||||
/**
|
||||
* Return the bind value taking into account named parameters.
|
||||
*/
|
||||
static Object value(Object val) {
|
||||
if (val instanceof SpiNamedParam) {
|
||||
return ((SpiNamedParam) val).getValue();
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value as a string.
|
||||
*/
|
||||
static String valueAsString(Object val) {
|
||||
Object value = value(val);
|
||||
return (value == null) ? null : value.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,31 +5,17 @@ import com.avaje.ebean.plugin.ExpressionPath;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
|
||||
import com.avaje.ebeaninternal.api.SpiExpression;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.api.SpiNamedParam;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SimpleExpression extends AbstractExpression {
|
||||
public class SimpleExpression extends AbstractValueExpression {
|
||||
|
||||
private final Op type;
|
||||
|
||||
private final Object val;
|
||||
|
||||
public SimpleExpression(String propertyName, Op type, Object value) {
|
||||
super(propertyName);
|
||||
super(propertyName, value);
|
||||
this.type = type;
|
||||
this.val = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bind value taking into account named parameters.
|
||||
*/
|
||||
private Object value() {
|
||||
if (val instanceof SpiNamedParam) {
|
||||
return ((SpiNamedParam)val).getValue();
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -202,7 +202,11 @@ class EqlAdapter<T> extends EQLBaseListener {
|
||||
return query.createNamedParameter(parameterName);
|
||||
}
|
||||
|
||||
public Expression like(LikeType likeType, String property, Object bindValue) {
|
||||
return query.getExpressionFactory().like(likeType, property, bindValue);
|
||||
public Expression like(boolean caseInsensitive, LikeType likeType, String property, Object bindValue) {
|
||||
return query.getExpressionFactory().like(property, bindValue, caseInsensitive, likeType);
|
||||
}
|
||||
|
||||
public Expression ieq(String property, Object bindValue) {
|
||||
return query.getExpressionFactory().ieqObject(property, bindValue);
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ class EqlAdapterHelper {
|
||||
peekExprList().eq(path, bind(value));
|
||||
break;
|
||||
case IEQ:
|
||||
peekExprList().ieq(path, bindString(value));
|
||||
peekExprList().add(owner.ieq(path, bind(value)));
|
||||
break;
|
||||
case NE:
|
||||
peekExprList().ne(path, bind(value));
|
||||
@@ -66,28 +66,28 @@ class EqlAdapterHelper {
|
||||
peekExprList().le(path, bind(value));
|
||||
break;
|
||||
case LIKE:
|
||||
addLike(LikeType.RAW, path, bind(value));
|
||||
addLike(false, LikeType.RAW, path, bind(value));
|
||||
break;
|
||||
case CONTAINS:
|
||||
addLike(LikeType.CONTAINS, path, bind(value));
|
||||
addLike(false, LikeType.CONTAINS, path, bind(value));
|
||||
break;
|
||||
case STARTS_WITH:
|
||||
addLike(LikeType.STARTS_WITH, path, bind(value));
|
||||
addLike(false, LikeType.STARTS_WITH, path, bind(value));
|
||||
break;
|
||||
case ENDS_WITH:
|
||||
addLike(LikeType.ENDS_WITH, path, bind(value));
|
||||
addLike(false, LikeType.ENDS_WITH, path, bind(value));
|
||||
break;
|
||||
case ILIKE:
|
||||
peekExprList().ilike(path, bindString(value));
|
||||
addLike(true, LikeType.RAW, path, bind(value));
|
||||
break;
|
||||
case ICONTAINS:
|
||||
peekExprList().icontains(path, bindString(value));
|
||||
addLike(true, LikeType.CONTAINS, path, bind(value));
|
||||
break;
|
||||
case ISTARTS_WITH:
|
||||
peekExprList().istartsWith(path, bindString(value));
|
||||
addLike(true, LikeType.STARTS_WITH, path, bind(value));
|
||||
break;
|
||||
case IENDS_WITH:
|
||||
peekExprList().iendsWith(path, bindString(value));
|
||||
addLike(true, LikeType.ENDS_WITH, path, bind(value));
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("Unhandled operator " + op);
|
||||
@@ -95,20 +95,8 @@ class EqlAdapterHelper {
|
||||
|
||||
}
|
||||
|
||||
private void addLike(LikeType likeType, String path, Object bindValue) {
|
||||
peekExprList().add(owner.like(likeType, path, bindValue));
|
||||
}
|
||||
|
||||
private String bindString(String value) {
|
||||
ValueType valueType = getValueType(value);
|
||||
switch (valueType) {
|
||||
case NAMED_PARAM:
|
||||
return NamedParameter.PREFIX + value;
|
||||
case STRING:
|
||||
return unquote(value);
|
||||
default:
|
||||
throw new IllegalArgumentException("Only STRING or NAMED PARAMETER argument allowed but got " + valueType);
|
||||
}
|
||||
private void addLike(boolean caseInsensitive, LikeType likeType, String path, Object bindValue) {
|
||||
peekExprList().add(owner.like(caseInsensitive, likeType, path, bindValue));
|
||||
}
|
||||
|
||||
private Object bind(String value) {
|
||||
|
||||
Reference in new issue
Block a user