Search in sources :

Example 1 with Limit

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

the class WhereClauseParser method parseRowCountCondition.

private void parseRowCountCondition(final SelectStatement selectStatement, final boolean includeRowCount) {
    SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
    if (null == selectStatement.getLimit()) {
        selectStatement.setLimit(new Limit(databaseType));
    }
    if (sqlExpression instanceof SQLNumberExpression) {
        int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
        selectStatement.getLimit().setRowCount(new LimitValue(rowCount, -1, includeRowCount));
        selectStatement.getSqlTokens().add(new RowCountToken(lexerEngine.getCurrentToken().getEndPosition() - String.valueOf(rowCount).length() - lexerEngine.getCurrentToken().getLiterals().length(), rowCount));
    } else if (sqlExpression instanceof SQLPlaceholderExpression) {
        selectStatement.getLimit().setRowCount(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex(), includeRowCount));
    }
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression) 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)

Example 2 with Limit

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

the class WhereClauseParser method parseOffsetCondition.

private void parseOffsetCondition(final SelectStatement selectStatement, final boolean includeOffset) {
    SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
    if (null == selectStatement.getLimit()) {
        selectStatement.setLimit(new Limit(databaseType));
    }
    if (sqlExpression instanceof SQLNumberExpression) {
        int offset = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
        selectStatement.getLimit().setOffset(new LimitValue(offset, -1, includeOffset));
        selectStatement.getSqlTokens().add(new OffsetToken(lexerEngine.getCurrentToken().getEndPosition() - String.valueOf(offset).length() - lexerEngine.getCurrentToken().getLiterals().length(), offset));
    } else if (sqlExpression instanceof SQLPlaceholderExpression) {
        selectStatement.getLimit().setOffset(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex(), includeOffset));
    }
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Example 3 with Limit

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

the class MySQLLimitClauseParser method getLimitWithComma.

private Limit getLimitWithComma(final int index, final int valueBeginPosition, final int value, final boolean isParameterForValue, final SelectStatement selectStatement) {
    int rowCountBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
    int rowCountValue;
    int rowCountIndex = -1;
    boolean isParameterForRowCount = false;
    if (lexerEngine.equalAny(Literals.INT)) {
        rowCountValue = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
        rowCountBeginPosition = rowCountBeginPosition - (rowCountValue + "").length();
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        rowCountIndex = -1 == index ? selectStatement.getParametersIndex() : index + 1;
        rowCountValue = -1;
        rowCountBeginPosition--;
        isParameterForRowCount = true;
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    if (isParameterForValue) {
        selectStatement.increaseParametersIndex();
    } else {
        selectStatement.getSqlTokens().add(new OffsetToken(valueBeginPosition, value));
    }
    if (isParameterForRowCount) {
        selectStatement.increaseParametersIndex();
    } else {
        selectStatement.getSqlTokens().add(new RowCountToken(rowCountBeginPosition, rowCountValue));
    }
    Limit result = new Limit(DatabaseType.MySQL);
    result.setRowCount(new LimitValue(rowCountValue, rowCountIndex, false));
    result.setOffset(new LimitValue(value, index, true));
    return result;
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) 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)

Example 4 with Limit

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

the class MySQLLimitClauseParser method parse.

/**
 * Parse limit.
 *
 * @param selectStatement select statement
 */
public void parse(final SelectStatement selectStatement) {
    if (!lexerEngine.skipIfEqual(MySQLKeyword.LIMIT)) {
        return;
    }
    int valueIndex = -1;
    int valueBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
    int value;
    boolean isParameterForValue = false;
    if (lexerEngine.equalAny(Literals.INT)) {
        value = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
        valueBeginPosition = valueBeginPosition - (value + "").length();
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        valueIndex = selectStatement.getParametersIndex();
        value = -1;
        valueBeginPosition--;
        isParameterForValue = true;
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    if (lexerEngine.skipIfEqual(Symbol.COMMA)) {
        selectStatement.setLimit(getLimitWithComma(valueIndex, valueBeginPosition, value, isParameterForValue, selectStatement));
        return;
    }
    if (lexerEngine.skipIfEqual(MySQLKeyword.OFFSET)) {
        selectStatement.setLimit(getLimitWithOffset(valueIndex, valueBeginPosition, value, isParameterForValue, selectStatement));
        return;
    }
    if (isParameterForValue) {
        selectStatement.increaseParametersIndex();
    } else {
        selectStatement.getSqlTokens().add(new RowCountToken(valueBeginPosition, value));
    }
    Limit limit = new Limit(DatabaseType.MySQL);
    limit.setRowCount(new LimitValue(value, valueIndex, false));
    selectStatement.setLimit(limit);
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) 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)

Example 5 with Limit

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

the class SQLServerOffsetClauseParser method parse.

/**
 * Parse offset.
 *
 * @param selectStatement select statement
 */
public void parse(final SelectStatement selectStatement) {
    if (!lexerEngine.skipIfEqual(SQLServerKeyword.OFFSET)) {
        return;
    }
    int offsetValue = -1;
    int offsetIndex = -1;
    if (lexerEngine.equalAny(Literals.INT)) {
        offsetValue = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        offsetIndex = selectStatement.getParametersIndex();
        selectStatement.increaseParametersIndex();
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    Limit limit = new Limit(DatabaseType.SQLServer);
    if (lexerEngine.skipIfEqual(DefaultKeyword.FETCH)) {
        lexerEngine.nextToken();
        int rowCountValue = -1;
        int rowCountIndex = -1;
        lexerEngine.nextToken();
        if (lexerEngine.equalAny(Literals.INT)) {
            rowCountValue = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
        } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
            rowCountIndex = selectStatement.getParametersIndex();
            selectStatement.increaseParametersIndex();
        } else {
            throw new SQLParsingException(lexerEngine);
        }
        lexerEngine.nextToken();
        lexerEngine.nextToken();
        limit.setRowCount(new LimitValue(rowCountValue, rowCountIndex, false));
        limit.setOffset(new LimitValue(offsetValue, offsetIndex, true));
    } else {
        limit.setOffset(new LimitValue(offsetValue, offsetIndex, true));
    }
    selectStatement.setLimit(limit);
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) 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