Search in sources :

Example 1 with ExpressionPair

use of org.datanucleus.store.query.expression.CaseExpression.ExpressionPair in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method processCaseExpression.

protected Object processCaseExpression(CaseExpression expr, SQLExpression typeExpr) {
    processingCase = true;
    try {
        List<ExpressionPair> conditions = expr.getConditions();
        Iterator<ExpressionPair> whenExprIter = conditions.iterator();
        SQLExpression[] whenSqlExprs = new SQLExpression[conditions.size()];
        SQLExpression[] actionSqlExprs = new SQLExpression[conditions.size()];
        boolean numericCase = false;
        boolean booleanCase = false;
        boolean stringCase = false;
        boolean typeSet = false;
        if (typeExpr != null) {
            if (typeExpr instanceof NumericExpression) {
                numericCase = true;
                typeSet = true;
            } else if (typeExpr instanceof BooleanExpression) {
                booleanCase = true;
                typeSet = true;
            } else if (typeExpr instanceof StringExpression) {
                stringCase = true;
                typeSet = true;
            }
        }
        int i = 0;
        while (whenExprIter.hasNext()) {
            ExpressionPair pair = whenExprIter.next();
            Expression whenExpr = pair.getWhenExpression();
            whenExpr.evaluate(this);
            whenSqlExprs[i] = stack.pop();
            if (!(whenSqlExprs[i] instanceof BooleanExpression)) {
                throw new QueryCompilerSyntaxException("IF/ELSE conditional expression should return boolean but doesn't : " + expr);
            }
            Expression actionExpr = pair.getActionExpression();
            actionExpr.evaluate(this);
            actionSqlExprs[i] = stack.pop();
            if (!typeSet) {
                if (actionSqlExprs[i] instanceof NumericExpression) {
                    numericCase = true;
                    typeSet = true;
                } else if (actionSqlExprs[i] instanceof BooleanExpression) {
                    booleanCase = true;
                    typeSet = true;
                } else if (actionSqlExprs[i] instanceof StringExpression) {
                    stringCase = true;
                    typeSet = true;
                }
            }
            i++;
        }
        Expression elseExpr = expr.getElseExpression();
        elseExpr.evaluate(this);
        SQLExpression elseActionSqlExpr = stack.pop();
        // Check that all action sql expressions are consistent
        for (int j = 1; j < actionSqlExprs.length; j++) {
            if (!checkCaseExpressionsConsistent(actionSqlExprs[0], actionSqlExprs[j])) {
                throw new QueryCompilerSyntaxException("IF/ELSE action expression " + actionSqlExprs[j] + " is of different type to first action " + actionSqlExprs[0] + " - must be consistent");
            }
        }
        if (!checkCaseExpressionsConsistent(actionSqlExprs[0], elseActionSqlExpr)) {
            throw new QueryCompilerSyntaxException("IF/ELSE action expression " + elseActionSqlExpr + " is of different type to first action " + actionSqlExprs[0] + " - must be consistent");
        }
        SQLExpression caseSqlExpr = null;
        if (numericCase) {
            caseSqlExpr = new org.datanucleus.store.rdbms.sql.expression.CaseNumericExpression(whenSqlExprs, actionSqlExprs, elseActionSqlExpr);
        } else if (booleanCase) {
            caseSqlExpr = new org.datanucleus.store.rdbms.sql.expression.CaseBooleanExpression(whenSqlExprs, actionSqlExprs, elseActionSqlExpr);
        } else if (stringCase) {
            caseSqlExpr = new org.datanucleus.store.rdbms.sql.expression.CaseStringExpression(whenSqlExprs, actionSqlExprs, elseActionSqlExpr);
        } else {
            caseSqlExpr = new org.datanucleus.store.rdbms.sql.expression.CaseExpression(whenSqlExprs, actionSqlExprs, elseActionSqlExpr);
        }
        stack.push(caseSqlExpr);
        return caseSqlExpr;
    } finally {
        processingCase = false;
    }
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) PrimaryExpression(org.datanucleus.store.query.expression.PrimaryExpression) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) TypeConverterExpression(org.datanucleus.store.rdbms.sql.expression.TypeConverterExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) ArrayExpression(org.datanucleus.store.query.expression.ArrayExpression) CaseExpression(org.datanucleus.store.query.expression.CaseExpression) CreatorExpression(org.datanucleus.store.query.expression.CreatorExpression) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) OrderExpression(org.datanucleus.store.query.expression.OrderExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) ClassExpression(org.datanucleus.store.query.expression.ClassExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) Expression(org.datanucleus.store.query.expression.Expression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ParameterExpression(org.datanucleus.store.query.expression.ParameterExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SubqueryExpression(org.datanucleus.store.query.expression.SubqueryExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) VariableExpression(org.datanucleus.store.query.expression.VariableExpression) TypeExpression(org.datanucleus.store.query.expression.TypeExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) DyadicExpression(org.datanucleus.store.query.expression.DyadicExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) JoinExpression(org.datanucleus.store.query.expression.JoinExpression) InvokeExpression(org.datanucleus.store.query.expression.InvokeExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) QueryCompilerSyntaxException(org.datanucleus.store.query.compiler.QueryCompilerSyntaxException) ExpressionPair(org.datanucleus.store.query.expression.CaseExpression.ExpressionPair)

Aggregations

QueryCompilerSyntaxException (org.datanucleus.store.query.compiler.QueryCompilerSyntaxException)1 ArrayExpression (org.datanucleus.store.query.expression.ArrayExpression)1 CaseExpression (org.datanucleus.store.query.expression.CaseExpression)1 ExpressionPair (org.datanucleus.store.query.expression.CaseExpression.ExpressionPair)1 ClassExpression (org.datanucleus.store.query.expression.ClassExpression)1 CreatorExpression (org.datanucleus.store.query.expression.CreatorExpression)1 DyadicExpression (org.datanucleus.store.query.expression.DyadicExpression)1 Expression (org.datanucleus.store.query.expression.Expression)1 InvokeExpression (org.datanucleus.store.query.expression.InvokeExpression)1 JoinExpression (org.datanucleus.store.query.expression.JoinExpression)1 OrderExpression (org.datanucleus.store.query.expression.OrderExpression)1 ParameterExpression (org.datanucleus.store.query.expression.ParameterExpression)1 PrimaryExpression (org.datanucleus.store.query.expression.PrimaryExpression)1 SubqueryExpression (org.datanucleus.store.query.expression.SubqueryExpression)1 TypeExpression (org.datanucleus.store.query.expression.TypeExpression)1 VariableExpression (org.datanucleus.store.query.expression.VariableExpression)1 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)1 BooleanSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression)1 CollectionExpression (org.datanucleus.store.rdbms.sql.expression.CollectionExpression)1 ColumnExpression (org.datanucleus.store.rdbms.sql.expression.ColumnExpression)1