Search in sources :

Example 6 with Symbol

use of org.datanucleus.store.query.compiler.Symbol in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method processUnboundExpression.

protected SQLExpression processUnboundExpression(UnboundExpression expr) {
    String varName = expr.getVariableName();
    Symbol varSym = compilation.getSymbolTable().getSymbol(varName);
    SQLExpression sqlExpr = bindVariable(expr, varSym.getValueType());
    if (sqlExpr != null) {
        stack.push(sqlExpr);
        return sqlExpr;
    }
    throw new NucleusUserException("Variable '" + varName + "' is unbound and cannot be determined (is it a misspelled field name? or is not intended to be a variable?)");
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) Symbol(org.datanucleus.store.query.compiler.Symbol) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 7 with Symbol

use of org.datanucleus.store.query.compiler.Symbol in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method compile.

/**
 * Method to update the supplied SQLStatement with the components of the specified query.
 * During the compilation process this updates the SQLStatement "compileComponent" to the
 * component of the query being compiled.
 */
public void compile() {
    if (NucleusLogger.QUERY.isDebugEnabled() && parentMapper == null) {
        // Give debug output of compilation
        StringBuilder str = new StringBuilder("JoinType : navigation(default=");
        str.append(defaultJoinType != null ? defaultJoinType : "(using nullability)");
        str.append(", filter=");
        str.append(defaultJoinTypeFilter != null ? defaultJoinTypeFilter : "(using nullability)");
        str.append(")");
        if (extensionsByName != null) {
            Iterator<Map.Entry<String, Object>> extensionsIter = extensionsByName.entrySet().iterator();
            while (extensionsIter.hasNext()) {
                Map.Entry<String, Object> entry = extensionsIter.next();
                String key = entry.getKey();
                if (key.startsWith("datanucleus.query.jdoql.") && key.endsWith(".join")) {
                    // Alias join definition
                    String alias = key.substring("datanucleus.query.jdoql.".length(), key.lastIndexOf(".join"));
                    str.append(", ").append(alias).append("=").append(entry.getValue());
                }
            }
        }
        NucleusLogger.QUERY.debug("Compile of " + compilation.getQueryLanguage() + " into SQL - " + str);
    }
    compileFrom();
    compileFilter();
    if (stmt instanceof UpdateStatement) {
        compileUpdate((UpdateStatement) stmt);
    } else if (stmt instanceof SelectStatement) {
        SelectStatement selectStmt = (SelectStatement) stmt;
        // the datastore doesn't allow select of some field types when used with DISTINCT
        if (compilation.getResultDistinct()) {
            selectStmt.setDistinct(true);
        } else if (!options.contains(OPTION_EXPLICIT_JOINS) && compilation.getExprResult() == null) {
            // Joins are made implicitly and no result so set distinct based on whether joining to other table groups
            if (selectStmt.getNumberOfTableGroups() > 1) {
                // Queries against an extent always consider only distinct candidate instances, regardless of whether distinct is specified (JDO spec)
                if (!options.contains(OPTION_NON_DISTINCT_IMPLICIT_JOINS)) {
                    // If user can guarantee distinct w/ query no reason to take performance hit of distinct clause
                    selectStmt.setDistinct(true);
                }
            }
        }
        compileResult(selectStmt);
        compileGrouping(selectStmt);
        compileHaving(selectStmt);
        compileOrdering(selectStmt);
    }
    // Check for variables that haven't been bound to the query (declared but not used)
    for (String symbol : compilation.getSymbolTable().getSymbolNames()) {
        Symbol sym = compilation.getSymbolTable().getSymbol(symbol);
        if (sym.getType() == Symbol.VARIABLE) {
            if (compilation.getCompilationForSubquery(sym.getQualifiedName()) == null && !hasSQLTableMappingForAlias(sym.getQualifiedName())) {
                // Variable not a subquery, nor had its table allocated
                throw new QueryCompilerSyntaxException("Query has variable \"" + sym.getQualifiedName() + "\" which is not bound to the query");
            }
        }
    }
}
Also used : SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) UpdateStatement(org.datanucleus.store.rdbms.sql.UpdateStatement) Symbol(org.datanucleus.store.query.compiler.Symbol) QueryCompilerSyntaxException(org.datanucleus.store.query.compiler.QueryCompilerSyntaxException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Symbol (org.datanucleus.store.query.compiler.Symbol)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)5 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)5 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)4 FetchPlanForClass (org.datanucleus.FetchPlanForClass)3 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)3 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 NucleusException (org.datanucleus.exceptions.NucleusException)2 DyadicExpression (org.datanucleus.store.query.expression.DyadicExpression)2 InvokeExpression (org.datanucleus.store.query.expression.InvokeExpression)2 Literal (org.datanucleus.store.query.expression.Literal)2 ParameterExpression (org.datanucleus.store.query.expression.ParameterExpression)2 PrimaryExpression (org.datanucleus.store.query.expression.PrimaryExpression)2 SubqueryExpression (org.datanucleus.store.query.expression.SubqueryExpression)2 VariableExpression (org.datanucleus.store.query.expression.VariableExpression)2