/** * Sets the total row count, either directly or through a supplied * query. Without setting this, {@link getPages()} selects the count * as a subquery (SELECT COUNT ... FROM (SELECT ...)). While this * yields an accurate count even with queries containing clauses like * LIMIT, it can be slow in some circumstances. For example, in MySQL, * subqueries are generally slow when using the InnoDB storage engine. * Users are therefore encouraged to profile their queries to find * the solution that best meets their needs. * * @param \Zend\Db\Sql\Select|integer $rowCount Total row count integer * or query * @throws Exception\InvalidArgumentException * @return DbSelect */ public function setRowCount($rowCount) { if ($rowCount instanceof Sql\Select) { $columns = $rowCount->getPart(Sql\Select::COLUMNS); $countColumnPart = $columns[0][1]; if ($countColumnPart instanceof Sql\ExpressionInterface) { $countColumnPart = $countColumnPart->__toString(); } $rowCountColumn = $this->select->getAdapter()->foldCase(self::ROW_COUNT_COLUMN); // The select query can contain only one column, which should be the row count column if (false === strpos($countColumnPart, $rowCountColumn)) { throw new Exception\InvalidArgumentException('Row count column not found'); } $result = $rowCount->query(Db\Db::FETCH_ASSOC)->fetch(); $this->rowCount = count($result) > 0 ? $result[$rowCountColumn] : 0; } elseif (is_integer($rowCount)) { $this->rowCount = $rowCount; } else { throw new Exception\InvalidArgumentException('Invalid row count'); } return $this; }