use of org.datanucleus.query.expression.VariableExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterCollectionContainsVariable.
/**
* Tests for collection.contains(element).
*/
public void testFilterCollectionContainsVariable() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Inventory.class, null, "products.contains(element) && element.price < 200", null, null, null, null, null, null, Product.class.getName() + " element", null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
DyadicExpression dyExpr = (DyadicExpression) expr;
// product.contains(element)
assertTrue("Left expression should have been InvokeExpression but wasnt", dyExpr.getLeft() instanceof InvokeExpression);
InvokeExpression leftExpr = (InvokeExpression) dyExpr.getLeft();
assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", leftExpr.getLeft() instanceof PrimaryExpression);
assertEquals("Left expression : Name of field upon which we invoke the method was wrong", "products", ((PrimaryExpression) leftExpr.getLeft()).getId());
assertEquals("Left expression : Name of invoked method was wrong", "contains", leftExpr.getOperation());
assertEquals("Left expression : Number of parameters to contains() is wrong", 1, leftExpr.getArguments().size());
Object param1 = leftExpr.getArguments().get(0);
assertTrue("Left expression : Parameter1 to contains() is of wrong type", param1 instanceof VariableExpression);
VariableExpression vrExpr = (VariableExpression) param1;
assertEquals("Left expression : Name of variable to contains() is incorrect", "element", vrExpr.getId());
// element.price < 200
assertTrue("Right expression should have been DyadicExpression but wasnt", dyExpr.getRight() instanceof DyadicExpression);
DyadicExpression rightExpr = (DyadicExpression) dyExpr.getRight();
assertTrue("Right expression (left) should have been PrimaryExpression but wasnt", rightExpr.getLeft() instanceof PrimaryExpression);
PrimaryExpression rightExprLeft = (PrimaryExpression) rightExpr.getLeft();
assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
VariableExpression rightExprLeftLeft = (VariableExpression) rightExprLeft.getLeft();
assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
assertEquals("Right expression (left) part1 is incorrect", "element", rightExprLeftLeft.getId());
assertEquals("Right expression (left) has incorrect number of tuples", 1, rightExprLeft.getTuples().size());
assertEquals("Right expression (left) part2 is incorrect", "price", rightExprLeft.getTuples().get(0));
assertEquals("Right expression : Operator between left and right is incorrect", Expression.OP_LT, rightExpr.getOperator());
assertTrue("Right expression (right) should have been Literal but wasnt", rightExpr.getRight() instanceof Literal);
Literal rightExprRight = (Literal) rightExpr.getRight();
assertEquals("Right expression (right) literal has incorrect value", 200, ((Long) rightExprRight.getLiteral()).longValue());
// Check symbols
SymbolTable symbols = compilation.getSymbolTable();
assertTrue("Symbol table doesnt have entry for 'element'", symbols.hasSymbol("element"));
assertTrue("Symbol table doesnt have entry for 'this'", symbols.hasSymbol("this"));
Symbol sy1 = symbols.getSymbol("element");
assertEquals("Type of symbol for 'element' is wrong", Product.class, sy1.getValueType());
Symbol sy2 = symbols.getSymbol("this");
assertEquals("Type of symbol for 'this' is wrong", Inventory.class, sy2.getValueType());
}
use of org.datanucleus.query.expression.VariableExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterImplicitVariable.
/**
* Test for use of an implicit variable in the filter.
*/
public void testFilterImplicitVariable() {
// Test use of implicit variable in filter
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "notaField == 2", null, null, null, null, null, null, null, null);
compilation = compiler.compile(null, null);
} catch (NucleusUserException ne) {
// TODO Debatable if this should throw a JDOUserException since the "notaField" is not bound, nor typed
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
DyadicExpression dyExpr = (DyadicExpression) expr;
assertTrue("Compiled left expression should be VariableExpression but isnt", dyExpr.getLeft() instanceof VariableExpression);
assertTrue("Compiled right expression should be Literal but isnt", dyExpr.getRight() instanceof Literal);
VariableExpression left = (VariableExpression) dyExpr.getLeft();
assertEquals("Variable expression name is wrong", left.getId(), "notaField");
Literal right = (Literal) dyExpr.getRight();
assertEquals("Literal has wrong value", new Long(2), right.getLiteral());
}
use of org.datanucleus.query.expression.VariableExpression in project datanucleus-core by datanucleus.
the class MapContainsEntryMethod 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 keyParam = expr.getArguments().get(0);
Object valParam = expr.getArguments().get(1);
Object keyValue = null;
if (keyParam instanceof Literal) {
keyValue = ((Literal) keyParam).getLiteral();
} else if (keyParam instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) keyParam;
keyValue = eval.getValueForPrimaryExpression(primExpr);
} else if (keyParam instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) keyParam;
keyValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (keyParam instanceof VariableExpression) {
VariableExpression varExpr = (VariableExpression) keyParam;
try {
keyValue = 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 containsEntry(" + keyParam.getClass().getName() + ",?)");
}
Object valValue = null;
if (valParam instanceof Literal) {
valValue = ((Literal) valParam).getLiteral();
} else if (keyParam instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) valParam;
valValue = eval.getValueForPrimaryExpression(primExpr);
} else if (keyParam instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) valParam;
valValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (keyParam instanceof VariableExpression) {
VariableExpression varExpr = (VariableExpression) valParam;
try {
valValue = 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 containsEntry(?," + valParam.getClass().getName() + ")");
}
Map invokedMap = (Map) invokedValue;
if (invokedMap.containsKey(keyValue)) {
Object currentValForKey = invokedMap.get(keyValue);
if (currentValForKey.equals(valValue)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}
use of org.datanucleus.query.expression.VariableExpression in project datanucleus-core by datanucleus.
the class MapGetMethod 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 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 get(" + param.getClass().getName() + ")");
}
return ((Map) invokedValue).get(paramValue);
}
use of org.datanucleus.query.expression.VariableExpression in project datanucleus-core by datanucleus.
the class JDOQLCompiler method containsOnlyGroupingOrAggregates.
/**
* Convenience method to check the provided expression for whether it contains only grouping expressions
* or aggregates
* @param expr The expression to check
* @param exprGrouping The grouping expressions
* @return Whether it contains only grouping or aggregates
*/
private static boolean containsOnlyGroupingOrAggregates(Expression expr, Expression[] exprGrouping) {
if (expr == null) {
return true;
} else if (expr instanceof DyadicExpression) {
Expression left = expr.getLeft();
Expression right = expr.getRight();
if (!containsOnlyGroupingOrAggregates(left, exprGrouping)) {
return false;
}
if (!containsOnlyGroupingOrAggregates(right, exprGrouping)) {
return false;
}
return true;
} else if (expr instanceof InvokeExpression) {
InvokeExpression invExpr = (InvokeExpression) expr;
if (isExpressionGroupingOrAggregate(invExpr, exprGrouping)) {
return true;
}
Expression invokedExpr = invExpr.getLeft();
if (invokedExpr != null && !containsOnlyGroupingOrAggregates(invokedExpr, exprGrouping)) {
// Check invoked object
return false;
}
List<Expression> invArgs = invExpr.getArguments();
if (invArgs != null) {
// Check invocation arguments
Iterator<Expression> iter = invArgs.iterator();
while (iter.hasNext()) {
Expression argExpr = iter.next();
if (!containsOnlyGroupingOrAggregates(argExpr, exprGrouping)) {
return false;
}
}
}
return true;
} else if (expr instanceof PrimaryExpression) {
return isExpressionGroupingOrAggregate(expr, exprGrouping);
} else if (expr instanceof Literal) {
return true;
} else if (expr instanceof ParameterExpression) {
return true;
} else if (expr instanceof VariableExpression) {
return true;
}
return false;
}
Aggregations