Search in sources :

Example 21 with OrderItem

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

the class OrderByAssert method assertOrderByItems.

/**
 * Assert order by items.
 *
 * @param actual actual order by items
 * @param expected expected order by items
 */
public void assertOrderByItems(final List<OrderItem> actual, final List<ExpectedOrderByColumn> expected) {
    assertThat(assertMessage.getFullAssertMessage("Order by items size error: "), actual.size(), is(expected.size()));
    int count = 0;
    for (OrderItem each : actual) {
        assertOrderByItem(each, expected.get(count));
        count++;
    }
}
Also used : OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem)

Example 22 with OrderItem

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

the class GroupByRowComparator method compare.

private int compare(final MemoryQueryResultRow o1, final MemoryQueryResultRow o2, final List<OrderItem> orderItems) {
    for (OrderItem each : orderItems) {
        Object orderValue1 = o1.getCell(each.getIndex());
        Preconditions.checkState(null == orderValue1 || orderValue1 instanceof Comparable, "Order by value must implements Comparable");
        Object orderValue2 = o2.getCell(each.getIndex());
        Preconditions.checkState(null == orderValue2 || orderValue2 instanceof Comparable, "Order by value must implements Comparable");
        int result = CompareUtil.compareTo((Comparable) orderValue1, (Comparable) orderValue2, each.getOrderDirection(), each.getNullOrderDirection());
        if (0 != result) {
            return result;
        }
    }
    return 0;
}
Also used : OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem)

Example 23 with OrderItem

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

the class OrderByValue method compareTo.

@Override
public int compareTo(final OrderByValue o) {
    for (int i = 0; i < orderByItems.size(); i++) {
        OrderItem thisOrderBy = orderByItems.get(i);
        int result = CompareUtil.compareTo(orderValues.get(i), o.orderValues.get(i), thisOrderBy.getOrderDirection(), thisOrderBy.getNullOrderDirection());
        if (0 != result) {
            return result;
        }
    }
    return 0;
}
Also used : OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem)

Example 24 with OrderItem

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

the class GroupByClauseParser method addGroupByItem.

private void addGroupByItem(final SQLExpression sqlExpression, final SelectStatement selectStatement) {
    lexerEngine.unsupportedIfEqual(getUnsupportedKeywordBeforeGroupByItem());
    OrderDirection orderDirection = OrderDirection.ASC;
    if (lexerEngine.equalAny(DefaultKeyword.ASC)) {
        lexerEngine.nextToken();
    } else if (lexerEngine.skipIfEqual(DefaultKeyword.DESC)) {
        orderDirection = OrderDirection.DESC;
    }
    OrderItem orderItem;
    if (sqlExpression instanceof SQLPropertyExpression) {
        SQLPropertyExpression sqlPropertyExpression = (SQLPropertyExpression) sqlExpression;
        orderItem = new OrderItem(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()), SQLUtil.getExactlyValue(sqlPropertyExpression.getName()), orderDirection, OrderDirection.ASC, selectStatement.getAlias(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName() + "." + SQLUtil.getExactlyValue(sqlPropertyExpression.getName()))));
    } else if (sqlExpression instanceof SQLIdentifierExpression) {
        SQLIdentifierExpression sqlIdentifierExpression = (SQLIdentifierExpression) sqlExpression;
        orderItem = new OrderItem(SQLUtil.getExactlyValue(sqlIdentifierExpression.getName()), orderDirection, OrderDirection.ASC, selectStatement.getAlias(SQLUtil.getExactlyValue(sqlIdentifierExpression.getName())));
    } else if (sqlExpression instanceof SQLIgnoreExpression) {
        SQLIgnoreExpression sqlIgnoreExpression = (SQLIgnoreExpression) sqlExpression;
        orderItem = new OrderItem(sqlIgnoreExpression.getExpression(), orderDirection, OrderDirection.ASC, selectStatement.getAlias(sqlIgnoreExpression.getExpression()));
    } else {
        return;
    }
    selectStatement.getGroupByItems().add(orderItem);
}
Also used : SQLPropertyExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression) SQLIdentifierExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) SQLIgnoreExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression) OrderDirection(io.shardingjdbc.core.constant.OrderDirection)

Example 25 with OrderItem

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

the class DQLMergeEngineTest method assertBuildGroupByMemoryMergedResultWithSQLServerLimit.

@Test
public void assertBuildGroupByMemoryMergedResultWithSQLServerLimit() throws SQLException {
    selectStatement.setLimit(new Limit(DatabaseType.SQLServer));
    selectStatement.getGroupByItems().add(new OrderItem(1, OrderDirection.DESC, OrderDirection.ASC));
    selectStatement.getGroupByItems().add(new OrderItem(1, OrderDirection.ASC, OrderDirection.ASC));
    mergeEngine = new DQLMergeEngine(queryResults, selectStatement);
    MergedResult actual = mergeEngine.merge();
    assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class));
    assertThat(((TopAndRowNumberDecoratorMergedResult) actual).getMergedResult(), instanceOf(GroupByMemoryMergedResult.class));
}
Also used : TopAndRowNumberDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.TopAndRowNumberDecoratorMergedResult) 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) GroupByMemoryMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) 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