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