Search in sources :

Example 41 with Expression

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

the class CNFConverter method gather.

/**
 * This method serves as dealing with the third step. It is used
 * to put all the adjacent same multi operators together. BFS the
 * tree and do it node by node. In the end we will get the tree
 * where all the same multi operators store in the same odd level
 * of the tree or in the same even level of the tree.
 */
private void gather() {
    Queue<Expression> queue = new LinkedList<Expression>();
    queue.offer(temp1);
    while (!queue.isEmpty()) {
        Expression express = queue.poll();
        /* at this level, we only deal with "multi and" and "multi or"
             * operators, so we only consider these two operators. 
             * that means we do nothing if the operator is not those two. */
        if (express instanceof MultiAndExpression) {
            MultiAndExpression and = (MultiAndExpression) express;
            while (true) {
                int index = 0;
                Expression get = null;
                for (; index < and.size(); index++) {
                    get = and.getChild(index);
                    if (get instanceof MultiAndExpression) {
                        break;
                    }
                }
                /* if the index is the size of the multi operator,
                     * that means this is already valid. jump out of the loop. */
                if (index == and.size()) {
                    break;
                } else {
                    /* if not, remove the child out and push the child of that child
                     * in the operator, starting from the index where the child 
                     * is removed. */
                    and.removeChild(index);
                    MultipleExpression order = (MultipleExpression) get;
                    for (int i = 0; i < order.size(); i++) {
                        and.addChild(index, order.getChild(i));
                        index++;
                    }
                }
            }
            /* Do the standard BFS now since all children are not and operators. */
            for (int i = 0; i < and.size(); i++) {
                queue.offer(and.getChild(i));
            }
        } else if (express instanceof MultiOrExpression) {
            /* for the multi or operator, the logic is the similar. */
            MultiOrExpression or = (MultiOrExpression) express;
            while (true) {
                int index = 0;
                Expression get = null;
                for (; index < or.size(); index++) {
                    get = or.getChild(index);
                    if (get instanceof MultiOrExpression) {
                        break;
                    }
                }
                /* if the index is the size of the multi operator,
                     * that means this is already valid. jump out of the loop. */
                if (index == or.size()) {
                    break;
                } else {
                    /* if not, remove the child out and push the child of that child
                         * in the operator, starting from the index where the child 
                         * is removed. */
                    or.removeChild(index);
                    MultipleExpression order = (MultipleExpression) get;
                    for (int i = 0; i < order.size(); i++) {
                        or.addChild(index, order.getChild(i));
                        index++;
                    }
                }
            }
            /* Do the standard BFS now since all children are not or operators. */
            for (int i = 0; i < or.size(); i++) {
                queue.offer(or.getChild(i));
            }
        }
    }
}
Also used : 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) LinkedList(java.util.LinkedList)

Example 42 with Expression

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

the class CNFConverter method pushAndUp.

/**
 * First, BFS the tree and gather all the or operators and their parents
 * into a stack. Next, pop them out and push the and operators under the
 * or operators upwards(if there are). Do this level by level, which means
 * during each level we will call the gather() method to make the tree uniform.
 * When we move out of the stack. The expression tree shall be in CNF form.
 */
private void pushAndUp() {
    Queue<Mule> queue = new LinkedList<Mule>();
    Stack<Mule> stack = new Stack<Mule>();
    Mule root = new Mule(temp2, temp1, 0);
    queue.offer(root);
    int level = 1;
    /* do the BFS and store valid mule into the stack. Notice the 
         * first parameter is parent and the second parameter is children. */
    while (!queue.isEmpty()) {
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            Mule mule = queue.poll();
            Expression parent = mule.parent;
            Expression child = mule.child;
            if (parent instanceof MultiAndExpression && child instanceof MultiOrExpression) {
                stack.push(mule);
            }
            /* Note the child may not be an instance of multiple expression!. */
            if (child instanceof MultipleExpression) {
                MultipleExpression multi = (MultipleExpression) child;
                for (int j = 0; j < multi.size(); j++) {
                    Expression get = multi.getChild(j);
                    if (get instanceof MultipleExpression) {
                        Mule added = new Mule(child, get, level);
                        queue.offer(added);
                    }
                }
            }
        }
        level++;
    }
    /* use another function to handle pushing and up. */
    pushAnd(stack);
    /* do not forget to set the operators back! */
    this.root = ((MultiAndExpression) dummy).getChild(0);
    temp1 = this.root;
    temp2 = dummy;
    /* at last, remember to gather again since there are no gather()
         * method called if there are some movements on the root. */
    gather();
}
Also used : 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) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 43 with Expression

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

use of net.sf.jsqlparser.expression.Expression in project InformationSystem by ObeoNetwork.

the class ViewContentFinder method visit.

// ItemListVisitor methods
public void visit(ExpressionList expressionList) {
    for (Iterator iter = expressionList.getExpressions().iterator(); iter.hasNext(); ) {
        Expression expression = (Expression) iter.next();
        expression.accept(this);
    }
}
Also used : InverseExpression(net.sf.jsqlparser.expression.InverseExpression) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) Expression(net.sf.jsqlparser.expression.Expression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) 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) AllComparisonExpression(net.sf.jsqlparser.expression.AllComparisonExpression) AnyComparisonExpression(net.sf.jsqlparser.expression.AnyComparisonExpression) Iterator(java.util.Iterator)

Example 45 with Expression

use of net.sf.jsqlparser.expression.Expression in project dbeaver by serge-rider.

the class SQLSemanticProcessor method addWhereToSelect.

public static void addWhereToSelect(PlainSelect select, String condString) throws JSQLParserException {
    Expression filterWhere;
    try {
        filterWhere = CCJSqlParserUtil.parseCondExpression(condString);
    } catch (JSQLParserException e) {
        throw new JSQLParserException("Bad query condition: [" + condString + "]", e);
    }
    addWhereToSelect(select, filterWhere);
}
Also used : AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) JSQLParserException(net.sf.jsqlparser.JSQLParserException)

Aggregations

Expression (net.sf.jsqlparser.expression.Expression)100 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)33 ArrayList (java.util.ArrayList)32 Test (org.junit.Test)30 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)28 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)26 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)21 Column (net.sf.jsqlparser.schema.Column)19 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)17 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)17 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)15 StatementExecutionException (herddb.model.StatementExecutionException)14 NotExpression (net.sf.jsqlparser.expression.NotExpression)14 Table (net.sf.jsqlparser.schema.Table)14 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)12 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)12 Column (herddb.model.Column)11 TimeKeyExpression (net.sf.jsqlparser.expression.TimeKeyExpression)11 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)11 Function (net.sf.jsqlparser.expression.Function)10