Search in sources :

Example 1 with OrderByToken

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

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

the class SQLRewriteEngineTest method assertRewriteForDerivedOrderBy.

@Test
public void assertRewriteForDerivedOrderBy() {
    selectStatement.setGroupByLastPosition(61);
    selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC, Optional.<String>absent()));
    selectStatement.getOrderByItems().add(new OrderItem("x", "name", OrderDirection.DESC, OrderDirection.ASC, Optional.<String>absent()));
    selectStatement.getSqlTokens().add(new TableToken(25, "table_x"));
    selectStatement.getSqlTokens().add(new OrderByToken(61));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id, x.name FROM table_x x GROUP BY x.id, x.name DESC", DatabaseType.MySQL, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT x.id, x.name FROM table_1 x GROUP BY x.id, x.name DESC ORDER BY id ASC,name DESC "));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) OrderByToken(io.shardingjdbc.core.parsing.parser.token.OrderByToken) Test(org.junit.Test)

Example 3 with OrderByToken

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

the class AbstractSelectParser method appendDerivedOrderBy.

private void appendDerivedOrderBy(final SelectStatement selectStatement) {
    if (!selectStatement.getGroupByItems().isEmpty() && selectStatement.getOrderByItems().isEmpty()) {
        selectStatement.getOrderByItems().addAll(selectStatement.getGroupByItems());
        selectStatement.getSqlTokens().add(new OrderByToken(selectStatement.getGroupByLastPosition()));
    }
}
Also used : OrderByToken(io.shardingjdbc.core.parsing.parser.token.OrderByToken)

Aggregations

OrderByToken (io.shardingjdbc.core.parsing.parser.token.OrderByToken)3 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)2 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)1 IndexToken (io.shardingjdbc.core.parsing.parser.token.IndexToken)1 ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)1 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)1 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)1 SQLToken (io.shardingjdbc.core.parsing.parser.token.SQLToken)1 SchemaToken (io.shardingjdbc.core.parsing.parser.token.SchemaToken)1 Test (org.junit.Test)1