diff -rup ../drupal-6.x-dev-orig/includes/database.inc ./includes/database.inc --- ../drupal-6.x-dev-orig/includes/database.inc 2009-09-14 03:49:34.000000000 -0700 +++ ./includes/database.inc 2009-11-18 21:54:41.000000000 -0800 @@ -339,6 +339,10 @@ function db_rewrite_sql($query, $primary ((?P # Everything within this set of parentheses is named "anonymous view" (?: + \s\*\sFROM\s\(SELECT # This is a special case for pgsql DISTINCT + # clause subselects, that prevents the re from + # missing. + | [^()]++ # anything not parentheses | \( (?P>anonymous_view) \) # an open parenthesis, more "anonymous view" and finally a close parenthesis. @@ -371,7 +375,12 @@ function db_rewrite_sql($query, $primary } if ($matches) { if ($pos === FALSE) { - $query = $first_part . $second_part .')'; + if ($distinct && preg_match('/(.*) node/s', $second_part, $pg_matches)) { + $query = $first_part . $pg_matches[1] .') node'; + } + else { + $query = $first_part . $second_part .')'; + } } else { $query = $first_part . substr($second_part, 0, -$pos) .')'. substr($second_part, -$pos); diff -rup ../drupal-6.x-dev-orig/includes/database.mysqli.inc ./includes/database.mysqli.inc --- ../drupal-6.x-dev-orig/includes/database.mysqli.inc 2009-07-21 01:52:30.000000000 -0700 +++ ./includes/database.mysqli.inc 2009-11-18 21:58:18.000000000 -0800 @@ -367,7 +367,7 @@ function db_distinct_field($table, $fiel $matches = array(); if (preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) { $select = preg_replace( - '/((?:^|,)\s*)(? t('Database functionality'), + 'description' => t('Exercise the database helper functions, this not not test the database but the functions that abstract the database.'), + 'group' => t('Database'), + ); + } + + + /** + * Test the db_distinct_field() function. + * + * See http://drupal.org/node/284392 for more information + */ + function testDbDistinctField() { + + // These assertions are taken from: + // http://drupal.org/node/284392 + + $assertions[] = array( + 'input' => array("table", "field", "SELECT table.field FROM table"), + 'expected' => 'SELECT DISTINCT(table.field) FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT table.field AS table_field FROM table"), + 'expected' => 'SELECT DISTINCT(table.field) AS table_field FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT DISTINCT(table.field) FROM table"), + 'expected' => 'SELECT DISTINCT(table.field) FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT DISTINCT(table.field) AS table_field FROM table"), + 'expected' => 'SELECT DISTINCT(table.field) AS table_field FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT table.field,table.field2 FROM table"), + 'expected' => 'SELECT DISTINCT(table.field),table.field2 FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT table.field AS table_field, table.field2 AS table_field2 FROM table"), + 'expected' => 'SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT DISTINCT(table.field),table.field2 FROM table"), + 'expected' => 'SELECT DISTINCT(table.field),table.field2 FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table"), + 'expected' => 'SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT COUNT(table.field) FROM table"), + 'expected' => 'SELECT COUNT(DISTINCT(table.field)) FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT COUNT(table.field) AS table_field FROM table"), + 'expected' => 'SELECT COUNT(DISTINCT(table.field)) AS table_field FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT table.field1, COUNT(table.field), table.field2 FROM table"), + 'expected' => 'SELECT table.field1, COUNT(DISTINCT(table.field)), table.field2 FROM table', + ); + + $assertions[] = array( + 'input' => array("table", "field", "SELECT COUNT(DISTINCT(table.field)) FROM table"), + 'expected' => 'SELECT COUNT(DISTINCT(table.field)) FROM table', + ); + + + foreach ($assertions as $assert) { + $distinct_added = call_user_func_array('db_distinct_field', $assert['input']); + if (!$this->assertEqual( + $distinct_added, + $assert['expected'], + t('Add DISTINCT to %query', array('%query' => $assert['input'][2])) + )) { + $this->pass(t('Query was rewritten to: %query, expected query: %expected_query', array('%query' => $distinct_added, '%expected_query' => $assert['expected']))); + } + } + + + + + + } + + +}