use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-core by datanucleus.
the class StringIndexOfMethod 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 Integer.valueOf(-1);
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
// Evaluate the first argument
String arg1 = null;
Object arg1Obj = null;
Object param = expr.getArguments().get(0);
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
arg1Obj = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
arg1Obj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
arg1Obj = ((Literal) param).getLiteral();
} else if (param instanceof InvokeExpression) {
arg1Obj = eval.getValueForInvokeExpression((InvokeExpression) param);
} else {
throw new NucleusException(method + "(param[, num1]) where param is instanceof " + param.getClass().getName() + " not supported");
}
arg1 = QueryUtils.getStringValue(arg1Obj);
Integer result = null;
if (expr.getArguments().size() == 2) {
// Evaluate the second argument
int arg2 = -1;
param = expr.getArguments().get(1);
Object arg2Obj = null;
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
arg2Obj = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
arg2Obj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
arg2Obj = ((Literal) param).getLiteral();
} else if (param instanceof DyadicExpression) {
arg2Obj = ((DyadicExpression) param).evaluate(eval);
} else {
throw new NucleusException(method + "(param1, param2) where param2 is instanceof " + param.getClass().getName() + " not supported");
}
if (!(arg2Obj instanceof Number)) {
throw new NucleusException(method + "(param1,param2) : param2 must be numeric");
}
arg2 = ((Number) arg2Obj).intValue();
result = Integer.valueOf(((String) invokedValue).indexOf(arg1, arg2));
} else {
result = Integer.valueOf(((String) invokedValue).indexOf(arg1));
}
return result;
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-core by datanucleus.
the class InMemoryExpressionEvaluator method processInExpression.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processInExpression(org.datanucleus.query.expression.Expression)
*/
@Override
protected Object processInExpression(Expression expr) {
if (expr instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) expr;
Expression left = dyExpr.getLeft();
Expression right = dyExpr.getRight();
Object leftVal = getValueForExpression(left);
Object rightVal = getValueForExpression(right);
if (rightVal instanceof Collection) {
Boolean result = Boolean.FALSE;
Iterator rightValIter = ((Collection) rightVal).iterator();
while (rightValIter.hasNext()) {
Object rightElem = rightValIter.next();
if (leftVal.equals(rightElem)) {
result = Boolean.TRUE;
break;
}
}
stack.push(result);
return result;
}
Boolean result = leftVal.equals(rightVal) ? Boolean.TRUE : Boolean.FALSE;
stack.push(result);
return result;
}
return super.processInExpression(expr);
}
use of org.datanucleus.query.expression.DyadicExpression 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.DyadicExpression in project datanucleus-core by datanucleus.
the class InMemoryExpressionEvaluator method processNotInExpression.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processNotInExpression(org.datanucleus.query.expression.Expression)
*/
@Override
protected Object processNotInExpression(Expression expr) {
if (expr instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) expr;
Expression left = dyExpr.getLeft();
Expression right = dyExpr.getRight();
Object leftVal = getValueForExpression(left);
Object rightVal = getValueForExpression(right);
if (rightVal instanceof Collection) {
Boolean result = Boolean.TRUE;
Iterator rightValIter = ((Collection) rightVal).iterator();
while (rightValIter.hasNext()) {
Object rightElem = rightValIter.next();
if (leftVal.equals(rightElem)) {
result = Boolean.FALSE;
break;
}
}
stack.push(result);
return result;
}
Boolean result = leftVal.equals(rightVal) ? Boolean.FALSE : Boolean.TRUE;
stack.push(result);
return result;
}
return super.processNotInExpression(expr);
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-core by datanucleus.
the class VarThisCompilationOptimiser method findRedundantFilterVariables.
/**
* Method to process the provided filter expression and find any variables that are to all intents
* and purposes redundant. Checks for "var == this". In this case we can just replace the variable
* occurrences with "this".
* @param filterExpr The filter
* @param varNames The variable names that are redundant (updated by this method)
*/
private void findRedundantFilterVariables(Expression filterExpr, Set<String> varNames) {
if (filterExpr instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) filterExpr;
if (dyExpr.getOperator() == Expression.OP_EQ) {
if (dyExpr.getLeft() instanceof VariableExpression) {
if (dyExpr.getRight() instanceof PrimaryExpression) {
PrimaryExpression rightExpr = (PrimaryExpression) dyExpr.getRight();
if (rightExpr.getId().equals(compilation.getCandidateAlias())) {
varNames.add(((VariableExpression) dyExpr.getLeft()).getId());
}
}
} else if (dyExpr.getRight() instanceof VariableExpression) {
if (dyExpr.getLeft() instanceof PrimaryExpression) {
PrimaryExpression leftExpr = (PrimaryExpression) dyExpr.getLeft();
if (leftExpr.getId().equals(compilation.getCandidateAlias())) {
varNames.add(((VariableExpression) dyExpr.getRight()).getId());
}
}
}
} else if (dyExpr.getOperator() == Expression.OP_AND) {
findRedundantFilterVariables(dyExpr.getLeft(), varNames);
findRedundantFilterVariables(dyExpr.getRight(), varNames);
} else if (dyExpr.getOperator() == Expression.OP_OR) {
findRedundantFilterVariables(dyExpr.getLeft(), varNames);
findRedundantFilterVariables(dyExpr.getRight(), varNames);
}
}
}
Aggregations