use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingForNullableWithAggregateTest method assertSelectSum.
@Test
public void assertSelectSum() throws SQLException, DatabaseUnitException {
String sql = "SELECT SUM(`user_id`) FROM `t_order`";
try (Connection conn = shardingDataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
assertThat(rs.next(), is(true));
assertThat(rs.getInt("SUM(`user_id`)"), is(0));
assertThat(rs.getInt(1), is(0));
assertThat(rs.getObject("SUM(`user_id`)"), nullValue());
assertThat(rs.getObject(1), nullValue());
assertThat(rs.next(), is(false));
}
}
use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingForNullableWithAggregateTest method assertSelectMax.
@Test
public void assertSelectMax() throws SQLException, DatabaseUnitException {
String sql = "SELECT MAX(`user_id`) FROM `t_order`";
try (Connection conn = shardingDataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
assertThat(rs.next(), is(true));
assertThat(rs.getInt("MAX(`user_id`)"), is(0));
assertThat(rs.getInt(1), is(0));
assertThat(rs.getObject("MAX(`user_id`)"), nullValue());
assertThat(rs.getObject(1), nullValue());
assertThat(rs.next(), is(false));
}
}
use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingForNullableWithAggregateTest method assertSelectCount.
@Test
public void assertSelectCount() throws SQLException, DatabaseUnitException {
String sql = "SELECT COUNT(`user_id`) FROM `t_order`";
try (Connection conn = shardingDataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
assertThat(rs.next(), is(true));
assertThat(rs.getInt("COUNT(`user_id`)"), is(0));
assertThat(rs.getInt(1), is(0));
assertThat(rs.getObject("COUNT(`user_id`)"), CoreMatchers.<Object>is(new BigDecimal("0")));
assertThat(rs.getObject(1), CoreMatchers.<Object>is(new BigDecimal("0")));
assertThat(rs.next(), is(false));
}
}
use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingTablesOnlyForPStatementWithDMLTest method assertDeleteWithoutAlias.
@Test
public void assertDeleteWithoutAlias() throws SQLException, DatabaseUnitException {
ShardingDataSource shardingDataSource = getShardingDataSource();
String sql = "DELETE `t_order` WHERE `order_id` = ? AND `user_id` = ? AND `status` = ?";
try (Connection connection = shardingDataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
for (int i = 10; i < 12; i++) {
for (int j = 0; j < 10; j++) {
preparedStatement.setInt(1, i * 100 + j);
preparedStatement.setInt(2, i);
preparedStatement.setString(3, "init");
assertThat(preparedStatement.executeUpdate(), is(1));
}
}
}
assertDataSet("delete", "init");
}
use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingTablesOnlyForPStatementWithDMLTest method assertInsertWithPlaceholdersForShardingKeys.
@Test
public void assertInsertWithPlaceholdersForShardingKeys() throws SQLException, DatabaseUnitException {
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, ?)";
for (int i = 1; i <= 10; i++) {
try (Connection connection = shardingDataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(String.format(sql, i, i))) {
preparedStatement.setString(1, "insert");
preparedStatement.executeUpdate();
}
}
assertDataSet("insert", "insert");
}
Aggregations