DbSelect.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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-2008 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-2008 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. * Database query
  49. *
  50. * @var Zend_Db_Select
  51. */
  52. protected $_select = null;
  53. /**
  54. * Total item count
  55. *
  56. * @var integer
  57. */
  58. protected $_rowCount = null;
  59. /**
  60. * Constructor.
  61. *
  62. * @param Zend_Db_Select $select The select query
  63. */
  64. public function __construct(Zend_Db_Select $select)
  65. {
  66. $this->_select = $select;
  67. }
  68. /**
  69. * Sets the total row count, either directly or through a supplied
  70. * query. Without setting this, {@link getPages()} selects the count
  71. * as a subquery (SELECT COUNT ... FROM (SELECT ...)). While this
  72. * yields an accurate count even with queries containing clauses like
  73. * LIMIT, it can be slow in some circumstances. For example, in MySQL,
  74. * subqueries are generally slow when using the InnoDB storage engine.
  75. * Users are therefore encouraged to profile their queries to find
  76. * the solution that best meets their needs.
  77. *
  78. * @param Zend_Db_Select|integer $totalRowCount Total row count integer
  79. * or query
  80. * @return Zend_Paginator_Adapter_DbSelect $this
  81. * @throws Zend_Paginator_Exception
  82. */
  83. public function setRowCount($rowCount)
  84. {
  85. if ($rowCount instanceof Zend_Db_Select) {
  86. $columns = $rowCount->getPart(Zend_Db_Select::COLUMNS);
  87. $countColumnPart = $columns[0][1];
  88. if ($countColumnPart instanceof Zend_Db_Expr) {
  89. $countColumnPart = $countColumnPart->__toString();
  90. }
  91. $rowCountColumn = $this->_select->getAdapter()->foldCase(self::ROW_COUNT_COLUMN);
  92. // The select query can contain only one column, which should be the row count column
  93. if (false === strpos($countColumnPart, $rowCountColumn)) {
  94. /**
  95. * @see Zend_Paginator_Exception
  96. */
  97. require_once 'Zend/Paginator/Exception.php';
  98. throw new Zend_Paginator_Exception('Row count column not found');
  99. }
  100. $result = $rowCount->query(Zend_Db::FETCH_ASSOC)->fetch();
  101. $this->_rowCount = count($result) > 0 ? $result[$rowCountColumn] : 0;
  102. } else if (is_integer($rowCount)) {
  103. $this->_rowCount = $rowCount;
  104. } else {
  105. /**
  106. * @see Zend_Paginator_Exception
  107. */
  108. require_once 'Zend/Paginator/Exception.php';
  109. throw new Zend_Paginator_Exception('Invalid row count');
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Returns an array of items for a page.
  115. *
  116. * @param integer $offset Page offset
  117. * @param integer $itemCountPerPage Number of items per page
  118. * @return array
  119. */
  120. public function getItems($offset, $itemCountPerPage)
  121. {
  122. $this->_select->limit($itemCountPerPage, $offset);
  123. return $this->_select->query()->fetchAll();
  124. }
  125. /**
  126. * Returns the total number of rows in the result set.
  127. *
  128. * @return integer
  129. */
  130. public function count()
  131. {
  132. if ($this->_rowCount === null) {
  133. $rowCount = clone $this->_select;
  134. $db = $rowCount->getAdapter();
  135. /**
  136. * The DISTINCT and GROUP BY queries only work when selecting one column.
  137. * The question is whether any RDBMS supports DISTINCT for multiple columns, without workarounds.
  138. */
  139. if (true === $rowCount->getPart(Zend_Db_Select::DISTINCT)) {
  140. $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS);
  141. $columns = array();
  142. foreach ($columnParts as $part) {
  143. if ($part[1] == Zend_Db_Select::SQL_WILDCARD || $part[1] instanceof Zend_Db_Expr) {
  144. $columns[] = $part[1];
  145. } else {
  146. $column = $db->quoteIdentifier($part[1], true);
  147. if (!empty($part[0])) {
  148. $column = $db->quoteIdentifier($part[0], true) . '.' . $column;
  149. }
  150. $columns[] = $column;
  151. }
  152. }
  153. if (count($columns) == 1 && $columns[0] == Zend_Db_Select::SQL_WILDCARD) {
  154. $groupPart = null;
  155. } else {
  156. $groupPart = implode(',', $columns);
  157. }
  158. } else {
  159. $groupParts = $rowCount->getPart(Zend_Db_Select::GROUP);
  160. foreach ($groupParts as &$part) {
  161. if (!($part == Zend_Db_Select::SQL_WILDCARD || $part instanceof Zend_Db_Expr)) {
  162. $part = $db->quoteIdentifier($part, true);
  163. }
  164. }
  165. $groupPart = implode(',', $groupParts);
  166. }
  167. $countPart = empty($groupPart) ? 'COUNT(*)' : 'COUNT(DISTINCT ' . $groupPart . ')';
  168. $expression = new Zend_Db_Expr(
  169. $countPart . ' AS ' . $db->quoteIdentifier($db->foldCase(self::ROW_COUNT_COLUMN))
  170. );
  171. $rowCount->__toString(); // Workaround for ZF-3719 and related
  172. $rowCount->reset(Zend_Db_Select::COLUMNS)
  173. ->reset(Zend_Db_Select::ORDER)
  174. ->reset(Zend_Db_Select::LIMIT_OFFSET)
  175. ->reset(Zend_Db_Select::GROUP)
  176. ->reset(Zend_Db_Select::DISTINCT)
  177. ->columns($expression);
  178. $this->setRowCount($rowCount);
  179. }
  180. return $this->_rowCount;
  181. }
  182. }