Search in sources :

Example 1 with SQLToken

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

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

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

Aggregations

SQLToken (io.shardingjdbc.core.parsing.parser.token.SQLToken)3 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)2 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)2 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)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 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)1 LinkedList (java.util.LinkedList)1