use of io.shardingjdbc.core.rule.TableRule in project sharding-jdbc by shardingjdbc.
the class ShowCreateTableMergedResult 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 tableName = memoryResultSetRow.getCell(1).toString();
Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(tableName);
if (tableRule.isPresent()) {
String logicTableName = tableRule.get().getLogicTable();
memoryResultSetRow.setCell(1, logicTableName);
String createTableDDL = memoryResultSetRow.getCell(2).toString();
SQLParsingEngine sqlParsingEngine = new SQLParsingEngine(DatabaseType.MySQL, createTableDDL, shardingRule);
String actualTableName = sqlParsingEngine.parse().getTables().getSingleTableName();
if (actualTableName.startsWith("`")) {
logicTableName = "`" + logicTableName + "`";
}
memoryResultSetRow.setCell(2, createTableDDL.replaceFirst(actualTableName, logicTableName));
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 GenerateKeyDBUnitTest method assertGenerateKeyColumn.
@SuppressWarnings("unchecked")
@Test
public void assertGenerateKeyColumn() {
Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, "shardingContext", true);
assertNotNull(shardingContext);
Object shardingRule = FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
assertNotNull(shardingRule);
Object defaultKeyGenerator = FieldValueUtil.getFieldValue(shardingRule, "defaultKeyGenerator");
assertNotNull(defaultKeyGenerator);
assertTrue(defaultKeyGenerator instanceof IncrementKeyGenerator);
Object tableRules = FieldValueUtil.getFieldValue(shardingRule, "tableRules");
assertNotNull(tableRules);
assertThat(((Collection<TableRule>) tableRules).size(), is(2));
Iterator<TableRule> tableRuleIterator = ((Collection<TableRule>) tableRules).iterator();
TableRule orderRule = tableRuleIterator.next();
assertThat(orderRule.getGenerateKeyColumn(), is("order_id"));
TableRule orderItemRule = tableRuleIterator.next();
assertThat(orderItemRule.getGenerateKeyColumn(), is("order_item_id"));
assertTrue(orderItemRule.getKeyGenerator() instanceof DecrementKeyGenerator);
}
use of io.shardingjdbc.core.rule.TableRule in project sharding-jdbc by shardingjdbc.
the class ShardingNamespaceTest 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("io.shardingjdbc.spring.fixture.IncrementKeyGenerator"));
}
use of io.shardingjdbc.core.rule.TableRule in project sharding-jdbc by shardingjdbc.
the class OrchestrationShardingNamespaceTest method assertDefaultActualDataNodes.
@Test
public void assertDefaultActualDataNodes() {
ShardingDataSource multiTableRulesDataSource = this.applicationContext.getBean("multiTableRulesDataSource", ShardingDataSource.class);
Object shardingContext = FieldValueUtil.getFieldValue(multiTableRulesDataSource, "shardingContext", true);
ShardingRule shardingRule = (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
assertThat(shardingRule.getTableRules().size(), is(2));
Iterator<TableRule> tableRules = shardingRule.getTableRules().iterator();
TableRule orderRule = tableRules.next();
assertThat(orderRule.getActualDataNodes().size(), is(2));
assertTrue(orderRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order")));
assertTrue(orderRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order")));
TableRule orderItemRule = tableRules.next();
assertThat(orderItemRule.getActualDataNodes().size(), is(2));
assertTrue(orderItemRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_item")));
assertTrue(orderItemRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_item")));
}
use of io.shardingjdbc.core.rule.TableRule in project sharding-jdbc by shardingjdbc.
the class OrchestrationShardingNamespaceTest method assertMultiTableRulesDataSource.
@Test
public void assertMultiTableRulesDataSource() {
ShardingRule shardingRule = getShardingRule("multiTableRulesDataSource");
assertThat(shardingRule.getTableRules().size(), is(2));
Iterator<TableRule> tableRules = shardingRule.getTableRules().iterator();
assertThat(tableRules.next().getLogicTable(), is("t_order"));
assertThat(tableRules.next().getLogicTable(), is("t_order_item"));
}
Aggregations