Search in sources :

Example 41 with Limit

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

the class RowNumberDecoratorMergedResultTest method assertNextForRowCountBoundOpenedFalse.

@Test
public void assertNextForRowCountBoundOpenedFalse() throws SQLException {
    Limit limit = new Limit(DatabaseType.Oracle);
    limit.setOffset(new LimitValue(2, -1, true));
    limit.setRowCount(new LimitValue(4, -1, false));
    selectStatement.setLimit(limit);
    mergeEngine = new DQLMergeEngine(queryResults, selectStatement);
    MergedResult actual = mergeEngine.merge();
    assertTrue(actual.next());
    assertTrue(actual.next());
    assertFalse(actual.next());
}
Also used : MergedResult(io.shardingjdbc.core.merger.MergedResult) DQLMergeEngine(io.shardingjdbc.core.merger.dql.DQLMergeEngine) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue) Test(org.junit.Test)

Example 42 with Limit

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

the class TopAndRowNumberDecoratorMergedResultTest method assertNextWithOffsetBoundOpenedFalse.

@Test
public void assertNextWithOffsetBoundOpenedFalse() throws SQLException {
    Limit limit = new Limit(DatabaseType.SQLServer);
    limit.setOffset(new LimitValue(2, -1, false));
    limit.setRowCount(new LimitValue(4, -1, false));
    selectStatement.setLimit(limit);
    mergeEngine = new DQLMergeEngine(queryResults, selectStatement);
    MergedResult actual = mergeEngine.merge();
    assertTrue(actual.next());
    assertTrue(actual.next());
    assertFalse(actual.next());
}
Also used : MergedResult(io.shardingjdbc.core.merger.MergedResult) DQLMergeEngine(io.shardingjdbc.core.merger.dql.DQLMergeEngine) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue) Test(org.junit.Test)

Example 43 with Limit

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

the class SelectStatement method processLimitForSubQuery.

private SelectStatement processLimitForSubQuery() {
    SelectStatement result = this;
    List<SQLToken> limitSQLTokens = getLimitTokens(result);
    Limit limit = result.getLimit();
    while (result.containsSubQuery()) {
        result = result.subQueryStatement;
        limitSQLTokens.addAll(getLimitTokens(result));
        if (null == result.getLimit()) {
            continue;
        }
        if (null == limit) {
            limit = result.getLimit();
        }
        if (null != result.getLimit().getRowCount()) {
            limit.setRowCount(result.getLimit().getRowCount());
        }
        if (null != result.getLimit().getOffset()) {
            limit.setOffset(result.getLimit().getOffset());
        }
    }
    resetLimitTokens(result, limitSQLTokens);
    result.setLimit(limit);
    return result;
}
Also used : SQLToken(io.shardingjdbc.core.parsing.parser.token.SQLToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit)

Example 44 with Limit

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

the class MySQLLimitClauseParser method getLimitWithOffset.

private Limit getLimitWithOffset(final int index, final int valueBeginPosition, final int value, final boolean isParameterForValue, final SelectStatement selectStatement) {
    int offsetBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
    int offsetValue = -1;
    int offsetIndex = -1;
    boolean isParameterForOffset = false;
    if (lexerEngine.equalAny(Literals.INT)) {
        offsetValue = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
        offsetBeginPosition = offsetBeginPosition - (offsetValue + "").length();
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        offsetIndex = -1 == index ? selectStatement.getParametersIndex() : index + 1;
        offsetBeginPosition--;
        isParameterForOffset = true;
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    if (isParameterForOffset) {
        selectStatement.increaseParametersIndex();
    } else {
        selectStatement.getSqlTokens().add(new OffsetToken(offsetBeginPosition, offsetValue));
    }
    if (isParameterForValue) {
        selectStatement.increaseParametersIndex();
    } else {
        selectStatement.getSqlTokens().add(new RowCountToken(valueBeginPosition, value));
    }
    Limit result = new Limit(DatabaseType.MySQL);
    result.setRowCount(new LimitValue(value, index, false));
    result.setOffset(new LimitValue(offsetValue, offsetIndex, 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 45 with Limit

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

the class PostgreSQLLimitClauseParser method setLimit.

private void setLimit(final Optional<LimitValue> offset, final Optional<LimitValue> rowCount, final SelectStatement selectStatement) {
    Limit limit = new Limit(DatabaseType.PostgreSQL);
    if (offset.isPresent()) {
        limit.setOffset(offset.get());
    }
    if (rowCount.isPresent()) {
        limit.setRowCount(rowCount.get());
    }
    selectStatement.setLimit(limit);
}
Also used : Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit)

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