Index: includes/database/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/database.inc,v
retrieving revision 1.36
diff -u -p -r1.36 database.inc
--- includes/database/database.inc	24 Dec 2008 09:53:40 -0000	1.36
+++ includes/database/database.inc	25 Dec 2008 05:59:38 -0000
@@ -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
Index: includes/database/query.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/query.inc,v
retrieving revision 1.10
diff -u -p -r1.10 query.inc
--- includes/database/query.inc	20 Dec 2008 18:24:33 -0000	1.10
+++ includes/database/query.inc	25 Dec 2008 05:59:38 -0000
@@ -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
Index: includes/database/mysql/query.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/mysql/query.inc,v
retrieving revision 1.7
diff -u -p -r1.7 query.inc
--- includes/database/mysql/query.inc	20 Dec 2008 18:24:34 -0000	1.7
+++ includes/database/mysql/query.inc	25 Dec 2008 05:59:39 -0000
@@ -80,6 +80,12 @@ class InsertQuery_mysql extends InsertQu
 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;
Index: modules/simpletest/tests/database_test.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v
retrieving revision 1.30
diff -u -p -r1.30 database_test.test
--- modules/simpletest/tests/database_test.test	24 Dec 2008 10:21:32 -0000	1.30
+++ modules/simpletest/tests/database_test.test	25 Dec 2008 05:59:40 -0000
@@ -957,6 +957,26 @@ class DatabaseMergeTestCase extends Data
     $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'));
+  }
 }
 
 /**
Index: modules/statistics/statistics.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.module,v
retrieving revision 1.291
diff -u -p -r1.291 statistics.module
--- modules/statistics/statistics.module	18 Dec 2008 03:58:24 -0000	1.291
+++ modules/statistics/statistics.module	25 Dec 2008 05:59:40 -0000
@@ -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();
