Search in sources :

Example 26 with LimitValue

use of io.shardingjdbc.core.parsing.parser.context.limit.LimitValue 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 27 with LimitValue

use of io.shardingjdbc.core.parsing.parser.context.limit.LimitValue 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 28 with LimitValue

use of io.shardingjdbc.core.parsing.parser.context.limit.LimitValue 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 29 with LimitValue

use of io.shardingjdbc.core.parsing.parser.context.limit.LimitValue 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

LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)29 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)27 Test (org.junit.Test)20 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)15 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)13 MergedResult (io.shardingjdbc.core.merger.MergedResult)11 DQLMergeEngine (io.shardingjdbc.core.merger.dql.DQLMergeEngine)11 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)9 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)7 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)3 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)3 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)3 SQLPlaceholderExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)3