use of org.datanucleus.query.expression.InvokeExpression in project datanucleus-core by datanucleus.
the class QueryUtils method getParameterExpressionForPosition.
/**
* Convenience method to return the ParameterExpression for the specified position
* if found in the expression tree starting at <pre>rootExpr</pre>
* @param rootExpr The expression
* @param pos The position
* @return The ParameterExpression (if found)
*/
public static ParameterExpression getParameterExpressionForPosition(Expression rootExpr, int pos) {
if (rootExpr instanceof ParameterExpression && ((ParameterExpression) rootExpr).getPosition() == pos) {
return (ParameterExpression) rootExpr;
}
if (rootExpr.getLeft() != null) {
ParameterExpression paramExpr = getParameterExpressionForPosition(rootExpr.getLeft(), pos);
if (paramExpr != null) {
return paramExpr;
}
}
if (rootExpr.getRight() != null) {
ParameterExpression paramExpr = getParameterExpressionForPosition(rootExpr.getRight(), pos);
if (paramExpr != null) {
return paramExpr;
}
}
if (rootExpr instanceof InvokeExpression) {
InvokeExpression invokeExpr = (InvokeExpression) rootExpr;
List<Expression> args = invokeExpr.getArguments();
if (args != null) {
Iterator<Expression> argIter = args.iterator();
while (argIter.hasNext()) {
ParameterExpression paramExpr = getParameterExpressionForPosition(argIter.next(), pos);
if (paramExpr != null) {
return paramExpr;
}
}
}
}
return null;
}
use of org.datanucleus.query.expression.InvokeExpression in project tests by datanucleus.
the class JPQLCompilerTest method testFromMemberOfExpression.
/**
* Tests for from clause with a "MEMBER OF {primary}" expression.
*/
public void testFromMemberOfExpression() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Department.class, null, ":param MEMBER OF projects", null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception during compile", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
// InvokeExpression{[PrimaryExpression{elements}].contains(ParameterExpression{param})}
Expression expr = compilation.getExprFilter();
assertTrue("Invalid type of filter expression", expr instanceof InvokeExpression);
InvokeExpression invokeExpr = (InvokeExpression) expr;
assertEquals("Invoke method is incorrect", "contains", invokeExpr.getOperation());
assertTrue("Invoke left expression is of incorrect type", invokeExpr.getLeft() instanceof PrimaryExpression);
PrimaryExpression leftExpr = (PrimaryExpression) invokeExpr.getLeft();
assertEquals("Invoke left expression id is wrong", "projects", leftExpr.getId());
List args = invokeExpr.getArguments();
assertNotNull("Number of args is null!", args);
assertEquals("Incorrect number of args to invoke", 1, args.size());
assertTrue("Argument is of incorrect type", args.get(0) instanceof ParameterExpression);
ParameterExpression argExpr = (ParameterExpression) args.get(0);
assertEquals("Argument param name is incorrect", "param", argExpr.getId());
}
use of org.datanucleus.query.expression.InvokeExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterCollectionContainsVariablePlusExtraJoin.
/**
* Tests for collection.contains(element) && elem.other.field == val.
*/
public void testFilterCollectionContainsVariablePlusExtraJoin() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Inventory.class, null, "products.contains(element) && element.guarantee.numberOfYears < 3", 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;
LOG.info(">> expr=" + dyExpr);
// 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.guarantee.numberOfYears < 3
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", 2, rightExprLeft.getTuples().size());
assertEquals("Right expression (left) part2 is incorrect", "guarantee", rightExprLeft.getTuples().get(0));
assertEquals("Right expression (left) part2 is incorrect", "numberOfYears", rightExprLeft.getTuples().get(1));
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", 3, ((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.InvokeExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterWithStringIndexOfLiteral.
/**
* Tests for "String.indexOf(Literal, int)" in filter.
*/
public void testFilterWithStringIndexOfLiteral() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name.indexOf(\"nd\", 3)", null, null, null, null, null, null, null, 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 InvokeExpression but wasnt", expr instanceof InvokeExpression);
InvokeExpression invExpr = (InvokeExpression) expr;
assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", invExpr.getLeft() instanceof PrimaryExpression);
assertEquals("Name of field upon which we invoke the method was wrong", "name", ((PrimaryExpression) invExpr.getLeft()).getId());
assertEquals("Name of invoked method was wrong", "indexOf", invExpr.getOperation());
assertEquals("Number of parameters is wrong", 2, invExpr.getArguments().size());
Object param1 = invExpr.getArguments().get(0);
assertTrue("Parameter1 to indexOf() is of wrong type", param1 instanceof Literal);
Literal param1Lit = (Literal) param1;
assertEquals("Parameter1 to indexOf() has wrong value", "nd", param1Lit.getLiteral());
Object param2 = invExpr.getArguments().get(1);
assertTrue("Parameter2 to indexOf() is of wrong type", param2 instanceof Literal);
Literal param2Lit = (Literal) param2;
assertEquals("Parameter2 to indexOf() has wrong value", new Long(3), param2Lit.getLiteral());
}
use of org.datanucleus.query.expression.InvokeExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterWithStringLiteralStartsWith.
/**
* Tests for "StringLiteral.startsWith(var)" in filter.
*/
public void testFilterWithStringLiteralStartsWith() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "\"SomeString\".startsWith(name)", null, null, null, null, null, null, null, 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("Filter should be InvokeExpression but is " + expr, expr instanceof InvokeExpression);
InvokeExpression invExpr = (InvokeExpression) expr;
assertTrue("InvokeExpression should have been invoked on Literal but wasnt", invExpr.getLeft() instanceof Literal);
assertEquals("Value of literal is wrong", "SomeString", ((Literal) invExpr.getLeft()).getLiteral());
assertEquals("Name of invoked method was wrong", "startsWith", invExpr.getOperation());
assertEquals("Number of parameters is wrong", 1, invExpr.getArguments().size());
Object param1 = invExpr.getArguments().get(0);
assertTrue("Parameter1 to startsWith() is of wrong type : " + param1, param1 instanceof PrimaryExpression);
PrimaryExpression param1Expr = (PrimaryExpression) param1;
assertEquals("Parameter1 expression has incorrect number of tuples", 1, param1Expr.getTuples().size());
assertEquals("Parameter1 expression 'id' is incorrect", "name", param1Expr.getId());
}
Aggregations