Search in sources :

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

Example 7 with BinaryExpression

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

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

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

the class SQLPlanner method findIndexAccess.

private static SQLRecordKeyFunction findIndexAccess(Expression where, String[] columnsToMatch, ColumnsList table, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
    List<Expression> expressions = new ArrayList<>();
    List<String> columns = new ArrayList<>();
    for (String pk : columnsToMatch) {
        Expression condition = findConstraintOnColumn(where, pk, tableAlias, expressionType);
        if (condition == null) {
            break;
        }
        columns.add(pk);
        expressions.add(condition);
    }
    if (expressions.isEmpty()) {
        // no match at all, there is no direct constraint on PK
        return null;
    }
    return new SQLRecordKeyFunction(table, columns, expressions);
}
Also used : 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) ArrayList(java.util.ArrayList)

Example 10 with BinaryExpression

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

Aggregations

BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)12 Expression (net.sf.jsqlparser.expression.Expression)10 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)9 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)7 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)5 ArrayList (java.util.ArrayList)5 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)4 List (java.util.List)3 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)3 Parenthesis (net.sf.jsqlparser.expression.Parenthesis)3 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)3 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)3 Column (herddb.model.Column)2 StatementExecutionException (herddb.model.StatementExecutionException)2 RawString (herddb.utils.RawString)2 AbstractMap (java.util.AbstractMap)2 Map (java.util.Map)2 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)2 DoubleValue (net.sf.jsqlparser.expression.DoubleValue)2 Function (net.sf.jsqlparser.expression.Function)2