? 284392-simplify-db_distinct_fields.D6.patch
? 336483-add-comment-count-index-d6.patch
? 336483-add-comment-count-index-rev2-d6.patch
? 582534-freetagging-fullwidth-comma-D6.patch
? node_load_caching_flag-D6.patch
Index: includes/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/Attic/database.inc,v
retrieving revision 1.92.2.8
diff -u -p -r1.92.2.8 database.inc
--- includes/database.inc	14 Sep 2009 10:49:34 -0000	1.92.2.8
+++ includes/database.inc	16 Dec 2009 02:12:29 -0000
@@ -329,7 +329,7 @@ function db_rewrite_sql($query, $primary
   list($join, $where, $distinct) = _db_rewrite_sql($query, $primary_table, $primary_field, $args);
 
   if ($distinct) {
-    $query = db_distinct_field($primary_table, $primary_field, $query);
+    $query = db_distinct_field($query);
   }
 
   if (!empty($where) || !empty($join)) {
Index: includes/database.mysql-common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/Attic/database.mysql-common.inc,v
retrieving revision 1.17.2.3
diff -u -p -r1.17.2.3 database.mysql-common.inc
--- includes/database.mysql-common.inc	14 Sep 2009 10:49:34 -0000	1.17.2.3
+++ includes/database.mysql-common.inc	16 Dec 2009 02:12:30 -0000
@@ -531,3 +531,18 @@ function db_change_field(&$ret, $table, 
 function db_last_insert_id($table, $field) {
   return db_result(db_query('SELECT LAST_INSERT_ID()'));
 }
+
+/**
+ * Adds the DISTINCT flag to the supplied query if a DISTINCT doesn't already
+ * exist in the query. Returns the altered query.
+ *
+ * @param $query Query to which the DISTINCT flag should be applied.
+ * @return SQL query with the DISTINCT flag set.
+ */
+function db_distinct_field($query) {
+  $matches = array();
+  if (!stristr('DISTINCT', $query) && preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) {
+    return 'SELECT DISTINCT '. $matches[1] .'FROM'. $matches[2];
+  }
+  return $query;
+}
Index: includes/database.mysql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/Attic/database.mysql.inc,v
retrieving revision 1.89.2.1
diff -u -p -r1.89.2.1 database.mysql.inc
--- includes/database.mysql.inc	21 Jul 2009 08:52:29 -0000	1.89.2.1
+++ includes/database.mysql.inc	16 Dec 2009 02:12:30 -0000
@@ -350,30 +350,5 @@ function db_column_exists($table, $colum
 }
 
 /**
- * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to
- * the SELECT list entry of the given query and the resulting query is returned.
- * This function only applies the wrapper if a DISTINCT doesn't already exist in
- * the query.
- *
- * @param $table Table containing the field to set as DISTINCT
- * @param $field Field to set as DISTINCT
- * @param $query Query to apply the wrapper to
- * @return SQL query with the DISTINCT wrapper surrounding the given table.field.
- */
-function db_distinct_field($table, $field, $query) {
-  $field_to_select = 'DISTINCT('. $table .'.'. $field .')';
-  $matches = array();
-  if (preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) {
-    $select = preg_replace(
-      '/((?:^|,)\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|$))/is',
-      '\1'. $field_to_select .'\2', $matches[1], 1
-    );
-    
-    return 'SELECT'. $select .'FROM'. $matches[2];
-  }
-  return $query;
-}
-
-/**
  * @} End of "ingroup database".
  */
Index: includes/database.mysqli.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/Attic/database.mysqli.inc,v
retrieving revision 1.54.2.1
diff -u -p -r1.54.2.1 database.mysqli.inc
--- includes/database.mysqli.inc	21 Jul 2009 08:52:30 -0000	1.54.2.1
+++ includes/database.mysqli.inc	16 Dec 2009 02:12:30 -0000
@@ -352,31 +352,6 @@ function db_column_exists($table, $colum
 }
 
 /**
- * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to
- * the SELECT list entry of the given query and the resulting query is returned.
- * This function only applies the wrapper if a DISTINCT doesn't already exist in
- * the query.
- *
- * @param $table Table containing the field to set as DISTINCT
- * @param $field Field to set as DISTINCT
- * @param $query Query to apply the wrapper to
- * @return SQL query with the DISTINCT wrapper surrounding the given table.field.
- */
-function db_distinct_field($table, $field, $query) {
-  $field_to_select = 'DISTINCT('. $table .'.'. $field .')';
-  $matches = array();
-  if (preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) {
-    $select = preg_replace(
-      '/((?:^|,)\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|$))/is',
-      '\1'. $field_to_select .'\2', $matches[1], 1
-    );
-    
-    return 'SELECT'. $select .'FROM'.$matches[2];
-  }
-  return $query;
-}
-
-/**
  * @} End of "ingroup database".
  */
 
