Search in sources :

Example 11 with TableToken

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

the class SQLRewriteEngineTest method assertRewriteForTopAndRowNumberForMemoryGroupBy.

@Test
public void assertRewriteForTopAndRowNumberForMemoryGroupBy() {
    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));
    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 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(true).toSQL(tableTokens, null), is("SELECT * FROM (SELECT TOP(2147483647) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.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 12 with TableToken

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

the class TableTokenAssert method assertTableTokens.

void assertTableTokens(final List<SQLToken> actual, final ExpectedTokens expected) {
    List<TableToken> tableTokens = getTableTokens(actual);
    assertThat(assertMessage.getFullAssertMessage("Table tokens size error: "), tableTokens.size(), is(expected.getTableTokens().size()));
    int count = 0;
    for (ExpectedTableToken each : expected.getTableTokens()) {
        assertTableToken(tableTokens.get(count), each);
        count++;
    }
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) ExpectedTableToken(io.shardingjdbc.core.parsing.integrate.jaxb.token.ExpectedTableToken) ExpectedTableToken(io.shardingjdbc.core.parsing.integrate.jaxb.token.ExpectedTableToken)

Example 13 with TableToken

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

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

the class SQLRewriteEngineTest method assertGenerateSQL.

@Test
public void assertGenerateSQL() {
    selectStatement.getSqlTokens().add(new TableToken(7, "table_x"));
    selectStatement.getSqlTokens().add(new TableToken(31, "table_x"));
    selectStatement.getSqlTokens().add(new TableToken(58, "table_x"));
    selectStatement.getTables().add(new Table("table_x", Optional.of("x")));
    selectStatement.getTables().add(new Table("table_y", Optional.of("y")));
    SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?", DatabaseType.MySQL, selectStatement);
    SQLBuilder sqlBuilder = sqlRewriteEngine.rewrite(true);
    assertThat(sqlRewriteEngine.generateSQL(new TableUnit("db0", "table_x", "table_x"), sqlBuilder), is("SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) Table(io.shardingjdbc.core.parsing.parser.context.table.Table) TableUnit(io.shardingjdbc.core.routing.type.TableUnit) Test(org.junit.Test)

Example 15 with TableToken

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

the class SQLRewriteEngineTest method assertRewriteForOrderByAndGroupByDerivedColumns.

@Test
public void assertRewriteForOrderByAndGroupByDerivedColumns() {
    selectStatement.getSqlTokens().add(new TableToken(18, "table_x"));
    ItemsToken itemsToken = new ItemsToken(12);
    itemsToken.getItems().addAll(Arrays.asList("x.id as ORDER_BY_DERIVED_0", "x.name as GROUP_BY_DERIVED_0"));
    selectStatement.getSqlTokens().add(itemsToken);
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.age FROM table_x x GROUP BY x.id ORDER BY x.name", DatabaseType.MySQL, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT x.age, x.id as ORDER_BY_DERIVED_0, x.name as GROUP_BY_DERIVED_0 FROM table_1 x GROUP BY x.id ORDER BY x.name"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken) Test(org.junit.Test)

Aggregations

TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)21 Test (org.junit.Test)16 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)10 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)10 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)9 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)9 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)4 ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)4 Table (io.shardingjdbc.core.parsing.parser.context.table.Table)3 OrderByToken (io.shardingjdbc.core.parsing.parser.token.OrderByToken)2 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)2 ExpectedTableToken (io.shardingjdbc.core.parsing.integrate.jaxb.token.ExpectedTableToken)1 IndexToken (io.shardingjdbc.core.parsing.parser.token.IndexToken)1 SQLToken (io.shardingjdbc.core.parsing.parser.token.SQLToken)1 SchemaToken (io.shardingjdbc.core.parsing.parser.token.SchemaToken)1 CartesianTableReference (io.shardingjdbc.core.routing.type.complex.CartesianTableReference)1