Search in sources :

Example 1 with TableUnit

use of io.shardingjdbc.core.routing.type.TableUnit in project sharding-jdbc by shardingjdbc.

the class DatabaseHintSQLRouter method route.

@Override
public // TODO insert SQL need parse gen key
SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
    SQLRouteResult result = new SQLRouteResult(sqlStatement);
    RoutingResult routingResult = new DatabaseHintRoutingEngine(shardingRule.getDataSourceNames(), (HintShardingStrategy) shardingRule.getDefaultDatabaseShardingStrategy()).route();
    for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
        result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), logicSQL));
    }
    if (showSQL) {
        SQLLogger.logSQL(logicSQL, sqlStatement, result.getExecutionUnits(), parameters);
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) DatabaseHintRoutingEngine(io.shardingjdbc.core.routing.type.hint.DatabaseHintRoutingEngine) SQLRouteResult(io.shardingjdbc.core.routing.SQLRouteResult) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) TableUnit(io.shardingjdbc.core.routing.type.TableUnit) HintShardingStrategy(io.shardingjdbc.core.routing.strategy.hint.HintShardingStrategy)

Example 2 with TableUnit

use of io.shardingjdbc.core.routing.type.TableUnit in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngine method getTableTokens.

private Map<String, String> getTableTokens(final CartesianTableReference cartesianTableReference) {
    Map<String, String> result = new HashMap<>();
    for (TableUnit each : cartesianTableReference.getTableUnits()) {
        String logicTableName = each.getLogicTableName().toLowerCase();
        result.put(logicTableName, each.getActualTableName());
        Optional<BindingTableRule> bindingTableRule = shardingRule.findBindingTableRule(logicTableName);
        if (bindingTableRule.isPresent()) {
            result.putAll(getBindingTableTokens(each, bindingTableRule.get()));
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableUnit(io.shardingjdbc.core.routing.type.TableUnit)

Example 3 with TableUnit

use of io.shardingjdbc.core.routing.type.TableUnit in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertGenerateSQLForCartesian.

@Test
public void assertGenerateSQLForCartesian() {
    selectStatement.getSqlTokens().add(new TableToken(7, "table_x"));
    selectStatement.getSqlTokens().add(new TableToken(31, "table_x"));
    selectStatement.getSqlTokens().add(new TableToken(47, "table_x"));
    SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_x.id, x.name FROM table_x x WHERE table_x.id=? AND x.name=?", DatabaseType.MySQL, selectStatement);
    SQLBuilder sqlBuilder = sqlRewriteEngine.rewrite(true);
    CartesianTableReference cartesianTableReference = new CartesianTableReference(Collections.singletonList(new TableUnit("db0", "table_x", "table_x")));
    assertThat(sqlRewriteEngine.generateSQL(cartesianTableReference, sqlBuilder), is("SELECT table_x.id, x.name FROM table_x x WHERE table_x.id=? AND x.name=?"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) CartesianTableReference(io.shardingjdbc.core.routing.type.complex.CartesianTableReference) TableUnit(io.shardingjdbc.core.routing.type.TableUnit) Test(org.junit.Test)

Example 4 with TableUnit

use of io.shardingjdbc.core.routing.type.TableUnit in project sharding-jdbc by shardingjdbc.

the class TableBroadcastRoutingEngine method getAllTableUnits.

private Collection<TableUnit> getAllTableUnits(final String logicTableName) {
    Collection<TableUnit> result = new LinkedList<>();
    TableRule tableRule = shardingRule.getTableRule(logicTableName);
    for (DataNode each : tableRule.getActualDataNodes()) {
        result.add(new TableUnit(each.getDataSourceName(), logicTableName, each.getTableName()));
    }
    return result;
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) DataNode(io.shardingjdbc.core.rule.DataNode) TableUnit(io.shardingjdbc.core.routing.type.TableUnit) LinkedList(java.util.LinkedList)

Example 5 with TableUnit

use of io.shardingjdbc.core.routing.type.TableUnit in project sharding-jdbc by shardingjdbc.

the class DatabaseHintRoutingEngine method route.

@Override
public RoutingResult route() {
    Optional<ShardingValue> shardingValue = HintManagerHolder.getDatabaseShardingValue(new ShardingKey(HintManagerHolder.DB_TABLE_NAME, HintManagerHolder.DB_COLUMN_NAME));
    Preconditions.checkState(shardingValue.isPresent());
    log.debug("Before database sharding only db:{} sharding values: {}", dataSourceNames, shardingValue.get());
    Collection<String> routingDataSources;
    routingDataSources = databaseShardingStrategy.doSharding(dataSourceNames, Collections.singletonList(shardingValue.get()));
    Preconditions.checkState(!routingDataSources.isEmpty(), "no database route info");
    log.debug("After database sharding only result: {}", routingDataSources);
    RoutingResult result = new RoutingResult();
    for (String each : routingDataSources) {
        result.getTableUnits().getTableUnits().add(new TableUnit(each, "", ""));
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) ShardingKey(io.shardingjdbc.core.hint.ShardingKey) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) TableUnit(io.shardingjdbc.core.routing.type.TableUnit)

Aggregations

TableUnit (io.shardingjdbc.core.routing.type.TableUnit)7 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)3 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)2 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)2 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)2 CartesianTableReference (io.shardingjdbc.core.routing.type.complex.CartesianTableReference)2 Test (org.junit.Test)2 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 ShardingKey (io.shardingjdbc.core.hint.ShardingKey)1 Table (io.shardingjdbc.core.parsing.parser.context.table.Table)1 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)1 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)1 SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)1 SQLRewriteEngine (io.shardingjdbc.core.rewrite.SQLRewriteEngine)1 HintShardingStrategy (io.shardingjdbc.core.routing.strategy.hint.HintShardingStrategy)1 CartesianDataSource (io.shardingjdbc.core.routing.type.complex.CartesianDataSource)1 CartesianRoutingResult (io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult)1 DatabaseHintRoutingEngine (io.shardingjdbc.core.routing.type.hint.DatabaseHintRoutingEngine)1 BindingTableRule (io.shardingjdbc.core.rule.BindingTableRule)1 DataNode (io.shardingjdbc.core.rule.DataNode)1