Search in sources :

Example 1 with JavaQueryCompiler

use of org.datanucleus.store.query.compiler.JavaQueryCompiler in project datanucleus-core by datanucleus.

the class AbstractJPQLQuery method compileSubqueries.

/**
 * Recursively compile the subqueries
 * @param subqueryMap The subquery definition map
 * @param parentCompilation The 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(), ((AbstractJPQLQuery) subquery).getSingleStringQuery()));
        }
        JavaQueryCompiler subCompiler = new JPQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), subquery.from, subquery.candidateClass, null, subquery.filter, getParsedImports(), subquery.ordering, subquery.result, subquery.grouping, subquery.having, null, null);
        if (getBooleanExtensionProperty(EXTENSION_JPQL_STRICT, false)) {
            subCompiler.setOption(EXTENSION_JPQL_STRICT, "true");
        }
        subCompiler.setLinkToParentQuery(parentCompiler, null);
        QueryCompilation subqueryCompilation = subCompiler.compile(parameterValues, subquery.subqueries);
        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);
        }
    }
}
Also used : JavaQueryCompiler(org.datanucleus.store.query.compiler.JavaQueryCompiler) QueryCompilation(org.datanucleus.store.query.compiler.QueryCompilation) Map(java.util.Map) JPQLCompiler(org.datanucleus.store.query.compiler.JPQLCompiler)

Example 2 with JavaQueryCompiler

use of org.datanucleus.store.query.compiler.JavaQueryCompiler in project datanucleus-core by datanucleus.

the class QueryUtils method orderCandidates.

/**
 * Convenience method to in-memory order the candidates, using the ordering supplied.
 * Assumes that the query is JDOQL.
 * @param candidates The candidates
 * @param type Candidate type
 * @param ordering The ordering clause
 * @param ec Execution Context
 * @param clr ClassLoader resolver
 * @param queryLanguage The query language in use
 * @return The ordered list. Note that this typically returns Arrays$List which may not be what you want
 */
public static List orderCandidates(List candidates, Class type, String ordering, ExecutionContext ec, ClassLoaderResolver clr, String queryLanguage) {
    if (candidates == null || candidates.isEmpty() || ordering == null || ordering.equals("#PK")) {
        return candidates;
    }
    JavaQueryCompiler compiler = new JPQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), null, type, null, null, null, ordering, null, null, null, null, null);
    QueryCompilation compilation = compiler.compile(null, null);
    return QueryUtils.orderCandidates(candidates, compilation.getExprOrdering(), new HashMap(), "this", ec, clr, null, null, queryLanguage);
}
Also used : JavaQueryCompiler(org.datanucleus.store.query.compiler.JavaQueryCompiler) HashMap(java.util.HashMap) QueryCompilation(org.datanucleus.store.query.compiler.QueryCompilation) JPQLCompiler(org.datanucleus.store.query.compiler.JPQLCompiler)

Example 3 with JavaQueryCompiler

use of org.datanucleus.store.query.compiler.JavaQueryCompiler in project tests by datanucleus.

the class JDOQLEvaluatorTest method testFilterGreaterThanLessThan.

/**
 * Test of filter with ">", "<" and "+".
 */
public void testFilterGreaterThanLessThan() {
    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);
        Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
        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 > 34");
        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());
        assertEquals("Age of result instance is wrong", 38, p.getAge());
        // Compile the query
        q = (JDOQuery) pm.newQuery(Person.class, "age+2 < 35");
        query = q.getInternalQuery();
        clr = query.getExecutionContext().getClassLoaderResolver();
        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);
        compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        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.store.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.store.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.store.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) JavaQueryCompiler(org.datanucleus.store.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.store.query.compiler.QueryCompilation) Person(org.datanucleus.samples.models.company.Person)

Example 4 with JavaQueryCompiler

use of org.datanucleus.store.query.compiler.JavaQueryCompiler 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.store.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.store.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.store.query.expression.PrimaryExpression) OrderExpression(org.datanucleus.store.query.expression.OrderExpression) DyadicExpression(org.datanucleus.store.query.expression.DyadicExpression) Expression(org.datanucleus.store.query.expression.Expression) VariableExpression(org.datanucleus.store.query.expression.VariableExpression) InvokeExpression(org.datanucleus.store.query.expression.InvokeExpression) ParameterExpression(org.datanucleus.store.query.expression.ParameterExpression) OrderExpression(org.datanucleus.store.query.expression.OrderExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.store.query.compiler.QueryCompilation)

Example 5 with JavaQueryCompiler

use of org.datanucleus.store.query.compiler.JavaQueryCompiler 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, ((Number) rightExprRight.getLiteral()).intValue());
    // 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.store.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.store.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.store.query.expression.PrimaryExpression) HashMap(java.util.HashMap) Symbol(org.datanucleus.store.query.compiler.Symbol) Product(org.datanucleus.samples.store.Product) SymbolTable(org.datanucleus.store.query.compiler.SymbolTable) VariableExpression(org.datanucleus.store.query.expression.VariableExpression) DyadicExpression(org.datanucleus.store.query.expression.DyadicExpression) JavaQueryCompiler(org.datanucleus.store.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.store.query.expression.PrimaryExpression) OrderExpression(org.datanucleus.store.query.expression.OrderExpression) DyadicExpression(org.datanucleus.store.query.expression.DyadicExpression) Expression(org.datanucleus.store.query.expression.Expression) VariableExpression(org.datanucleus.store.query.expression.VariableExpression) InvokeExpression(org.datanucleus.store.query.expression.InvokeExpression) ParameterExpression(org.datanucleus.store.query.expression.ParameterExpression) Literal(org.datanucleus.store.query.expression.Literal) QueryCompilation(org.datanucleus.store.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) Inventory(org.datanucleus.samples.store.Inventory)

Aggregations

JavaQueryCompiler (org.datanucleus.store.query.compiler.JavaQueryCompiler)47 QueryCompilation (org.datanucleus.store.query.compiler.QueryCompilation)44 HashMap (java.util.HashMap)35 JDOQLCompiler (org.datanucleus.store.query.compiler.JDOQLCompiler)33 DyadicExpression (org.datanucleus.store.query.expression.DyadicExpression)25 Expression (org.datanucleus.store.query.expression.Expression)25 InvokeExpression (org.datanucleus.store.query.expression.InvokeExpression)25 ParameterExpression (org.datanucleus.store.query.expression.ParameterExpression)25 PrimaryExpression (org.datanucleus.store.query.expression.PrimaryExpression)25 VariableExpression (org.datanucleus.store.query.expression.VariableExpression)25 NucleusException (org.datanucleus.exceptions.NucleusException)23 Product (org.datanucleus.samples.store.Product)17 Literal (org.datanucleus.store.query.expression.Literal)17 List (java.util.List)16 OrderExpression (org.datanucleus.store.query.expression.OrderExpression)16 ArrayList (java.util.ArrayList)14 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 JDOQuery (org.datanucleus.api.jdo.JDOQuery)14