summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/database/database.inc8
-rw-r--r--includes/database/mysql/query.inc6
-rw-r--r--includes/database/query.inc5
-rw-r--r--modules/simpletest/tests/database_test.test20
-rw-r--r--modules/statistics/statistics.module13
5 files changed, 45 insertions, 7 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc
index eecb8f88c..043515548 100644
--- a/includes/database/database.inc
+++ b/includes/database/database.inc
@@ -1170,6 +1170,14 @@ abstract class Database {
class TransactionsNotSupportedException extends PDOException { }
/**
+ * Exception thrown for merge queries that do not make semantic sense.
+ *
+ * There are many ways that a merge query could be malformed. They should all
+ * throw this exception and set an appropriately descriptive message.
+ */
+class InvalidMergeQueryException extends Exception {}
+
+/**
* A wrapper class for creating and managing database transactions.
*
* Not all databases or database configurations support transactions. For
diff --git a/includes/database/mysql/query.inc b/includes/database/mysql/query.inc
index d48681316..751137602 100644
--- a/includes/database/mysql/query.inc
+++ b/includes/database/mysql/query.inc
@@ -80,6 +80,12 @@ class InsertQuery_mysql extends InsertQuery {
class MergeQuery_mysql extends MergeQuery {
public function execute() {
+
+ // A merge query without any key field is invalid.
+ if (count($this->keyFields) == 0) {
+ throw new InvalidMergeQueryException("You need to specify key fields before executing a merge query");
+ }
+
// Set defaults.
if ($this->updateFields) {
$update_fields = $this->updateFields;
diff --git a/includes/database/query.inc b/includes/database/query.inc
index 396b59afb..c8e312dec 100644
--- a/includes/database/query.inc
+++ b/includes/database/query.inc
@@ -635,6 +635,11 @@ class MergeQuery extends Query {
public function execute() {
+ // A merge query without any key field is invalid.
+ if (count($this->keyFields) == 0) {
+ throw new InvalidMergeQueryException("You need to specify key fields before executing a merge query");
+ }
+
// In the degenerate case of this query type, we have to run multiple
// queries as there is no universal single-query mechanism that will work.
// Our degenerate case is not designed for performance efficiency but
diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test
index a725b2eee..d6a3b305f 100644
--- a/modules/simpletest/tests/database_test.test
+++ b/modules/simpletest/tests/database_test.test
@@ -957,6 +957,26 @@ class DatabaseMergeTestCase extends DatabaseTestCase {
$this->assertEqual($person->age, $age_before + 4, t('Age updated correctly.'));
$this->assertEqual($person->job, 'Speaker', t('Job set correctly.'));
}
+
+ /**
+ * Test that an invalid merge query throws an exception like it is supposed to.
+ */
+ function testInvalidMerge() {
+ try {
+ // This query should die because there is no key field specified.
+ db_merge('test_people')
+ ->fields(array(
+ 'age' => 31,
+ 'name' => 'Tiffany',
+ ))
+ ->execute();
+ }
+ catch (InvalidMergeQueryException $e) {
+ $this->pass(t('InvalidMergeQueryException thrown for invalid query.'));
+ return;
+ }
+ $this->fail(t('No InvalidMergeQueryException thrown'));
+ }
}
/**
diff --git a/modules/statistics/statistics.module b/modules/statistics/statistics.module
index d90c257d2..8cb1cc6e2 100644
--- a/modules/statistics/statistics.module
+++ b/modules/statistics/statistics.module
@@ -51,14 +51,13 @@ function statistics_exit() {
// We are counting content views.
if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') {
// A node has been viewed, so update the node's counters.
- $fields = array(
- 'daycount' => 1,
- 'totalcount' => 1,
- 'nid' => arg(1),
- 'timestamp' => REQUEST_TIME,
- );
db_merge('node_counter')
- ->fields($fields)
+ ->key(array('nid' => arg(1)))
+ ->fields(array(
+ 'daycount' => 1,
+ 'totalcount' => 1,
+ 'timestamp' => REQUEST_TIME,
+ ))
->expression('daycount', 'daycount + 1')
->expression('totalcount', 'totalcount + 1')
->execute();