Search in sources :

Example 26 with QueryCompilation

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

the class Query method applyImplicitParameterValueToSubqueries.

protected boolean applyImplicitParameterValueToSubqueries(String name, Object value, QueryCompilation comp) {
    boolean symbolFound = false;
    Symbol sym = null;
    // Apply to any subqueries
    String[] subqueryNames = comp.getSubqueryAliases();
    if (subqueryNames != null) {
        for (int i = 0; i < subqueryNames.length; i++) {
            QueryCompilation subCompilation = comp.getCompilationForSubquery(subqueryNames[i]);
            sym = subCompilation.getSymbolTable().getSymbol(name);
            if (sym != null) {
                symbolFound = true;
                if (sym.getValueType() == null && value != null) {
                    // Update the compilation providing the type of this parameter
                    sym.setValueType(value.getClass());
                } else if (sym.getValueType() != null && value != null) {
                    if (!QueryUtils.queryParameterTypesAreCompatible(sym.getValueType(), value.getClass())) {
                        // Parameter value supplied is not consistent with what the query compilation expects
                        throw new QueryInvalidParametersException("Parameter " + name + " needs to be assignable from " + sym.getValueType().getName() + " yet the value is of type " + value.getClass().getName());
                    }
                }
            }
            boolean subSymbolFound = applyImplicitParameterValueToSubqueries(name, value, subCompilation);
            if (subSymbolFound) {
                symbolFound = true;
            }
        }
    }
    return symbolFound;
}
Also used : Symbol(org.datanucleus.query.compiler.Symbol) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 27 with QueryCompilation

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

the class AbstractJDOQLQuery 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;
            checkParameterTypesAgainstCompilation(parameterValues);
            return;
        }
    }
    // Resolve resultClass name if defined
    if (resultClassName != null) {
        // Throws NucleusUserException if not resolvable
        resultClass = resolveClassDeclaration(resultClassName);
        resultClassName = null;
    }
    long startTime = 0;
    if (NucleusLogger.QUERY.isDebugEnabled()) {
        startTime = System.currentTimeMillis();
        NucleusLogger.QUERY.debug(Localiser.msg("021044", getLanguage(), getSingleStringQuery()));
    }
    JDOQLCompiler compiler = new JDOQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), from, candidateClass, candidateCollection, this.filter, getParsedImports(), this.ordering, this.result, this.grouping, this.having, explicitParameters, explicitVariables, this.update);
    if (getBooleanExtensionProperty(PropertyNames.PROPERTY_QUERY_COMPILE_OPTIMISE_VAR_THIS, false)) {
        compiler.setOption(PropertyNames.PROPERTY_QUERY_COMPILE_OPTIMISE_VAR_THIS, true);
    }
    if (getBooleanExtensionProperty(EXTENSION_JDOQL_STRICT, false)) {
        compiler.setOption(EXTENSION_JDOQL_STRICT, "true");
    }
    boolean allowAllSyntax = ec.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
    if (ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL) != null) {
        allowAllSyntax = ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
    }
    compiler.setAllowAll(allowAllSyntax);
    compilation = compiler.compile(parameterValues, subqueries);
    if (QueryUtils.queryReturnsSingleRow(this)) {
        compilation.setReturnsSingleRow();
    }
    if (resultDistinct) {
        compilation.setResultDistinct();
    }
    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());
    }
    checkParameterTypesAgainstCompilation(parameterValues);
    if (useCaching() && queryCacheKey != null) {
        // Cache for future reference
        queryMgr.addQueryCompilation(getLanguage(), queryCacheKey, compilation);
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 28 with QueryCompilation

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

the class AbstractJDOQLQuery method compileSubqueries.

/**
 * Recursively compile the subqueries
 * @param subqueryMap The subquery definition map
 * @param parentCompilation 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(), ((AbstractJDOQLQuery) subquery).getSingleStringQuery()));
        }
        JDOQLCompiler subCompiler = new JDOQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), subquery.from, subquery.candidateClass, null, subquery.filter, getParsedImports(), subquery.ordering, subquery.result, subquery.grouping, subquery.having, subquery.explicitParameters, null, null);
        if (getBooleanExtensionProperty(EXTENSION_JDOQL_STRICT, false)) {
            subCompiler.setOption(EXTENSION_JDOQL_STRICT, "true");
        }
        boolean allowAllSyntax = ec.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
        if (ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL) != null) {
            allowAllSyntax = ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
        }
        subCompiler.setAllowAll(allowAllSyntax);
        subCompiler.setLinkToParentQuery(parentCompiler, subqueryDefinition.getParameterMap());
        QueryCompilation subqueryCompilation = subCompiler.compile(parameterValues, null);
        if (QueryUtils.queryReturnsSingleRow(subquery)) {
            subqueryCompilation.setReturnsSingleRow();
        }
        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 : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) Map(java.util.Map)

Example 29 with QueryCompilation

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

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

the class QueryManagerImpl method getQueryCompilationForQuery.

/* (non-Javadoc)
     * @see org.datanucleus.store.query.QueryManager#getQueryCompilationForQuery(java.lang.String, java.lang.String)
     */
@Override
public QueryCompilation getQueryCompilationForQuery(String language, String query) {
    if (queryCompilationCache != null) {
        String queryKey = language + ":" + query;
        QueryCompilation compilation = queryCompilationCache.get(queryKey);
        if (compilation != null) {
            if (NucleusLogger.QUERY.isDebugEnabled()) {
                NucleusLogger.QUERY.debug(Localiser.msg("021079", query, language));
            }
            return compilation;
        }
    }
    return null;
}
Also used : QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

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