From b15f5c66386a0ede4fabe6c5f267bfffcfee4095 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 16 Aug 2016 19:49:24 +0200 Subject: [PATCH] add DatabaseUtils#executeLargeUpdates which returns rows count --- .../main/java/org/sonar/db/DatabaseUtils.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java index 3e459468880..278d932390f 100644 --- a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java +++ b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java @@ -149,6 +149,25 @@ public class DatabaseUtils { } } + /** + * Partition by 1000 elements a list of input and execute a consumer on each part. + * + * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' + * and with MsSQL when there's more than 2000 parameters in a query + * + * @param sqlCaller a {@link Function} which calls the SQL update/delete and returns the number of updated/deleted rows. + * + * @return the total number of updated/deleted rows (computed as the sum of the values returned by {@code sqlCaller}). + */ + public static > int executeLargeUpdates(Collection inputs, Function, Integer> sqlCaller) { + Iterable> partitions = toUniqueAndSortedPartitions(inputs); + Integer res = 0; + for (List partition : partitions) { + res += sqlCaller.apply(partition); + } + return res; + } + /** * Ensure values {@code inputs} are unique (which avoids useless arguments) and sorted before creating the partition. */ -- 2.39.5