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);
}
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 assertExecuteQueryForSingleStatementFailure.
@Test
public void assertExecuteQueryForSingleStatementFailure() throws SQLException {
Statement statement = mock(Statement.class);
SQLException exp = new SQLException();
StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDQL(statement, "ds_0");
when(statement.executeQuery(SELECT_FROM_DUAL)).thenThrow(exp);
StatementExecutor actual = new StatementExecutor(executorEngine);
actual.addStatement(wrapper);
assertThat(actual.executeQuery(), is(Collections.singletonList((ResultSet) null)));
verify(statement).executeQuery(SELECT_FROM_DUAL);
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);
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class StatementExecutorTest method assertExecuteUpdateForSingleStatementFailure.
@Test
public void assertExecuteUpdateForSingleStatementFailure() throws SQLException {
Statement statement = mock(Statement.class);
StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
SQLException exp = new SQLException();
when(statement.executeUpdate(DELETE_FROM_DUAL)).thenThrow(exp);
StatementExecutor actual = new StatementExecutor(executorEngine);
actual.addStatement(wrapper);
assertThat(actual.executeUpdate(), is(0));
verify(statement).executeUpdate(DELETE_FROM_DUAL);
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);
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class StatementExecutorTest method assertExecuteUpdateWithColumnIndexes.
@Test
public void assertExecuteUpdateWithColumnIndexes() throws SQLException {
Statement statement = mock(Statement.class);
StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
when(statement.executeUpdate(DELETE_FROM_DUAL, new int[] { 1 })).thenReturn(10);
StatementExecutor actual = new StatementExecutor(executorEngine);
actual.addStatement(wrapper);
assertThat(actual.executeUpdate(new int[] { 1 }), is(10));
verify(statement).executeUpdate(DELETE_FROM_DUAL, new int[] { 1 });
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