Index: modules/comment/comment.api.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.api.php,v
retrieving revision 1.7
diff -u -p -r1.7 comment.api.php
--- modules/comment/comment.api.php	10 Jun 2009 05:09:51 -0000	1.7
+++ modules/comment/comment.api.php	28 Jun 2009 10:16:14 -0000
@@ -55,6 +55,19 @@ function hook_comment_update($form_value
 }
 
 /**
+ * Comments are being loaded from the database.
+ *
+ * @param $comments
+ *  An array of comment objects indexed by cid.
+ */
+function hook_comment_load($comments) {
+  $result = db_query('SELECT cid, foo FROM {mytable} WHERE cid IN (:cids)', array(':cids' => array_keys($comments)));
+  foreach ($result as $record) {
+    $comments[$record->cid]->foo = $record->foo;
+  }
+}
+
+/**
  * The comment is being viewed. This hook can be used to add additional data to the comment before theming.
  *
  * @param $comment
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.729
diff -u -p -r1.729 comment.module
--- modules/comment/comment.module	27 Jun 2009 10:13:28 -0000	1.729
+++ modules/comment/comment.module	28 Jun 2009 10:16:15 -0000
@@ -1147,22 +1147,9 @@ function comment_render($node, $cid = 0)
     $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
 
     if ($cid && is_numeric($cid)) {
+      $comment = current(comment_load_multiple(array('cid' => $cid, 'status' => COMMENT_PUBLISHED)));
       // Single comment view.
-      $query = db_select('comment', 'c');
-      $query->addField('u', 'name', 'registered_name');
-      $query->innerJoin('users', 'u', 'c.uid = u.uid');
-      $query
-        ->fields('c', array('cid', 'nid', 'pid', 'comment', 'subject', 'format', 'timestamp', 'name', 'mail', 'homepage', 'status'))
-        ->fields('u', array( 'uid', 'signature', 'picture', 'data', 'status'))
-        ->condition('c.cid', $cid);
-
-      if (!user_access('administer comments')) {
-        $query->condition('c.status', COMMENT_PUBLISHED);
-      }
-
-      $result = $query->execute();
-
-      if ($comment = $result->fetchObject()) {
+      if ($comment) {
         $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
         $links = module_invoke_all('link', 'comment', $comment, 1);
         drupal_alter('link', $links, $node);
@@ -1177,11 +1164,8 @@ function comment_render($node, $cid = 0)
 
       // Multiple comment view.
       $query = db_select('comment', 'c')->extend('PagerDefault');
-      $query->join('users', 'u', 'c.uid = u.uid');
-      $query->addField('u', 'name', 'registered_name');
+      $query->addField('c', 'cid');
       $query
-        ->fields('c', array('cid', 'pid', 'nid', 'subject', 'comment', 'format', 'timestamp', 'name', 'mail', 'homepage', 'thread', 'status'))
-        ->fields('u', array('uid', 'signature', 'picture', 'data'))
         ->condition('c.nid', $nid)
         ->addTag('node_access')
         ->limit($comments_per_page);
@@ -1207,13 +1191,14 @@ function comment_render($node, $cid = 0)
       }
 
       $query->setCountQuery($count_query);
-      $result = $query->execute();
+      $cids = $query->execute()->fetchCol();
 
       $divs = 0;
       $num_rows = FALSE;
-      $comments = '';
+      $render = '';
+      $comments = comment_load_multiple($cids);
       drupal_add_css(drupal_get_path('module', 'comment') . '/comment.css');
-      foreach ($result as $comment) {
+      foreach ($comments as $comment) {
         $comment = drupal_unpack($comment);
         $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
         $comment->depth = count(explode('.', $comment->thread)) - 1;
@@ -1221,34 +1206,34 @@ function comment_render($node, $cid = 0)
         if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
           if ($comment->depth > $divs) {
             $divs++;
-            $comments .= '<div class="indented">';
+            $render .= '<div class="indented">';
           }
           else {
             while ($comment->depth < $divs) {
               $divs--;
-              $comments .= '</div>';
+              $render .= '</div>';
             }
           }
         }
 
         if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
-          $comments .= theme('comment_flat_collapsed', $comment, $node);
+          $render .= theme('comment_flat_collapsed', $comment, $node);
         }
         elseif ($mode == COMMENT_MODE_FLAT_EXPANDED) {
-          $comments .= theme('comment_flat_expanded', $comment, $node);
+          $render .= theme('comment_flat_expanded', $comment, $node);
         }
         elseif ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
-          $comments .= theme('comment_thread_collapsed', $comment, $node);
+          $render .= theme('comment_thread_collapsed', $comment, $node);
         }
         elseif ($mode == COMMENT_MODE_THREADED_EXPANDED) {
-          $comments .= theme('comment_thread_expanded', $comment, $node);
+          $render .= theme('comment_thread_expanded', $comment, $node);
         }
         $num_rows = TRUE;
       }
       while ($divs-- > 0) {
-        $comments .= '</div>';
+        $render .= '</div>';
       }
-      $output .= $comments;
+      $output .= $render;
       $output .= theme('pager', NULL);
     }
 
@@ -1299,8 +1284,49 @@ function comment_operations($action = NU
 }
 
 /**
- * Begin the misc functions: helpers, privates, history.
+ * Load comments from the database.
+ *
+ * @param $cids
+ *   An array of comment IDs.
+ * @param $conditions
+ *   An array of conditions to match against the {comments} table. These
+ *   should be supplied in the form array('field_name' => 'field_value').
+ * @return
+ *  An array of comment objects, indexed by comment ID.
  */
+function comment_load_multiple($cids = array(), $conditions = array()) {
+  $comments = array();
+  $query = db_select('comment', 'c');
+  $query->innerJoin('users', 'u', 'c.uid = u.uid');
+  $query->addField('u', 'name', 'registered_name');
+  $query
+    ->fields('c', array('cid', 'nid', 'pid', 'comment', 'subject', 'format', 'timestamp', 'name', 'mail', 'homepage', 'status', 'thread'))
+    ->fields('u', array( 'uid', 'signature', 'picture', 'data', 'status'));
+
+  // If the $cids array is populated, add those to the query.
+  if ($cids) {
+    $query->condition('c.cid', $cids, 'IN');
+  }
+
+  // If the conditions array is populated, add those to the query.
+  if ($conditions) {
+    foreach ($conditions as $field => $value) {
+      $query->condition('c.' . $field, $value);
+    }
+  }
+  if ($cids || $conditions) {
+    $comments = $query->execute()->fetchAllAssoc('cid');
+  }
+
+  // Invoke hook_comment_load() on the comments loaded from the database.
+  if (!empty($comments)) {
+    foreach (module_implements('comment_load') as $module) {
+      $function = $module . '_comment_load';
+      $function($comments);
+    }
+  }
+  return $comments;
+}
 
 /**
  * Load the entire comment by cid.
@@ -1311,7 +1337,7 @@ function comment_operations($action = NU
  *   The comment object.
  */
 function comment_load($cid) {
-  return db_query('SELECT * FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchObject();
+  return current(comment_load_multiple(array($cid)));
 }
 
 /**
