Search in sources :

Example 1 with OrderItem

use of io.shardingjdbc.core.parsing.parser.context.OrderItem in project sharding-jdbc by shardingjdbc.

the class OrderByValue method getOrderValues.

private List<Comparable<?>> getOrderValues() throws SQLException {
    List<Comparable<?>> result = new ArrayList<>(orderByItems.size());
    for (OrderItem each : orderByItems) {
        Object value = queryResult.getValue(each.getIndex(), Object.class);
        Preconditions.checkState(null == value || value instanceof Comparable, "Order by value must implements Comparable");
        result.add((Comparable<?>) value);
    }
    return result;
}
Also used : OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) ArrayList(java.util.ArrayList)

Example 2 with OrderItem

use of io.shardingjdbc.core.parsing.parser.context.OrderItem in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngine method appendOrderByToken.

private void appendOrderByToken(final SQLBuilder sqlBuilder, final int count, final List<SQLToken> sqlTokens) {
    SelectStatement selectStatement = (SelectStatement) sqlStatement;
    StringBuilder orderByLiterals = new StringBuilder();
    orderByLiterals.append(" ").append(DefaultKeyword.ORDER).append(" ").append(DefaultKeyword.BY).append(" ");
    int i = 0;
    for (OrderItem each : selectStatement.getOrderByItems()) {
        String columnLabel = SQLUtil.getOriginalValue(each.getColumnLabel(), databaseType);
        if (0 == i) {
            orderByLiterals.append(columnLabel).append(" ").append(each.getOrderDirection().name());
        } else {
            orderByLiterals.append(",").append(columnLabel).append(" ").append(each.getOrderDirection().name());
        }
        i++;
    }
    orderByLiterals.append(" ");
    sqlBuilder.appendLiterals(orderByLiterals.toString());
    int beginPosition = ((SelectStatement) sqlStatement).getGroupByLastPosition();
    appendRest(sqlBuilder, count, sqlTokens, beginPosition);
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem)

Example 3 with OrderItem

use of io.shardingjdbc.core.parsing.parser.context.OrderItem in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertRewriteForRowNumForMemoryGroupBy.

@Test
public void assertRewriteForRowNumForMemoryGroupBy() {
    selectStatement.setLimit(new Limit(DatabaseType.Oracle));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(68, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(119, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(98, 4));
    selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC, Optional.<String>absent()));
    selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC, Optional.<String>absent()));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=2147483647) t WHERE t.rownum_>0"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue) Test(org.junit.Test)

Example 4 with OrderItem

use of io.shardingjdbc.core.parsing.parser.context.OrderItem in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertRewriteForTopAndRowNumberForMemoryGroupBy.

@Test
public void assertRewriteForTopAndRowNumberForMemoryGroupBy() {
    selectStatement.setLimit(new Limit(DatabaseType.SQLServer));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(85, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(123, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(26, 4));
    selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC, Optional.<String>absent()));
    selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC, Optional.<String>absent()));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT * FROM (SELECT TOP(2147483647) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>0"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue) Test(org.junit.Test)

Example 5 with OrderItem

use of io.shardingjdbc.core.parsing.parser.context.OrderItem in project sharding-jdbc by shardingjdbc.

the class DQLMergeEngineTest method assertBuildGroupByStreamMergedResultWithMySQLLimit.

@Test
public void assertBuildGroupByStreamMergedResultWithMySQLLimit() throws SQLException {
    selectStatement.setLimit(new Limit(DatabaseType.MySQL));
    selectStatement.getGroupByItems().add(new OrderItem(1, OrderDirection.DESC, OrderDirection.ASC));
    selectStatement.getOrderByItems().add(new OrderItem(1, OrderDirection.DESC, OrderDirection.ASC));
    mergeEngine = new DQLMergeEngine(queryResults, selectStatement);
    MergedResult actual = mergeEngine.merge();
    assertThat(actual, instanceOf(LimitDecoratorMergedResult.class));
    assertThat(((LimitDecoratorMergedResult) actual).getMergedResult(), instanceOf(GroupByStreamMergedResult.class));
}
Also used : OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) GroupByStreamMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByStreamMergedResult) MergedResult(io.shardingjdbc.core.merger.MergedResult) LimitDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.LimitDecoratorMergedResult) GroupByMemoryMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult) TopAndRowNumberDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.TopAndRowNumberDecoratorMergedResult) RowNumberDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.RowNumberDecoratorMergedResult) IteratorStreamMergedResult(io.shardingjdbc.core.merger.dql.iterator.IteratorStreamMergedResult) OrderByStreamMergedResult(io.shardingjdbc.core.merger.dql.orderby.OrderByStreamMergedResult) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.LimitDecoratorMergedResult) GroupByStreamMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByStreamMergedResult) Test(org.junit.Test)

Aggregations

OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)42 Test (org.junit.Test)30 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)12 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)12 GroupByMemoryMergedResult (io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult)10 GroupByStreamMergedResult (io.shardingjdbc.core.merger.dql.groupby.GroupByStreamMergedResult)10 OrderByStreamMergedResult (io.shardingjdbc.core.merger.dql.orderby.OrderByStreamMergedResult)10 MergedResult (io.shardingjdbc.core.merger.MergedResult)9 IteratorStreamMergedResult (io.shardingjdbc.core.merger.dql.iterator.IteratorStreamMergedResult)9 LimitDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.LimitDecoratorMergedResult)9 RowNumberDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.RowNumberDecoratorMergedResult)9 TopAndRowNumberDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.TopAndRowNumberDecoratorMergedResult)9 TestQueryResult (io.shardingjdbc.core.merger.fixture.TestQueryResult)9 MemoryQueryResultRow (io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow)6 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)4 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)3 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)3 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)3 ResultSet (java.sql.ResultSet)3 Before (org.junit.Before)3