DatabaseImpl implements Database { @Override public int[] batchExecute(String sql, List<Object[]> params) { validateStringValue(sql); StopWatch watch = new StopWatch(); int updatedRows = 0; try { int[] results = operation.batchUpdate(sql, params); updatedRows = Arrays.stream(results).sum(); return results; } finally { long elapsed = watch.elapsed(); int operations = ActionLogContext.track("db", elapsed, 0, updatedRows); logger.debug("batchExecute, sql={}, params={}, size={}, updatedRows={}, elapsed={}", sql, new SQLBatchParams(operation.enumMapper, params), params.size(), updatedRows, elapsed); checkOperation(elapsed, operations); } } DatabaseImpl(String name); void close(); void timeout(Duration timeout); void url(String url); void view(Class<T> viewClass); Repository<T> repository(Class<T> entityClass); @Override Transaction beginTransaction(); @Override List<T> select(String sql, Class<T> viewClass, Object... params); @Override Optional<T> selectOne(String sql, Class<T> viewClass, Object... params); @Override int execute(String sql, Object... params); @Override int[] batchExecute(String sql, List<Object[]> params); final Pool<Connection> pool; final DatabaseOperation operation; public String user; public String password; public int maxOperations; public int tooManyRowsReturnedThreshold; public long slowOperationThresholdInNanos; public IsolationLevel isolationLevel; }
@Test void batchExecute() { insertRow(1, "string1", TestEnum.V1); insertRow(2, "string2", TestEnum.V2); List<Object[]> params = List.of(new Object[]{"string3", 1}, new Object[]{"string4", 2}); int[] results = database.batchExecute("UPDATE database_test SET string_field = ? WHERE id = ?", params); assertThat(results).containsExactly(1, 1); }