Search in sources :

Example 1 with TableRule

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();
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) QueryResult(io.shardingjdbc.core.merger.QueryResult) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) LinkedList(java.util.LinkedList)

Example 2 with TableRule

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);
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) Collection(java.util.Collection) IncrementKeyGenerator(io.shardingjdbc.spring.fixture.IncrementKeyGenerator) DecrementKeyGenerator(io.shardingjdbc.spring.fixture.DecrementKeyGenerator) Test(org.junit.Test)

Example 3 with TableRule

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"));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) Test(org.junit.Test)

Example 4 with TableRule

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")));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Example 5 with TableRule

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"));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Aggregations

TableRule (io.shardingjdbc.core.rule.TableRule)12 Test (org.junit.Test)7 BindingTableRule (io.shardingjdbc.core.rule.BindingTableRule)6 DataNode (io.shardingjdbc.core.rule.DataNode)6 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)6 LinkedList (java.util.LinkedList)4 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)2 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)2 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)2 QueryResult (io.shardingjdbc.core.merger.QueryResult)2 MemoryQueryResultRow (io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow)2 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)1 GeneratedKeyToken (io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken)1 ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)1 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)1 SpringShardingDataSource (io.shardingjdbc.spring.datasource.SpringShardingDataSource)1 DecrementKeyGenerator (io.shardingjdbc.spring.fixture.DecrementKeyGenerator)1 IncrementKeyGenerator (io.shardingjdbc.spring.fixture.IncrementKeyGenerator)1 Collection (java.util.Collection)1