aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-08-16 19:49:24 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-08-18 12:33:52 +0200
commitb15f5c66386a0ede4fabe6c5f267bfffcfee4095 (patch)
treea723f2ce93d684f5ae08758eca196463231ae53c /sonar-db
parent12d9bb1c9a523ba995eef45e2ebd6ccce039c746 (diff)
downloadsonarqube-b15f5c66386a0ede4fabe6c5f267bfffcfee4095.tar.gz
sonarqube-b15f5c66386a0ede4fabe6c5f267bfffcfee4095.zip
add DatabaseUtils#executeLargeUpdates which returns rows count
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java19
1 files changed, 19 insertions, 0 deletions
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
@@ -150,6 +150,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 <INPUT extends Comparable<INPUT>> int executeLargeUpdates(Collection<INPUT> inputs, Function<List<INPUT>, Integer> sqlCaller) {
+ Iterable<List<INPUT>> partitions = toUniqueAndSortedPartitions(inputs);
+ Integer res = 0;
+ for (List<INPUT> partition : partitions) {
+ res += sqlCaller.apply(partition);
+ }
+ return res;
+ }
+
+ /**
* Ensure values {@code inputs} are unique (which avoids useless arguments) and sorted before creating the partition.
*/
private static <INPUT extends Comparable<INPUT>> Iterable<List<INPUT>> toUniqueAndSortedPartitions(Collection<INPUT> inputs) {