From ed7f531118818dfe6d1f7b349d657fa3d528b16f Mon Sep 17 00:00:00 2001 From: Thomas Skovgaard Gielfeldt Date: Tue, 12 Feb 2013 15:47:21 +0100 Subject: [PATCH] Issue #1889328 by gielfeldt: Not all objects respect the query option "throw_exception" --- core/lib/Drupal/Core/Database/Query/Merge.php | 71 ++++++++++++++---------- core/lib/Drupal/Core/Database/Query/Select.php | 2 +- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/core/lib/Drupal/Core/Database/Query/Merge.php b/core/lib/Drupal/Core/Database/Query/Merge.php index ee0406a..3d1b16c 100644 --- a/core/lib/Drupal/Core/Database/Query/Merge.php +++ b/core/lib/Drupal/Core/Database/Query/Merge.php @@ -403,42 +403,53 @@ class Merge extends Query implements ConditionInterface { } public function execute() { - if (!count($this->condition)) { - throw new InvalidMergeQueryException(t('Invalid merge query: no conditions')); - } - $select = $this->connection->select($this->conditionTable) - ->condition($this->condition); - $select->addExpression('1'); - if (!$select->execute()->fetchField()) { - try { - $insert = $this->connection->insert($this->table)->fields($this->insertFields); - if ($this->defaultFields) { - $insert->useDefaults($this->defaultFields); + try { + if (!count($this->condition)) { + throw new InvalidMergeQueryException(t('Invalid merge query: no conditions')); + } + $select = $this->connection->select($this->conditionTable) + ->condition($this->condition); + $select->addExpression('1'); + if (!$select->execute()->fetchField()) { + try { + $insert = $this->connection->insert($this->table)->fields($this->insertFields); + if ($this->defaultFields) { + $insert->useDefaults($this->defaultFields); + } + $insert->execute(); + return self::STATUS_INSERT; + } + catch (IntegrityConstraintViolationException $e) { + // The insert query failed, maybe it's because a racing insert query + // beat us in inserting the same row. Retry the select query, if it + // returns a row, ignore the error and continue with the update + // query below. + if (!$select->execute()->fetchField()) { + throw $e; + } } - $insert->execute(); - return self::STATUS_INSERT; } - catch (IntegrityConstraintViolationException $e) { - // The insert query failed, maybe it's because a racing insert query - // beat us in inserting the same row. Retry the select query, if it - // returns a row, ignore the error and continue with the update - // query below. - if (!$select->execute()->fetchField()) { - throw $e; + if ($this->needsUpdate) { + $update = $this->connection->update($this->table) + ->fields($this->updateFields) + ->condition($this->condition); + if ($this->expressionFields) { + foreach ($this->expressionFields as $field => $data) { + $update->expression($field, $data['expression'], $data['arguments']); + } } + $update->execute(); + return self::STATUS_UPDATE; } } - if ($this->needsUpdate) { - $update = $this->connection->update($this->table) - ->fields($this->updateFields) - ->condition($this->condition); - if ($this->expressionFields) { - foreach ($this->expressionFields as $field => $data) { - $update->expression($field, $data['expression'], $data['arguments']); - } + catch (\Exception $e) { + // If throw_exception is not set at all, we assume that exceptions are wanted + if (!isset($this->queryOptions['throw_exception']) || $this->queryOptions['throw_exception']) { + throw $e; } - $update->execute(); - return self::STATUS_UPDATE; + else { + return NULL; + } } } } diff --git a/core/lib/Drupal/Core/Database/Query/Select.php b/core/lib/Drupal/Core/Database/Query/Select.php index 1bfe9a0..e5eeaf2 100644 --- a/core/lib/Drupal/Core/Database/Query/Select.php +++ b/core/lib/Drupal/Core/Database/Query/Select.php @@ -589,7 +589,7 @@ class Select extends Query implements SelectInterface { public function countQuery() { $count = $this->prepareCountQuery(); - $query = $this->connection->select($count); + $query = $this->connection->select($count, NULL, $this->queryOptions); $query->addExpression('COUNT(*)'); return $query; -- 1.7.0.4