use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class StatementExecutorTest method assertExecuteQueryForMultipleStatementsSuccess.
@Test
public void assertExecuteQueryForMultipleStatementsSuccess() throws SQLException {
Statement statement1 = mock(Statement.class);
Statement statement2 = mock(Statement.class);
ResultSet resultSet1 = mock(ResultSet.class);
ResultSet resultSet2 = mock(ResultSet.class);
when(statement1.executeQuery(SELECT_FROM_DUAL)).thenReturn(resultSet1);
when(statement1.getConnection()).thenReturn(mock(Connection.class));
when(statement2.executeQuery(SELECT_FROM_DUAL)).thenReturn(resultSet2);
when(statement2.getConnection()).thenReturn(mock(Connection.class));
StatementExecutor actual = new StatementExecutor(executorEngine);
StatementExecutorWrapper wrapper1 = createStatementExecutorWrapperForDQL(statement1, "ds_0");
actual.addStatement(wrapper1);
StatementExecutorWrapper wrapper2 = createStatementExecutorWrapperForDQL(statement2, "ds_1");
actual.addStatement(wrapper2);
List<ResultSet> actualResultSets = actual.executeQuery();
assertThat(actualResultSets, hasItem(resultSet1));
assertThat(actualResultSets, hasItem(resultSet2));
verify(statement1).executeQuery(SELECT_FROM_DUAL);
verify(statement1).getConnection();
verify(statement2).executeQuery(SELECT_FROM_DUAL);
verify(statement2).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);
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class StatementExecutorTest method assertExecuteForMultipleStatements.
@Test
public void assertExecuteForMultipleStatements() throws SQLException {
Statement statement1 = mock(Statement.class);
Statement statement2 = mock(Statement.class);
when(statement1.execute(SELECT_FROM_DUAL)).thenReturn(true);
when(statement2.execute(SELECT_FROM_DUAL)).thenReturn(true);
when(statement1.getConnection()).thenReturn(mock(Connection.class));
when(statement2.getConnection()).thenReturn(mock(Connection.class));
StatementExecutor actual = new StatementExecutor(executorEngine);
StatementExecutorWrapper wrapper1 = createStatementExecutorWrapperForDQL(statement1, "ds_0");
actual.addStatement(wrapper1);
StatementExecutorWrapper wrapper2 = createStatementExecutorWrapperForDQL(statement2, "ds_0");
actual.addStatement(wrapper2);
assertTrue(actual.execute());
verify(statement1).execute(SELECT_FROM_DUAL);
verify(statement2).execute(SELECT_FROM_DUAL);
verify(statement1).getConnection();
verify(statement2).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);
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class StatementExecutorTest method assertExecuteForMultipleStatementsSuccessWithDML.
@Test
public void assertExecuteForMultipleStatementsSuccessWithDML() throws SQLException {
Statement statement1 = mock(Statement.class);
Statement statement2 = mock(Statement.class);
when(statement1.execute(DELETE_FROM_DUAL)).thenReturn(false);
when(statement2.execute(DELETE_FROM_DUAL)).thenReturn(false);
when(statement1.getConnection()).thenReturn(mock(Connection.class));
when(statement2.getConnection()).thenReturn(mock(Connection.class));
StatementExecutor actual = new StatementExecutor(executorEngine);
StatementExecutorWrapper wrapper1 = createStatementExecutorWrapperForDML(statement1, "ds_0");
actual.addStatement(wrapper1);
StatementExecutorWrapper wrapper2 = createStatementExecutorWrapperForDML(statement2, "ds_1");
actual.addStatement(wrapper2);
assertFalse(actual.execute());
verify(statement1).execute(DELETE_FROM_DUAL);
verify(statement2).execute(DELETE_FROM_DUAL);
verify(statement1).getConnection();
verify(statement2).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);
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class StatementExecutor method executeUpdate.
private int executeUpdate(final Updater updater) {
Context context = MetricsContext.start("ShardingStatement-executeUpdate");
eventPostman.postExecutionEvents();
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
try {
if (1 == statementExecutorWrappers.size()) {
return executeUpdateInternal(updater, statementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
}
return executorEngine.execute(statementExecutorWrappers, new ExecuteUnit<StatementExecutorWrapper, Integer>() {
@Override
public Integer execute(final StatementExecutorWrapper input) throws Exception {
synchronized (input.getStatement().getConnection()) {
return executeUpdateInternal(updater, input, isExceptionThrown, dataMap);
}
}
}, new MergeUnit<Integer, Integer>() {
@Override
public Integer merge(final List<Integer> results) {
if (null == results) {
return 0;
}
int result = 0;
for (int each : results) {
result += each;
}
return result;
}
});
} finally {
MetricsContext.stop(context);
}
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class StatementExecutorTest method assertExecuteWithAutoGeneratedKeys.
@Test
public void assertExecuteWithAutoGeneratedKeys() throws SQLException {
Statement statement = mock(Statement.class);
StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
when(statement.execute(DELETE_FROM_DUAL, Statement.NO_GENERATED_KEYS)).thenReturn(false);
StatementExecutor actual = new StatementExecutor(executorEngine);
actual.addStatement(wrapper);
assertFalse(actual.execute(Statement.NO_GENERATED_KEYS));
verify(statement).execute(DELETE_FROM_DUAL, Statement.NO_GENERATED_KEYS);
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);
}
Aggregations