use of io.shardingjdbc.core.rule.TableRule in project sharding-jdbc by shardingjdbc.
the class OrchestrationShardingNamespaceTest method assertTableRuleWithAttributesDataSource.
@Test
public void assertTableRuleWithAttributesDataSource() {
ShardingRule shardingRule = getShardingRule("tableRuleWithAttributesDataSource");
assertThat(shardingRule.getTableRules().size(), is(1));
TableRule tableRule = shardingRule.getTableRules().iterator().next();
assertThat(tableRule.getLogicTable(), is("t_order"));
assertThat(tableRule.getActualDataNodes().size(), is(8));
assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_0")));
assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_1")));
assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_2")));
assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_3")));
assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_0")));
assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_1")));
assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_2")));
assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_3")));
assertTrue(Arrays.equals(tableRule.getDatabaseShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("standardStrategy", StandardShardingStrategyConfiguration.class).getShardingColumn() }));
assertTrue(Arrays.equals(tableRule.getTableShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("inlineStrategy", InlineShardingStrategyConfiguration.class).getShardingColumn() }));
assertThat(tableRule.getGenerateKeyColumn(), is("order_id"));
assertThat(tableRule.getKeyGenerator().getClass().getName(), is(IncrementKeyGenerator.class.getCanonicalName()));
}
use of io.shardingjdbc.core.rule.TableRule in project sharding-jdbc by shardingjdbc.
the class ShowTablesMergedResult method init.
private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults) throws SQLException {
List<MemoryQueryResultRow> result = new LinkedList<>();
for (QueryResult each : queryResults) {
while (each.next()) {
MemoryQueryResultRow memoryResultSetRow = new MemoryQueryResultRow(each);
String actualTableName = memoryResultSetRow.getCell(1).toString();
Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(actualTableName);
if (!tableRule.isPresent()) {
result.add(memoryResultSetRow);
} else if (tableNames.add(tableRule.get().getLogicTable())) {
memoryResultSetRow.setCell(1, tableRule.get().getLogicTable());
result.add(memoryResultSetRow);
}
}
}
if (!result.isEmpty()) {
setCurrentResultSetRow(result.get(0));
}
return result.iterator();
}
use of io.shardingjdbc.core.rule.TableRule 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.rule.TableRule in project sharding-jdbc by shardingjdbc.
the class StandardRoutingEngine method route.
@Override
public RoutingResult route() {
TableRule tableRule = shardingRule.getTableRule(logicTableName);
List<ShardingValue> databaseShardingValues = getDatabaseShardingValues(tableRule);
List<ShardingValue> tableShardingValues = getTableShardingValues(tableRule);
Collection<String> routedDataSources = routeDataSources(tableRule, databaseShardingValues);
Collection<DataNode> routedDataNodes = new LinkedList<>();
for (String each : routedDataSources) {
routedDataNodes.addAll(routeTables(tableRule, each, tableShardingValues));
}
return generateRoutingResult(routedDataNodes);
}
use of io.shardingjdbc.core.rule.TableRule in project sharding-jdbc by shardingjdbc.
the class InsertStatement method appendGenerateKeyToken.
/**
* Append generate key token.
*
* @param shardingRule databases and tables sharding rule
*/
public void appendGenerateKeyToken(final ShardingRule shardingRule) {
if (null != generatedKey) {
return;
}
Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByLogicTable(getTables().getSingleTableName());
if (!tableRule.isPresent()) {
return;
}
Optional<GeneratedKeyToken> generatedKeysToken = findGeneratedKeyToken();
if (!generatedKeysToken.isPresent()) {
return;
}
ItemsToken valuesToken = new ItemsToken(generatedKeysToken.get().getBeginPosition());
appendGenerateKeyToken(shardingRule, tableRule.get(), valuesToken);
getSqlTokens().remove(generatedKeysToken.get());
getSqlTokens().add(valuesToken);
}
Aggregations