Search in sources :

Example 61 with Expression

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

the class StatementDeParserTest method shouldUseProvidedDeparsersWhenDeParsingInsert.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeparsersWhenDeParsingInsert() throws JSQLParserException {
    Insert insert = new Insert();
    Table table = new Table();
    List<Column> duplicateUpdateColumns = new ArrayList<Column>();
    List<Expression> duplicateUpdateExpressionList = new ArrayList<Expression>();
    Column duplicateUpdateColumn1 = new Column();
    Column duplicateUpdateColumn2 = new Column();
    Expression duplicateUpdateExpression1 = mock(Expression.class);
    Expression duplicateUpdateExpression2 = mock(Expression.class);
    Select select = new Select();
    List<WithItem> withItemsList = new ArrayList<WithItem>();
    WithItem withItem1 = spy(new WithItem());
    WithItem withItem2 = spy(new WithItem());
    SelectBody withItem1SelectBody = mock(SelectBody.class);
    SelectBody withItem2SelectBody = mock(SelectBody.class);
    SelectBody selectBody = mock(SelectBody.class);
    insert.setSelect(select);
    insert.setTable(table);
    insert.setUseDuplicate(true);
    insert.setDuplicateUpdateColumns(duplicateUpdateColumns);
    insert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
    duplicateUpdateColumns.add(duplicateUpdateColumn1);
    duplicateUpdateColumns.add(duplicateUpdateColumn2);
    duplicateUpdateExpressionList.add(duplicateUpdateExpression1);
    duplicateUpdateExpressionList.add(duplicateUpdateExpression2);
    insert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
    select.setWithItemsList(withItemsList);
    select.setSelectBody(selectBody);
    withItemsList.add(withItem1);
    withItemsList.add(withItem2);
    withItem1.setSelectBody(withItem1SelectBody);
    withItem2.setSelectBody(withItem2SelectBody);
    statementDeParser.visit(insert);
    then(withItem1).should().accept(selectDeParser);
    then(withItem2).should().accept(selectDeParser);
    then(selectBody).should().accept(selectDeParser);
    then(duplicateUpdateExpression1).should().accept(expressionDeParser);
    then(duplicateUpdateExpression1).should().accept(expressionDeParser);
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) Select(net.sf.jsqlparser.statement.select.Select) WithItem(net.sf.jsqlparser.statement.select.WithItem) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) Insert(net.sf.jsqlparser.statement.insert.Insert) Test(org.junit.Test)

Example 62 with Expression

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

the class ExpressionDeParser method visit.

@Override
public void visit(AnalyticExpression aexpr) {
    String name = aexpr.getName();
    Expression expression = aexpr.getExpression();
    Expression offset = aexpr.getOffset();
    Expression defaultValue = aexpr.getDefaultValue();
    boolean isAllColumns = aexpr.isAllColumns();
    KeepExpression keep = aexpr.getKeep();
    ExpressionList partitionExpressionList = aexpr.getPartitionExpressionList();
    List<OrderByElement> orderByElements = aexpr.getOrderByElements();
    WindowElement windowElement = aexpr.getWindowElement();
    buffer.append(name).append("(");
    if (expression != null) {
        expression.accept(this);
        if (offset != null) {
            buffer.append(", ");
            offset.accept(this);
            if (defaultValue != null) {
                buffer.append(", ");
                defaultValue.accept(this);
            }
        }
    } else if (isAllColumns) {
        buffer.append("*");
    }
    buffer.append(") ");
    if (keep != null) {
        keep.accept(this);
        buffer.append(" ");
    }
    switch(aexpr.getType()) {
        case WITHIN_GROUP:
            buffer.append("WITHIN GROUP");
            break;
        default:
            buffer.append("OVER");
    }
    buffer.append(" (");
    if (partitionExpressionList != null && !partitionExpressionList.getExpressions().isEmpty()) {
        buffer.append("PARTITION BY ");
        List<Expression> expressions = partitionExpressionList.getExpressions();
        for (int i = 0; i < expressions.size(); i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            expressions.get(i).accept(this);
        }
        buffer.append(" ");
    }
    if (orderByElements != null && !orderByElements.isEmpty()) {
        buffer.append("ORDER BY ");
        orderByDeParser.setExpressionVisitor(this);
        orderByDeParser.setBuffer(buffer);
        for (int i = 0; i < orderByElements.size(); i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            orderByDeParser.deParseElement(orderByElements.get(i));
        }
        if (windowElement != null) {
            buffer.append(' ');
            buffer.append(windowElement);
        }
    }
    buffer.append(")");
}
Also used : KeepExpression(net.sf.jsqlparser.expression.KeepExpression) WindowElement(net.sf.jsqlparser.expression.WindowElement) KeepExpression(net.sf.jsqlparser.expression.KeepExpression) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) ValueListExpression(net.sf.jsqlparser.expression.ValueListExpression) CastExpression(net.sf.jsqlparser.expression.CastExpression) OracleHierarchicalExpression(net.sf.jsqlparser.expression.OracleHierarchicalExpression) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) Expression(net.sf.jsqlparser.expression.Expression) IntervalExpression(net.sf.jsqlparser.expression.IntervalExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) JsonExpression(net.sf.jsqlparser.expression.JsonExpression) 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) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) AllComparisonExpression(net.sf.jsqlparser.expression.AllComparisonExpression) ExtractExpression(net.sf.jsqlparser.expression.ExtractExpression) AnyComparisonExpression(net.sf.jsqlparser.expression.AnyComparisonExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) OldOracleJoinBinaryExpression(net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression) DateTimeLiteralExpression(net.sf.jsqlparser.expression.DateTimeLiteralExpression) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) OracleHint(net.sf.jsqlparser.expression.OracleHint)

Example 63 with Expression

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

the class ExpressionDeParser method visit.

@Override
public void visit(ExpressionList expressionList) {
    if (useBracketsInExprList) {
        buffer.append("(");
    }
    for (Iterator<Expression> iter = expressionList.getExpressions().iterator(); iter.hasNext(); ) {
        Expression expression = iter.next();
        expression.accept(this);
        if (iter.hasNext()) {
            buffer.append(", ");
        }
    }
    if (useBracketsInExprList) {
        buffer.append(")");
    }
}
Also used : KeepExpression(net.sf.jsqlparser.expression.KeepExpression) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) ValueListExpression(net.sf.jsqlparser.expression.ValueListExpression) CastExpression(net.sf.jsqlparser.expression.CastExpression) OracleHierarchicalExpression(net.sf.jsqlparser.expression.OracleHierarchicalExpression) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) Expression(net.sf.jsqlparser.expression.Expression) IntervalExpression(net.sf.jsqlparser.expression.IntervalExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) JsonExpression(net.sf.jsqlparser.expression.JsonExpression) 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) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) AllComparisonExpression(net.sf.jsqlparser.expression.AllComparisonExpression) ExtractExpression(net.sf.jsqlparser.expression.ExtractExpression) AnyComparisonExpression(net.sf.jsqlparser.expression.AnyComparisonExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) OldOracleJoinBinaryExpression(net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression) DateTimeLiteralExpression(net.sf.jsqlparser.expression.DateTimeLiteralExpression)

Example 64 with Expression

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

the class InsertDeParser 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 65 with Expression

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

the class ReplaceDeParser method deParse.

public void deParse(Replace replace) {
    buffer.append("REPLACE ");
    if (replace.isUseIntoTables()) {
        buffer.append("INTO ");
    }
    buffer.append(replace.getTable().getFullyQualifiedName());
    if (replace.getItemsList() != null) {
        if (replace.getColumns() != null) {
            buffer.append(" (");
            for (int i = 0; i < replace.getColumns().size(); i++) {
                Column column = replace.getColumns().get(i);
                buffer.append(column.getFullyQualifiedName());
                if (i < replace.getColumns().size() - 1) {
                    buffer.append(", ");
                }
            }
            buffer.append(") ");
        } else {
            buffer.append(" ");
        }
    } else {
        buffer.append(" SET ");
        for (int i = 0; i < replace.getColumns().size(); i++) {
            Column column = replace.getColumns().get(i);
            buffer.append(column.getFullyQualifiedName()).append("=");
            Expression expression = replace.getExpressions().get(i);
            expression.accept(expressionVisitor);
            if (i < replace.getColumns().size() - 1) {
                buffer.append(", ");
            }
        }
    }
    if (replace.getItemsList() != null) {
        // REPLACE mytab SELECT * FROM mytab2
        // or VALUES ('as', ?, 565)
        replace.getItemsList().accept(this);
    }
}
Also used : Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression)

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