Search in sources :

Example 46 with Limit

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

the class SQLServerTopClauseParser method parse.

/**
 * Parse top.
 *
 * @param selectStatement select statement
 */
public void parse(final SelectStatement selectStatement) {
    if (!lexerEngine.skipIfEqual(SQLServerKeyword.TOP)) {
        return;
    }
    int beginPosition = lexerEngine.getCurrentToken().getEndPosition();
    if (!lexerEngine.skipIfEqual(Symbol.LEFT_PAREN)) {
        beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
    }
    SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
    lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
    LimitValue rowCountValue;
    if (sqlExpression instanceof SQLNumberExpression) {
        int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
        rowCountValue = new LimitValue(rowCount, -1, false);
        selectStatement.getSqlTokens().add(new RowCountToken(beginPosition, rowCount));
    } else if (sqlExpression instanceof SQLPlaceholderExpression) {
        rowCountValue = new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex(), false);
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.unsupportedIfEqual(SQLServerKeyword.PERCENT);
    lexerEngine.skipIfEqual(DefaultKeyword.WITH, SQLServerKeyword.TIES);
    if (null == selectStatement.getLimit()) {
        Limit limit = new Limit(DatabaseType.SQLServer);
        limit.setRowCount(rowCountValue);
        selectStatement.setLimit(limit);
    } else {
        selectStatement.getLimit().setRowCount(rowCountValue);
    }
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Aggregations

Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)46 Test (org.junit.Test)36 MergedResult (io.shardingjdbc.core.merger.MergedResult)27 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)27 GroupByMemoryMergedResult (io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult)15 GroupByStreamMergedResult (io.shardingjdbc.core.merger.dql.groupby.GroupByStreamMergedResult)15 IteratorStreamMergedResult (io.shardingjdbc.core.merger.dql.iterator.IteratorStreamMergedResult)15 OrderByStreamMergedResult (io.shardingjdbc.core.merger.dql.orderby.OrderByStreamMergedResult)15 LimitDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.LimitDecoratorMergedResult)15 RowNumberDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.RowNumberDecoratorMergedResult)15 TopAndRowNumberDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.TopAndRowNumberDecoratorMergedResult)15 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)14 DQLMergeEngine (io.shardingjdbc.core.merger.dql.DQLMergeEngine)12 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)12 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)12 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)9 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)5 AggregationSelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)3 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)3 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)3