PreparedStatementExecutor extends AbstractStatementExecutor { @Override public int executeUpdate(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext) throws SQLException { boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecutorCallback<Integer> sqlExecutorCallback = createDefaultSQLExecutorCallbackWithInteger(isExceptionThrown); List<Integer> results = getSqlExecutor().execute(inputGroups, sqlExecutorCallback); refreshTableMetaData(getSchemaContexts().getDefaultSchema(), sqlStatementContext); return isNeedAccumulate( getSchemaContexts().getDefaultSchema().getRules().stream().filter(rule -> rule instanceof DataNodeRoutedRule).collect(Collectors.toList()), sqlStatementContext) ? accumulate(results) : results.get(0); } PreparedStatementExecutor(final Map<String, DataSource> dataSourceMap, final SchemaContexts schemaContexts, final SQLExecutor sqlExecutor); @Override List<QueryResult> executeQuery(final Collection<InputGroup<StatementExecuteUnit>> inputGroups); @Override int executeUpdate(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext); @Override boolean execute(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext);  }
@Test public void assertExecuteUpdateForSinglePreparedStatementSuccess() throws SQLException { PreparedStatement preparedStatement = getPreparedStatement(); when(preparedStatement.executeUpdate()).thenReturn(10); assertThat(actual.executeUpdate(getExecuteGroups(Collections.singletonList(preparedStatement), false), createSQLStatementContext()), is(10)); verify(preparedStatement).executeUpdate(); }
@Test public void assertExecuteUpdateForMultiplePreparedStatementsSuccess() throws SQLException { PreparedStatement preparedStatement1 = getPreparedStatement(); PreparedStatement preparedStatement2 = getPreparedStatement(); when(preparedStatement1.executeUpdate()).thenReturn(10); when(preparedStatement2.executeUpdate()).thenReturn(20); assertThat(actual.executeUpdate(getExecuteGroups(Arrays.asList(preparedStatement1, preparedStatement2), false), createSQLStatementContext()), is(30)); verify(preparedStatement1).executeUpdate(); verify(preparedStatement2).executeUpdate(); }
@Test public void assertExecuteUpdateForSinglePreparedStatementFailure() throws SQLException { PreparedStatement preparedStatement = getPreparedStatement(); SQLException ex = new SQLException(""); when(preparedStatement.executeUpdate()).thenThrow(ex); assertThat(actual.executeUpdate(getExecuteGroups(Collections.singletonList(preparedStatement), false), createSQLStatementContext()), is(0)); verify(preparedStatement).executeUpdate(); }
@Test public void assertExecuteUpdateForMultiplePreparedStatementsFailure() throws SQLException { PreparedStatement preparedStatement1 = getPreparedStatement(); PreparedStatement preparedStatement2 = getPreparedStatement(); SQLException ex = new SQLException(""); when(preparedStatement1.executeUpdate()).thenThrow(ex); when(preparedStatement2.executeUpdate()).thenThrow(ex); assertThat(actual.executeUpdate(getExecuteGroups(Arrays.asList(preparedStatement1, preparedStatement2), false), createSQLStatementContext()), is(0)); verify(preparedStatement1).executeUpdate(); verify(preparedStatement2).executeUpdate(); }