Search in sources :

Example 41 with OrderItem

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

the class AbstractSelectParser method appendDerivedOrderColumns.

private void appendDerivedOrderColumns(final ItemsToken itemsToken, final List<OrderItem> orderItems, final String aliasPattern, final SelectStatement selectStatement) {
    int derivedColumnOffset = 0;
    for (OrderItem each : orderItems) {
        if (!isContainsItem(each, selectStatement)) {
            String alias = String.format(aliasPattern, derivedColumnOffset++);
            each.setAlias(Optional.of(alias));
            itemsToken.getItems().add(each.getQualifiedName().get() + " AS " + alias + " ");
        }
    }
}
Also used : OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem)

Example 42 with OrderItem

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

the class OrderByClauseParser method parseSelectOrderByItem.

private OrderItem parseSelectOrderByItem(final SelectStatement selectStatement) {
    SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
    OrderDirection orderDirection = OrderDirection.ASC;
    if (lexerEngine.skipIfEqual(DefaultKeyword.ASC)) {
        orderDirection = OrderDirection.ASC;
    } else if (lexerEngine.skipIfEqual(DefaultKeyword.DESC)) {
        orderDirection = OrderDirection.DESC;
    }
    if (sqlExpression instanceof SQLNumberExpression) {
        return new OrderItem(((SQLNumberExpression) sqlExpression).getNumber().intValue(), orderDirection, getNullOrderDirection());
    }
    if (sqlExpression instanceof SQLIdentifierExpression) {
        return new OrderItem(SQLUtil.getExactlyValue(((SQLIdentifierExpression) sqlExpression).getName()), orderDirection, getNullOrderDirection(), selectStatement.getAlias(SQLUtil.getExactlyValue(((SQLIdentifierExpression) sqlExpression).getName())));
    }
    if (sqlExpression instanceof SQLPropertyExpression) {
        SQLPropertyExpression sqlPropertyExpression = (SQLPropertyExpression) sqlExpression;
        return new OrderItem(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()), SQLUtil.getExactlyValue(sqlPropertyExpression.getName()), orderDirection, getNullOrderDirection(), selectStatement.getAlias(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()) + "." + SQLUtil.getExactlyValue(sqlPropertyExpression.getName())));
    }
    if (sqlExpression instanceof SQLIgnoreExpression) {
        SQLIgnoreExpression sqlIgnoreExpression = (SQLIgnoreExpression) sqlExpression;
        return new OrderItem(sqlIgnoreExpression.getExpression(), orderDirection, getNullOrderDirection(), selectStatement.getAlias(sqlIgnoreExpression.getExpression()));
    }
    throw new SQLParsingException(lexerEngine);
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) SQLPropertyExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) 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)

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