Search in sources :

Example 1 with CaseExpression

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

the class SQLExpressionCompiler method compileExpression.

// this method never returns NULL
public static CompiledSQLExpression compileExpression(String validatedTableAlias, Expression exp) {
    if (exp instanceof BinaryExpression) {
        CompiledSQLExpression compiled = compileSpecialBinaryExpression(validatedTableAlias, exp);
        if (compiled != null) {
            return compiled;
        }
    }
    if (exp instanceof net.sf.jsqlparser.schema.Column) {
        return compileColumnExpression(validatedTableAlias, exp);
    } else if (exp instanceof StringValue) {
        return new ConstantExpression(RawString.of(((StringValue) exp).getValue()));
    } else if (exp instanceof LongValue) {
        try {
            return new ConstantExpression(((LongValue) exp).getValue());
        } catch (NumberFormatException largeNumber) {
            return new ConstantExpression(Double.valueOf(((LongValue) exp).getStringValue()));
        }
    } else if (exp instanceof DoubleValue) {
        return new ConstantExpression(((DoubleValue) exp).getValue());
    } else if (exp instanceof TimestampValue) {
        return new ConstantExpression(((TimestampValue) exp).getValue());
    } else if (exp instanceof NullValue) {
        return new ConstantExpression(null);
    } else if (exp instanceof TimeKeyExpression) {
        TimeKeyExpression ext = (TimeKeyExpression) exp;
        if (CURRENT_TIMESTAMP.equalsIgnoreCase(ext.getStringValue())) {
            return new ConstantExpression(new java.sql.Timestamp(System.currentTimeMillis()));
        } else {
            throw new StatementExecutionException("unhandled expression " + exp);
        }
    } else if (exp instanceof JdbcParameter) {
        int index = ((JdbcParameter) exp).getIndex() - 1;
        return new JdbcParameterExpression(index);
    } else if (exp instanceof AndExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAndExpression(a, b, c));
    } else if (exp instanceof OrExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledOrExpression(a, b, c));
    } else if (exp instanceof Function) {
        return CompiledFunction.create((Function) exp, validatedTableAlias);
    } else if (exp instanceof Addition) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAddExpression(a, b, c));
    } else if (exp instanceof Subtraction) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledSubtractExpression(a, b, c));
    } else if (exp instanceof Multiplication) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMultiplyExpression(a, b, c));
    } else if (exp instanceof Division) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledDivideExpression(a, b, c));
    } else if (exp instanceof Parenthesis) {
        Parenthesis p = (Parenthesis) exp;
        CompiledSQLExpression inner = compileExpression(validatedTableAlias, p.getExpression());
        return new CompiledParenthesisExpression(p.isNot(), inner);
    } else if (exp instanceof EqualsTo) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledEqualsExpression(a, b, c));
    } else if (exp instanceof NotEqualsTo) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledNotEqualsExpression(a, b, c));
    } else if (exp instanceof MinorThan) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenExpression(a, b, c));
    } else if (exp instanceof MinorThanEquals) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenEqualsExpression(a, b, c));
    } else if (exp instanceof GreaterThan) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenExpression(a, b, c));
    } else if (exp instanceof GreaterThanEquals) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenEqualsExpression(a, b, c));
    } else if (exp instanceof LikeExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledLikeExpression(a, b, c));
    } else if (exp instanceof Between) {
        return CompiledBetweenExpression.create(validatedTableAlias, (Between) exp);
    } else if (exp instanceof SignedExpression) {
        SignedExpression s = (SignedExpression) exp;
        CompiledSQLExpression inner = compileExpression(validatedTableAlias, s.getExpression());
        return new CompiledSignedExpression(s.getSign(), inner);
    } else if (exp instanceof InExpression) {
        InExpression in = (InExpression) exp;
        return CompiledInExpression.create(in, validatedTableAlias);
    } else if (exp instanceof IsNullExpression) {
        IsNullExpression i = (IsNullExpression) exp;
        CompiledSQLExpression left = compileExpression(validatedTableAlias, i.getLeftExpression());
        return new CompiledIsNullExpression(i.isNot(), left);
    } else if (exp instanceof CaseExpression) {
        return CompiledCaseExpression.create(validatedTableAlias, (CaseExpression) exp);
    }
    throw new StatementExecutionException("unsupported operand " + exp.getClass() + ", expression is " + exp);
}
Also used : Arrays(java.util.Arrays) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) BigDecimal(java.math.BigDecimal) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) RexLiteral(org.apache.calcite.rex.RexLiteral) NullValue(net.sf.jsqlparser.expression.NullValue) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) Expression(net.sf.jsqlparser.expression.Expression) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) RexInputRef(org.apache.calcite.rex.RexInputRef) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) Subtraction(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) List(java.util.List) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) BinaryExpressionBuilder(herddb.sql.expressions.CompiledSQLExpression.BinaryExpressionBuilder) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) CalcitePlanner(herddb.sql.CalcitePlanner) RexCall(org.apache.calcite.rex.RexCall) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) BuiltinFunctions(herddb.sql.functions.BuiltinFunctions) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) CURRENT_TIMESTAMP(herddb.sql.functions.BuiltinFunctions.CURRENT_TIMESTAMP) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) ArrayList(java.util.ArrayList) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Function(net.sf.jsqlparser.expression.Function) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) LongValue(net.sf.jsqlparser.expression.LongValue) AbstractMap(java.util.AbstractMap) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) Between(net.sf.jsqlparser.expression.operators.relational.Between) Collections(java.util.Collections) StringValue(net.sf.jsqlparser.expression.StringValue) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) StatementExecutionException(herddb.model.StatementExecutionException) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NullValue(net.sf.jsqlparser.expression.NullValue) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) StringValue(net.sf.jsqlparser.expression.StringValue) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) Between(net.sf.jsqlparser.expression.operators.relational.Between) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) LongValue(net.sf.jsqlparser.expression.LongValue) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) Function(net.sf.jsqlparser.expression.Function) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Subtraction(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) SignedExpression(net.sf.jsqlparser.expression.SignedExpression)

Example 2 with CaseExpression

use of net.sf.jsqlparser.expression.CaseExpression 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)

Example 3 with CaseExpression

use of net.sf.jsqlparser.expression.CaseExpression in project JSqlParser by JSQLParser.

the class ExpressionDeParser method visit.

@Override
public void visit(CaseExpression caseExpression) {
    buffer.append("CASE ");
    Expression switchExp = caseExpression.getSwitchExpression();
    if (switchExp != null) {
        switchExp.accept(this);
        buffer.append(" ");
    }
    for (Expression exp : caseExpression.getWhenClauses()) {
        exp.accept(this);
    }
    Expression elseExp = caseExpression.getElseExpression();
    if (elseExp != null) {
        buffer.append("ELSE ");
        elseExp.accept(this);
        buffer.append(" ");
    }
    buffer.append("END");
}
Also used : KeepExpression(net.sf.jsqlparser.expression.KeepExpression) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) ValueListExpression(net.sf.jsqlparser.expression.ValueListExpression) CastExpression(net.sf.jsqlparser.expression.CastExpression) OracleHierarchicalExpression(net.sf.jsqlparser.expression.OracleHierarchicalExpression) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) Expression(net.sf.jsqlparser.expression.Expression) IntervalExpression(net.sf.jsqlparser.expression.IntervalExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) JsonExpression(net.sf.jsqlparser.expression.JsonExpression) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) ExistsExpression(net.sf.jsqlparser.expression.operators.relational.ExistsExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) AllComparisonExpression(net.sf.jsqlparser.expression.AllComparisonExpression) ExtractExpression(net.sf.jsqlparser.expression.ExtractExpression) AnyComparisonExpression(net.sf.jsqlparser.expression.AnyComparisonExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) OldOracleJoinBinaryExpression(net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression) DateTimeLiteralExpression(net.sf.jsqlparser.expression.DateTimeLiteralExpression)

Aggregations

CaseExpression (net.sf.jsqlparser.expression.CaseExpression)3 Expression (net.sf.jsqlparser.expression.Expression)3 StatementExecutionException (herddb.model.StatementExecutionException)2 AbstractMap (java.util.AbstractMap)2 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)2 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)2 TimeKeyExpression (net.sf.jsqlparser.expression.TimeKeyExpression)2 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)2 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)2 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)2 IsNullExpression (net.sf.jsqlparser.expression.operators.relational.IsNullExpression)2 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)2 CalcitePlanner (herddb.sql.CalcitePlanner)1 BinaryExpressionBuilder (herddb.sql.expressions.CompiledSQLExpression.BinaryExpressionBuilder)1 SQLExpressionCompiler.compileExpression (herddb.sql.expressions.SQLExpressionCompiler.compileExpression)1 BuiltinFunctions (herddb.sql.functions.BuiltinFunctions)1 CURRENT_TIMESTAMP (herddb.sql.functions.BuiltinFunctions.CURRENT_TIMESTAMP)1 RawString (herddb.utils.RawString)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1