? 284392-simplify-with-tests-db_distinct_fields-D5.patch ? backport-db_distinct_field-changes-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 7 Mar 2010 14:01:37 -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 7 Mar 2010 14:01:37 -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 .')'; - // (?