Search in sources :

Example 6 with SelectBody

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

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

use of net.sf.jsqlparser.statement.select.SelectBody in project yyl_example by Relucent.

the class CountSqlParser method sqlToCount.

/**
 * 将 SQL 转换为 COUNT 查询
 */
public void sqlToCount(Select select) {
    SelectBody selectBody = select.getSelectBody();
    List<SelectItem> countItem = new ArrayList<SelectItem>();
    countItem.add(new SelectExpressionItem(new Column("count(*)")));
    if (selectBody instanceof PlainSelect && isSimpleCount((PlainSelect) selectBody)) {
        ((PlainSelect) selectBody).setSelectItems(countItem);
    } else {
        SubSelect subSelect = new SubSelect();
        subSelect.setSelectBody(selectBody);
        subSelect.setAlias(TABLE_ALIAS);
        PlainSelect plainSelect = new PlainSelect();
        plainSelect.setFromItem(subSelect);
        plainSelect.setSelectItems(countItem);
        select.setSelectBody(plainSelect);
    }
}
Also used : Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) ArrayList(java.util.ArrayList) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) LateralSubSelect(net.sf.jsqlparser.statement.select.LateralSubSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect)

Aggregations

SelectBody (net.sf.jsqlparser.statement.select.SelectBody)8 Select (net.sf.jsqlparser.statement.select.Select)7 ArrayList (java.util.ArrayList)5 Expression (net.sf.jsqlparser.expression.Expression)4 Column (net.sf.jsqlparser.schema.Column)4 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)4 Test (org.junit.Test)4 Statement (net.sf.jsqlparser.statement.Statement)3 WithItem (net.sf.jsqlparser.statement.select.WithItem)3 Table (net.sf.jsqlparser.schema.Table)2 LateralSubSelect (net.sf.jsqlparser.statement.select.LateralSubSelect)2 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)2 SubSelect (net.sf.jsqlparser.statement.select.SubSelect)2 JSQLParserException (net.sf.jsqlparser.JSQLParserException)1 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)1 Insert (net.sf.jsqlparser.statement.insert.Insert)1 OrderByElement (net.sf.jsqlparser.statement.select.OrderByElement)1 SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)1 Update (net.sf.jsqlparser.statement.update.Update)1 Upsert (net.sf.jsqlparser.statement.upsert.Upsert)1