Search in sources :

Example 21 with ShardingRule

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

the class OrchestrationShardingNamespaceTest method assertShardingRuleWithAttributesDataSource.

@Test
public void assertShardingRuleWithAttributesDataSource() {
    Map<String, DataSource> dataSourceMap = getDataSourceMap("shardingRuleWithAttributesDataSource");
    ShardingRule shardingRule = getShardingRule("shardingRuleWithAttributesDataSource");
    assertNotNull(dataSourceMap.get("dbtbl_0"));
    assertNotNull(dataSourceMap.get("dbtbl_1"));
    assertThat(shardingRule.getDefaultDataSourceName(), is("dbtbl_0"));
    assertTrue(Arrays.equals(shardingRule.getDefaultDatabaseShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("standardStrategy", StandardShardingStrategyConfiguration.class).getShardingColumn() }));
    assertTrue(Arrays.equals(shardingRule.getDefaultTableShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("inlineStrategy", InlineShardingStrategyConfiguration.class).getShardingColumn() }));
    assertThat(shardingRule.getDefaultKeyGenerator().getClass().getName(), is(IncrementKeyGenerator.class.getCanonicalName()));
}
Also used : StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) DataSource(javax.sql.DataSource) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) Test(org.junit.Test)

Example 22 with ShardingRule

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

the class OrchestrationShardingNamespaceTest method getShardingRule.

private ShardingRule getShardingRule(final String shardingDataSourceName) {
    ShardingDataSource shardingDataSource = this.applicationContext.getBean(shardingDataSourceName, 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 23 with ShardingRule

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

the class OrchestrationShardingNamespaceTest method assertBindingTableRuleDatasource.

@Test
public void assertBindingTableRuleDatasource() {
    ShardingRule shardingRule = getShardingRule("bindingTableRuleDatasource");
    assertThat(shardingRule.getBindingTableRules().size(), is(1));
    BindingTableRule bindingTableRule = shardingRule.getBindingTableRules().iterator().next();
    assertThat(bindingTableRule.getBindingActualTable("dbtbl_0", "t_order", "t_order_item"), is("t_order"));
    assertThat(bindingTableRule.getBindingActualTable("dbtbl_1", "t_order", "t_order_item"), is("t_order"));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Example 24 with ShardingRule

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

the class ShardingConnectionTest method setUp.

@Before
public void setUp() {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("test");
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
    dataSourceMap.put(DS_NAME, masterSlaveDataSource);
    ShardingContext shardingContext = new ShardingContext(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), null, null, false);
    connection = new ShardingConnection(shardingContext);
}
Also used : ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) HashMap(java.util.HashMap) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext) Before(org.junit.Before)

Example 25 with ShardingRule

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

the class ShardingDataSourceTest method assertRenewWithDatabaseTypeChanged.

// TODO to be discuss
// @Test(expected = IllegalStateException.class)
@Test
public void assertRenewWithDatabaseTypeChanged() throws SQLException {
    DataSource originalDataSource = mockDataSource("H2");
    Map<String, DataSource> originalDataSourceMap = new HashMap<>(1, 1);
    originalDataSourceMap.put("ds", originalDataSource);
    ShardingDataSource shardingDataSource = createShardingDataSource(originalDataSourceMap);
    DataSource newDataSource = mockDataSource("MySQL");
    Map<String, DataSource> newDataSourceMap = new HashMap<>(1, 1);
    newDataSourceMap.put("ds", newDataSource);
    shardingDataSource.renew(newDataSourceMap, new ShardingRule(createShardingRuleConfig(newDataSourceMap), newDataSourceMap.keySet()), new Properties());
}
Also used : HashMap(java.util.HashMap) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Properties(java.util.Properties) DataSource(javax.sql.DataSource) 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