Implemented conservative count, implemented count 'cache'

This commit is contained in:
Thomas Gelf 2013-08-20 23:15:18 +02:00 committed by Eric Lippmann
parent 0f48d0e2d6
commit 1d1214b8b9

View File

@ -32,6 +32,12 @@ class Query extends AbstractQuery
*/ */
protected $countColumns; protected $countColumns;
protected $uglySlowConservativeCount = false;
protected $countCache;
protected $maxCount;
protected function init() protected function init()
{ {
$this->db = $this->ds->getConnection()->getDb(); $this->db = $this->ds->getConnection()->getDb();
@ -60,7 +66,6 @@ class Query extends AbstractQuery
protected function createQueryObjects() protected function createQueryObjects()
{ {
$this->beforeCreatingCountQuery();
$this->beforeCreatingSelectQuery(); $this->beforeCreatingSelectQuery();
$this->selectQuery = clone($this->baseQuery); $this->selectQuery = clone($this->baseQuery);
$this->selectQuery->columns($this->columns); $this->selectQuery->columns($this->columns);
@ -74,11 +79,28 @@ class Query extends AbstractQuery
} }
} }
$this->countQuery = clone($this->baseQuery); $this->beforeCreatingCountQuery();
if ($this->countColumns === null) { if ($this->uglySlowConservativeCount) {
$this->countColumns = array('cnt' => 'COUNT(*)'); $query = clone($this->selectQuery);
if ($this->maxCount === null) {
$this->countQuery = $this->db->select()->from(
$query,
'COUNT(*)'
);
} else {
$this->countQuery = $this->db->select()->from(
$query->reset('order')->limit($this->maxCount),
'COUNT(*)'
);
}
} else {
$this->countQuery = clone($this->baseQuery);
if ($this->countColumns === null) {
$this->countColumns = array('cnt' => 'COUNT(*)');
}
$this->countQuery->columns($this->countColumns);
} }
$this->countQuery->columns($this->countColumns);
} }
protected function beforeCreatingCountQuery() protected function beforeCreatingCountQuery()
@ -91,7 +113,10 @@ class Query extends AbstractQuery
public function count() public function count()
{ {
return $this->db->fetchOne($this->getCountQuery()); if ($this->countCache === null) {
$this->countCache = $this->db->fetchOne($this->getCountQuery());
}
return $this->countCache;
} }
public function fetchAll() public function fetchAll()