Search in sources :

Example 6 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)

Example 7 with AndExpression

use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project herddb by diennea.

the class SQLPlanner method validateColumnConstaintToExpression.

private static Expression validateColumnConstaintToExpression(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.getRightExpression();
            }
        } else if (e.getLeftExpression() instanceof AndExpression) {
            result = findConstraintOnColumn(e.getLeftExpression(), columnName, tableAlias, expressionType);
            if (result != null) {
                return result;
            }
        } else if (e.getRightExpression() instanceof AndExpression) {
            result = findConstraintOnColumn(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 8 with AndExpression

use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project herddb by diennea.

the class SQLPlanner method findConstraintExpressionOnColumn.

private Expression findConstraintExpressionOnColumn(Expression where, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
    if (where instanceof AndExpression) {
        AndExpression and = (AndExpression) where;
        Expression keyOnLeft = findConstraintExpressionOnColumn(and.getLeftExpression(), columnName, tableAlias, expressionType);
        if (keyOnLeft != null) {
            return keyOnLeft;
        }
        Expression keyOnRight = findConstraintExpressionOnColumn(and.getRightExpression(), columnName, tableAlias, expressionType);
        if (keyOnRight != null) {
            return keyOnRight;
        }
    } else if (expressionType.isAssignableFrom(where.getClass())) {
        Expression keyDirect = validateColumnConstaintExpressionToExpression(where, columnName, tableAlias, expressionType);
        if (keyDirect != null) {
            return keyDirect;
        }
    }
    return null;
}
Also used : AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) 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)

Example 9 with AndExpression

use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project herddb by diennea.

the class SQLPlanner method findConstraintOnColumn.

private static Expression findConstraintOnColumn(Expression where, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
    if (where instanceof AndExpression) {
        AndExpression and = (AndExpression) where;
        Expression keyOnLeft = findConstraintOnColumn(and.getLeftExpression(), columnName, tableAlias, expressionType);
        if (keyOnLeft != null) {
            return keyOnLeft;
        }
        Expression keyOnRight = findConstraintOnColumn(and.getRightExpression(), columnName, tableAlias, expressionType);
        if (keyOnRight != null) {
            return keyOnRight;
        }
    } else if (expressionType.isAssignableFrom(where.getClass())) {
        Expression keyDirect = validateColumnConstaintToExpression(where, columnName, tableAlias, expressionType);
        if (keyDirect != null) {
            return keyDirect;
        }
    }
    return null;
}
Also used : AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) 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)

Aggregations

AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)9 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)8 Expression (net.sf.jsqlparser.expression.Expression)7 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)5 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)4 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)4 Parenthesis (net.sf.jsqlparser.expression.Parenthesis)3 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)3 Column (herddb.model.Column)2 StatementExecutionException (herddb.model.StatementExecutionException)2 ArrayList (java.util.ArrayList)2 NotExpression (net.sf.jsqlparser.expression.NotExpression)2 Addition (net.sf.jsqlparser.expression.operators.arithmetic.Addition)2 Division (net.sf.jsqlparser.expression.operators.arithmetic.Division)2 Multiplication (net.sf.jsqlparser.expression.operators.arithmetic.Multiplication)2 Subtraction (net.sf.jsqlparser.expression.operators.arithmetic.Subtraction)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 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)2