PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Paginator/Adapter/DbSelect.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 262 lines | 97 code | 37 blank | 128 comment | 23 complexity | 262b9af7af95fd4a11b3e3f2fad8deef MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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 = $columns[0][1];
  94. if ($countColumnPart instanceof Zend_Db_Expr) {
  95. $countColumnPart = $countColumnPart->__toString();
  96. }
  97. $rowCountColumn = $this->_select->getAdapter()->foldCase(self::ROW_COUNT_COLUMN);
  98. // The select query can contain only one column, which should be the row count column
  99. if (false === strpos($countColumnPart, $rowCountColumn)) {
  100. /**
  101. * @see Zend_Paginator_Exception
  102. */
  103. #require_once 'Zend/Paginator/Exception.php';
  104. throw new Zend_Paginator_Exception('Row count column not found');
  105. }
  106. $result = $rowCount->query(Zend_Db::FETCH_ASSOC)->fetch();
  107. $this->_rowCount = count($result) > 0 ? $result[$rowCountColumn] : 0;
  108. } else if (is_integer($rowCount)) {
  109. $this->_rowCount = $rowCount;
  110. } else {
  111. /**
  112. * @see Zend_Paginator_Exception
  113. */
  114. #require_once 'Zend/Paginator/Exception.php';
  115. throw new Zend_Paginator_Exception('Invalid row count');
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Returns an array of items for a page.
  121. *
  122. * @param integer $offset Page offset
  123. * @param integer $itemCountPerPage Number of items per page
  124. * @return array
  125. */
  126. public function getItems($offset, $itemCountPerPage)
  127. {
  128. $this->_select->limit($itemCountPerPage, $offset);
  129. return $this->_select->query()->fetchAll();
  130. }
  131. /**
  132. * Returns the total number of rows in the result set.
  133. *
  134. * @return integer
  135. */
  136. public function count()
  137. {
  138. if ($this->_rowCount === null) {
  139. $this->setRowCount(
  140. $this->getCountSelect()
  141. );
  142. }
  143. return $this->_rowCount;
  144. }
  145. /**
  146. * Get the COUNT select object for the provided query
  147. *
  148. * TODO: Have a look at queries that have both GROUP BY and DISTINCT specified.
  149. * In that use-case I'm expecting problems when either GROUP BY or DISTINCT
  150. * has one column.
  151. *
  152. * @return Zend_Db_Select
  153. */
  154. public function getCountSelect()
  155. {
  156. /**
  157. * We only need to generate a COUNT query once. It will not change for
  158. * this instance.
  159. */
  160. if ($this->_countSelect !== null) {
  161. return $this->_countSelect;
  162. }
  163. $rowCount = clone $this->_select;
  164. $rowCount->__toString(); // Workaround for ZF-3719 and related
  165. $db = $rowCount->getAdapter();
  166. $countColumn = $db->quoteIdentifier($db->foldCase(self::ROW_COUNT_COLUMN));
  167. $countPart = 'COUNT(1) AS ';
  168. $groupPart = null;
  169. $unionParts = $rowCount->getPart(Zend_Db_Select::UNION);
  170. /**
  171. * If we're dealing with a UNION query, execute the UNION as a subquery
  172. * to the COUNT query.
  173. */
  174. if (!empty($unionParts)) {
  175. $expression = new Zend_Db_Expr($countPart . $countColumn);
  176. $rowCount = $db->select()->from($rowCount, $expression);
  177. } else {
  178. $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS);
  179. $groupParts = $rowCount->getPart(Zend_Db_Select::GROUP);
  180. $havingParts = $rowCount->getPart(Zend_Db_Select::HAVING);
  181. $isDistinct = $rowCount->getPart(Zend_Db_Select::DISTINCT);
  182. /**
  183. * If there is more than one column AND it's a DISTINCT query, more
  184. * than one group, or if the query has a HAVING clause, then take
  185. * the original query and use it as a subquery os the COUNT query.
  186. */
  187. if (($isDistinct && count($columnParts) > 1) || count($groupParts) > 1 || !empty($havingParts)) {
  188. $rowCount = $db->select()->from($this->_select);
  189. } else if ($isDistinct) {
  190. $part = $columnParts[0];
  191. if ($part[1] !== Zend_Db_Select::SQL_WILDCARD && !($part[1] instanceof Zend_Db_Expr)) {
  192. $column = $db->quoteIdentifier($part[1], true);
  193. if (!empty($part[0])) {
  194. $column = $db->quoteIdentifier($part[0], true) . '.' . $column;
  195. }
  196. $groupPart = $column;
  197. }
  198. } else if (!empty($groupParts) && $groupParts[0] !== Zend_Db_Select::SQL_WILDCARD &&
  199. !($groupParts[0] instanceof Zend_Db_Expr)) {
  200. $groupPart = $db->quoteIdentifier($groupParts[0], true);
  201. }
  202. /**
  203. * If the original query had a GROUP BY or a DISTINCT part and only
  204. * one column was specified, create a COUNT(DISTINCT ) query instead
  205. * of a regular COUNT query.
  206. */
  207. if (!empty($groupPart)) {
  208. $countPart = 'COUNT(DISTINCT ' . $groupPart . ') AS ';
  209. }
  210. /**
  211. * Create the COUNT part of the query
  212. */
  213. $expression = new Zend_Db_Expr($countPart . $countColumn);
  214. $rowCount->reset(Zend_Db_Select::COLUMNS)
  215. ->reset(Zend_Db_Select::ORDER)
  216. ->reset(Zend_Db_Select::LIMIT_OFFSET)
  217. ->reset(Zend_Db_Select::GROUP)
  218. ->reset(Zend_Db_Select::DISTINCT)
  219. ->reset(Zend_Db_Select::HAVING)
  220. ->columns($expression);
  221. }
  222. $this->_countSelect = $rowCount;
  223. return $rowCount;
  224. }
  225. }