]> source.dussan.org Git - sonarqube.git/commitdiff
add DatabaseUtils#executeLargeUpdates which returns rows count
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 16 Aug 2016 17:49:24 +0000 (19:49 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 18 Aug 2016 10:33:52 +0000 (12:33 +0200)
sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java

index 3e45946888067ea69a5585f4fbe5c7c65360d9d0..278d932390f22962e8022af85107c003b56a35db 100644 (file)
@@ -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 <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.
    */