diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-02-17 05:24:53 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-02-17 05:24:53 +0000 |
commit | dcb47ed24d54665f87ce61bb1f8dc9761a9f219b (patch) | |
tree | 3e5aabea89c91976c65647b159a755ec77226a09 /includes/database | |
parent | 89ade19964b1b330b4e26046b4fd259272d94abe (diff) | |
download | brdo-dcb47ed24d54665f87ce61bb1f8dc9761a9f219b.tar.gz brdo-dcb47ed24d54665f87ce61bb1f8dc9761a9f219b.tar.bz2 |
#706248 by bellHead and Damien Tournoud: Fixed field_sql_storage_field_storage_query() with 'count' option breaks on PostgreSQL and SQLite. (with tests)
Diffstat (limited to 'includes/database')
-rw-r--r-- | includes/database/select.inc | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/includes/database/select.inc b/includes/database/select.inc index f3fd8368a..b071c7454 100644 --- a/includes/database/select.inc +++ b/includes/database/select.inc @@ -1250,17 +1250,19 @@ class SelectQuery extends Query implements SelectQueryInterface { // Create our new query object that we will mutate into a count query. $count = clone($this); - // Zero-out existing fields and expressions. - $fields =& $count->getFields(); - $fields = array(); - $expressions =& $count->getExpressions(); - $expressions = array(); - + if (!$count->distinct) { + // When not executing a distinct query, we can zero-out existing fields + // and expressions. + $fields =& $count->getFields(); + $fields = array(); + $expressions =& $count->getExpressions(); + $expressions = array(); - // Also remove 'all_fields' statements, which are expanded into tablename.* - // when the query is executed. - foreach ($count->tables as $alias => &$table) { - unset($table['all_fields']); + // Also remove 'all_fields' statements, which are expanded into tablename.* + // when the query is executed. + foreach ($count->tables as $alias => &$table) { + unset($table['all_fields']); + } } // Ordering a count query is a waste of cycles, and breaks on some @@ -1268,6 +1270,13 @@ class SelectQuery extends Query implements SelectQueryInterface { $orders = &$count->getOrderBy(); $orders = array(); + if ($count->distinct) { + // If the query is distinct, we need to execute it in a subquery, + // because SQL99 does not support counting on distinct multiple fields. + $count = db_select($count); + $count->distinct = FALSE; + } + // COUNT() is an expression, so we add that back in. $count->addExpression('COUNT(*)'); |