Search in sources :

Example 1 with BinaryExpression

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

the class SQLPlanner method validateColumnConstaintExpressionToExpression.

private Expression validateColumnConstaintExpressionToExpression(Expression testExpression, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
    Expression result = null;
    if (expressionType.isAssignableFrom(testExpression.getClass())) {
        BinaryExpression e = (BinaryExpression) testExpression;
        if (e.getLeftExpression() instanceof net.sf.jsqlparser.schema.Column) {
            net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) e.getLeftExpression();
            boolean okAlias = true;
            if (c.getTable() != null && c.getTable().getName() != null && !c.getTable().getName().equals(tableAlias)) {
                okAlias = false;
            }
            if (okAlias && columnName.equalsIgnoreCase(c.getColumnName()) && SQLRecordPredicate.isConstant(e.getRightExpression())) {
                return e;
            }
        } else if (e.getLeftExpression() instanceof AndExpression) {
            result = findConstraintExpressionOnColumn(e.getLeftExpression(), columnName, tableAlias, expressionType);
            if (result != null) {
                return result;
            }
        } else if (e.getRightExpression() instanceof AndExpression) {
            result = findConstraintExpressionOnColumn(e.getRightExpression(), columnName, tableAlias, expressionType);
            if (result != null) {
                return result;
            }
        }
    }
    return result;
}
Also used : AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) Expression(net.sf.jsqlparser.expression.Expression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Column(herddb.model.Column)

Example 2 with BinaryExpression

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

the class SQLExpressionCompiler method compileSpecialBinaryExpression.

private static CompiledSQLExpression compileSpecialBinaryExpression(String validatedTableAlias, Expression exp) {
    BinaryExpression be = (BinaryExpression) exp;
    // MOST frequent expressions "TABLE.COLUMNNAME OPERATOR ?", we can hardcode the access to the column and to the JDBC parameter
    if (be.getLeftExpression() instanceof net.sf.jsqlparser.schema.Column) {
        net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) be.getLeftExpression();
        if (validatedTableAlias != null) {
            if (c.getTable() != null && c.getTable().getName() != null && !c.getTable().getName().equals(validatedTableAlias)) {
                return null;
            }
        }
        String columnName = c.getColumnName();
        switch(columnName) {
            case BuiltinFunctions.BOOLEAN_TRUE:
                return null;
            case BuiltinFunctions.BOOLEAN_FALSE:
                return null;
            default:
                // OK !
                break;
        }
        if (be.getRightExpression() instanceof JdbcParameter) {
            JdbcParameter jdbcParam = (JdbcParameter) be.getRightExpression();
            int jdbcIndex = jdbcParam.getIndex() - 1;
            if (be instanceof EqualsTo) {
                return new ColumnEqualsJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof NotEqualsTo) {
                return new ColumnNotEqualsJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof GreaterThanEquals) {
                return new ColumnGreaterThanEqualsJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof GreaterThan) {
                return new ColumnGreaterThanJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof MinorThan) {
                return new ColumnMinorThanJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof MinorThanEquals) {
                return new ColumnMinorThanEqualsJdbcParameter(be.isNot(), columnName, jdbcIndex);
            }
        }
    // TODO handle "TABLE.COLUMNNAME OPERATOR CONSTANT"
    }
    return null;
}
Also used : MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) RawString(herddb.utils.RawString) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo)

Example 3 with BinaryExpression

use of net.sf.jsqlparser.expression.BinaryExpression 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 4 with BinaryExpression

use of net.sf.jsqlparser.expression.BinaryExpression in project yyl_example by Relucent.

the class JsqlparserExample method main.

public static void main(String[] args) throws Exception {
    String sql = // 
    "update demo_table t1 set " + // 
    "  column1 = '1' " + // 
    " ,column2 = '1' " + // 
    " ,column3 = '2' " + // 
    " ,column4 = '3' " + " where id=0";
    CCJSqlParserManager pm = new CCJSqlParserManager();
    Statement statement = pm.parse(new StringReader(sql));
    if (statement instanceof Update) {
        Update updateStatement = (Update) statement;
        System.out.println(updateStatement.getTable());
        Expression where = updateStatement.getWhere();
        if (where instanceof BinaryExpression) {
            BinaryExpression expression = (BinaryExpression) where;
            System.out.println(expression.getLeftExpression());
            System.out.println(expression.getStringExpression());
            System.out.println(expression.getRightExpression());
        }
    }
}
Also used : BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) Expression(net.sf.jsqlparser.expression.Expression) Statement(net.sf.jsqlparser.statement.Statement) StringReader(java.io.StringReader) Update(net.sf.jsqlparser.statement.update.Update) CCJSqlParserManager(net.sf.jsqlparser.parser.CCJSqlParserManager)

Example 5 with BinaryExpression

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

the class CNFConverter method handleNot.

/**
 * This function mainly deals with pushing not operators down.
 * check the child. If it is not a logic operator(and or or).
 * stop at that point. Else use De Morgan law to push not downwards.
 * @param index the index of the children appeared in parents array.
 */
private void handleNot(int index) {
    child = ((NotExpression) temp1).getExpression();
    // takes down the number of not operators.
    int nums = 1;
    while (child instanceof NotExpression) {
        child = ((NotExpression) child).getExpression();
        nums++;
    }
    /* if the number of not operators are even. we could get
         * rid of all the not operators. set the child to the parent. */
    if (nums % 2 == 0) {
        ((MultipleExpression) temp2).setChild(index, child);
        temp1 = child;
        pushNot(-1);
    } else {
        /* otherwise there will be one not left to push. 
             * if the child is not these two types of operators.
             * that means we reach the leaves of the logical part.
             * set a new not operator whose child is the current one
             * and connect that operator with the parent and return. */
        if (!(child instanceof MultiAndExpression) && !(child instanceof MultiOrExpression)) {
            if (child instanceof LikeExpression) {
                ((LikeExpression) child).setNot(true);
            } else if (child instanceof BinaryExpression) {
                ((BinaryExpression) child).setNot();
            } else {
                child = new NotExpression(child);
            }
            ((MultipleExpression) temp2).setChild(index, child);
            return;
        } else if (child instanceof MultiAndExpression) {
            MultiAndExpression and = (MultiAndExpression) child;
            List<Expression> list = new ArrayList<Expression>();
            for (int i = 0; i < and.size(); i++) {
                /* push not to every element in the operator. */
                NotExpression not = new NotExpression(and.getChild(i));
                list.add(not);
            }
            /* the De Morgan law shows we need to change and to or. */
            temp1 = new MultiOrExpression(list);
            ((MultipleExpression) temp2).setChild(index, temp1);
            pushNot(-1);
        } else if (child instanceof MultiOrExpression) {
            MultiOrExpression or = (MultiOrExpression) child;
            List<Expression> list = new ArrayList<Expression>();
            for (int i = 0; i < or.size(); i++) {
                /* push not to every element in the operator. */
                NotExpression not = new NotExpression(or.getChild(i));
                list.add(not);
            }
            /* the De Morgan law shows we need to change or to and. */
            temp1 = new MultiAndExpression(list);
            ((MultipleExpression) temp2).setChild(index, temp1);
            pushNot(-1);
        }
    }
}
Also used : LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) NotExpression(net.sf.jsqlparser.expression.NotExpression) List(java.util.List) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList)

Aggregations

BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)11 Expression (net.sf.jsqlparser.expression.Expression)9 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)8 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)6 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)5 ArrayList (java.util.ArrayList)4 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)4 Column (herddb.model.Column)2 RawString (herddb.utils.RawString)2 List (java.util.List)2 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)2 NotExpression (net.sf.jsqlparser.expression.NotExpression)2 Parenthesis (net.sf.jsqlparser.expression.Parenthesis)2 EqualsTo (net.sf.jsqlparser.expression.operators.relational.EqualsTo)2 GreaterThan (net.sf.jsqlparser.expression.operators.relational.GreaterThan)2 GreaterThanEquals (net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals)2 MinorThan (net.sf.jsqlparser.expression.operators.relational.MinorThan)2 MinorThanEquals (net.sf.jsqlparser.expression.operators.relational.MinorThanEquals)2 NotEqualsTo (net.sf.jsqlparser.expression.operators.relational.NotEqualsTo)2 StatementExecutionException (herddb.model.StatementExecutionException)1