? 284392-simplify-with-tests-db_distinct_fields.D5.patch
Index: includes/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/Attic/database.inc,v
retrieving revision 1.62.2.6
diff -u -p -r1.62.2.6 database.inc
--- includes/database.inc	7 Jan 2008 00:55:44 -0000	1.62.2.6
+++ includes/database.inc	9 Feb 2010 21:16:39 -0000
@@ -304,6 +304,27 @@ function db_rewrite_sql($query, $primary
 }
 
 /**
+ * Adds the DISTINCT flag to the supplied query if a DISTINCT doesn't already
+ * exist in the query. Returns the altered query.
+ *
+ * This will not, and never did guarantee that you will obtain distinct 
+ * values of $table.$field.
+ *
+ * @param $table Unused. Kept to retain API compatibility.
+ * @param $field Unused. Kept to retain API compatibility.
+ * @param $query Query to which the DISTINCT flag should be applied.
+ * @return SQL query with the DISTINCT flag set.
+ */
+function db_distinct_field($table, $field, $query) {
+  $matches = array();
+  if (!preg_match('/^SELECT\s*DISTINCT/i', $query, $matches)) {
+    // Only add distinct to the outer SELECT to avoid messing up subqueries.
+    $query = preg_replace('/^SELECT/i', 'SELECT DISTINCT', $query);
+  }
+  return $query;
+}
+
+/**
  * Restrict a dynamic tablename to safe characters.
  *
  * Only keeps alphanumeric and underscores.
Index: includes/database.mysql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/Attic/database.mysql.inc,v
retrieving revision 1.66.2.4
diff -u -p -r1.66.2.4 database.mysql.inc
--- includes/database.mysql.inc	10 Jul 2009 06:09:38 -0000	1.66.2.4
+++ includes/database.mysql.inc	9 Feb 2010 21:16:39 -0000
@@ -424,23 +424,6 @@ function db_table_exists($table) {
 }
 
 /**
- * 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 .')';
-  // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
-  return preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query);
-}
-
-/**
  * @} End of "ingroup database".
  */
 
Index: includes/database.mysqli.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/Attic/database.mysqli.inc,v
retrieving revision 1.32.2.6
diff -u -p -r1.32.2.6 database.mysqli.inc
--- includes/database.mysqli.inc	10 Jul 2009 06:09:38 -0000	1.32.2.6
+++ includes/database.mysqli.inc	9 Feb 2010 21:16:39 -0000
@@ -407,23 +407,6 @@ function db_table_exists($table) {
 }
 
 /**
- * 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 .')';
-  // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
-  return preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query);
-}
-
-/**
  * @} End of "ingroup database".
  */
 
Index: includes/database.pgsql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/Attic/database.pgsql.inc,v
retrieving revision 1.43.2.3
diff -u -p -r1.43.2.3 database.pgsql.inc
--- includes/database.pgsql.inc	15 Sep 2008 06:14:52 -0000	1.43.2.3
+++ includes/database.pgsql.inc	9 Feb 2010 21:16:39 -0000
@@ -408,29 +408,6 @@ function db_check_setup() {
 }
 
 /**
- * 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) {
-  if (!preg_match('/FROM\s+\S+\s+AS/si', $query)
-    && !preg_match('/DISTINCT\s+ON\s*\(\s*(' . $table . '\s*\.\s*)?' . $field . '\s*\)/si', $query)
-    && !preg_match('/DISTINCT[ (]' . $field . '/si', $query)
-    && preg_match('/(.*FROM\s+)(.*?\s)(\s*(WHERE|GROUP|HAVING|ORDER|LIMIT|FOR).*)/Asi', $query, $m)) {
-      $query = $m[1];
-      $query .= preg_replace('/([\{\w+\}]+)\s+(' . $table . ')\s/Usi', '(SELECT DISTINCT ON (' . $field . ') * FROM \1) \2 ', $m[2]);
-      $query .= $m[3];
-  }
-  return $query;
-}
-
-/**
  * @} End of "ingroup database".
  */
 
