Search in sources :

Example 11 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngine method rewrite.

/**
 * rewrite SQL.
 *
 * @param isRewriteLimit is rewrite limit
 * @return SQL builder
 */
public SQLBuilder rewrite(final boolean isRewriteLimit) {
    SQLBuilder result = new SQLBuilder();
    if (sqlTokens.isEmpty()) {
        result.appendLiterals(originalSQL);
        return result;
    }
    int count = 0;
    sortByBeginPosition();
    for (SQLToken each : sqlTokens) {
        if (0 == count) {
            result.appendLiterals(originalSQL.substring(0, each.getBeginPosition()));
        }
        if (each instanceof TableToken) {
            appendTablePlaceholder(result, (TableToken) each, count, sqlTokens);
        } else if (each instanceof SchemaToken) {
            appendSchemaPlaceholder(result, (SchemaToken) each, count, sqlTokens);
        } else if (each instanceof IndexToken) {
            appendIndexPlaceholder(result, (IndexToken) each, count, sqlTokens);
        } else if (each instanceof ItemsToken) {
            appendItemsToken(result, (ItemsToken) each, count, sqlTokens);
        } else if (each instanceof RowCountToken) {
            appendLimitRowCount(result, (RowCountToken) each, count, sqlTokens, isRewriteLimit);
        } else if (each instanceof OffsetToken) {
            appendLimitOffsetToken(result, (OffsetToken) each, count, sqlTokens, isRewriteLimit);
        } else if (each instanceof OrderByToken) {
            appendOrderByToken(result, count, sqlTokens);
        }
        count++;
    }
    return result;
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) SQLToken(io.shardingjdbc.core.parsing.parser.token.SQLToken) SchemaToken(io.shardingjdbc.core.parsing.parser.token.SchemaToken) IndexToken(io.shardingjdbc.core.parsing.parser.token.IndexToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) OrderByToken(io.shardingjdbc.core.parsing.parser.token.OrderByToken)

Example 12 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertRewriteForLimitForMemoryGroupBy.

@Test
public void assertRewriteForLimitForMemoryGroupBy() {
    selectStatement.setLimit(new Limit(DatabaseType.MySQL));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false));
    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()));
    selectStatement.getSqlTokens().add(new TableToken(17, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(33, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(36, 2));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT x.id FROM table_1 x LIMIT 0, 2147483647"));
}
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 13 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertRewriteForRowNumForNotRewriteLimit.

@Test
public void assertRewriteForRowNumForNotRewriteLimit() {
    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(false).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_>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 14 with OffsetToken

use of io.shardingjdbc.core.parsing.parser.token.OffsetToken in project sharding-jdbc by shardingjdbc.

the class SelectStatement method resetLimitTokens.

private void resetLimitTokens(final SelectStatement selectStatement, final List<SQLToken> limitSQLTokens) {
    int count = 0;
    List<Integer> toBeRemovedIndexes = new LinkedList<>();
    for (SQLToken each : selectStatement.getSqlTokens()) {
        if (each instanceof RowCountToken || each instanceof OffsetToken) {
            toBeRemovedIndexes.add(count);
        }
        count++;
    }
    for (int each : toBeRemovedIndexes) {
        selectStatement.getSqlTokens().remove(each);
    }
    selectStatement.getSqlTokens().addAll(limitSQLTokens);
}
Also used : SQLToken(io.shardingjdbc.core.parsing.parser.token.SQLToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LinkedList(java.util.LinkedList)

Example 15 with OffsetToken

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

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