Search in sources :

Example 1 with AndExpression

use of net.sf.jsqlparser.expression.operators.conditional.AndExpression 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 AndExpression

use of net.sf.jsqlparser.expression.operators.conditional.AndExpression 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 3 with AndExpression

use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project JSqlParser by JSQLParser.

the class CloneHelper method changeBack.

/**
 * This helper method is used to change the multiple expression into
 * the binary form, respectively and return the root of the expression tree.
 * @param isMultiOr variable tells whether the expression is or.
 * @param exp the expression that needs to be converted.
 * @return the root of the expression tree.
 */
public Expression changeBack(Boolean isMultiOr, Expression exp) {
    if (!(exp instanceof MultipleExpression)) {
        return exp;
    }
    MultipleExpression changed = (MultipleExpression) exp;
    Expression result = changed.getChild(0);
    for (int i = 1; i < changed.size(); i++) {
        Expression left = result;
        Expression right = changed.getChild(i);
        if (isMultiOr) {
            result = new OrExpression(left, right);
        } else {
            result = new AndExpression(left, right);
        }
    }
    if (isMultiOr) {
        return new Parenthesis(result);
    }
    return result;
}
Also used : Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) Expression(net.sf.jsqlparser.expression.Expression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression)

Example 4 with AndExpression

use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project JSqlParser by JSQLParser.

the class CloneHelper method modify.

/**
 * This method is used for changing the logical structure of the tree.
 * The main method is to convert and operator and or operator to let
 * them have multiple children (reflected in MultipleExpression.java
 * from the conditional package). Note if the value from the conditional
 * operator has a not attached to it we need to append an not operator
 * ahead of it since the not operator needed to be pushed down during
 * the second step. Also, I will leave out all the parenthesis expression
 * which is connected to the conditional operator.
 * @param express the expression that will be modified
 * @return the modified expression.
 */
public Expression modify(Expression express) {
    if (express instanceof NotExpression) {
        return new NotExpression(modify(((NotExpression) express).getExpression()));
    }
    if (express instanceof Parenthesis) {
        Parenthesis parenthesis = (Parenthesis) express;
        Expression result = modify(parenthesis.getExpression());
        if (parenthesis.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof AndExpression) {
        AndExpression and = (AndExpression) express;
        List<Expression> list = new ArrayList<Expression>();
        list.add(modify(and.getLeftExpression()));
        list.add(modify(and.getRightExpression()));
        MultiAndExpression result = new MultiAndExpression(list);
        if (and.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof OrExpression) {
        OrExpression or = (OrExpression) express;
        List<Expression> list = new ArrayList<Expression>();
        list.add(modify(or.getLeftExpression()));
        list.add(modify(or.getRightExpression()));
        MultiOrExpression result = new MultiOrExpression(list);
        if (or.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof BinaryExpression) {
        BinaryExpression binary = (BinaryExpression) express;
        if (binary.isNot()) {
            binary.removeNot();
            return new NotExpression(modify(binary));
        }
    }
    /* at this stage, there is no need to modify, just simply return. */
    return express;
}
Also used : Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) NotExpression(net.sf.jsqlparser.expression.NotExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression)

Example 5 with AndExpression

use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project JSqlParser by JSQLParser.

the class AdaptersTest method testAdapters.

/**
 * Test extracting JDBC named parameters using adapters
 */
@Test
public void testAdapters() throws JSQLParserException {
    String sql = "SELECT * FROM MYTABLE WHERE COLUMN_A = :paramA AND COLUMN_B <> :paramB";
    Statement stmnt = CCJSqlParserUtil.parse(sql);
    final Stack<Pair<String, String>> params = new Stack<Pair<String, String>>();
    stmnt.accept(new StatementVisitorAdapter() {

        @Override
        public void visit(Select select) {
            select.getSelectBody().accept(new SelectVisitorAdapter() {

                @Override
                public void visit(PlainSelect plainSelect) {
                    plainSelect.getWhere().accept(new ExpressionVisitorAdapter() {

                        @Override
                        protected void visitBinaryExpression(BinaryExpression expr) {
                            if (!(expr instanceof AndExpression)) {
                                params.push(new Pair<String, String>(null, null));
                            }
                            super.visitBinaryExpression(expr);
                        }

                        @Override
                        public void visit(Column column) {
                            params.push(new Pair<String, String>(column.getColumnName(), params.pop().getRight()));
                        }

                        @Override
                        public void visit(JdbcNamedParameter parameter) {
                            params.push(new Pair<String, String>(params.pop().getLeft(), parameter.getName()));
                        }
                    });
                }
            });
        }
    });
    assertEquals(2, params.size());
    Pair<String, String> param2 = params.pop();
    assertEquals("COLUMN_B", param2.getLeft());
    assertEquals("paramB", param2.getRight());
    Pair<String, String> param1 = params.pop();
    assertEquals("COLUMN_A", param1.getLeft());
    assertEquals("paramA", param1.getRight());
}
Also used : JdbcNamedParameter(net.sf.jsqlparser.expression.JdbcNamedParameter) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Stack(java.util.Stack) SelectVisitorAdapter(net.sf.jsqlparser.statement.select.SelectVisitorAdapter) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) Column(net.sf.jsqlparser.schema.Column) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) ExpressionVisitorAdapter(net.sf.jsqlparser.expression.ExpressionVisitorAdapter) Test(org.junit.Test)

Aggregations

BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)8 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)8 Expression (net.sf.jsqlparser.expression.Expression)7 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)5 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)4 Parenthesis (net.sf.jsqlparser.expression.Parenthesis)3 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)3 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)3 Column (herddb.model.Column)2 ArrayList (java.util.ArrayList)2 NotExpression (net.sf.jsqlparser.expression.NotExpression)2 StatementExecutionException (herddb.model.StatementExecutionException)1 CalcitePlanner (herddb.sql.CalcitePlanner)1 BinaryExpressionBuilder (herddb.sql.expressions.CompiledSQLExpression.BinaryExpressionBuilder)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 AbstractMap (java.util.AbstractMap)1 Arrays (java.util.Arrays)1