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));
}
}
}
}
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();
}
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;
}
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);
}
}
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);
}
Aggregations