Search in sources :

Example 16 with QueryCompilation

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

the class JDOQLEvaluatorTest method testFilterOrWithEqualityOperator.

/**
 * Test of filter with OR and equality operator.
 */
public void testFilterOrWithEqualityOperator() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<Person> instances = new ArrayList<>();
        Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
        Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
        Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
        instances.add(p1);
        instances.add(p2);
        instances.add(p3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName == 'Mickey' || lastName == 'Mouse'");
        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", 2, results.size());
        Iterator iter = results.iterator();
        while (iter.hasNext()) {
            Person p = (Person) iter.next();
            if (p.getFirstName().equals("Mickey")) {
                assertEquals("Last name of result instance is wrong", "Mouse", p.getLastName());
                assertEquals("Person number of result instance is wrong", 101, p.getPersonNum());
            } else if (p.getFirstName().equals("Minnie")) {
                assertEquals("Last name of result instance is wrong", "Mouse", p.getLastName());
                assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
            } else {
                fail("Incorrect Person found by query " + p.asString());
            }
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) 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) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) Person(org.jpox.samples.models.company.Person)

Example 17 with QueryCompilation

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

the class JDOQLEvaluatorTest method testFilterCollectionContainsNonPC.

/**
 * Test of filter with collectionField.contains(nonPC).
 */
public void testFilterCollectionContainsNonPC() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<SetHolder> instances = new ArrayList<>();
        SetHolder holder1 = new SetHolder("First");
        SetHolder holder2 = new SetHolder("Second");
        SetHolder holder3 = new SetHolder("Third");
        holder1.getJoinSetNonPC1().add("First Element");
        holder2.getJoinSetNonPC1().add("First Element");
        holder2.getJoinSetNonPC1().add("Second Element");
        holder3.getJoinSetNonPC1().add("Second Element");
        holder3.getJoinSetNonPC1().add("Third Element");
        instances.add(holder1);
        instances.add(holder2);
        instances.add(holder3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(SetHolder.class, "joinSetNonPC1.contains('Third Element')");
        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());
        SetHolder holder = (SetHolder) results.get(0);
        assertEquals("Result instance has wrong name", "Third", holder.getName());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) 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) SetHolder(org.jpox.samples.one_many.collection.SetHolder) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 18 with QueryCompilation

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

the class JDOQLEvaluatorTest method testFilterNegate.

/**
 * Test of filter with !(condition).
 */
public void testFilterNegate() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<Person> instances = new ArrayList<>();
        Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
        p1.setAge(34);
        Employee p2 = new Employee(102, "Donald", "Duck", "donald.duck@warnerbros.com", 13400.0f, "12345");
        p2.setAge(38);
        Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
        p3.setAge(31);
        instances.add(p1);
        instances.add(p2);
        instances.add(p3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "!(age > 32)");
        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());
        Person p = (Person) results.get(0);
        assertEquals("Result instance has wrong first name", "Minnie", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
        assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
        assertEquals("Age of result instance is wrong", 31, p.getAge());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) 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) Employee(org.jpox.samples.models.company.Employee) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) Person(org.jpox.samples.models.company.Person)

Example 19 with QueryCompilation

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

the class JDOQLEvaluatorTest method testFilterStringEndsWith.

/**
 * Test of filter with String.endsWith().
 */
public void testFilterStringEndsWith() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<Person> instances = new ArrayList<>();
        Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
        Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
        Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
        instances.add(p1);
        instances.add(p2);
        instances.add(p3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName.endsWith('d')");
        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());
        Person p = (Person) results.get(0);
        assertEquals("Result instance has wrong first name", "Donald", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Duck", p.getLastName());
        assertEquals("Person number of result instance is wrong", 102, p.getPersonNum());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) 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) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) Person(org.jpox.samples.models.company.Person)

Example 20 with QueryCompilation

use of org.datanucleus.query.compiler.QueryCompilation 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());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) Symbol(org.datanucleus.query.compiler.Symbol) Product(org.datanucleus.samples.store.Product) SymbolTable(org.datanucleus.query.compiler.SymbolTable) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) 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) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) Inventory(org.datanucleus.samples.store.Inventory)

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