Search in sources :

Example 16 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule 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 17 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class ConfigurationListenerManager method start.

private void start(final String node, final ShardingDataSource shardingDataSource) {
    String cachePath = configNode.getFullPath(node);
    regCenter.watch(cachePath, new EventListener() {

        @Override
        public void onChange(final DataChangedEvent event) {
            if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
                Map<String, DataSource> dataSourceMap = dataSourceService.getAvailableDataSources();
                shardingDataSource.renew(dataSourceMap, new ShardingRule(dataSourceService.getAvailableShardingRuleConfiguration(), dataSourceMap.keySet()), configService.loadShardingProperties());
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) EventListener(io.shardingjdbc.orchestration.reg.listener.EventListener) Map(java.util.Map)

Example 18 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingMasterSlaveNamespaceTest method getShardingRule.

private ShardingRule getShardingRule() {
    ShardingDataSource shardingDataSource = this.applicationContext.getBean("defaultShardingDataSource", ShardingDataSource.class);
    Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, "shardingContext", true);
    return (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
}
Also used : ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) ShardingRule(io.shardingjdbc.core.rule.ShardingRule)

Example 19 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingMasterSlaveNamespaceTest method assertDefaultShardingDataSource.

@Test
public void assertDefaultShardingDataSource() {
    Map<String, DataSource> dataSourceMap = getDataSourceMap();
    assertNotNull(dataSourceMap.get("randomMasterSlaveDataSource"));
    assertNotNull(dataSourceMap.get("refMasterSlaveDataSource"));
    assertNotNull(dataSourceMap.get("defaultMasterSlaveDataSource"));
    ShardingRule shardingRule = getShardingRule();
    assertThat(shardingRule.getDefaultDataSourceName(), is("defaultMasterSlaveDataSource"));
    assertThat(shardingRule.getTableRules().size(), is(1));
    assertThat(shardingRule.getTableRules().iterator().next().getLogicTable(), is("t_order"));
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 20 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingNamespaceTest method assertMultiBindingTableRulesDatasource.

@Test
public void assertMultiBindingTableRulesDatasource() {
    ShardingRule shardingRule = getShardingRule("multiBindingTableRulesDatasource");
    assertThat(shardingRule.getBindingTableRules().size(), is(2));
    Iterator<BindingTableRule> bindingTableRules = shardingRule.getBindingTableRules().iterator();
    BindingTableRule orderRule = bindingTableRules.next();
    assertThat(orderRule.getBindingActualTable("dbtbl_0", "t_order", "t_order_item"), is("t_order"));
    assertThat(orderRule.getBindingActualTable("dbtbl_1", "t_order", "t_order_item"), is("t_order"));
    BindingTableRule userRule = bindingTableRules.next();
    assertThat(userRule.getBindingActualTable("dbtbl_0", "t_user", "t_user_detail"), is("t_user"));
    assertThat(userRule.getBindingActualTable("dbtbl_1", "t_user", "t_user_detail"), is("t_user"));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Aggregations

ShardingRule (io.shardingjdbc.core.rule.ShardingRule)60 Test (org.junit.Test)35 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)20 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)19 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)16 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)14 DataSource (javax.sql.DataSource)14 BindingTableRule (io.shardingjdbc.core.rule.BindingTableRule)10 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)8 DMLStatement (io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement)8 Map (java.util.Map)8 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)7 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)7 TableRule (io.shardingjdbc.core.rule.TableRule)6 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)5 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)5 HashMap (java.util.HashMap)5 Before (org.junit.Before)5 TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)4 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)4