Search in sources :

Example 1 with WhenClause

use of net.sf.jsqlparser.expression.WhenClause in project herddb by diennea.

the class CompiledCaseExpression method create.

public static CompiledCaseExpression create(String validatedTableAlias, CaseExpression caseExpression) {
    Expression switchExpression = caseExpression.getSwitchExpression();
    if (switchExpression != null) {
        throw new StatementExecutionException("unhandled expression CASE SWITCH, type " + caseExpression.getClass() + ": " + caseExpression);
    }
    List<Entry<CompiledSQLExpression, CompiledSQLExpression>> whens = null;
    if (caseExpression.getWhenClauses() != null) {
        whens = new ArrayList<>();
        for (Expression when : caseExpression.getWhenClauses()) {
            WhenClause whenClause = (WhenClause) when;
            CompiledSQLExpression whenCondition = compileExpression(validatedTableAlias, whenClause.getWhenExpression());
            if (whenCondition == null) {
                return null;
            }
            CompiledSQLExpression thenExpr = compileExpression(validatedTableAlias, whenClause.getThenExpression());
            whens.add(new AbstractMap.SimpleImmutableEntry<>(whenCondition, thenExpr));
        }
    }
    Expression elseExp = caseExpression.getElseExpression();
    if (elseExp != null) {
        CompiledSQLExpression elseExpression = compileExpression(validatedTableAlias, elseExp);
        if (elseExpression == null) {
            return null;
        }
        return new CompiledCaseExpression(whens, elseExpression);
    } else {
        return new CompiledCaseExpression(whens, null);
    }
}
Also used : AbstractMap(java.util.AbstractMap) Entry(java.util.Map.Entry) WhenClause(net.sf.jsqlparser.expression.WhenClause) SQLExpressionCompiler.compileExpression(herddb.sql.expressions.SQLExpressionCompiler.compileExpression) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) Expression(net.sf.jsqlparser.expression.Expression) StatementExecutionException(herddb.model.StatementExecutionException)

Aggregations

StatementExecutionException (herddb.model.StatementExecutionException)1 SQLExpressionCompiler.compileExpression (herddb.sql.expressions.SQLExpressionCompiler.compileExpression)1 AbstractMap (java.util.AbstractMap)1 Entry (java.util.Map.Entry)1 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)1 Expression (net.sf.jsqlparser.expression.Expression)1 WhenClause (net.sf.jsqlparser.expression.WhenClause)1