From 957f8884525fef426a849e4cba84ae33b5c2d218 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Fri, 13 Mar 2009 14:28:18 +0000
Subject: [PATCH] - Patch #302268 by tizzo, bubbasan, Ryan Palmer et al:
 converted module to new database abstraction layer.

---
 modules/dblog/dblog.admin.inc | 52 +++++++++++++++++++++++++++--------
 modules/dblog/dblog.module    | 17 ++++++++----
 modules/dblog/dblog.test      | 24 ++++++++--------
 3 files changed, 64 insertions(+), 29 deletions(-)

diff --git a/modules/dblog/dblog.admin.inc b/modules/dblog/dblog.admin.inc
index 921fb7c4d3ae..ce0f79de29d8 100644
--- a/modules/dblog/dblog.admin.inc
+++ b/modules/dblog/dblog.admin.inc
@@ -63,16 +63,30 @@ function dblog_overview() {
     array('data' => t('Operations')),
   );
 
-  $sql = "SELECT w.wid, w.uid, w.severity, w.type, w.timestamp, w.message, w.variables, w.link, u.name FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
-  $tablesort = tablesort_sql($header);
+  $query = db_select('watchdog', 'w');
+  $query->join('users', 'u', 'w.uid = u.uid');
+  $query
+    ->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
+    ->addField('u', 'name');
   if (!empty($filter['where'])) {
-    $result = pager_query($sql . " WHERE " . $filter['where'] . $tablesort, 50, 0, NULL, $filter['args']);
+    //setHeader may not be chainable see Line 138
+    $query
+      ->where($filter['where'], $filter['args'])
+      ->extend('PagerDefault')->extend('TableSort')
+      ->limit(50, 0)
+      ->setHeader($header);
+    $result = $query->execute();
   }
   else {
-    $result = pager_query($sql . $tablesort, 50);
+    //setHeader may not be chainable see Line 138
+    $query
+      ->extend('PagerDefault')->extend('TableSort')
+      ->limit(50)
+      ->setHeader($header);
+    $result = $query->execute();
   }
 
-  while ($dblog = db_fetch_object($result)) {
+  foreach ($result as $dblog) {
     $rows[] = array('data' =>
       array(
         // Cells
@@ -108,11 +122,25 @@ function dblog_top($type) {
     array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
     array('data' => t('Message'), 'field' => 'message')
   );
-
-  $result = pager_query("SELECT COUNT(wid) AS count, message, variables FROM {watchdog} WHERE type = '%s' GROUP BY message, variables " . tablesort_sql($header), 30, 0, "SELECT COUNT(DISTINCT(message)) FROM {watchdog} WHERE type = '%s'", $type);
+  $count_query = db_select('watchdog');
+  $count_query->addExpression('COUNT(DISTINCT(message))');
+  $count_query->condition('type', $type);
+
+  $query = db_select('watchdog', 'w');
+  $query->addExpression('COUNT(wid)', 'count');
+  $query = $query
+    ->fields('w', array('message', 'variables'))
+    ->condition('w.type', $type)
+    ->groupBy('message')
+    ->groupBy('variables')
+    ->extend('PagerDefault')->extend('TableSort')
+    ->limit(30);
+  $query = $query->setHeader($header);
+  $query->setCountQuery($count_query);
+  $result = $query->execute();
 
   $rows = array();
-  while ($dblog = db_fetch_object($result)) {
+  foreach ($result as $dblog) {
     $rows[] = array($dblog->count, truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE));
   }
 
@@ -132,8 +160,8 @@ function dblog_top($type) {
 function dblog_event($id) {
   $severity = watchdog_severity_levels();
   $output = '';
-  $result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = %d', $id);
-  if ($dblog = db_fetch_object($result)) {
+  $result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = :id', array(':id' => $id))->fetchObject();
+  if ($dblog = $result) {
     $rows = array(
       array(
         array('data' => t('Type'), 'header' => TRUE),
@@ -222,14 +250,14 @@ function dblog_filters() {
   if (!empty($types)) {
     $filters['type'] = array(
       'title' => t('Type'),
-      'where' => "w.type = '%s'",
+      'where' => "w.type = ':s'",
       'options' => $types,
     );
   }
 
   $filters['severity'] = array(
     'title' => t('Severity'),
-    'where' => 'w.severity = %d',
+    'where' => 'w.severity = :d',
     'options' => watchdog_severity_levels(),
   );
 
diff --git a/modules/dblog/dblog.module b/modules/dblog/dblog.module
index 59ebe233a5be..f16f5a4c9775 100644
--- a/modules/dblog/dblog.module
+++ b/modules/dblog/dblog.module
@@ -97,8 +97,10 @@ function dblog_init() {
  */
 function dblog_cron() {
   // Cleanup the watchdog table
-  $max = db_result(db_query('SELECT MAX(wid) FROM {watchdog}'));
-  db_query('DELETE FROM {watchdog} WHERE wid <= %d', $max - variable_get('dblog_row_limit', 1000));
+  $max = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
+  db_delete('watchdog')
+    ->condition('wid', $max - variable_get('dblog_row_limit', 1000), '<=')
+    ->execute();
 }
 
 /**
@@ -107,11 +109,16 @@ function dblog_cron() {
 function dblog_user_cancel($edit, $account, $method) {
   switch ($method) {
     case 'user_cancel_reassign':
-      db_update('watchdog')->fields(array('uid' => 0))->condition('uid', $account->uid)->execute();
+      db_update('watchdog')
+        ->fields(array('uid' => 0))
+        ->condition('uid', $account->uid)
+        ->execute();
       break;
 
     case 'user_cancel_delete':
-      db_delete('watchdog')->condition('uid', $account->uid)->execute();
+      db_delete('watchdog')
+        ->condition('uid', $account->uid)
+        ->execute();
       break;
   }
 }
@@ -120,7 +127,7 @@ function _dblog_get_message_types() {
   $types = array();
 
   $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
-  while ($object = db_fetch_object($result)) {
+  foreach ($result as $object) {
     $types[] = $object->type;
   }
 
diff --git a/modules/dblog/dblog.test b/modules/dblog/dblog.test
index e7b752b1e1bb..6704e016740e 100644
--- a/modules/dblog/dblog.test
+++ b/modules/dblog/dblog.test
@@ -57,7 +57,7 @@ class DBLogTestCase extends DrupalWebTestCase {
     $current_limit = variable_get('dblog_row_limit', 1000);
     $this->assertTrue($current_limit == $row_limit, t('[Cache] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
     // Verify dblog row limit equals specified row limit.
-    $current_limit = unserialize(db_result(db_query("SELECT value FROM {variable} WHERE name = '%s'", 'dblog_row_limit')));
+    $current_limit = unserialize(db_query("SELECT value FROM {variable} WHERE name = :dblog_limit", array(':dblog_limit' => 'dblog_row_limit'))->fetchField());
     $this->assertTrue($current_limit == $row_limit, t('[Variable table] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
   }
 
@@ -70,7 +70,7 @@ class DBLogTestCase extends DrupalWebTestCase {
     // Generate additional log entries.
     $this->generateLogEntries($row_limit + 10);
     // Verify dblog row count exceeds row limit.
-    $count = db_result(db_query('SELECT COUNT(wid) FROM {watchdog}'));
+    $count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
     $this->assertTrue($count > $row_limit, t('Dblog row count of @count exceeds row limit of @limit', array('@count' => $count, '@limit' => $row_limit)));
 
     // Run cron job.
@@ -78,7 +78,7 @@ class DBLogTestCase extends DrupalWebTestCase {
     $this->assertResponse(200);
     $this->assertText(t('Cron ran successfully'), t('Cron ran successfully'));
     // Verify dblog row count equals row limit plus one because cron adds a record after it runs.
-    $count = db_result(db_query('SELECT COUNT(wid) FROM {watchdog}'));
+    $count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
     $this->assertTrue($count == $row_limit + 1, t('Dblog row count of @count equals row limit of @limit plus one', array('@count' => $count, '@limit' => $row_limit)));
   }
 
@@ -196,9 +196,9 @@ class DBLogTestCase extends DrupalWebTestCase {
     // Logout user.
     $this->drupalLogout();
     // Fetch row ids in watchdog that relate to the user.
-    $result = db_query('SELECT wid FROM {watchdog} WHERE uid = %d', $user->uid);
-    while ($row = db_fetch_array($result)) {
-      $ids[] = $row['wid'];
+    $result = db_query('SELECT wid FROM {watchdog} WHERE uid = :uid', array(':uid' => $user->uid));
+    foreach($result as $row) {
+      $ids[] = $row->wid;
     }
     $count_before = (isset($ids)) ? count($ids) : 0;
     $this->assertTrue($count_before > 0, t('DBLog contains @count records for @name', array('@count' => $count_before, '@name' => $user->name)));
@@ -210,7 +210,7 @@ class DBLogTestCase extends DrupalWebTestCase {
     $this->drupalPost('user/' . $user->uid . '/cancel', array('user_cancel_method' => 'user_cancel_reassign'), t('Cancel account'));
 
     // Count rows that have uids for the user.
-    $count = db_result(db_query('SELECT COUNT(wid) FROM {watchdog} WHERE uid = %d', $user->uid));
+    $count = db_query('SELECT COUNT(wid) FROM {watchdog} WHERE uid = %d', $user->uid)->fetchField();
     $this->assertTrue($count == 0, t('DBLog contains @count records for @name', array('@count' => $count, '@name' => $user->name)));
 
     // Count rows in watchdog that previously related to the deleted user.
@@ -224,9 +224,9 @@ class DBLogTestCase extends DrupalWebTestCase {
     $this->assertTrue($count_after == $count_before, t('DBLog contains @count records for @name that now have uid = 0', array('@count' => $count_before, '@name' => $user->name)));
     unset($ids);
     // Fetch row ids in watchdog that relate to the user.
-    $result = db_query('SELECT wid FROM {watchdog} WHERE uid = %d', $user->uid);
-    while ($row = db_fetch_array($result)) {
-      $ids[] = $row['wid'];
+    $result = db_query('SELECT wid FROM {watchdog} WHERE uid = :uid', array(':uid' => $user->uid));
+    foreach($result as $row) {
+      $ids[] = $row->wid;
     }
     $this->assertTrue(!isset($ids), t('DBLog contains no records for @name', array('@name' => $user->name)));
 
@@ -380,13 +380,13 @@ class DBLogTestCase extends DrupalWebTestCase {
     // Add a watchdog entry.
     dblog_watchdog($log);
     // Make sure the table count has actually incremented.
-    $this->assertEqual($count + 1, db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField(), t('dblog_watchdog() added an entry to the dblog %count', array('%count' => $count)));
+    $this->assertEqual($count + 1, db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField(), t('dblog_watchdog() added an entry to the dblog :count', array(':count' => $count)));
     // Login the admin user.
     $this->drupalLogin($this->big_user);
     // Now post to clear the db table.
     $this->drupalPost('admin/reports/dblog', array(), t('Clear log messages'));
     // Count rows in watchdog that previously related to the deleted user.
     $count = db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField();
-    $this->assertEqual($count, 0, t('DBLog contains %count records after a clear.', array('%count' => $count)));
+    $this->assertEqual($count, 0, t('DBLog contains :count records after a clear.', array(':count' => $count)));
   }
 }
-- 
GitLab