use of org.datanucleus.query.expression.PrimaryExpression 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.PrimaryExpression 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.PrimaryExpression 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;
}
use of org.datanucleus.query.expression.PrimaryExpression in project datanucleus-core by datanucleus.
the class ArrayContainsMethod 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.getClass().isArray()) {
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, (Object[]) invokedValue);
}
} else {
// TODO Implement this
throw new NucleusException("Dont currently support use of Array.contains(" + param.getClass().getName() + ")");
}
for (int i = 0; i < Array.getLength(invokedValue); i++) {
Object elem = Array.get(invokedValue, i);
if (elem == null && paramValue == null) {
return Boolean.TRUE;
} else if (elem != null && paramValue != null) {
if (elem.equals(paramValue)) {
return Boolean.TRUE;
}
if (!paramValue.getClass().isAssignableFrom(elem.getClass()) && !elem.getClass().isAssignableFrom(paramValue.getClass())) {
// Types are different, so add specific type conversion handling
if ((paramValue.getClass() == Long.class || paramValue.getClass() == Integer.class || paramValue.getClass() == Short.class) && (elem.getClass() == Long.class || elem.getClass() == Integer.class || elem.getClass() == Short.class)) {
long paramLong = ((Number) paramValue).longValue();
long elemLong = ((Number) elem).longValue();
if (paramLong == elemLong) {
return Boolean.TRUE;
}
}
}
}
}
return Boolean.FALSE;
}
use of org.datanucleus.query.expression.PrimaryExpression in project datanucleus-core by datanucleus.
the class ListGetMethod 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 null;
}
if (!(invokedValue instanceof List)) {
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 elements
throw new VariableNotSetException(varExpr, ((List) invokedValue).toArray());
}
} else {
throw new NucleusException("Dont currently support use of get(" + param.getClass().getName() + ")");
}
int paramInt;
if (paramValue instanceof Number) {
paramInt = ((Number) paramValue).intValue();
} else {
throw new NucleusException("List.get() should take in an integer but is " + paramValue);
}
return ((List) invokedValue).get(paramInt);
}
Aggregations