use of org.datanucleus.query.expression.PrimaryExpression in project datanucleus-core by datanucleus.
the class InMemoryExpressionEvaluator method processComExpression.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processComExpression(org.datanucleus.query.expression.Expression)
*/
protected Object processComExpression(Expression expr) {
// Bitwise complement - only for integer values
PrimaryExpression primExpr = (PrimaryExpression) expr.getLeft();
Object primVal = getValueForPrimaryExpression(primExpr);
int val = -1;
if (primVal instanceof Number) {
val = ((Number) primVal).intValue();
}
Integer result = Integer.valueOf(~val);
stack.push(result);
return stack.peek();
}
use of org.datanucleus.query.expression.PrimaryExpression in project datanucleus-core by datanucleus.
the class InMemoryExpressionEvaluator method getValueForPrimaryExpression.
/**
* Convenience method to get the value for a PrimaryExpression.
* @param primExpr Expression
* @return The value in the object for this expression
*/
public Object getValueForPrimaryExpression(PrimaryExpression primExpr) {
Object value = null;
if (primExpr.getLeft() != null) {
// Get value of left expression
if (primExpr.getLeft() instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) primExpr.getLeft();
if (dyExpr.getOperator() == Expression.OP_CAST) {
Expression castLeftExpr = dyExpr.getLeft();
if (castLeftExpr instanceof PrimaryExpression) {
value = getValueForPrimaryExpression((PrimaryExpression) castLeftExpr);
String castClassName = (String) ((Literal) dyExpr.getRight()).getLiteral();
if (value != null) {
// TODO Do this in the compilation stage, and check for ClassNotResolvedException
Class castClass = imports.resolveClassDeclaration(castClassName, clr, null);
if (!castClass.isAssignableFrom(value.getClass())) {
NucleusLogger.QUERY.debug("In-memory query requires cast of " + StringUtils.toJVMIDString(value) + " to " + castClass.getName() + " which is impossible, so exiting for this candidate");
return new InMemoryFailure();
}
}
} else if (castLeftExpr instanceof VariableExpression) {
value = getValueForVariableExpression((VariableExpression) castLeftExpr);
String castClassName = (String) ((Literal) dyExpr.getRight()).getLiteral();
if (value != null) {
// TODO Do this in the compilation stage, and check for ClassNotResolvedException
Class castClass = imports.resolveClassDeclaration(castClassName, clr, null);
if (!castClass.isAssignableFrom(value.getClass())) {
NucleusLogger.QUERY.debug("In-memory query requires cast of " + StringUtils.toJVMIDString(value) + " to " + castClass.getName() + " which is impossible, so exiting for this candidate");
return new InMemoryFailure();
}
}
} else {
// TODO Allow for cast of ParameterExpression
NucleusLogger.QUERY.warn("Dont currently support CastExpression of " + castLeftExpr);
return new InMemoryFailure();
}
} else {
NucleusLogger.QUERY.error("Dont currently support PrimaryExpression starting with DyadicExpression of " + dyExpr);
return new InMemoryFailure();
}
} else if (primExpr.getLeft() instanceof ParameterExpression) {
value = QueryUtils.getValueForParameterExpression(parameterValues, (ParameterExpression) primExpr.getLeft());
} else if (primExpr.getLeft() instanceof InvokeExpression) {
InvokeExpression invokeExpr = (InvokeExpression) primExpr.getLeft();
value = getValueForInvokeExpression(invokeExpr);
} else if (primExpr.getLeft() instanceof VariableExpression) {
VariableExpression varExpr = (VariableExpression) primExpr.getLeft();
try {
value = getValueForVariableExpression(varExpr);
} catch (VariableNotSetException vnse) {
// We don't know the possible values here!
NucleusLogger.QUERY.error("Attempt to access variable " + varExpr.getId() + " as part of primaryExpression " + primExpr + " : variable is not yet set!");
return new InMemoryFailure();
}
} else {
NucleusLogger.QUERY.warn("Dont currently support PrimaryExpression with left-side of " + primExpr.getLeft());
return new InMemoryFailure();
}
}
int firstTupleToProcess = 0;
if (value == null) {
if (state.containsKey(primExpr.getTuples().get(0))) {
// Get value from the first node
value = state.get(primExpr.getTuples().get(0));
firstTupleToProcess = 1;
} else if (state.containsKey(candidateAlias)) {
// Get value from the candidate
value = state.get(candidateAlias);
}
}
// Evaluate the field of this value
for (int i = firstTupleToProcess; i < primExpr.getTuples().size(); i++) {
String fieldName = primExpr.getTuples().get(i);
if (value instanceof Optional) {
// Treat Optional as the wrapped value
Optional opt = (Optional) value;
value = opt.isPresent() ? opt.get() : null;
}
if (!fieldName.equals(candidateAlias)) {
boolean getValueByReflection = true;
if (ec.getApiAdapter().isPersistent(value)) {
// Make sure this field is loaded
ObjectProvider valueOP = ec.findObjectProvider(value);
if (valueOP != null) {
AbstractMemberMetaData mmd = valueOP.getClassMetaData().getMetaDataForMember(fieldName);
if (mmd == null) {
NucleusLogger.QUERY.error("Cannot find " + fieldName + " member of " + valueOP.getClassMetaData().getFullClassName());
return new InMemoryFailure();
}
if (mmd.getAbsoluteFieldNumber() >= 0) {
// Field is managed so make sure it is loaded, and get its value
valueOP.isLoaded(mmd.getAbsoluteFieldNumber());
value = valueOP.provideField(mmd.getAbsoluteFieldNumber());
getValueByReflection = false;
}
}
}
if (getValueByReflection) {
value = ClassUtils.getValueOfFieldByReflection(value, fieldName);
}
}
}
return value;
}
use of org.datanucleus.query.expression.PrimaryExpression in project datanucleus-core by datanucleus.
the class MapContainsKeyMethod 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 keys
throw new VariableNotSetException(varExpr, ((Map) invokedValue).keySet().toArray());
}
} else {
throw new NucleusException("Dont currently support use of containsKey(" + param.getClass().getName() + ")");
}
return ((Map) invokedValue).containsKey(paramValue) ? Boolean.TRUE : Boolean.FALSE;
}
use of org.datanucleus.query.expression.PrimaryExpression 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.PrimaryExpression 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);
}
Aggregations