diff options
Diffstat (limited to 'modules/search/search.extender.inc')
-rw-r--r-- | modules/search/search.extender.inc | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/modules/search/search.extender.inc b/modules/search/search.extender.inc index 047853a9c..1eae2caa6 100644 --- a/modules/search/search.extender.inc +++ b/modules/search/search.extender.inc @@ -407,6 +407,7 @@ class SearchQuery extends SelectQueryExtender { return new DatabaseStatementEmpty(); } + // Add conditions to query. $this->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type'); $this->condition($this->conditions); @@ -443,4 +444,35 @@ class SearchQuery extends SelectQueryExtender { return $this->query->execute(); } + + /** + * Build the default count query for SearchQuery. + * + * Since SearchQuery always uses GROUP BY, we can default to a subquery. Also + * adding the same conditions as execute() because countQuery() is called + * first. + */ + public function countQuery() { + // Clone the inner query. + $inner = clone $this->query; + + // Add conditions to query. + $inner->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type'); + $inner->condition($this->conditions); + + // Remove existing fields and expressions, they are not needed for a count + // query. + $fields =& $inner->getFields(); + $fields = array(); + $expressions =& $inner->getExpressions(); + $expressions = array(); + + // Add the sid as the only field and count them as a subquery. + $count = db_select($inner->fields('i', array('sid')), NULL, array('target' => 'slave')); + + // Add the COUNT() expression. + $count->addExpression('COUNT(*)'); + + return $count; + } } |