Search in sources :

Example 1 with TablePlaceholder

use of io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder in project sharding-jdbc by shardingjdbc.

the class SQLBuilderTest method assertAppendTableWithoutTableToken.

@Test
public void assertAppendTableWithoutTableToken() {
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals("SELECT ");
    sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x"));
    sqlBuilder.appendLiterals(".id");
    sqlBuilder.appendLiterals(" FROM ");
    sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x"));
    assertThat(sqlBuilder.toSQL(Collections.<String, String>emptyMap(), null), is("SELECT table_x.id FROM table_x"));
}
Also used : TablePlaceholder(io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder) Test(org.junit.Test)

Example 2 with TablePlaceholder

use of io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder in project sharding-jdbc by shardingjdbc.

the class SQLBuilderTest method assertIndexPlaceholderAppendTableWithTableToken.

@Test
public void assertIndexPlaceholderAppendTableWithTableToken() {
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals("CREATE INDEX ");
    sqlBuilder.appendPlaceholder(new IndexPlaceholder("index_name", "table_x"));
    sqlBuilder.appendLiterals(" ON ");
    sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x"));
    sqlBuilder.appendLiterals(" ('column')");
    Map<String, String> tableTokens = new HashMap<>(1, 1);
    tableTokens.put("table_x", "table_x_1");
    assertThat(sqlBuilder.toSQL(tableTokens, null), is("CREATE INDEX index_name_table_x_1 ON table_x_1 ('column')"));
}
Also used : TablePlaceholder(io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder) HashMap(java.util.HashMap) IndexPlaceholder(io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder) Test(org.junit.Test)

Example 3 with TablePlaceholder

use of io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder in project sharding-jdbc by shardingjdbc.

the class SQLBuilderTest method assertIndexPlaceholderAppendTableWithoutTableToken.

@Test
public void assertIndexPlaceholderAppendTableWithoutTableToken() {
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals("CREATE INDEX ");
    sqlBuilder.appendPlaceholder(new IndexPlaceholder("index_name", "table_x"));
    sqlBuilder.appendLiterals(" ON ");
    sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x"));
    sqlBuilder.appendLiterals(" ('column')");
    assertThat(sqlBuilder.toSQL(Collections.<String, String>emptyMap(), null), is("CREATE INDEX index_name ON table_x ('column')"));
}
Also used : TablePlaceholder(io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder) IndexPlaceholder(io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder) Test(org.junit.Test)

Example 4 with TablePlaceholder

use of io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder in project sharding-jdbc by shardingjdbc.

the class SQLBuilderTest method assertSchemaPlaceholderAppendTableWithoutTableToken.

@Test(expected = ShardingJdbcException.class)
public void assertSchemaPlaceholderAppendTableWithoutTableToken() {
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals("SHOW ");
    sqlBuilder.appendLiterals("CREATE TABLE ");
    sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x"));
    sqlBuilder.appendLiterals("ON ");
    sqlBuilder.appendPlaceholder(new SchemaPlaceholder("dx", "table_x"));
    sqlBuilder.toSQL(Collections.<String, String>emptyMap(), createShardingRule());
}
Also used : TablePlaceholder(io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder) SchemaPlaceholder(io.shardingjdbc.core.rewrite.placeholder.SchemaPlaceholder) Test(org.junit.Test)

Example 5 with TablePlaceholder

use of io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder in project sharding-jdbc by shardingjdbc.

the class SQLBuilder method toSQL.

/**
 * Convert to SQL string.
 *
 * @param logicAndActualTableMap logic and actual map
 * @param shardingRule sharding rule
 * @return SQL string
 */
public String toSQL(final Map<String, String> logicAndActualTableMap, final ShardingRule shardingRule) {
    StringBuilder result = new StringBuilder();
    for (Object each : segments) {
        if (!(each instanceof ShardingPlaceholder)) {
            result.append(each);
            continue;
        }
        String logicTableName = ((ShardingPlaceholder) each).getLogicTableName();
        String actualTableName = logicAndActualTableMap.get(logicTableName);
        if (each instanceof TablePlaceholder) {
            result.append(null == actualTableName ? logicTableName : actualTableName);
        } else if (each instanceof SchemaPlaceholder) {
            SchemaPlaceholder schemaPlaceholder = (SchemaPlaceholder) each;
            Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(actualTableName);
            if (!tableRule.isPresent() && Strings.isNullOrEmpty(shardingRule.getDefaultDataSourceName())) {
                throw new ShardingJdbcException("Cannot found schema name '%s' in sharding rule.", schemaPlaceholder.getLogicSchemaName());
            }
            // TODO 目前只能找到真实数据源名称. 未来需要在初始化sharding rule时创建connection,并验证连接是否正确,并获取出真实的schema的名字, 然后在这里替换actualDataSourceName为actualSchemaName
            // TODO 目前actualDataSourceName必须actualSchemaName一样,才能保证替换schema的场景不出错, 如: show columns xxx
            result.append(tableRule.get().getActualDatasourceNames().iterator().next());
        } else if (each instanceof IndexPlaceholder) {
            IndexPlaceholder indexPlaceholder = (IndexPlaceholder) each;
            result.append(indexPlaceholder.getLogicIndexName());
            if (!Strings.isNullOrEmpty(actualTableName)) {
                result.append("_");
                result.append(actualTableName);
            }
        } else {
            result.append(each);
        }
    }
    return result.toString();
}
Also used : TablePlaceholder(io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder) Optional(com.google.common.base.Optional) ShardingPlaceholder(io.shardingjdbc.core.rewrite.placeholder.ShardingPlaceholder) ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) SchemaPlaceholder(io.shardingjdbc.core.rewrite.placeholder.SchemaPlaceholder) IndexPlaceholder(io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder)

Aggregations

TablePlaceholder (io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder)8 Test (org.junit.Test)6 IndexPlaceholder (io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder)3 SchemaPlaceholder (io.shardingjdbc.core.rewrite.placeholder.SchemaPlaceholder)3 HashMap (java.util.HashMap)3 Optional (com.google.common.base.Optional)1 ShardingJdbcException (io.shardingjdbc.core.exception.ShardingJdbcException)1 ShardingPlaceholder (io.shardingjdbc.core.rewrite.placeholder.ShardingPlaceholder)1