Search in sources :

Example 66 with Expression

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

the class ReplaceDeParser method visit.

@Override
public void visit(MultiExpressionList multiExprList) {
    buffer.append("VALUES ");
    for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
        buffer.append("(");
        for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
            Expression expression = iter.next();
            expression.accept(expressionVisitor);
            if (iter.hasNext()) {
                buffer.append(", ");
            }
        }
        buffer.append(")");
        if (it.hasNext()) {
            buffer.append(", ");
        }
    }
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 67 with Expression

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

the class UpdateDeParser method deParse.

public void deParse(Update update) {
    buffer.append("UPDATE ").append(PlainSelect.getStringList(update.getTables(), true, false)).append(" SET ");
    if (!update.isUseSelect()) {
        for (int i = 0; i < update.getColumns().size(); i++) {
            Column column = update.getColumns().get(i);
            column.accept(expressionVisitor);
            buffer.append(" = ");
            Expression expression = update.getExpressions().get(i);
            expression.accept(expressionVisitor);
            if (i < update.getColumns().size() - 1) {
                buffer.append(", ");
            }
        }
    } else {
        if (update.isUseColumnsBrackets()) {
            buffer.append("(");
        }
        for (int i = 0; i < update.getColumns().size(); i++) {
            if (i != 0) {
                buffer.append(", ");
            }
            Column column = update.getColumns().get(i);
            column.accept(expressionVisitor);
        }
        if (update.isUseColumnsBrackets()) {
            buffer.append(")");
        }
        buffer.append(" = ");
        buffer.append("(");
        Select select = update.getSelect();
        select.getSelectBody().accept(selectVisitor);
        buffer.append(")");
    }
    if (update.getFromItem() != null) {
        buffer.append(" FROM ").append(update.getFromItem());
        if (update.getJoins() != null) {
            for (Join join : update.getJoins()) {
                if (join.isSimple()) {
                    buffer.append(", ").append(join);
                } else {
                    buffer.append(" ").append(join);
                }
            }
        }
    }
    if (update.getWhere() != null) {
        buffer.append(" WHERE ");
        update.getWhere().accept(expressionVisitor);
    }
    if (update.getOrderByElements() != null) {
        new OrderByDeParser(expressionVisitor, buffer).deParse(update.getOrderByElements());
    }
    if (update.getLimit() != null) {
        new LimitDeparser(buffer).deParse(update.getLimit());
    }
    if (update.isReturningAllColumns()) {
        buffer.append(" RETURNING *");
    } else if (update.getReturningExpressionList() != null) {
        buffer.append(" RETURNING ");
        for (Iterator<SelectExpressionItem> iter = update.getReturningExpressionList().iterator(); iter.hasNext(); ) {
            buffer.append(iter.next().toString());
            if (iter.hasNext()) {
                buffer.append(", ");
            }
        }
    }
}
Also used : Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) Iterator(java.util.Iterator) Join(net.sf.jsqlparser.statement.select.Join)

Example 68 with Expression

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

the class UpsertDeParser method visit.

@Override
public void visit(MultiExpressionList multiExprList) {
    buffer.append(" VALUES ");
    for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
        buffer.append("(");
        for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
            Expression expression = iter.next();
            expression.accept(expressionVisitor);
            if (iter.hasNext()) {
                buffer.append(", ");
            }
        }
        buffer.append(")");
        if (it.hasNext()) {
            buffer.append(", ");
        }
    }
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 69 with Expression

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

the class UpsertDeParser method visit.

@Override
public void visit(ExpressionList expressionList) {
    buffer.append(" VALUES (");
    for (Iterator<Expression> iter = expressionList.getExpressions().iterator(); iter.hasNext(); ) {
        Expression expression = iter.next();
        expression.accept(expressionVisitor);
        if (iter.hasNext()) {
            buffer.append(", ");
        }
    }
    buffer.append(")");
}
Also used : Expression(net.sf.jsqlparser.expression.Expression)

Example 70 with Expression

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

Aggregations

Expression (net.sf.jsqlparser.expression.Expression)92 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)30 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)26 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)25 Column (net.sf.jsqlparser.schema.Column)19 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)18 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)17 Table (net.sf.jsqlparser.schema.Table)14 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)12 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)12 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)12 NotExpression (net.sf.jsqlparser.expression.NotExpression)11 StatementExecutionException (herddb.model.StatementExecutionException)10 Select (net.sf.jsqlparser.statement.select.Select)10 AnalyticExpression (net.sf.jsqlparser.expression.AnalyticExpression)9 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)9 KeepExpression (net.sf.jsqlparser.expression.KeepExpression)9 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)9 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)9