PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Paginator/Adapter/DbSelect.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 290 lines | 117 code | 39 blank | 134 comment | 26 complexity | bfe13c8fe7a37eb8ddf393c2ace18d7a MD5 | raw file
  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: DbSelect.php 24754 2012-05-05 02:30:56Z adamlundrigan $
  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. * Identifies this adapter for caching purposes. This value will remain constant for
  67. * the entire life of this adapter regardless of how many different pages are queried.
  68. *
  69. * @var string
  70. */
  71. protected $_cacheIdentifier = null;
  72. /**
  73. * Constructor.
  74. *
  75. * @param Zend_Db_Select $select The select query
  76. */
  77. public function __construct(Zend_Db_Select $select)
  78. {
  79. $this->_select = $select;
  80. $this->_cacheIdentifier = md5($select->assemble());
  81. }
  82. /**
  83. * Returns the cache identifier.
  84. *
  85. * @return string
  86. */
  87. public function getCacheIdentifier()
  88. {
  89. return $this->_cacheIdentifier;
  90. }
  91. /**
  92. * Sets the total row count, either directly or through a supplied
  93. * query. Without setting this, {@link getPages()} selects the count
  94. * as a subquery (SELECT COUNT ... FROM (SELECT ...)). While this
  95. * yields an accurate count even with queries containing clauses like
  96. * LIMIT, it can be slow in some circumstances. For example, in MySQL,
  97. * subqueries are generally slow when using the InnoDB storage engine.
  98. * Users are therefore encouraged to profile their queries to find
  99. * the solution that best meets their needs.
  100. *
  101. * @param Zend_Db_Select|integer $totalRowCount Total row count integer
  102. * or query
  103. * @return Zend_Paginator_Adapter_DbSelect $this
  104. * @throws Zend_Paginator_Exception
  105. */
  106. public function setRowCount($rowCount)
  107. {
  108. if ($rowCount instanceof Zend_Db_Select) {
  109. $columns = $rowCount->getPart(Zend_Db_Select::COLUMNS);
  110. $countColumnPart = empty($columns[0][2])
  111. ? $columns[0][1]
  112. : $columns[0][2];
  113. if ($countColumnPart instanceof Zend_Db_Expr) {
  114. $countColumnPart = $countColumnPart->__toString();
  115. }
  116. $rowCountColumn = $this->_select->getAdapter()->foldCase(self::ROW_COUNT_COLUMN);
  117. // The select query can contain only one column, which should be the row count column
  118. if (false === strpos($countColumnPart, $rowCountColumn)) {
  119. /**
  120. * @see Zend_Paginator_Exception
  121. */
  122. require_once 'Zend/Paginator/Exception.php';
  123. throw new Zend_Paginator_Exception('Row count column not found');
  124. }
  125. $result = $rowCount->query(Zend_Db::FETCH_ASSOC)->fetch();
  126. $this->_rowCount = count($result) > 0 ? $result[$rowCountColumn] : 0;
  127. } else if (is_integer($rowCount)) {
  128. $this->_rowCount = $rowCount;
  129. } else {
  130. /**
  131. * @see Zend_Paginator_Exception
  132. */
  133. require_once 'Zend/Paginator/Exception.php';
  134. throw new Zend_Paginator_Exception('Invalid row count');
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Returns an array of items for a page.
  140. *
  141. * @param integer $offset Page offset
  142. * @param integer $itemCountPerPage Number of items per page
  143. * @return array
  144. */
  145. public function getItems($offset, $itemCountPerPage)
  146. {
  147. $this->_select->limit($itemCountPerPage, $offset);
  148. return $this->_select->query()->fetchAll();
  149. }
  150. /**
  151. * Returns the total number of rows in the result set.
  152. *
  153. * @return integer
  154. */
  155. public function count()
  156. {
  157. if ($this->_rowCount === null) {
  158. $this->setRowCount(
  159. $this->getCountSelect()
  160. );
  161. }
  162. return $this->_rowCount;
  163. }
  164. /**
  165. * Get the COUNT select object for the provided query
  166. *
  167. * TODO: Have a look at queries that have both GROUP BY and DISTINCT specified.
  168. * In that use-case I'm expecting problems when either GROUP BY or DISTINCT
  169. * has one column.
  170. *
  171. * @return Zend_Db_Select
  172. */
  173. public function getCountSelect()
  174. {
  175. /**
  176. * We only need to generate a COUNT query once. It will not change for
  177. * this instance.
  178. */
  179. if ($this->_countSelect !== null) {
  180. return $this->_countSelect;
  181. }
  182. $rowCount = clone $this->_select;
  183. $rowCount->__toString(); // Workaround for ZF-3719 and related
  184. $db = $rowCount->getAdapter();
  185. $countColumn = $db->quoteIdentifier($db->foldCase(self::ROW_COUNT_COLUMN));
  186. $countPart = 'COUNT(1) AS ';
  187. $groupPart = null;
  188. $unionParts = $rowCount->getPart(Zend_Db_Select::UNION);
  189. /**
  190. * If we're dealing with a UNION query, execute the UNION as a subquery
  191. * to the COUNT query.
  192. */
  193. if (!empty($unionParts)) {
  194. $expression = new Zend_Db_Expr($countPart . $countColumn);
  195. $rowCount = $db
  196. ->select()
  197. ->bind($rowCount->getBind())
  198. ->from($rowCount, $expression);
  199. } else {
  200. $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS);
  201. $groupParts = $rowCount->getPart(Zend_Db_Select::GROUP);
  202. $havingParts = $rowCount->getPart(Zend_Db_Select::HAVING);
  203. $isDistinct = $rowCount->getPart(Zend_Db_Select::DISTINCT);
  204. /**
  205. * If there is more than one column AND it's a DISTINCT query, more
  206. * than one group, or if the query has a HAVING clause, then take
  207. * the original query and use it as a subquery os the COUNT query.
  208. */
  209. if (($isDistinct && ((count($columnParts) == 1 && $columnParts[0][1] == Zend_Db_Select::SQL_WILDCARD)
  210. || count($columnParts) > 1)) || count($groupParts) > 1 || !empty($havingParts)) {
  211. $rowCount->reset(Zend_Db_Select::ORDER);
  212. $rowCount = $db
  213. ->select()
  214. ->bind($rowCount->getBind())
  215. ->from($rowCount);
  216. } else if ($isDistinct) {
  217. $part = $columnParts[0];
  218. if ($part[1] !== Zend_Db_Select::SQL_WILDCARD && !($part[1] instanceof Zend_Db_Expr)) {
  219. $column = $db->quoteIdentifier($part[1], true);
  220. if (!empty($part[0])) {
  221. $column = $db->quoteIdentifier($part[0], true) . '.' . $column;
  222. }
  223. $groupPart = $column;
  224. }
  225. } else if (!empty($groupParts)) {
  226. $groupPart = $db->quoteIdentifier($groupParts[0], true);
  227. }
  228. /**
  229. * If the original query had a GROUP BY or a DISTINCT part and only
  230. * one column was specified, create a COUNT(DISTINCT ) query instead
  231. * of a regular COUNT query.
  232. */
  233. if (!empty($groupPart)) {
  234. $countPart = 'COUNT(DISTINCT ' . $groupPart . ') AS ';
  235. }
  236. /**
  237. * Create the COUNT part of the query
  238. */
  239. $expression = new Zend_Db_Expr($countPart . $countColumn);
  240. $rowCount->reset(Zend_Db_Select::COLUMNS)
  241. ->reset(Zend_Db_Select::ORDER)
  242. ->reset(Zend_Db_Select::LIMIT_OFFSET)
  243. ->reset(Zend_Db_Select::GROUP)
  244. ->reset(Zend_Db_Select::DISTINCT)
  245. ->reset(Zend_Db_Select::HAVING)
  246. ->columns($expression);
  247. }
  248. $this->_countSelect = $rowCount;
  249. return $rowCount;
  250. }
  251. }