use of io.shardingjdbc.core.parsing.parser.token.ItemsToken in project sharding-jdbc by shardingjdbc.
the class SQLRewriteEngineTest method assertRewriteForAutoGeneratedKeyColumn.
@Test
public void assertRewriteForAutoGeneratedKeyColumn() {
selectStatement.getSqlTokens().add(new TableToken(12, "table_x"));
ItemsToken itemsToken1 = new ItemsToken(30);
itemsToken1.getItems().add("id");
ItemsToken itemsToken2 = new ItemsToken(44);
itemsToken2.getItems().add("?");
selectStatement.getSqlTokens().addAll(Arrays.asList(itemsToken1, itemsToken2));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "INSERT INTO table_x (name, age) VALUES (?, ?)", DatabaseType.MySQL, selectStatement);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("INSERT INTO table_1 (name, age, id) VALUES (?, ?, ?)"));
}
use of io.shardingjdbc.core.parsing.parser.token.ItemsToken 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.ItemsToken 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"));
}
use of io.shardingjdbc.core.parsing.parser.token.ItemsToken in project sharding-jdbc by shardingjdbc.
the class SQLRewriteEngineTest method assertRewriteForAggregationDerivedColumns.
@Test
public void assertRewriteForAggregationDerivedColumns() {
selectStatement.getSqlTokens().add(new TableToken(23, "table_x"));
ItemsToken itemsToken = new ItemsToken(17);
itemsToken.getItems().addAll(Arrays.asList("COUNT(x.age) as AVG_DERIVED_COUNT_0", "SUM(x.age) as AVG_DERIVED_SUM_0"));
selectStatement.getSqlTokens().add(itemsToken);
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT AVG(x.age) FROM table_x x", DatabaseType.MySQL, selectStatement);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT AVG(x.age), COUNT(x.age) as AVG_DERIVED_COUNT_0, SUM(x.age) as AVG_DERIVED_SUM_0 FROM table_1 x"));
}
use of io.shardingjdbc.core.parsing.parser.token.ItemsToken in project sharding-jdbc by shardingjdbc.
the class AbstractInsertParser method appendGenerateKey.
private void appendGenerateKey(final InsertStatement insertStatement) {
String tableName = insertStatement.getTables().getSingleTableName();
Optional<String> generateKeyColumn = shardingRule.getGenerateKeyColumn(tableName);
if (!generateKeyColumn.isPresent() || null != insertStatement.getGeneratedKey()) {
return;
}
ItemsToken columnsToken = new ItemsToken(insertStatement.getColumnsListLastPosition());
columnsToken.getItems().add(generateKeyColumn.get());
insertStatement.getSqlTokens().add(columnsToken);
insertStatement.getSqlTokens().add(new GeneratedKeyToken(insertStatement.getValuesListLastPosition()));
}
Aggregations