Search in sources :

Example 1 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)

Example 2 with HintManager

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

the class MasterSlaveDataSourceTest method assertGetDataSourceForHintToMasterOnly.

@Test
public void assertGetDataSourceForHintToMasterOnly() {
    HintManager hintManager = HintManager.getInstance();
    hintManager.setMasterRouteOnly();
    assertThat(masterSlaveDataSource.getDataSource(SQLStatementType.SELECT), is(masterDataSource));
    hintManager.close();
}
Also used : HintManager(com.dangdang.ddframe.rdb.sharding.api.HintManager) Test(org.junit.Test)

Example 3 with HintManager

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

the class AbstractDynamicRouteSqlTest method assertMultipleTargets.

private void assertMultipleTargets(final List<ShardingValuePair> shardingValuePairs, final String originSql, final List<Object> parameters, final int expectedSize, final Collection<String> targetDataSources, final Collection<String> targetSQLs) throws SQLParserException {
    try (HintManager hintManager = HintManager.getInstance()) {
        for (ShardingValuePair each : shardingValuePairs) {
            hintManager.addDatabaseShardingValue(each.logicTable, "order_id", each.binaryOperator, each.shardingValue);
            hintManager.addTableShardingValue(each.logicTable, "order_id", each.binaryOperator, each.shardingValue);
        }
        assertMultipleTargets(originSql, parameters, expectedSize, targetDataSources, targetSQLs);
    }
}
Also used : HintManager(com.dangdang.ddframe.rdb.sharding.api.HintManager)

Example 4 with HintManager

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

the class ShardingMasterSlaveForPStatementWithDMLTest method assertInsertWithHint.

@Test
public void assertInsertWithHint() throws SQLException, DatabaseUnitException {
    String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
    for (int i = 1; i <= 10; i++) {
        for (int j = 1; j <= 10; j++) {
            try (Connection connection = shardingDataSource.getConnection();
                HintManager hintManager = HintManager.getInstance()) {
                hintManager.addDatabaseShardingValue("t_order", "user_id", j);
                hintManager.addTableShardingValue("t_order", "order_id", i);
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, i);
                preparedStatement.setInt(2, j);
                preparedStatement.setString(3, "insert");
                preparedStatement.executeUpdate();
            }
        }
    }
    try (HintManager hintManager = HintManager.getInstance()) {
        hintManager.setMasterRouteOnly();
        assertDataSet("insert", "insert");
    }
}
Also used : HintManager(com.dangdang.ddframe.rdb.sharding.api.HintManager) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AbstractShardingMasterSlaveDBUnitTest(com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest) Test(org.junit.Test)

Example 5 with HintManager

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

the class ShardingMasterSlaveForPStatementWithSelectTest method assertSelectForHint.

@Test
public void assertSelectForHint() throws SQLException, DatabaseUnitException {
    HintManager hintManager = HintManager.getInstance();
    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/SelectEqualsWithSingleTable_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)

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