Search in sources :

Example 6 with TableRule

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()));
}
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 7 with TableRule

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

Example 8 with TableRule

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;
}
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 9 with TableRule

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);
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) LinkedList(java.util.LinkedList)

Example 10 with TableRule

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);
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) GeneratedKeyToken(io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken)

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