Search in sources :

Example 6 with JPQLCompiler

use of org.datanucleus.query.compiler.JPQLCompiler 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.query.compiler.JavaQueryCompiler) HashMap(java.util.HashMap) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 7 with JPQLCompiler

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

the class AbstractJPQLQuery method compileGeneric.

/* (non-Javadoc)
     * @see org.datanucleus.store.query.AbstractJavaQuery#compileGeneric(java.util.Map)
     */
@Override
public void compileGeneric(Map parameterValues) {
    if (compilation != null) {
        return;
    }
    QueryManager queryMgr = getQueryManager();
    String queryCacheKey = getQueryCacheKey();
    if (useCaching() && queryCacheKey != null) {
        QueryCompilation cachedCompilation = queryMgr.getQueryCompilationForQuery(getLanguage(), queryCacheKey);
        if (cachedCompilation != null) {
            compilation = cachedCompilation;
            if (compilation.getExprResult() == null) {
                // If the result was "Object(e)" or "e" then this is meaningless so remove
                result = null;
            }
            checkParameterTypesAgainstCompilation(parameterValues);
            return;
        }
    }
    long startTime = 0;
    if (NucleusLogger.QUERY.isDebugEnabled()) {
        startTime = System.currentTimeMillis();
        NucleusLogger.QUERY.debug(Localiser.msg("021044", getLanguage(), getSingleStringQuery()));
    }
    String from = this.from;
    if (type == QueryType.BULK_INSERT) {
        // Append artificial alias so the compilation passes
        from += " this";
    }
    JavaQueryCompiler compiler = new JPQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), from, candidateClass, candidateCollection, this.filter, getParsedImports(), this.ordering, this.result, this.grouping, this.having, explicitParameters, update);
    if (getBooleanExtensionProperty(EXTENSION_JPQL_STRICT, false)) {
        compiler.setOption(EXTENSION_JPQL_STRICT, "true");
    }
    compilation = compiler.compile(parameterValues, subqueries);
    if (QueryUtils.queryReturnsSingleRow(this)) {
        compilation.setReturnsSingleRow();
    }
    if (resultDistinct) {
        compilation.setResultDistinct();
    }
    if (compilation.getExprResult() == null) {
        // If the result was "Object(e)" or "e" then this is meaningless so remove
        result = null;
    }
    if (NucleusLogger.QUERY.isDebugEnabled()) {
        NucleusLogger.QUERY.debug(Localiser.msg("021045", getLanguage(), "" + (System.currentTimeMillis() - startTime)));
    }
    if (subqueries != null) {
        // Compile any subqueries
        compileSubqueries(subqueries, compilation, compiler, parameterValues);
    }
    if (NucleusLogger.QUERY.isDebugEnabled()) {
        // Log the query compilation
        NucleusLogger.QUERY.debug(compilation.toString());
    }
    if (implicitParameters != null) {
        // Make sure any implicit parameters have their values in the compilation
        Iterator paramKeyIter = implicitParameters.keySet().iterator();
        while (paramKeyIter.hasNext()) {
            Object paramKey = paramKeyIter.next();
            String paramName = "" + paramKey;
            applyImplicitParameterValueToCompilation(paramName, implicitParameters.get(paramName));
        }
    }
    checkParameterTypesAgainstCompilation(parameterValues);
    if (useCaching() && queryCacheKey != null) {
        // Cache for future reference
        queryMgr.addQueryCompilation(getLanguage(), queryCacheKey, compilation);
    }
}
Also used : JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Iterator(java.util.Iterator) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 8 with JPQLCompiler

use of org.datanucleus.query.compiler.JPQLCompiler 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 9 with JPQLCompiler

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

the class JPQLCompilerTest method testFilterComparisonWithAndOrMissingBrace.

/**
 * Tests for filter with field-literal comparison AND another comparison, and ORed with
 * another set of expressions and a missing bracket.
 */
public void testFilterComparisonWithAndOrMissingBrace() {
    try {
        JPQLCompiler compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "(budget = 2 AND 'Sales' == name) OR (budget >= 50 AND name == 'Marketing'", null, null, null, null, null, null, null);
        compiler.compile(null, null);
        fail("compilation of filter with valid field didnt throw exception, yet there was a missing bracket");
    } catch (NucleusException ne) {
    // Expected
    }
}
Also used : NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 10 with JPQLCompiler

use of org.datanucleus.query.compiler.JPQLCompiler 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)

Aggregations

JPQLCompiler (org.datanucleus.query.compiler.JPQLCompiler)17 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)14 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)14 HashMap (java.util.HashMap)11 NucleusException (org.datanucleus.exceptions.NucleusException)11 ClassExpression (org.datanucleus.query.expression.ClassExpression)10 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)10 Expression (org.datanucleus.query.expression.Expression)10 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)10 JoinExpression (org.datanucleus.query.expression.JoinExpression)10 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)10 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)10 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)10 VariableExpression (org.datanucleus.query.expression.VariableExpression)10 Project (org.datanucleus.samples.annotations.models.company.Project)8 Literal (org.datanucleus.query.expression.Literal)7 List (java.util.List)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 Department (org.datanucleus.samples.annotations.models.company.Department)2 BigDecimal (java.math.BigDecimal)1