use of org.datanucleus.query.compiler.JDOQLCompiler in project datanucleus-core by datanucleus.
the class AbstractJDOQLQuery method compileSubqueries.
/**
* Recursively compile the subqueries
* @param subqueryMap The subquery definition map
* @param parentCompilation Parent compilation
* @param parentCompiler The parent compiler
* @param parameterValues The parameters map
*/
protected void compileSubqueries(Map<String, SubqueryDefinition> subqueryMap, QueryCompilation parentCompilation, JavaQueryCompiler parentCompiler, Map parameterValues) {
long startTime = System.currentTimeMillis();
Iterator<Map.Entry<String, SubqueryDefinition>> iter = subqueryMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, SubqueryDefinition> entry = iter.next();
SubqueryDefinition subqueryDefinition = entry.getValue();
Query subquery = subqueryDefinition.getQuery();
if (NucleusLogger.QUERY.isDebugEnabled()) {
startTime = System.currentTimeMillis();
NucleusLogger.QUERY.debug(Localiser.msg("021044", getLanguage(), ((AbstractJDOQLQuery) subquery).getSingleStringQuery()));
}
JDOQLCompiler subCompiler = new JDOQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), subquery.from, subquery.candidateClass, null, subquery.filter, getParsedImports(), subquery.ordering, subquery.result, subquery.grouping, subquery.having, subquery.explicitParameters, null, null);
if (getBooleanExtensionProperty(EXTENSION_JDOQL_STRICT, false)) {
subCompiler.setOption(EXTENSION_JDOQL_STRICT, "true");
}
boolean allowAllSyntax = ec.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
if (ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL) != null) {
allowAllSyntax = ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
}
subCompiler.setAllowAll(allowAllSyntax);
subCompiler.setLinkToParentQuery(parentCompiler, subqueryDefinition.getParameterMap());
QueryCompilation subqueryCompilation = subCompiler.compile(parameterValues, null);
if (QueryUtils.queryReturnsSingleRow(subquery)) {
subqueryCompilation.setReturnsSingleRow();
}
parentCompilation.addSubqueryCompilation(entry.getKey(), subqueryCompilation);
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021045", getLanguage(), "" + (System.currentTimeMillis() - startTime)));
}
if (subquery.subqueries != null) {
// Recurse to nested subqueries
compileSubqueries(subquery.subqueries, subqueryCompilation, subCompiler, parameterValues);
}
}
}
use of org.datanucleus.query.compiler.JDOQLCompiler in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterUnaryMinus.
/**
* Test for unary minus.
*/
public void testFilterUnaryMinus() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Inventory.class, null, "1 > -1", null, null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error(">> Exception thrown when compiling filter unary minus", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
// TODO Check the content of the compilation when it is compiled
NucleusLogger.QUERY.info(">> compilation=" + compilation);
}
use of org.datanucleus.query.compiler.JDOQLCompiler in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterExplicitParameterAsImplicit.
/**
* Test for subquery.
* TODO Update this to reflect a valid subquery JDOQL.
*/
/*public void testSubquery()
{
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try
{
compiler = new JDOQLCompiler(mmgr, nucCtx.getClassLoaderResolver(null),
null, Employee.class, null,
"salary > (SELECT avg(salary) FROM " + Employee.class.getName() + " e)",
null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap());
}
catch (NucleusException ne)
{
NucleusLogger.QUERY.error(">> Exception thrown when compiling subquery", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
// TODO Check the content of the compilation when it is compiled
NucleusLogger.QUERY.info(">> compilation=" + compilation);
}*/
/**
* Test for detection of invalid param specification where the user defines explicit params
* and puts an implicit param in the filter. Should cause exception
*/
public void testFilterExplicitParameterAsImplicit() {
JavaQueryCompiler compiler = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name == :param1", null, null, null, null, null, "java.lang.String param1", null, null);
compiler.compile(null, null);
fail("Compiled query without exception even though it had explicit params defined and" + " had an implicit param in the filter! Should have thrown exception");
} catch (NucleusUserException ne) {
// Expected
}
}
use of org.datanucleus.query.compiler.JDOQLCompiler in project tests by datanucleus.
the class JDOQLCompilerTest method testExpressionSerializable.
/**
* Test for serialisability of Expressions.
*/
public void testExpressionSerializable() {
// 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, "name == param1", null, null, null, null, null, "java.lang.String param1", 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 PrimaryExpression but isnt", dyExpr.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr.getRight() instanceof ParameterExpression);
PrimaryExpression left = (PrimaryExpression) dyExpr.getLeft();
assertEquals("Primary expression name is wrong", left.getId(), "name");
ParameterExpression right = (ParameterExpression) dyExpr.getRight();
assertEquals("ParameterExpression has wrong value", "param1", right.getId());
try {
try {
// Serialise the Expression
FileOutputStream fileStream = new FileOutputStream("expr.ser");
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(expr);
os.close();
} catch (Exception e) {
NucleusLogger.GENERAL.error(">> Exception in serialise", e);
fail("Failed to serialise " + StringUtils.toJVMIDString(expr));
}
try {
// Deserialise the Expression
FileInputStream fileInputStream = new FileInputStream("expr.ser");
ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
Object obj = oInputStream.readObject();
if (obj instanceof Expression) {
Expression expr1 = (Expression) obj;
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr1 instanceof DyadicExpression);
DyadicExpression dyExpr1 = (DyadicExpression) expr1;
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr1.getRight() instanceof ParameterExpression);
PrimaryExpression left1 = (PrimaryExpression) dyExpr1.getLeft();
assertEquals("Primary expression name is wrong", left1.getId(), "name");
ParameterExpression right1 = (ParameterExpression) dyExpr1.getRight();
assertEquals("ParameterExpression has wrong value", "param1", right1.getId());
} else {
fail("Deserialised object is " + obj.getClass().getName() + " not Expression");
}
oInputStream.close();
} catch (Exception e) {
NucleusLogger.GENERAL.error(">> Exception in deserialise", e);
fail("Failed to deserialise " + StringUtils.toJVMIDString(expr));
}
} finally {
// Delete the file
File file = new File("expr.ser");
if (file.exists()) {
file.delete();
}
}
}
use of org.datanucleus.query.compiler.JDOQLCompiler 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());
}
Aggregations