Search in sources :

Example 1 with StringSubqueryExpression

use of org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method processVariableExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processVariableExpression(org.datanucleus.query.expression.VariableExpression)
     */
@Override
protected Object processVariableExpression(VariableExpression expr) {
    String varName = expr.getId();
    Symbol varSym = expr.getSymbol();
    if (varSym != null) {
        // Use name from symbol if possible
        varName = varSym.getQualifiedName();
    }
    if (hasSQLTableMappingForAlias(varName)) {
        // Variable already found
        SQLTableMapping tblMapping = getSQLTableMappingForAlias(varName);
        SQLExpression sqlExpr = exprFactory.newExpression(tblMapping.table.getSQLStatement(), tblMapping.table, tblMapping.mapping);
        stack.push(sqlExpr);
        return sqlExpr;
    } else if (compilation.getCompilationForSubquery(varName) != null) {
        // Subquery variable
        QueryCompilation subCompilation = compilation.getCompilationForSubquery(varName);
        AbstractClassMetaData subCmd = ec.getMetaDataManager().getMetaDataForClass(subCompilation.getCandidateClass(), ec.getClassLoaderResolver());
        // Create subquery statement, using any provided alias if possible
        String subAlias = null;
        if (subCompilation.getCandidateAlias() != null && !subCompilation.getCandidateAlias().equals(candidateAlias)) {
            subAlias = subCompilation.getCandidateAlias();
        }
        StatementResultMapping subqueryResultMapping = new StatementResultMapping();
        // TODO Fix "avg(something)" arg - not essential but is a hack right now
        SQLStatement subStmt = RDBMSQueryUtils.getStatementForCandidates(storeMgr, stmt, subCmd, null, ec, subCompilation.getCandidateClass(), true, "avg(something)", subAlias, null, null);
        QueryToSQLMapper sqlMapper = new QueryToSQLMapper(subStmt, subCompilation, parameters, null, subqueryResultMapping, subCmd, true, fetchPlan, ec, importsDefinition, options, extensionsByName);
        sqlMapper.setDefaultJoinType(defaultJoinType);
        sqlMapper.setDefaultJoinTypeFilter(defaultJoinTypeFilter);
        sqlMapper.setParentMapper(this);
        sqlMapper.compile();
        if (subqueryResultMapping.getNumberOfResultExpressions() > 1) {
            throw new NucleusUserException("Number of result expressions in subquery should be 1");
        }
        SQLExpression subExpr = null;
        // TODO Cater for subquery select of its own candidate
        if (subqueryResultMapping.getNumberOfResultExpressions() == 0) {
            subExpr = new org.datanucleus.store.rdbms.sql.expression.SubqueryExpression(stmt, subStmt);
        } else {
            JavaTypeMapping subMapping = ((StatementMappingIndex) subqueryResultMapping.getMappingForResultExpression(0)).getMapping();
            if (subMapping instanceof TemporalMapping) {
                subExpr = new TemporalSubqueryExpression(stmt, subStmt);
            } else if (subMapping instanceof StringMapping) {
                subExpr = new StringSubqueryExpression(stmt, subStmt);
            } else {
                subExpr = new NumericSubqueryExpression(stmt, subStmt);
            }
            if (subExpr.getJavaTypeMapping() == null) {
                subExpr.setJavaTypeMapping(subMapping);
            }
        }
        stack.push(subExpr);
        return subExpr;
    } else if (stmt.getParentStatement() != null && parentMapper != null && parentMapper.candidateAlias != null && parentMapper.candidateAlias.equals(varName)) {
        // Variable in subquery linking back to parent query
        SQLExpression varExpr = exprFactory.newExpression(stmt.getParentStatement(), stmt.getParentStatement().getPrimaryTable(), stmt.getParentStatement().getPrimaryTable().getTable().getIdMapping());
        stack.push(varExpr);
        return varExpr;
    } else {
        // Variable never met before, so return as UnboundExpression - process later if needing binding
        NucleusLogger.QUERY.debug("QueryToSQL.processVariable (unbound) variable=" + varName + " is not yet bound so returning UnboundExpression");
        UnboundExpression unbExpr = new UnboundExpression(stmt, varName);
        stack.push(unbExpr);
        return unbExpr;
    }
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) Symbol(org.datanucleus.query.compiler.Symbol) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) StringMapping(org.datanucleus.store.rdbms.mapping.java.StringMapping) SQLStatement(org.datanucleus.store.rdbms.sql.SQLStatement) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) TemporalMapping(org.datanucleus.store.rdbms.mapping.java.TemporalMapping)

Example 2 with StringSubqueryExpression

use of org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression in project datanucleus-rdbms by datanucleus.

the class SimpleOrderableAggregateMethod method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression expr, List<SQLExpression> args) {
    if (expr != null) {
        throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
    } else if (args == null || args.size() != 1) {
        throw new NucleusException(getFunctionName() + " is only supported with a single argument");
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.RESULT || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.HAVING) {
        // FUNC(argExpr)
        // Use same java type as the argument
        SQLExpression argExpr = args.get(0);
        JavaTypeMapping m = argExpr.getJavaTypeMapping();
        if (argExpr instanceof TemporalExpression) {
            return new AggregateTemporalExpression(stmt, m, getFunctionName(), args);
        } else if (argExpr instanceof StringExpression) {
            return new AggregateStringExpression(stmt, m, getFunctionName(), args);
        }
        return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
    }
    // Handle as Subquery "SELECT AVG(expr) FROM tbl"
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    SQLExpression argExpr = args.get(0);
    SelectStatement subStmt = new SelectStatement(stmt, stmt.getRDBMSManager(), argExpr.getSQLTable().getTable(), argExpr.getSQLTable().getAlias(), null);
    subStmt.setClassLoaderResolver(clr);
    JavaTypeMapping mapping = stmt.getRDBMSManager().getMappingManager().getMappingWithDatastoreMapping(String.class, false, false, clr);
    String aggregateString = getFunctionName() + "(" + argExpr.toSQLText() + ")";
    SQLExpression aggExpr = exprFactory.newLiteral(subStmt, mapping, aggregateString);
    ((StringLiteral) aggExpr).generateStatementWithoutQuotes();
    subStmt.select(aggExpr, null);
    JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
    SQLExpression subqExpr = null;
    if (argExpr instanceof TemporalExpression) {
        subqExpr = new TemporalSubqueryExpression(stmt, subStmt);
    } else if (argExpr instanceof StringExpression) {
        subqExpr = new StringSubqueryExpression(stmt, subStmt);
    } else {
        subqExpr = new NumericSubqueryExpression(stmt, subStmt);
    }
    subqExpr.setJavaTypeMapping(subqMapping);
    return subqExpr;
}
Also used : AggregateStringExpression(org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression) AggregateTemporalExpression(org.datanucleus.store.rdbms.sql.expression.AggregateTemporalExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) AggregateStringExpression(org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) AggregateTemporalExpression(org.datanucleus.store.rdbms.sql.expression.AggregateTemporalExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)2 NumericSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression)2 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)2 StringSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression)2 TemporalSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)1 Symbol (org.datanucleus.query.compiler.Symbol)1 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)1 StringMapping (org.datanucleus.store.rdbms.mapping.java.StringMapping)1 TemporalMapping (org.datanucleus.store.rdbms.mapping.java.TemporalMapping)1 SQLStatement (org.datanucleus.store.rdbms.sql.SQLStatement)1 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)1 AggregateNumericExpression (org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression)1 AggregateStringExpression (org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression)1 AggregateTemporalExpression (org.datanucleus.store.rdbms.sql.expression.AggregateTemporalExpression)1 BooleanSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression)1