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"));
}
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++;
}
}
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;
}
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=?"));
}
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"));
}
Aggregations