Search in sources :

Example 41 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class PreparedStatementExecutorTest method assertExecuteForMultiplePreparedStatementsSuccessWithDML.

@Test
public void assertExecuteForMultiplePreparedStatementsSuccessWithDML() throws SQLException {
    PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper1 = createPreparedStatementExecutorWrapperForDML(preparedStatement1, "ds_0");
    PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper2 = createPreparedStatementExecutorWrapperForDML(preparedStatement2, "ds_1");
    when(preparedStatement1.execute()).thenReturn(false);
    when(preparedStatement2.execute()).thenReturn(false);
    when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
    when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Arrays.asList(wrapper1, wrapper2));
    assertFalse(actual.execute());
    verify(preparedStatement1).execute();
    verify(preparedStatement2).execute();
    verify(preparedStatement1).getConnection();
    verify(preparedStatement2).getConnection();
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifyDataSource("ds_1");
    verify(eventCaller, times(4)).verifySQL("DELETE FROM dual");
    verify(eventCaller, times(4)).verifyParameters(Collections.emptyList());
    verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 42 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class PreparedStatementExecutorTest method assertExecuteQueryForSinglePreparedStatementFailure.

@Test
public void assertExecuteQueryForSinglePreparedStatementFailure() throws SQLException {
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper = createPreparedStatementExecutorWrapperForDQL(preparedStatement, "ds_0");
    SQLException exp = new SQLException();
    when(preparedStatement.executeQuery()).thenThrow(exp);
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Collections.singleton(wrapper));
    assertThat(actual.executeQuery(), is(Collections.singletonList((ResultSet) null)));
    verify(preparedStatement).executeQuery();
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL("SELECT * FROM dual");
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
    verify(eventCaller).verifyException(exp);
}
Also used : SQLException(java.sql.SQLException) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 43 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class ShardingTablesOnlyForPStatementWithDMLTest method assertUpdateWithoutAlias.

@Test
public void assertUpdateWithoutAlias() throws SQLException, DatabaseUnitException {
    ShardingDataSource shardingDataSource = getShardingDataSource();
    String sql = "UPDATE `t_order` SET `status` = ? WHERE `order_id` = ? AND `user_id` = ?";
    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.setString(1, "updated");
                preparedStatement.setInt(2, i * 100 + j);
                preparedStatement.setInt(3, i);
                assertThat(preparedStatement.executeUpdate(), is(1));
            }
        }
    }
    assertDataSet("update", "updated");
}
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 44 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class RoutingDatabaseOnlyWithHintForDMLTest method assertInsertWithoutPlaceholder.

@Test
public void assertInsertWithoutPlaceholder() throws SQLException, DatabaseUnitException {
    String sql = "INSERT INTO `t_order` VALUES (%s, %s, 'insert')";
    for (int i = 1; i <= 10; i++) {
        try (DynamicShardingValueHelper helper = new DynamicDatabaseShardingValueHelper(i);
            Connection connection = shardingDataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(String.format(sql, i, i))) {
            preparedStatement.executeUpdate();
        }
    }
    assertDataSet("insert", "insert");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 45 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class RoutingDatabaseOnlyWithHintForDMLTest method assertInsertWithAllPlaceholders.

@Test
public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitException {
    String sql = "INSERT INTO `t_order` VALUES (?, ?, ?)";
    for (int i = 1; i <= 10; i++) {
        try (DynamicShardingValueHelper helper = new DynamicDatabaseShardingValueHelper(i);
            Connection connection = shardingDataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, i);
            preparedStatement.setInt(2, i);
            preparedStatement.setString(3, "insert");
            preparedStatement.executeUpdate();
        }
    }
    assertDataSet("insert", "insert");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

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