Search in sources :

Example 1 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken 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 2 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken 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 3 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken 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 4 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken 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 5 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken 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)

Aggregations

OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)15 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)13 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)13 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)12 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)10 Test (org.junit.Test)9 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)3 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)3 SQLToken (io.shardingjdbc.core.parsing.parser.token.SQLToken)2 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)1 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)1 SQLPlaceholderExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)1 IndexToken (io.shardingjdbc.core.parsing.parser.token.IndexToken)1 ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)1 OrderByToken (io.shardingjdbc.core.parsing.parser.token.OrderByToken)1 SchemaToken (io.shardingjdbc.core.parsing.parser.token.SchemaToken)1 LinkedList (java.util.LinkedList)1