ResultCacheStatement.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Cache;
  20. use Doctrine\DBAL\Driver\Statement;
  21. use Doctrine\DBAL\Driver\ResultStatement;
  22. use Doctrine\DBAL\Connection;
  23. use Doctrine\Common\Cache\Cache;
  24. use PDO;
  25. /**
  26. * Cache statement for SQL results.
  27. *
  28. * A result is saved in multiple cache keys, there is the originally specified
  29. * cache key which is just pointing to result rows by key. The following things
  30. * have to be ensured:
  31. *
  32. * 1. lifetime of the original key has to be longer than that of all the individual rows keys
  33. * 2. if any one row key is missing the query has to be re-executed.
  34. *
  35. * Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
  36. * This means that the memory usage for cached results might increase by using this feature.
  37. */
  38. class ResultCacheStatement implements \IteratorAggregate, ResultStatement
  39. {
  40. /**
  41. * @var \Doctrine\Common\Cache\Cache
  42. */
  43. private $resultCache;
  44. /**
  45. *
  46. * @var string
  47. */
  48. private $cacheKey;
  49. /**
  50. * @var string
  51. */
  52. private $realKey;
  53. /**
  54. * @var int
  55. */
  56. private $lifetime;
  57. /**
  58. * @var Doctrine\DBAL\Driver\Statement
  59. */
  60. private $statement;
  61. /**
  62. * Did we reach the end of the statement?
  63. *
  64. * @var bool
  65. */
  66. private $emptied = false;
  67. /**
  68. * @var array
  69. */
  70. private $data;
  71. /**
  72. * @var int
  73. */
  74. private $defaultFetchStyle = PDO::FETCH_BOTH;
  75. /**
  76. * @param Statement $stmt
  77. * @param Cache $resultCache
  78. * @param string $cacheKey
  79. * @param string $realKey
  80. * @param int $lifetime
  81. */
  82. public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
  83. {
  84. $this->statement = $stmt;
  85. $this->resultCache = $resultCache;
  86. $this->cacheKey = $cacheKey;
  87. $this->realKey = $realKey;
  88. $this->lifetime = $lifetime;
  89. }
  90. /**
  91. * Closes the cursor, enabling the statement to be executed again.
  92. *
  93. * @return boolean Returns TRUE on success or FALSE on failure.
  94. */
  95. public function closeCursor()
  96. {
  97. $this->statement->closeCursor();
  98. if ($this->emptied && $this->data !== null) {
  99. $data = $this->resultCache->fetch($this->cacheKey);
  100. if (!$data) {
  101. $data = array();
  102. }
  103. $data[$this->realKey] = $this->data;
  104. $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
  105. unset($this->data);
  106. }
  107. }
  108. /**
  109. * columnCount
  110. * Returns the number of columns in the result set
  111. *
  112. * @return integer Returns the number of columns in the result set represented
  113. * by the PDOStatement object. If there is no result set,
  114. * this method should return 0.
  115. */
  116. public function columnCount()
  117. {
  118. return $this->statement->columnCount();
  119. }
  120. public function setFetchMode($fetchStyle)
  121. {
  122. $this->defaultFetchStyle = $fetchStyle;
  123. }
  124. public function getIterator()
  125. {
  126. $data = $this->fetchAll($this->defaultFetchStyle);
  127. return new \ArrayIterator($data);
  128. }
  129. /**
  130. * fetch
  131. *
  132. * @see Query::HYDRATE_* constants
  133. * @param integer $fetchStyle Controls how the next row will be returned to the caller.
  134. * This value must be one of the Query::HYDRATE_* constants,
  135. * defaulting to Query::HYDRATE_BOTH
  136. *
  137. * @param integer $cursorOrientation For a PDOStatement object representing a scrollable cursor,
  138. * this value determines which row will be returned to the caller.
  139. * This value must be one of the Query::HYDRATE_ORI_* constants, defaulting to
  140. * Query::HYDRATE_ORI_NEXT. To request a scrollable cursor for your
  141. * PDOStatement object,
  142. * you must set the PDO::ATTR_CURSOR attribute to Doctrine::CURSOR_SCROLL when you
  143. * prepare the SQL statement with Doctrine_Adapter_Interface->prepare().
  144. *
  145. * @param integer $cursorOffset For a PDOStatement object representing a scrollable cursor for which the
  146. * $cursorOrientation parameter is set to Query::HYDRATE_ORI_ABS, this value specifies
  147. * the absolute number of the row in the result set that shall be fetched.
  148. *
  149. * For a PDOStatement object representing a scrollable cursor for
  150. * which the $cursorOrientation parameter is set to Query::HYDRATE_ORI_REL, this value
  151. * specifies the row to fetch relative to the cursor position before
  152. * PDOStatement->fetch() was called.
  153. *
  154. * @return mixed
  155. */
  156. public function fetch($fetchStyle = PDO::FETCH_BOTH)
  157. {
  158. if ($this->data === null) {
  159. $this->data = array();
  160. }
  161. $row = $this->statement->fetch(PDO::FETCH_ASSOC);
  162. if ($row) {
  163. $this->data[] = $row;
  164. if ($fetchStyle == PDO::FETCH_ASSOC) {
  165. return $row;
  166. } else if ($fetchStyle == PDO::FETCH_NUM) {
  167. return array_values($row);
  168. } else if ($fetchStyle == PDO::FETCH_BOTH) {
  169. return array_merge($row, array_values($row));
  170. } else {
  171. throw new \InvalidArgumentException("Invalid fetch-style given for caching result.");
  172. }
  173. }
  174. $this->emptied = true;
  175. return false;
  176. }
  177. /**
  178. * Returns an array containing all of the result set rows
  179. *
  180. * @param integer $fetchStyle Controls how the next row will be returned to the caller.
  181. * This value must be one of the Query::HYDRATE_* constants,
  182. * defaulting to Query::HYDRATE_BOTH
  183. *
  184. * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is
  185. * Query::HYDRATE_COLUMN. Defaults to 0.
  186. *
  187. * @return array
  188. */
  189. public function fetchAll($fetchStyle = PDO::FETCH_BOTH)
  190. {
  191. $rows = array();
  192. while ($row = $this->fetch($fetchStyle)) {
  193. $rows[] = $row;
  194. }
  195. return $rows;
  196. }
  197. /**
  198. * fetchColumn
  199. * Returns a single column from the next row of a
  200. * result set or FALSE if there are no more rows.
  201. *
  202. * @param integer $columnIndex 0-indexed number of the column you wish to retrieve from the row. If no
  203. * value is supplied, PDOStatement->fetchColumn()
  204. * fetches the first column.
  205. *
  206. * @return string returns a single column in the next row of a result set.
  207. */
  208. public function fetchColumn($columnIndex = 0)
  209. {
  210. $row = $this->fetch(PDO::FETCH_NUM);
  211. if (!isset($row[$columnIndex])) {
  212. // TODO: verify this is correct behavior
  213. return false;
  214. }
  215. return $row[$columnIndex];
  216. }
  217. /**
  218. * rowCount
  219. * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
  220. * executed by the corresponding object.
  221. *
  222. * If the last SQL statement executed by the associated Statement object was a SELECT statement,
  223. * some databases may return the number of rows returned by that statement. However,
  224. * this behaviour is not guaranteed for all databases and should not be
  225. * relied on for portable applications.
  226. *
  227. * @return integer Returns the number of rows.
  228. */
  229. public function rowCount()
  230. {
  231. return $this->statement->rowCount();
  232. }
  233. }