use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class MapContainsValueMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return Boolean.FALSE;
}
if (!(invokedValue instanceof Map)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
Object param = expr.getArguments().get(0);
Object paramValue = null;
if (param instanceof Literal) {
paramValue = ((Literal) param).getLiteral();
} else if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
paramValue = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
paramValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof VariableExpression) {
VariableExpression varExpr = (VariableExpression) param;
try {
paramValue = eval.getValueForVariableExpression(varExpr);
} catch (VariableNotSetException vnse) {
// Throw an exception with the possible values of values
throw new VariableNotSetException(varExpr, ((Map) invokedValue).values().toArray());
}
} else {
throw new NucleusException("Dont currently support use of containsValue(" + param.getClass().getName() + ")");
}
return ((Map) invokedValue).containsValue(paramValue) ? Boolean.TRUE : Boolean.FALSE;
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class ModFunction method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
Object param1 = expr.getArguments().get(0);
int param1Value = -1;
if (param1 instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param1;
Object val = eval.getValueForPrimaryExpression(primExpr);
if (val instanceof Number) {
param1Value = ((Number) val).intValue();
} else {
throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param1.getClass().getName() + " but should be integer");
}
} else if (param1 instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param1;
Object val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
if (val instanceof Number) {
param1Value = ((Number) val).intValue();
} else {
throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param1.getClass().getName() + " but should be integer");
}
} else if (param1 instanceof Literal) {
Object val = ((Literal) param1).getLiteral();
if (val instanceof Number) {
param1Value = ((Number) val).intValue();
} else {
throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param1.getClass().getName() + " but should be integer");
}
} else {
throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param1.getClass().getName() + " not supported");
}
Object param2 = expr.getArguments().get(1);
int param2Value = -1;
if (param2 instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param2;
Object val = eval.getValueForPrimaryExpression(primExpr);
if (val instanceof Number) {
param2Value = ((Number) val).intValue();
} else {
throw new NucleusException(method + "(num1, num2) where num2 is instanceof " + param2.getClass().getName() + " but should be integer");
}
} else if (param2 instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param2;
Object val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
if (val instanceof Number) {
param2Value = ((Number) val).intValue();
} else {
throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param2.getClass().getName() + " but should be integer");
}
} else if (param2 instanceof Literal) {
Object val = ((Literal) param2).getLiteral();
if (val instanceof Number) {
param2Value = ((Number) val).intValue();
} else {
throw new NucleusException(method + "(num1, num2) where num2 is instanceof " + param2.getClass().getName() + " but should be integer");
}
} else {
throw new NucleusException(method + "(num1, num2) where num2 is instanceof " + param2.getClass().getName() + " not supported");
}
return Integer.valueOf(param1Value % param2Value);
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class NullIfFunction method getValueForArgExpression.
protected Object getValueForArgExpression(Expression argExpr, InMemoryExpressionEvaluator eval) {
Object argValue = null;
if (argExpr instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) argExpr;
argValue = eval.getValueForPrimaryExpression(primExpr);
} else if (argExpr instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) argExpr;
argValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (argExpr instanceof Literal) {
argValue = ((Literal) argExpr).getLiteral();
} else {
throw new NucleusException("Don't support NULLIF with argument of type " + argExpr.getClass().getName());
}
return argValue;
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class EnumMatchesMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return Boolean.FALSE;
}
if (!(invokedValue instanceof Enum)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
String arg = null;
Object argObj = null;
Object param = expr.getArguments().get(0);
if (expr.getArguments().size() > 1) {
NucleusLogger.QUERY.info("Please note that any escape character is currently ignored");
// TODO Cater for optional second argument that is the escape character
}
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
argObj = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
argObj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
argObj = ((Literal) param).getLiteral();
} else {
throw new NucleusException(method + "(param, num1, num2) where param is instanceof " + param.getClass().getName() + " not supported");
}
arg = QueryUtils.getStringValue(argObj);
return ((Enum) invokedValue).toString().matches(arg) ? Boolean.TRUE : Boolean.FALSE;
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class CollectionContainsMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return Boolean.FALSE;
}
if (!(invokedValue instanceof Collection)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
Object param = expr.getArguments().get(0);
Object paramValue = null;
if (param instanceof Literal) {
paramValue = ((Literal) param).getLiteral();
} else if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
paramValue = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
paramValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof VariableExpression) {
VariableExpression varExpr = (VariableExpression) param;
try {
paramValue = eval.getValueForVariableExpression(varExpr);
} catch (VariableNotSetException vnse) {
// Throw an exception with the possible values
throw new VariableNotSetException(varExpr, ((Collection) invokedValue).toArray());
}
} else {
// TODO Implement this
throw new NucleusException("Dont currently support use of contains(" + param.getClass().getName() + ")");
}
return ((Collection) invokedValue).contains(paramValue) ? Boolean.TRUE : Boolean.FALSE;
}
Aggregations