2
0

DbSelect.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Paginator
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Paginator_Adapter_Interface
  23. */
  24. require_once 'Zend/Paginator/Adapter/Interface.php';
  25. /**
  26. * @see Zend_Db
  27. */
  28. require_once 'Zend/Db.php';
  29. /**
  30. * @see Zend_Db_Select
  31. */
  32. require_once 'Zend/Db/Select.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Paginator
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Paginator_Adapter_DbSelect implements Zend_Paginator_Adapter_Interface
  40. {
  41. /**
  42. * Name of the row count column
  43. *
  44. * @var string
  45. */
  46. const ROW_COUNT_COLUMN = 'zend_paginator_row_count';
  47. /**
  48. * The COUNT query
  49. *
  50. * @var Zend_Db_Select
  51. */
  52. protected $_countSelect = null;
  53. /**
  54. * Database query
  55. *
  56. * @var Zend_Db_Select
  57. */
  58. protected $_select = null;
  59. /**
  60. * Total item count
  61. *
  62. * @var integer
  63. */
  64. protected $_rowCount = null;
  65. /**
  66. * Constructor.
  67. *
  68. * @param Zend_Db_Select $select The select query
  69. */
  70. public function __construct(Zend_Db_Select $select)
  71. {
  72. $this->_select = $select;
  73. }
  74. /**
  75. * Sets the total row count, either directly or through a supplied
  76. * query. Without setting this, {@link getPages()} selects the count
  77. * as a subquery (SELECT COUNT ... FROM (SELECT ...)). While this
  78. * yields an accurate count even with queries containing clauses like
  79. * LIMIT, it can be slow in some circumstances. For example, in MySQL,
  80. * subqueries are generally slow when using the InnoDB storage engine.
  81. * Users are therefore encouraged to profile their queries to find
  82. * the solution that best meets their needs.
  83. *
  84. * @param Zend_Db_Select|integer $totalRowCount Total row count integer
  85. * or query
  86. * @return Zend_Paginator_Adapter_DbSelect $this
  87. * @throws Zend_Paginator_Exception
  88. */
  89. public function setRowCount($rowCount)
  90. {
  91. if ($rowCount instanceof Zend_Db_Select) {
  92. $columns = $rowCount->getPart(Zend_Db_Select::COLUMNS);
  93. $countColumnPart = empty($columns[0][2])
  94. ? $columns[0][1]
  95. : $columns[0][2];
  96. if ($countColumnPart instanceof Zend_Db_Expr) {
  97. $countColumnPart = $countColumnPart->__toString();
  98. }
  99. $rowCountColumn = $this->_select->getAdapter()->foldCase(self::ROW_COUNT_COLUMN);
  100. // The select query can contain only one column, which should be the row count column
  101. if (false === strpos($countColumnPart, $rowCountColumn)) {
  102. /**
  103. * @see Zend_Paginator_Exception
  104. */
  105. require_once 'Zend/Paginator/Exception.php';
  106. throw new Zend_Paginator_Exception('Row count column not found');
  107. }
  108. $result = $rowCount->query(Zend_Db::FETCH_ASSOC)->fetch();
  109. $this->_rowCount = count($result) > 0 ? $result[$rowCountColumn] : 0;
  110. } else if (is_integer($rowCount)) {
  111. $this->_rowCount = $rowCount;
  112. } else {
  113. /**
  114. * @see Zend_Paginator_Exception
  115. */
  116. require_once 'Zend/Paginator/Exception.php';
  117. throw new Zend_Paginator_Exception('Invalid row count');
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Returns an array of items for a page.
  123. *
  124. * @param integer $offset Page offset
  125. * @param integer $itemCountPerPage Number of items per page
  126. * @return array
  127. */
  128. public function getItems($offset, $itemCountPerPage)
  129. {
  130. $this->_select->limit($itemCountPerPage, $offset);
  131. return $this->_select->query()->fetchAll();
  132. }
  133. /**
  134. * Returns the total number of rows in the result set.
  135. *
  136. * @return integer
  137. */
  138. public function count()
  139. {
  140. if ($this->_rowCount === null) {
  141. $this->setRowCount(
  142. $this->getCountSelect()
  143. );
  144. }
  145. return $this->_rowCount;
  146. }
  147. /**
  148. * Get the COUNT select object for the provided query
  149. *
  150. * TODO: Have a look at queries that have both GROUP BY and DISTINCT specified.
  151. * In that use-case I'm expecting problems when either GROUP BY or DISTINCT
  152. * has one column.
  153. *
  154. * @return Zend_Db_Select
  155. */
  156. public function getCountSelect()
  157. {
  158. /**
  159. * We only need to generate a COUNT query once. It will not change for
  160. * this instance.
  161. */
  162. if ($this->_countSelect !== null) {
  163. return $this->_countSelect;
  164. }
  165. $rowCount = clone $this->_select;
  166. $rowCount->__toString(); // Workaround for ZF-3719 and related
  167. $db = $rowCount->getAdapter();
  168. $countColumn = $db->quoteIdentifier($db->foldCase(self::ROW_COUNT_COLUMN));
  169. $countPart = 'COUNT(1) AS ';
  170. $groupPart = null;
  171. $unionParts = $rowCount->getPart(Zend_Db_Select::UNION);
  172. /**
  173. * If we're dealing with a UNION query, execute the UNION as a subquery
  174. * to the COUNT query.
  175. */
  176. if (!empty($unionParts)) {
  177. $expression = new Zend_Db_Expr($countPart . $countColumn);
  178. $rowCount = $db
  179. ->select()
  180. ->bind($rowCount->getBind())
  181. ->from($rowCount, $expression);
  182. } else {
  183. $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS);
  184. $groupParts = $rowCount->getPart(Zend_Db_Select::GROUP);
  185. $havingParts = $rowCount->getPart(Zend_Db_Select::HAVING);
  186. $isDistinct = $rowCount->getPart(Zend_Db_Select::DISTINCT);
  187. /**
  188. * If there is more than one column AND it's a DISTINCT query, more
  189. * than one group, or if the query has a HAVING clause, then take
  190. * the original query and use it as a subquery os the COUNT query.
  191. */
  192. if (($isDistinct && ((count($columnParts) == 1 && $columnParts[0][1] == Zend_Db_Select::SQL_WILDCARD)
  193. || count($columnParts) > 1)) || count($groupParts) > 1 || !empty($havingParts)) {
  194. $rowCount->reset(Zend_Db_Select::ORDER);
  195. $rowCount = $db
  196. ->select()
  197. ->bind($rowCount->getBind())
  198. ->from($rowCount);
  199. } else if ($isDistinct) {
  200. $part = $columnParts[0];
  201. if ($part[1] !== Zend_Db_Select::SQL_WILDCARD && !($part[1] instanceof Zend_Db_Expr)) {
  202. $column = $db->quoteIdentifier($part[1], true);
  203. if (!empty($part[0])) {
  204. $column = $db->quoteIdentifier($part[0], true) . '.' . $column;
  205. }
  206. $groupPart = $column;
  207. }
  208. } else if (!empty($groupParts)) {
  209. $groupPart = $db->quoteIdentifier($groupParts[0], true);
  210. }
  211. /**
  212. * If the original query had a GROUP BY or a DISTINCT part and only
  213. * one column was specified, create a COUNT(DISTINCT ) query instead
  214. * of a regular COUNT query.
  215. */
  216. if (!empty($groupPart)) {
  217. $countPart = 'COUNT(DISTINCT ' . $groupPart . ') AS ';
  218. }
  219. /**
  220. * Create the COUNT part of the query
  221. */
  222. $expression = new Zend_Db_Expr($countPart . $countColumn);
  223. $rowCount->reset(Zend_Db_Select::COLUMNS)
  224. ->reset(Zend_Db_Select::ORDER)
  225. ->reset(Zend_Db_Select::LIMIT_OFFSET)
  226. ->reset(Zend_Db_Select::GROUP)
  227. ->reset(Zend_Db_Select::DISTINCT)
  228. ->reset(Zend_Db_Select::HAVING)
  229. ->columns($expression);
  230. }
  231. $this->_countSelect = $rowCount;
  232. return $rowCount;
  233. }
  234. }