Search in sources :

Example 6 with LimitValue

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

the class PostgreSQLLimitClauseParser method buildOffset.

private Optional<LimitValue> buildOffset(final SelectStatement selectStatement) {
    int parameterIndex = selectStatement.getParametersIndex();
    int offsetValue = -1;
    int offsetIndex = -1;
    int offsetBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
    if (lexerEngine.equalAny(Literals.INT, Literals.FLOAT)) {
        offsetValue = NumberUtil.roundHalfUp(lexerEngine.getCurrentToken().getLiterals());
        offsetBeginPosition = offsetBeginPosition - (offsetValue + "").length();
        selectStatement.getSqlTokens().add(new OffsetToken(offsetBeginPosition, offsetValue));
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        offsetIndex = parameterIndex++;
        selectStatement.setParametersIndex(parameterIndex);
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    lexerEngine.skipIfEqual(DefaultKeyword.ROW, PostgreSQLKeyword.ROWS);
    return Optional.of(new LimitValue(offsetValue, offsetIndex, true));
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Example 7 with LimitValue

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

Example 8 with LimitValue

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

the class SQLRewriteEngineTest method assertRewriteForTopAndRowNumberForNotRewriteLimit.

@Test
public void assertRewriteForTopAndRowNumberForNotRewriteLimit() {
    selectStatement.setLimit(new Limit(DatabaseType.SQLServer));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(85, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(123, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(26, 4));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement);
    assertThat(rewriteEngine.rewrite(false).toSQL(tableTokens, null), is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>2"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) 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) Test(org.junit.Test)

Example 9 with LimitValue

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

the class SQLRewriteEngineTest method assertRewriteForRowNumForMemoryGroupBy.

@Test
public void assertRewriteForRowNumForMemoryGroupBy() {
    selectStatement.setLimit(new Limit(DatabaseType.Oracle));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(68, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(119, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(98, 4));
    selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC, Optional.<String>absent()));
    selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC, Optional.<String>absent()));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=2147483647) t WHERE t.rownum_>0"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) 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) Test(org.junit.Test)

Example 10 with LimitValue

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

the class SQLRewriteEngineTest method assertRewriteForRowNum.

@Test
public void assertRewriteForRowNum() {
    selectStatement.setLimit(new Limit(DatabaseType.Oracle));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(68, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(119, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(98, 4));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=4) t WHERE t.rownum_>0"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) 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) Test(org.junit.Test)

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