Search in sources :

Example 1 with WithItem

use of net.sf.jsqlparser.statement.select.WithItem in project JSqlParser by JSQLParser.

the class StatementDeParserTest method shouldUseProvidedDeparsersWhenDeParsingUpsertWithExpressionList.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeparsersWhenDeParsingUpsertWithExpressionList() throws JSQLParserException {
    Upsert upsert = new Upsert();
    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);
    upsert.setSelect(select);
    upsert.setTable(table);
    upsert.setUseDuplicate(true);
    upsert.setDuplicateUpdateColumns(duplicateUpdateColumns);
    upsert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
    duplicateUpdateColumns.add(duplicateUpdateColumn1);
    duplicateUpdateColumns.add(duplicateUpdateColumn2);
    duplicateUpdateExpressionList.add(duplicateUpdateExpression1);
    duplicateUpdateExpressionList.add(duplicateUpdateExpression2);
    upsert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
    select.setWithItemsList(withItemsList);
    select.setSelectBody(selectBody);
    withItemsList.add(withItem1);
    withItemsList.add(withItem2);
    withItem1.setSelectBody(withItem1SelectBody);
    withItem2.setSelectBody(withItem2SelectBody);
    statementDeParser.visit(upsert);
    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 : Upsert(net.sf.jsqlparser.statement.upsert.Upsert) 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) Test(org.junit.Test)

Example 2 with WithItem

use of net.sf.jsqlparser.statement.select.WithItem in project JSqlParser by JSQLParser.

the class InsertDeParser method deParse.

public void deParse(Insert insert) {
    buffer.append("INSERT ");
    if (insert.getModifierPriority() != null) {
        buffer.append(insert.getModifierPriority()).append(" ");
    }
    if (insert.isModifierIgnore()) {
        buffer.append("IGNORE ");
    }
    buffer.append("INTO ");
    buffer.append(insert.getTable().toString());
    if (insert.getColumns() != null) {
        buffer.append(" (");
        for (Iterator<Column> iter = insert.getColumns().iterator(); iter.hasNext(); ) {
            Column column = iter.next();
            buffer.append(column.getColumnName());
            if (iter.hasNext()) {
                buffer.append(", ");
            }
        }
        buffer.append(")");
    }
    if (insert.getItemsList() != null) {
        insert.getItemsList().accept(this);
    }
    if (insert.getSelect() != null) {
        buffer.append(" ");
        if (insert.isUseSelectBrackets()) {
            buffer.append("(");
        }
        if (insert.getSelect().getWithItemsList() != null) {
            buffer.append("WITH ");
            for (WithItem with : insert.getSelect().getWithItemsList()) {
                with.accept(selectVisitor);
            }
            buffer.append(" ");
        }
        insert.getSelect().getSelectBody().accept(selectVisitor);
        if (insert.isUseSelectBrackets()) {
            buffer.append(")");
        }
    }
    if (insert.isUseSet()) {
        buffer.append(" SET ");
        for (int i = 0; i < insert.getSetColumns().size(); i++) {
            Column column = insert.getSetColumns().get(i);
            column.accept(expressionVisitor);
            buffer.append(" = ");
            Expression expression = insert.getSetExpressionList().get(i);
            expression.accept(expressionVisitor);
            if (i < insert.getSetColumns().size() - 1) {
                buffer.append(", ");
            }
        }
    }
    if (insert.isUseDuplicate()) {
        buffer.append(" ON DUPLICATE KEY UPDATE ");
        for (int i = 0; i < insert.getDuplicateUpdateColumns().size(); i++) {
            Column column = insert.getDuplicateUpdateColumns().get(i);
            buffer.append(column.getFullyQualifiedName()).append(" = ");
            Expression expression = insert.getDuplicateUpdateExpressionList().get(i);
            expression.accept(expressionVisitor);
            if (i < insert.getDuplicateUpdateColumns().size() - 1) {
                buffer.append(", ");
            }
        }
    }
    if (insert.isReturningAllColumns()) {
        buffer.append(" RETURNING *");
    } else if (insert.getReturningExpressionList() != null) {
        buffer.append(" RETURNING ");
        for (Iterator<SelectExpressionItem> iter = insert.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) Iterator(java.util.Iterator) WithItem(net.sf.jsqlparser.statement.select.WithItem)

Example 3 with WithItem

use of net.sf.jsqlparser.statement.select.WithItem 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 4 with WithItem

use of net.sf.jsqlparser.statement.select.WithItem in project JSqlParser by JSQLParser.

the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingSelect.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingSelect() {
    Select select = new Select();
    WithItem withItem1 = spy(new WithItem());
    WithItem withItem2 = spy(new WithItem());
    SelectBody selectBody = mock(SelectBody.class);
    List<WithItem> withItemsList = new ArrayList<WithItem>();
    select.setWithItemsList(withItemsList);
    select.setSelectBody(selectBody);
    withItemsList.add(withItem1);
    withItemsList.add(withItem2);
    statementDeParser.visit(select);
    then(withItem1).should().accept(selectDeParser);
    then(withItem2).should().accept(selectDeParser);
    then(selectBody).should().accept(selectDeParser);
}
Also used : Select(net.sf.jsqlparser.statement.select.Select) ArrayList(java.util.ArrayList) WithItem(net.sf.jsqlparser.statement.select.WithItem) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) Test(org.junit.Test)

Example 5 with WithItem

use of net.sf.jsqlparser.statement.select.WithItem in project JSqlParser by JSQLParser.

the class StatementDeParser method visit.

@Override
public void visit(Select select) {
    selectDeParser.setBuffer(buffer);
    expressionDeParser.setSelectVisitor(selectDeParser);
    expressionDeParser.setBuffer(buffer);
    selectDeParser.setExpressionVisitor(expressionDeParser);
    if (select.getWithItemsList() != null && !select.getWithItemsList().isEmpty()) {
        buffer.append("WITH ");
        for (Iterator<WithItem> iter = select.getWithItemsList().iterator(); iter.hasNext(); ) {
            WithItem withItem = iter.next();
            withItem.accept(selectDeParser);
            if (iter.hasNext()) {
                buffer.append(",");
            }
            buffer.append(" ");
        }
    }
    select.getSelectBody().accept(selectDeParser);
}
Also used : WithItem(net.sf.jsqlparser.statement.select.WithItem)

Aggregations

WithItem (net.sf.jsqlparser.statement.select.WithItem)6 ArrayList (java.util.ArrayList)3 Expression (net.sf.jsqlparser.expression.Expression)3 Column (net.sf.jsqlparser.schema.Column)3 Select (net.sf.jsqlparser.statement.select.Select)3 SelectBody (net.sf.jsqlparser.statement.select.SelectBody)3 Test (org.junit.Test)3 Table (net.sf.jsqlparser.schema.Table)2 Iterator (java.util.Iterator)1 Insert (net.sf.jsqlparser.statement.insert.Insert)1 Upsert (net.sf.jsqlparser.statement.upsert.Upsert)1