use of org.datanucleus.query.expression.VariableExpression in project datanucleus-api-jdo by datanucleus.
the class JDOQLTypedSubqueryImpl method internalSelect.
protected Expression internalSelect(Expression expr, Class implClass) {
discardCompiled();
this.result = new ArrayList<ExpressionImpl>();
this.result.add((ExpressionImpl) expr);
VariableExpression varExpr = new VariableExpression(getAlias());
try {
Constructor ctr = implClass.getConstructor(new Class[] { org.datanucleus.query.expression.Expression.class });
return (Expression) ctr.newInstance(new Object[] { varExpr });
} catch (NoSuchMethodException nsme) {
throw new JDOException("Unable to create expression of type " + expr.getClass().getName() + " since required constructor doesnt exist");
} catch (InvocationTargetException ite) {
throw new JDOException("Unable to create expression of type " + expr.getClass().getName() + " due to error in constructor");
} catch (IllegalAccessException iae) {
throw new JDOException("Unable to create expression of type " + expr.getClass().getName() + " due to error in constructor");
} catch (InstantiationException ie) {
throw new JDOException("Unable to create expression of type " + expr.getClass().getName() + " due to error in constructor");
}
}
use of org.datanucleus.query.expression.VariableExpression 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.VariableExpression 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.VariableExpression 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.VariableExpression 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