Search in sources :

Example 16 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource 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 17 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class AbstractSQLTest method clear.

@AfterClass
public static void clear() throws SQLException, ReflectiveOperationException {
    if (!shardingDataSources.isEmpty()) {
        for (ShardingDataSource each : shardingDataSources.values()) {
            each.close();
            closeDataSources(getDataSourceMap(each).values());
        }
        shardingDataSources.clear();
    }
}
Also used : ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) AfterClass(org.junit.AfterClass)

Example 18 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class AbstractHintTest method initShardingDataSources.

protected final Map<DatabaseType, ShardingDataSource> initShardingDataSources() throws SQLException {
    Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
    for (Map.Entry<DatabaseType, Map<String, DataSource>> each : dataSourceMap.entrySet()) {
        ShardingRule shardingRule = getShardingRule(each);
        getShardingDataSources().put(each.getKey(), new ShardingDataSource(each.getValue(), shardingRule));
    }
    return getShardingDataSources();
}
Also used : DatabaseType(io.shardingjdbc.core.constant.DatabaseType) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Map(java.util.Map)

Example 19 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class RoutingDatabaseOnlyWithHintForDMLTest method assertInsertWithPartialPlaceholders.

@Test
public void assertInsertWithPartialPlaceholders() throws SQLException, DatabaseUnitException {
    for (Map.Entry<DatabaseType, ShardingDataSource> each : shardingDataSources.entrySet()) {
        for (int i = 1; i <= 10; i++) {
            try (HintShardingValueHelper helper = new HintDatabaseShardingValueHelper(i);
                Connection connection = each.getValue().getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(String.format(DatabaseTestSQL.INSERT_WITH_PARTIAL_PLACEHOLDERS_SQL, i, i))) {
                preparedStatement.setString(1, "insert");
                preparedStatement.executeUpdate();
            }
        }
        assertDataSet(each.getValue().getConnection(), each.getKey(), "insert", "insert");
    }
}
Also used : HintShardingValueHelper(io.shardingjdbc.core.integrate.type.sharding.hint.helper.HintShardingValueHelper) DatabaseType(io.shardingjdbc.core.constant.DatabaseType) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) Connection(java.sql.Connection) ShardingConnection(io.shardingjdbc.core.jdbc.core.connection.ShardingConnection) PreparedStatement(java.sql.PreparedStatement) HintDatabaseShardingValueHelper(io.shardingjdbc.core.integrate.type.sharding.hint.helper.HintDatabaseShardingValueHelper) Map(java.util.Map) AbstractRoutingDatabaseOnlyWithHintTest(io.shardingjdbc.core.integrate.type.sharding.hint.base.AbstractRoutingDatabaseOnlyWithHintTest) Test(org.junit.Test)

Example 20 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class ShardingDatabaseOnlyWithHintForDMLTest method assertInsertWithPartialPlaceholders.

@Test
public void assertInsertWithPartialPlaceholders() throws SQLException, DatabaseUnitException {
    for (Map.Entry<DatabaseType, ShardingDataSource> each : shardingDataSources.entrySet()) {
        for (int i = 1; i <= 10; i++) {
            try (HintShardingValueHelper helper = new HintShardingValueHelper(i, i);
                Connection connection = each.getValue().getConnection()) {
                PreparedStatement preparedStatement = connection.prepareStatement(String.format(DatabaseTestSQL.INSERT_WITH_PARTIAL_PLACEHOLDERS_SQL, i, i));
                preparedStatement.setString(1, "insert");
                preparedStatement.executeUpdate();
            }
        }
        assertDataSet(each.getValue().getConnection(), each.getKey(), "insert", "insert");
    }
}
Also used : HintShardingValueHelper(io.shardingjdbc.core.integrate.type.sharding.hint.helper.HintShardingValueHelper) DatabaseType(io.shardingjdbc.core.constant.DatabaseType) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) Connection(java.sql.Connection) ShardingConnection(io.shardingjdbc.core.jdbc.core.connection.ShardingConnection) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) Test(org.junit.Test) AbstractShardingDatabaseOnlyWithHintTest(io.shardingjdbc.core.integrate.type.sharding.hint.base.AbstractShardingDatabaseOnlyWithHintTest)

Aggregations

ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)32 Map (java.util.Map)20 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)16 Test (org.junit.Test)15 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)14 HintShardingValueHelper (io.shardingjdbc.core.integrate.type.sharding.hint.helper.HintShardingValueHelper)8 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)7 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)7 AbstractShardingDatabaseOnlyWithHintTest (io.shardingjdbc.core.integrate.type.sharding.hint.base.AbstractShardingDatabaseOnlyWithHintTest)6 HashMap (java.util.HashMap)6 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)5 ShardingConnection (io.shardingjdbc.core.jdbc.core.connection.ShardingConnection)5 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)4 ShardingProperties (io.shardingjdbc.core.constant.ShardingProperties)4 SpringShardingDataSource (io.shardingjdbc.spring.datasource.SpringShardingDataSource)4 LinkedList (java.util.LinkedList)4 DataSource (javax.sql.DataSource)4 AbstractRoutingDatabaseOnlyWithHintTest (io.shardingjdbc.core.integrate.type.sharding.hint.base.AbstractRoutingDatabaseOnlyWithHintTest)3