BufferedStatement.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Statement;
  16. use CachingIterator;
  17. use IteratorIterator;
  18. /**
  19. * A statement decorator that implements buffered results.
  20. *
  21. * This statement decorator will save fetched results in memory, allowing
  22. * the iterator to be rewound and reused.
  23. */
  24. class BufferedStatement extends StatementDecorator
  25. {
  26. /**
  27. * Records count
  28. *
  29. * @var int
  30. */
  31. protected $_count = 0;
  32. /**
  33. * Array of results
  34. *
  35. * @var array
  36. */
  37. protected $_records = [];
  38. /**
  39. * If true, all rows were fetched
  40. *
  41. * @var bool
  42. */
  43. protected $_allFetched = false;
  44. /**
  45. * Current record pointer
  46. *
  47. * @var int
  48. */
  49. protected $_counter = 0;
  50. /**
  51. * Execute the statement and return the results.
  52. *
  53. * @param array|null $params list of values to be bound to query
  54. * @return bool true on success, false otherwise
  55. */
  56. public function execute($params = null)
  57. {
  58. $this->_reset();
  59. return parent::execute($params);
  60. }
  61. /**
  62. * {@inheritDoc}
  63. *
  64. * @param string $type The type to fetch.
  65. * @return array|false
  66. */
  67. public function fetch($type = parent::FETCH_TYPE_NUM)
  68. {
  69. if ($this->_allFetched) {
  70. $row = ($this->_counter < $this->_count) ? $this->_records[$this->_counter++] : false;
  71. $row = ($row && $type === static::FETCH_TYPE_NUM) ? array_values($row) : $row;
  72. return $row;
  73. }
  74. $record = parent::fetch($type);
  75. if ($record === false) {
  76. $this->_allFetched = true;
  77. $this->_counter = $this->_count + 1;
  78. $this->_statement->closeCursor();
  79. return false;
  80. }
  81. $this->_count++;
  82. return $this->_records[] = $record;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function fetchAssoc()
  88. {
  89. $result = $this->fetch(static::FETCH_TYPE_ASSOC);
  90. return $result ?: [];
  91. }
  92. /**
  93. * {@inheritDoc}
  94. *
  95. * @param string $type The type to fetch.
  96. * @return array
  97. */
  98. public function fetchAll($type = parent::FETCH_TYPE_NUM)
  99. {
  100. if ($this->_allFetched) {
  101. return $this->_records;
  102. }
  103. $this->_records = parent::fetchAll($type);
  104. $this->_count = count($this->_records);
  105. $this->_allFetched = true;
  106. $this->_statement->closeCursor();
  107. return $this->_records;
  108. }
  109. /**
  110. * {@inheritDoc}
  111. */
  112. public function rowCount()
  113. {
  114. if (!$this->_allFetched) {
  115. $counter = $this->_counter;
  116. while ($this->fetch(static::FETCH_TYPE_ASSOC)) {
  117. }
  118. $this->_counter = $counter;
  119. }
  120. return $this->_count;
  121. }
  122. /**
  123. * Rewind the _counter property
  124. *
  125. * @return void
  126. */
  127. public function rewind()
  128. {
  129. $this->_counter = 0;
  130. }
  131. /**
  132. * Reset all properties
  133. *
  134. * @return void
  135. */
  136. protected function _reset()
  137. {
  138. $this->_count = $this->_counter = 0;
  139. $this->_records = [];
  140. $this->_allFetched = false;
  141. }
  142. /**
  143. * Get an iterator that buffers results.
  144. *
  145. * @return Traversable
  146. */
  147. public function getIterator()
  148. {
  149. $statement = parent::getIterator();
  150. if (!$this->_iterator) {
  151. $this->_iterator = new BufferedIterator(new IteratorIterator($statement));
  152. }
  153. return $this->_iterator;
  154. }
  155. }