comparison 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
comparison
equal deleted inserted replaced
6:2cfdc3c92142 7:fff6d4c8c043
1 <?php 1 <?php
2 // $Id: database.pgsql.inc,v 1.68.2.1 2008/02/07 10:17:26 goba Exp $ 2 // $Id: database.pgsql.inc,v 1.68.2.2 2008/07/08 09:50:03 goba Exp $
3 3
4 /** 4 /**
5 * @file 5 * @file
6 * Database interface code for PostgreSQL database servers. 6 * Database interface code for PostgreSQL database servers.
7 */ 7 */
416 * @param $field Field to set as DISTINCT 416 * @param $field Field to set as DISTINCT
417 * @param $query Query to apply the wrapper to 417 * @param $query Query to apply the wrapper to
418 * @return SQL query with the DISTINCT wrapper surrounding the given table.field. 418 * @return SQL query with the DISTINCT wrapper surrounding the given table.field.
419 */ 419 */
420 function db_distinct_field($table, $field, $query) { 420 function db_distinct_field($table, $field, $query) {
421 $field_to_select = 'DISTINCT ON ('. $table .'.'. $field .") $table.$field"; 421 if (!preg_match('/FROM\s+\S+\s+AS/si', $query)
422 // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT). 422 && !preg_match('/DISTINCT\s+ON\s*\(\s*(' . $table . '\s*\.\s*)?' . $field . '\s*\)/si', $query)
423 $query = preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query); 423 && !preg_match('/DISTINCT[ (]' . $field . '/si', $query)
424 $query = preg_replace('/(ORDER BY )(?!'. $table .'\.'. $field .')/', '\1'."$table.$field, ", $query); 424 && preg_match('/(.*FROM\s+)(.*?\s)(\s*(WHERE|GROUP|HAVING|ORDER|LIMIT|FOR).*)/Asi', $query, $m)) {
425 $query = $m[1];
426 $query .= preg_replace('/([\{\w+\}]+)\s+(' . $table . ')\s/Usi', '(SELECT DISTINCT ON (' . $field . ') * FROM \1) \2 ', $m[2]);
427 $query .= $m[3];
428 }
425 return $query; 429 return $query;
426 } 430 }
427 431
428 /** 432 /**
429 * @} End of "ingroup database". 433 * @} End of "ingroup database".