Search in sources :

Example 6 with HintManager

use of com.dangdang.ddframe.rdb.sharding.api.HintManager in project sharding-jdbc by dangdangdotcom.

the class ShardingMasterSlaveForPStatementWithSelectTest method assertSelectForHintAndForceMaster.

@Test
public void assertSelectForHintAndForceMaster() throws SQLException, DatabaseUnitException {
    HintManager hintManager = HintManager.getInstance();
    hintManager.setMasterRouteOnly();
    hintManager.addDatabaseShardingValue("t_order", "user_id", 10);
    hintManager.addTableShardingValue("t_order", "order_id", 1000);
    String sql = "SELECT `t_order`.order_id, `t_order`.user_id, `t_order`.status FROM `t_order` WHERE `t_order`.`user_id` = ? AND `t_order`.`order_id` = ?";
    assertDataSet("integrate/dataset/masterslave/expect/select/SelectEqualsWithSingleMasterTable_0.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 1000);
}
Also used : HintManager(com.dangdang.ddframe.rdb.sharding.api.HintManager) AbstractShardingMasterSlaveDBUnitTest(com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest) Test(org.junit.Test)

Example 7 with HintManager

use of com.dangdang.ddframe.rdb.sharding.api.HintManager in project sharding-jdbc by dangdangdotcom.

the class DatabaseTest method assertSQL.

@Test
public void assertSQL() {
    try (HintManager hintManager = HintManager.getInstance()) {
        hintManager.setDatabaseShardingValue(1);
        assertTarget("select * from tesT", "ds_1");
        assertTarget("insert into test values (1,2)", "ds_1");
        assertTarget("update test set a = 1", "ds_1");
        assertTarget("delete from test where id = 2", "ds_1");
        hintManager.setDatabaseShardingValue(2);
        assertTarget("select * from tesT", "ds_0");
    }
}
Also used : HintManager(com.dangdang.ddframe.rdb.sharding.api.HintManager) Test(org.junit.Test)

Example 8 with HintManager

use of com.dangdang.ddframe.rdb.sharding.api.HintManager in project sharding-jdbc by dangdangdotcom.

the class Main method printHintSimpleSelect.

private static void printHintSimpleSelect(final DataSource dataSource) throws SQLException {
    String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id";
    try (HintManager hintManager = HintManager.getInstance();
        Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        hintManager.addDatabaseShardingValue("t_order", "user_id", 10);
        hintManager.addTableShardingValue("t_order", "order_id", 1001);
        try (ResultSet rs = preparedStatement.executeQuery()) {
            while (rs.next()) {
                System.out.println(rs.getInt(1));
                System.out.println(rs.getInt(2));
                System.out.println(rs.getInt(3));
            }
        }
    }
}
Also used : HintManager(com.dangdang.ddframe.rdb.sharding.api.HintManager) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

HintManager (com.dangdang.ddframe.rdb.sharding.api.HintManager)8 Test (org.junit.Test)5 AbstractShardingMasterSlaveDBUnitTest (com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)2