Search in sources :

Example 1 with ItemsToken

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 (?, ?, ?)"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken) Test(org.junit.Test)

Example 2 with ItemsToken

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;
}
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 3 with ItemsToken

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"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken) Test(org.junit.Test)

Example 4 with ItemsToken

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"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken) Test(org.junit.Test)

Example 5 with ItemsToken

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()));
}
Also used : GeneratedKeyToken(io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken)

Aggregations

ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)7 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)4 Test (org.junit.Test)3 GeneratedKeyToken (io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken)2 IndexToken (io.shardingjdbc.core.parsing.parser.token.IndexToken)1 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)1 OrderByToken (io.shardingjdbc.core.parsing.parser.token.OrderByToken)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 TableRule (io.shardingjdbc.core.rule.TableRule)1