Mercurial > defr > drupal > core
diff includes/database.pgsql.inc @ 7:fff6d4c8c043 6.3
Drupal 6.3
author | Franck Deroche <webmaster@defr.org> |
---|---|
date | Tue, 23 Dec 2008 14:30:28 +0100 |
parents | c1f4ac30525a |
children | 4347c45bb494 |
line wrap: on
line diff
--- a/includes/database.pgsql.inc Tue Dec 23 14:30:08 2008 +0100 +++ b/includes/database.pgsql.inc Tue Dec 23 14:30:28 2008 +0100 @@ -1,5 +1,5 @@ <?php -// $Id: database.pgsql.inc,v 1.68.2.1 2008/02/07 10:17:26 goba Exp $ +// $Id: database.pgsql.inc,v 1.68.2.2 2008/07/08 09:50:03 goba Exp $ /** * @file @@ -418,10 +418,14 @@ * @return SQL query with the DISTINCT wrapper surrounding the given table.field. */ function db_distinct_field($table, $field, $query) { - $field_to_select = 'DISTINCT ON ('. $table .'.'. $field .") $table.$field"; - // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT). - $query = preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query); - $query = preg_replace('/(ORDER BY )(?!'. $table .'\.'. $field .')/', '\1'."$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; }