Search in sources :

Example 31 with QueryCompilation

use of org.datanucleus.query.compiler.QueryCompilation in project tests by datanucleus.

the class JPQLCompilerTest method testFilterWithExistsSubquery.

/**
 * Tests for "EXISTS (subquery)".
 */
public void testFilterWithExistsSubquery() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "EXISTS (SUBQ_1)", 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());
    }
    Expression expr = compilation.getExprFilter();
    assertTrue("Expression is not a SubqueryExpression", expr instanceof SubqueryExpression);
    SubqueryExpression subExpr = (SubqueryExpression) expr;
    assertEquals("Subquery keyword is incorrect", "EXISTS", subExpr.getKeyword());
    assertTrue("Subquery right expression is not VariableExpression", subExpr.getRight() instanceof VariableExpression);
    VariableExpression varExpr = (VariableExpression) subExpr.getRight();
    assertEquals("VariableExpression name is incorrect", "SUBQ_1", varExpr.getId());
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) HashMap(java.util.HashMap) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression)

Example 32 with QueryCompilation

use of org.datanucleus.query.compiler.QueryCompilation in project tests by datanucleus.

the class JPQLCompilerTest method testFilterWithNegateExpression.

/**
 * Tests for "!(expression)".
 */
public void testFilterWithNegateExpression() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "NOT (budget > 32)", 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());
    }
    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 have been DyadicExpression but wasnt", dyExpr.getLeft() instanceof DyadicExpression);
    assertNull("Compiled right expression should have been null but wasnt", dyExpr.getRight());
    assertEquals("Expression operator is wrong", Expression.OP_NOT, dyExpr.getOperator());
    DyadicExpression leftExpr = (DyadicExpression) dyExpr.getLeft();
    assertTrue("Left (left) should be PrimaryExpression but isnt", leftExpr.getLeft() instanceof PrimaryExpression);
    assertTrue("Left (right) should be Literal but isnt", leftExpr.getRight() instanceof Literal);
    assertEquals("Left expression operator is wrong", Expression.OP_GT, leftExpr.getOperator());
    PrimaryExpression primExpr = (PrimaryExpression) leftExpr.getLeft();
    assertEquals("Left (left) expression has incorrect number of tuples", 1, primExpr.getTuples().size());
    assertEquals("Left (left) expression 'id' is incorrect", "budget", primExpr.getId());
    Literal lit = (Literal) leftExpr.getRight();
    assertTrue("Left (right) expression literal is of incorrect type", lit.getLiteral() instanceof Long);
    assertEquals("Left (right) expression literal has incorrect value", 32, ((Long) lit.getLiteral()).longValue());
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 33 with QueryCompilation

use of org.datanucleus.query.compiler.QueryCompilation 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());
}
Also used : Department(org.datanucleus.samples.annotations.models.company.Department) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 34 with QueryCompilation

use of org.datanucleus.query.compiler.QueryCompilation in project tests by datanucleus.

the class JPQLCompilerTest method testUpdateSimple.

/**
 * Test for use of update clause.
 */
public void testUpdateSimple() {
    // Test use of UPDATE clause
    try {
        JPQLCompiler compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, null, null, null, null, null, null, null, "name = \"Sample Name\"");
        QueryCompilation compilation = compiler.compile(null, null);
        Expression[] updateExprs = compilation.getExprUpdate();
        assertNotNull("Update clause is null but shouldnt be", updateExprs);
        assertEquals("Number of update expressions is incorrect", 1, updateExprs.length);
        assertTrue("Update expression is of incorrect type " + updateExprs[0].getClass().getName(), updateExprs[0] instanceof DyadicExpression);
        DyadicExpression updateExpr = (DyadicExpression) updateExprs[0];
        Expression left = updateExpr.getLeft();
        Expression right = updateExpr.getRight();
        Operator op = updateExpr.getOperator();
        assertEquals("Operator in update expression is wrong", op, Expression.OP_EQ);
        assertTrue("Left hand side in update is wrong", left instanceof PrimaryExpression);
        assertTrue("Right hand side in update is wrong", right instanceof Literal);
        PrimaryExpression primExpr = (PrimaryExpression) left;
        assertEquals("Left hand side primary is wrong", "name", primExpr.getId());
        Literal lit = (Literal) right;
        assertEquals("Right hand side literal is wrong", "Sample Name", lit.getLiteral());
    } catch (NucleusUserException ne) {
        fail("Exception thrown in compile of update clause : " + ne.getMessage());
    }
}
Also used : Operator(org.datanucleus.query.expression.Expression.Operator) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 35 with QueryCompilation

use of org.datanucleus.query.compiler.QueryCompilation in project tests by datanucleus.

the class JPQLCompilerTest method testFilterComparison.

/**
 * Tests for simple field-literal comparison in filter.
 */
public void testFilterComparison() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "budget = 2", 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());
    }
    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 Literal but isnt", dyExpr.getRight() instanceof Literal);
    assertEquals("Operator between left and right is incorrect", Expression.OP_EQ, dyExpr.getOperator());
    PrimaryExpression leftExpr1 = (PrimaryExpression) dyExpr.getLeft();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr1.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "budget", leftExpr1.getId());
    Literal rightExpr1 = (Literal) dyExpr.getRight();
    assertTrue("Compiled right expression literal is of incorrect type", rightExpr1.getLiteral() instanceof Long);
    assertEquals("Compiled right expression literal has incorrect value", 2, ((Long) rightExpr1.getLiteral()).longValue());
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "100.0 > budget", null, null, null, null, null, null, null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception during compile", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    compilation = compiler.compile(new HashMap(), null);
    expr = compilation.getExprFilter();
    assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
    dyExpr = (DyadicExpression) expr;
    assertTrue("Compiled right expression should be PrimaryExpression but isnt", dyExpr.getRight() instanceof PrimaryExpression);
    assertTrue("Compiled left expression should be Literal but isnt", dyExpr.getLeft() instanceof Literal);
    assertEquals("Operator between left and right is incorrect", Expression.OP_GT, dyExpr.getOperator());
    PrimaryExpression rightExpr2 = (PrimaryExpression) dyExpr.getRight();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, rightExpr2.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "budget", rightExpr2.getId());
    Literal leftExpr2 = (Literal) dyExpr.getLeft();
    // TODO Why BigDecimal and not Double??
    assertTrue("Compiled right expression literal is of incorrect type", leftExpr2.getLiteral() instanceof BigDecimal);
    assertEquals("Compiled right expression literal has incorrect value", 100.0, ((BigDecimal) leftExpr2.getLiteral()).longValue(), 0.1);
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) BigDecimal(java.math.BigDecimal)

Aggregations

QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)54 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)44 HashMap (java.util.HashMap)34 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)33 Expression (org.datanucleus.query.expression.Expression)28 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)28 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)27 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)27 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)27 VariableExpression (org.datanucleus.query.expression.VariableExpression)27 NucleusException (org.datanucleus.exceptions.NucleusException)23 Literal (org.datanucleus.query.expression.Literal)18 List (java.util.List)17 OrderExpression (org.datanucleus.query.expression.OrderExpression)16 Product (org.datanucleus.samples.store.Product)16 Query (org.datanucleus.store.query.Query)16 ArrayList (java.util.ArrayList)15 JDOQuery (org.datanucleus.api.jdo.JDOQuery)15 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14