Search in sources :

Example 6 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class GroupByMemoryMergedResultTest method setUp.

@Before
public void setUp() throws SQLException {
    resultSets = Lists.newArrayList(mockResultSet(), mockResultSet(), mockResultSet());
    queryResults = new ArrayList<>(resultSets.size());
    for (ResultSet each : resultSets) {
        queryResults.add(new TestQueryResult(each));
    }
    selectStatement = new SelectStatement();
    AggregationSelectItem aggregationSelectItem1 = new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.<String>absent());
    aggregationSelectItem1.setIndex(1);
    AggregationSelectItem aggregationSelectItem2 = new AggregationSelectItem(AggregationType.AVG, "(num)", Optional.<String>absent());
    aggregationSelectItem2.setIndex(2);
    AggregationSelectItem derivedAggregationSelectItem1 = new AggregationSelectItem(AggregationType.COUNT, "(num)", Optional.of("AVG_DERIVED_COUNT_0"));
    aggregationSelectItem2.setIndex(4);
    aggregationSelectItem2.getDerivedAggregationSelectItems().add(derivedAggregationSelectItem1);
    AggregationSelectItem derivedAggregationSelectItem2 = new AggregationSelectItem(AggregationType.SUM, "(num)", Optional.of("AVG_DERIVED_SUM_0"));
    aggregationSelectItem2.setIndex(5);
    aggregationSelectItem2.getDerivedAggregationSelectItems().add(derivedAggregationSelectItem2);
    selectStatement.getItems().add(aggregationSelectItem1);
    selectStatement.getItems().add(aggregationSelectItem2);
    selectStatement.getGroupByItems().add(new OrderItem(3, OrderDirection.ASC, OrderDirection.ASC));
    selectStatement.getOrderByItems().add(new OrderItem(3, OrderDirection.DESC, OrderDirection.ASC));
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) TestQueryResult(io.shardingjdbc.core.merger.fixture.TestQueryResult) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) ResultSet(java.sql.ResultSet) Before(org.junit.Before)

Example 7 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class GroupByRowComparatorTest method assertCompareToForEqualWithGroupByItems.

@Test
public void assertCompareToForEqualWithGroupByItems() throws SQLException {
    MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    SelectStatement selectStatement = new SelectStatement();
    selectStatement.getGroupByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.ASC, OrderDirection.ASC), new OrderItem(2, OrderDirection.DESC, OrderDirection.ASC)));
    GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatement);
    assertThat(groupByRowComparator.compare(o1, o2), is(0));
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) Test(org.junit.Test)

Example 8 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class GroupByRowComparatorTest method assertCompareToForEqualWithOrderByItems.

@Test
public void assertCompareToForEqualWithOrderByItems() throws SQLException {
    MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    SelectStatement selectStatement = new SelectStatement();
    selectStatement.getOrderByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.ASC, OrderDirection.ASC), new OrderItem(2, OrderDirection.DESC, OrderDirection.ASC)));
    selectStatement.getGroupByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.DESC, OrderDirection.ASC), new OrderItem(2, OrderDirection.ASC, OrderDirection.ASC)));
    GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatement);
    assertThat(groupByRowComparator.compare(o1, o2), is(0));
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) Test(org.junit.Test)

Example 9 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class GroupByRowComparatorTest method assertCompareToForAscWithGroupByItems.

@Test
public void assertCompareToForAscWithGroupByItems() throws SQLException {
    MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("3", "4"));
    SelectStatement selectStatement = new SelectStatement();
    selectStatement.getGroupByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.ASC, OrderDirection.ASC), new OrderItem(2, OrderDirection.ASC, OrderDirection.ASC)));
    GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatement);
    assertTrue(groupByRowComparator.compare(o1, o2) < 0);
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) Test(org.junit.Test)

Example 10 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class GroupByRowComparatorTest method assertCompareToForDecsWithOrderByItems.

@Test
public void assertCompareToForDecsWithOrderByItems() throws SQLException {
    MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("3", "4"));
    SelectStatement selectStatement = new SelectStatement();
    selectStatement.getOrderByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.DESC, OrderDirection.ASC), new OrderItem(2, OrderDirection.DESC, OrderDirection.ASC)));
    selectStatement.getGroupByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.ASC, OrderDirection.ASC), new OrderItem(2, OrderDirection.ASC, OrderDirection.ASC)));
    GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatement);
    assertTrue(groupByRowComparator.compare(o1, o2) > 0);
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) Test(org.junit.Test)

Aggregations

SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)25 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)12 ResultSet (java.sql.ResultSet)10 Before (org.junit.Before)9 Test (org.junit.Test)9 TestQueryResult (io.shardingjdbc.core.merger.fixture.TestQueryResult)8 MemoryQueryResultRow (io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow)6 ResultSetMetaData (java.sql.ResultSetMetaData)6 DALStatement (io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement)3 GeneratedKeysResultSet (io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet)2 ShardingResultSet (io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet)2 JDBCQueryResult (io.shardingjdbc.core.merger.JDBCQueryResult)2 MergeEngine (io.shardingjdbc.core.merger.MergeEngine)2 QueryResult (io.shardingjdbc.core.merger.QueryResult)2 AggregationSelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)2 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)2 DQLStatement (io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement)2 ArrayList (java.util.ArrayList)2 DQLMergeEngine (io.shardingjdbc.core.merger.dql.DQLMergeEngine)1 DefaultKeyword (io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword)1