/**
* Returns a record count for the query.
*
* Note: Current implementation of this method assumes that getListQuery() returns a set of unique rows,
* thus it uses SELECT COUNT(*) to count the rows. In cases that getListQuery() uses DISTINCT
* then either this method must be overridden by a custom implementation at the derived Model Class
* or a GROUP BY clause should be used to make the set unique.
*
* @param DatabaseQuery|string $query The query.
*
* @return integer Number of rows for query.
*
* @since 3.0
*/
protected function _getListCount($query)
{
// Use fast COUNT(*) on DatabaseQuery objects if there is no GROUP BY or HAVING clause:
if ($query instanceof DatabaseQuery && $query->type === 'select' && $query->group === null && $query->merge === null && $query->querySet === null && $query->having === null) {
$query = clone $query;
$query->clear('select')->clear('order')->clear('limit')->clear('offset')->select('COUNT(*)');
$this->getDbo()->setQuery($query);
return (int) $this->getDbo()->loadResult();
}
// Otherwise fall back to inefficient way of counting all results.
// Remove the limit, offset and order parts if it's a DatabaseQuery object
if ($query instanceof DatabaseQuery) {
$query = clone $query;
$query->clear('limit')->clear('offset')->clear('order');
}
$this->getDbo()->setQuery($query);
$this->getDbo()->execute();
return (int) $this->getDbo()->getNumRows();
}