Search in sources :

Example 1 with JDOQLCompiler

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

the class JDOQLCompilerTest method testFilterComparisonWithAnd.

/**
 * Tests for simple field-literal comparison in filter and AND of another comparison.
 */
public void testFilterComparisonWithAnd() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "statusId == 2 && 100.0 > price", 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 DyadicExpression but wasnt", expr instanceof DyadicExpression);
    DyadicExpression dyExpr = (DyadicExpression) expr;
    assertTrue("Compiled left expression should be DyadicExpression but isnt", dyExpr.getLeft() instanceof DyadicExpression);
    assertTrue("Compiled right expression should be DyadicExpression but isnt", dyExpr.getRight() instanceof DyadicExpression);
    DyadicExpression dyExpr1 = (DyadicExpression) dyExpr.getLeft();
    DyadicExpression dyExpr2 = (DyadicExpression) dyExpr.getRight();
    assertEquals("Operator between left and right is incorrect", Expression.OP_AND, dyExpr.getOperator());
    assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1.getLeft() instanceof PrimaryExpression);
    assertTrue("Compiled right expression should be Literal but isnt", dyExpr1.getRight() instanceof Literal);
    assertEquals("Operator between left (left and right) is incorrect", Expression.OP_EQ, dyExpr1.getOperator());
    PrimaryExpression leftExpr1 = (PrimaryExpression) dyExpr1.getLeft();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr1.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "statusId", leftExpr1.getId());
    Literal rightExpr1 = (Literal) dyExpr1.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());
    assertTrue("Compiled right expression should be PrimaryExpression but isnt", dyExpr2.getRight() instanceof PrimaryExpression);
    assertTrue("Compiled left expression should be Literal but isnt", dyExpr2.getLeft() instanceof Literal);
    assertEquals("Operator between right (left and right) is incorrect", Expression.OP_GT, dyExpr2.getOperator());
    PrimaryExpression rightExpr2 = (PrimaryExpression) dyExpr2.getRight();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, rightExpr2.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "price", rightExpr2.getId());
    Literal leftExpr2 = (Literal) dyExpr2.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 : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) BigDecimal(java.math.BigDecimal)

Example 2 with JDOQLCompiler

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

the class JDOQLCompilerTest method testFilterComparison.

/**
 * Tests for simple field-literal comparison in filter.
 */
public void testFilterComparison() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "statusId == 2", 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 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", "statusId", 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 JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "100.0 > price", null, null, null, null, null, null, null, null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", 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", "price", 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 : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) BigDecimal(java.math.BigDecimal)

Example 3 with JDOQLCompiler

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

the class JDOQLCompilerTest method testFilterWithStringEqualsLiteral.

/**
 * Tests for "String.equals(Literal)" in filter.
 */
public void testFilterWithStringEqualsLiteral() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name.equals(\"Kettle\")", 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", "equals", invExpr.getOperation());
    assertEquals("Number of parameters is wrong", 1, invExpr.getArguments().size());
    Object param = invExpr.getArguments().get(0);
    assertTrue("Parameter to equals() is of wrong type", param instanceof Literal);
    Literal paramLit = (Literal) param;
    assertEquals("Parameter to equals() has wrong value", "Kettle", paramLit.getLiteral());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 4 with JDOQLCompiler

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

the class JDOQLCompilerTest method testOrderNulls.

/**
 * Test for order clause.
 */
public void testOrderNulls() {
    // 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, null, null, "name ASC", null, null, null, null, null, null);
        compilation = compiler.compile(null, null);
    } catch (NucleusUserException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression[] orderExprs1 = compilation.getExprOrdering();
    assertEquals(1, orderExprs1.length);
    assertTrue(orderExprs1[0] instanceof OrderExpression);
    OrderExpression orderExpr1 = (OrderExpression) orderExprs1[0];
    assertEquals("name", ((PrimaryExpression) orderExpr1.getLeft()).getId());
    assertEquals("ascending", orderExpr1.getSortOrder());
    assertNull(orderExpr1.getNullOrder());
    // Test use of NULLS
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, null, null, "name ASC NULLS FIRST", null, null, null, null, null, null);
        compilation = compiler.compile(null, null);
    } catch (NucleusUserException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression[] orderExprs2 = compilation.getExprOrdering();
    assertEquals(1, orderExprs2.length);
    assertTrue(orderExprs2[0] instanceof OrderExpression);
    OrderExpression orderExpr2 = (OrderExpression) orderExprs2[0];
    assertEquals("name", ((PrimaryExpression) orderExpr2.getLeft()).getId());
    assertEquals("ascending", orderExpr2.getSortOrder());
    assertEquals(NullOrderingType.NULLS_FIRST, orderExpr2.getNullOrder());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 5 with JDOQLCompiler

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

the class JDOQLEvaluatorTest method testFilterCollectionContains.

/**
 * Test of filter with collectionField.contains(element).
 */
public void testFilterCollectionContains() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<UserGroup> instances = new ArrayList<>();
        UserGroup grp1 = new UserGroup(101, "First Group");
        UserGroup grp2 = new UserGroup(102, "Second Group");
        UserGroup grp3 = new UserGroup(103, "Third Group");
        GroupMember mem1 = new GroupMember(201, "Donald Duck");
        GroupMember mem2 = new GroupMember(202, "Mickey Mouse");
        GroupMember mem3 = new GroupMember(203, "Minnie Mouse");
        grp1.getMembers().add(mem1);
        grp2.getMembers().add(mem1);
        grp2.getMembers().add(mem2);
        grp3.getMembers().add(mem2);
        grp3.getMembers().add(mem3);
        instances.add(grp1);
        instances.add(grp2);
        instances.add(grp3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(UserGroup.class, "members.contains(el) && el.name == 'Minnie Mouse'");
        q.declareVariables(GroupMember.class.getName() + " el");
        Query query = q.getInternalQuery();
        ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
        JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        QueryCompilation compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        List results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        UserGroup grp = (UserGroup) results.get(0);
        assertEquals("Result instance has wrong name", "Third Group", grp.getName());
        tx.commit();
    } catch (Exception e) {
        LOG.info(">> Unexpected exception thrown during test", e);
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Aggregations

JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)36 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)33 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)33 HashMap (java.util.HashMap)24 Product (org.datanucleus.samples.store.Product)17 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)16 Expression (org.datanucleus.query.expression.Expression)16 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)16 OrderExpression (org.datanucleus.query.expression.OrderExpression)16 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)16 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)16 VariableExpression (org.datanucleus.query.expression.VariableExpression)16 ArrayList (java.util.ArrayList)14 List (java.util.List)14 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 JDOQuery (org.datanucleus.api.jdo.JDOQuery)14 NucleusException (org.datanucleus.exceptions.NucleusException)14 JDOQLInMemoryEvaluator (org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator)14