Search in sources :

Example 26 with PreparedStatement

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));
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 27 with PreparedStatement

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));
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 28 with PreparedStatement

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));
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 29 with PreparedStatement

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");
}
Also used : ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Example 30 with PreparedStatement

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");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Aggregations

PreparedStatement (java.sql.PreparedStatement)5900 ResultSet (java.sql.ResultSet)3733 SQLException (java.sql.SQLException)3074 Connection (java.sql.Connection)2478 Test (org.junit.Test)1099 ArrayList (java.util.ArrayList)851 Properties (java.util.Properties)742 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)345 DatabaseException (net.jforum.exceptions.DatabaseException)254 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)251 Timestamp (java.sql.Timestamp)248 Statement (java.sql.Statement)243 BigDecimal (java.math.BigDecimal)206 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)174 HashMap (java.util.HashMap)169 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)165 List (java.util.List)136 Date (java.util.Date)124 Date (java.sql.Date)123 BaseTest (org.apache.phoenix.query.BaseTest)114