Search in sources :

Example 36 with PreparedStatement

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

the class PreparedStatementExecutorTest method assertExecuteForMultiplePreparedStatements.

@Test
public void assertExecuteForMultiplePreparedStatements() throws SQLException {
    PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper1 = createPreparedStatementExecutorWrapperForDQL(preparedStatement1, "ds_0");
    PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper2 = createPreparedStatementExecutorWrapperForDQL(preparedStatement2, "ds_0");
    when(preparedStatement1.execute()).thenReturn(true);
    when(preparedStatement2.execute()).thenReturn(true);
    when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
    when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Arrays.asList(wrapper1, wrapper2));
    assertTrue(actual.execute());
    verify(preparedStatement1).execute();
    verify(preparedStatement2).execute();
    verify(preparedStatement1).getConnection();
    verify(preparedStatement2).getConnection();
    verify(eventCaller, times(4)).verifyDataSource("ds_0");
    verify(eventCaller, times(4)).verifySQL("SELECT * 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 37 with PreparedStatement

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

the class PreparedStatementExecutorTest method assertExecuteForSinglePreparedStatementSuccessWithDML.

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

Example 38 with PreparedStatement

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

the class PreparedStatementExecutorTest method assertExecuteQueryForSinglePreparedStatementSuccess.

@Test
public void assertExecuteQueryForSinglePreparedStatementSuccess() throws SQLException {
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper = createPreparedStatementExecutorWrapperForDQL(preparedStatement, "ds_0");
    ResultSet resultSet = mock(ResultSet.class);
    when(preparedStatement.executeQuery()).thenReturn(resultSet);
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Collections.singleton(wrapper));
    assertThat(actual.executeQuery(), is(Collections.singletonList(resultSet)));
    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_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 39 with PreparedStatement

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

the class PreparedStatementExecutorTest method assertExecuteQueryForMultiplePreparedStatementsSuccess.

@Test
public void assertExecuteQueryForMultiplePreparedStatementsSuccess() throws SQLException {
    PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper1 = createPreparedStatementExecutorWrapperForDQL(preparedStatement1, "ds_0");
    PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper2 = createPreparedStatementExecutorWrapperForDQL(preparedStatement2, "ds_1");
    ResultSet resultSet1 = mock(ResultSet.class);
    ResultSet resultSet2 = mock(ResultSet.class);
    when(preparedStatement1.executeQuery()).thenReturn(resultSet1);
    when(preparedStatement2.executeQuery()).thenReturn(resultSet2);
    when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
    when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Arrays.asList(wrapper1, wrapper2));
    List<ResultSet> actualResultSets = actual.executeQuery();
    assertThat(actualResultSets, hasItem(resultSet1));
    assertThat(actualResultSets, hasItem(resultSet2));
    verify(preparedStatement1).executeQuery();
    verify(preparedStatement2).executeQuery();
    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("SELECT * 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) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 40 with PreparedStatement

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

the class PreparedStatementExecutorTest method assertExecuteUpdateForSinglePreparedStatementFailure.

@Test
public void assertExecuteUpdateForSinglePreparedStatementFailure() throws SQLException {
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper = createPreparedStatementExecutorWrapperForDML(preparedStatement, "ds_0");
    SQLException exp = new SQLException();
    when(preparedStatement.executeUpdate()).thenThrow(exp);
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Collections.singleton(wrapper));
    assertThat(actual.executeUpdate(), is(0));
    verify(preparedStatement).executeUpdate();
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL("DELETE 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)

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