[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Paginator / Adapter / DbSelect.php
blobd7527d5fba82da41166afbb69f2c85175e02dd27
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Paginator
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
22 /**
23 * @see Zend_Paginator_Adapter_Interface
25 require_once 'Zend/Paginator/Adapter/Interface.php';
27 /**
28 * @see Zend_Db
30 require_once 'Zend/Db.php';
32 /**
33 * @see Zend_Db_Select
35 require_once 'Zend/Db/Select.php';
37 /**
38 * @category Zend
39 * @package Zend_Paginator
40 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
41 * @license http://framework.zend.com/license/new-bsd New BSD License
43 class Zend_Paginator_Adapter_DbSelect implements Zend_Paginator_Adapter_Interface
45 /**
46 * Name of the row count column
48 * @var string
50 const ROW_COUNT_COLUMN = 'zend_paginator_row_count';
52 /**
53 * The COUNT query
55 * @var Zend_Db_Select
57 protected $_countSelect = null;
59 /**
60 * Database query
62 * @var Zend_Db_Select
64 protected $_select = null;
66 /**
67 * Total item count
69 * @var integer
71 protected $_rowCount = null;
73 /**
74 * Constructor.
76 * @param Zend_Db_Select $select The select query
78 public function __construct(Zend_Db_Select $select)
80 $this->_select = $select;
83 /**
84 * Sets the total row count, either directly or through a supplied
85 * query. Without setting this, {@link getPages()} selects the count
86 * as a subquery (SELECT COUNT ... FROM (SELECT ...)). While this
87 * yields an accurate count even with queries containing clauses like
88 * LIMIT, it can be slow in some circumstances. For example, in MySQL,
89 * subqueries are generally slow when using the InnoDB storage engine.
90 * Users are therefore encouraged to profile their queries to find
91 * the solution that best meets their needs.
93 * @param Zend_Db_Select|integer $totalRowCount Total row count integer
94 * or query
95 * @return Zend_Paginator_Adapter_DbSelect $this
96 * @throws Zend_Paginator_Exception
98 public function setRowCount($rowCount)
100 if ($rowCount instanceof Zend_Db_Select) {
101 $columns = $rowCount->getPart(Zend_Db_Select::COLUMNS);
103 $countColumnPart = $columns[0][1];
105 if ($countColumnPart instanceof Zend_Db_Expr) {
106 $countColumnPart = $countColumnPart->__toString();
109 $rowCountColumn = $this->_select->getAdapter()->foldCase(self::ROW_COUNT_COLUMN);
111 // The select query can contain only one column, which should be the row count column
112 if (false === strpos($countColumnPart, $rowCountColumn)) {
114 * @see Zend_Paginator_Exception
116 require_once 'Zend/Paginator/Exception.php';
118 throw new Zend_Paginator_Exception('Row count column not found');
121 $result = $rowCount->query(Zend_Db::FETCH_ASSOC)->fetch();
123 $this->_rowCount = count($result) > 0 ? $result[$rowCountColumn] : 0;
124 } else if (is_integer($rowCount)) {
125 $this->_rowCount = $rowCount;
126 } else {
128 * @see Zend_Paginator_Exception
130 require_once 'Zend/Paginator/Exception.php';
132 throw new Zend_Paginator_Exception('Invalid row count');
135 return $this;
139 * Returns an array of items for a page.
141 * @param integer $offset Page offset
142 * @param integer $itemCountPerPage Number of items per page
143 * @return array
145 public function getItems($offset, $itemCountPerPage)
147 $this->_select->limit($itemCountPerPage, $offset);
149 return $this->_select->query()->fetchAll();
153 * Returns the total number of rows in the result set.
155 * @return integer
157 public function count()
159 if ($this->_rowCount === null) {
160 $this->setRowCount(
161 $this->getCountSelect()
165 return $this->_rowCount;
169 * Get the COUNT select object for the provided query
171 * TODO: Have a look at queries that have both GROUP BY and DISTINCT specified.
172 * In that use-case I'm expecting problems when either GROUP BY or DISTINCT
173 * has one column.
175 * @return Zend_Db_Select
177 public function getCountSelect()
180 * We only need to generate a COUNT query once. It will not change for
181 * this instance.
183 if ($this->_countSelect !== null) {
184 return $this->_countSelect;
187 $rowCount = clone $this->_select;
188 $rowCount->__toString(); // Workaround for ZF-3719 and related
190 $db = $rowCount->getAdapter();
192 $countColumn = $db->quoteIdentifier($db->foldCase(self::ROW_COUNT_COLUMN));
193 $countPart = 'COUNT(1) AS ';
194 $groupPart = null;
195 $unionParts = $rowCount->getPart(Zend_Db_Select::UNION);
198 * If we're dealing with a UNION query, execute the UNION as a subquery
199 * to the COUNT query.
201 if (!empty($unionParts)) {
202 $expression = new Zend_Db_Expr($countPart . $countColumn);
204 $rowCount = $db->select()->from($rowCount, $expression);
205 } else {
206 $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS);
207 $groupParts = $rowCount->getPart(Zend_Db_Select::GROUP);
208 $havingParts = $rowCount->getPart(Zend_Db_Select::HAVING);
209 $isDistinct = $rowCount->getPart(Zend_Db_Select::DISTINCT);
212 * If there is more than one column AND it's a DISTINCT query, more
213 * than one group, or if the query has a HAVING clause, then take
214 * the original query and use it as a subquery os the COUNT query.
216 if (($isDistinct && count($columnParts) > 1) || count($groupParts) > 1 || !empty($havingParts)) {
217 $rowCount = $db->select()->from($this->_select);
218 } else if ($isDistinct) {
219 $part = $columnParts[0];
221 if ($part[1] !== Zend_Db_Select::SQL_WILDCARD && !($part[1] instanceof Zend_Db_Expr)) {
222 $column = $db->quoteIdentifier($part[1], true);
224 if (!empty($part[0])) {
225 $column = $db->quoteIdentifier($part[0], true) . '.' . $column;
228 $groupPart = $column;
230 } else if (!empty($groupParts) && $groupParts[0] !== Zend_Db_Select::SQL_WILDCARD &&
231 !($groupParts[0] instanceof Zend_Db_Expr)) {
232 $groupPart = $db->quoteIdentifier($groupParts[0], true);
236 * If the original query had a GROUP BY or a DISTINCT part and only
237 * one column was specified, create a COUNT(DISTINCT ) query instead
238 * of a regular COUNT query.
240 if (!empty($groupPart)) {
241 $countPart = 'COUNT(DISTINCT ' . $groupPart . ') AS ';
245 * Create the COUNT part of the query
247 $expression = new Zend_Db_Expr($countPart . $countColumn);
249 $rowCount->reset(Zend_Db_Select::COLUMNS)
250 ->reset(Zend_Db_Select::ORDER)
251 ->reset(Zend_Db_Select::LIMIT_OFFSET)
252 ->reset(Zend_Db_Select::GROUP)
253 ->reset(Zend_Db_Select::DISTINCT)
254 ->reset(Zend_Db_Select::HAVING)
255 ->columns($expression);
258 $this->_countSelect = $rowCount;
260 return $rowCount;