#851110: the default implementation of InsertQuery should rollback the whole transaction if one insert fails.

From: Damien Tournoud <damien@commerceguys.com>


---
 database/query.inc |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)

diff --git includes/database/query.inc includes/database/query.inc
index a9f21bb..769566d 100644
--- includes/database/query.inc
+++ includes/database/query.inc
@@ -486,9 +486,18 @@ class InsertQuery extends Query {
     // we wrap it in a transaction so that it is atomic where possible. On many
     // databases, such as SQLite, this is also a notable performance boost.
     $transaction = $this->connection->startTransaction();
-    $sql = (string) $this;
-    foreach ($this->insertValues as $insert_values) {
-      $last_insert_id = $this->connection->query($sql, $insert_values, $this->queryOptions);
+
+    try {
+      $sql = (string) $this;
+      foreach ($this->insertValues as $insert_values) {
+        $last_insert_id = $this->connection->query($sql, $insert_values, $this->queryOptions);
+      }
+    }
+    catch (Exception $e) {
+      // One of the INSERTs failed, rollback the whole batch.
+      $transaction->rollback();
+      // Rethrow the exception for the calling code.
+      throw $e;
     }
 
     // Re-initialize the values array so that we can re-use this query.
