ResultCacheStatement.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 MIT license. 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\Common\Cache\Cache;
  23. use PDO;
  24. /**
  25. * Cache statement for SQL results.
  26. *
  27. * A result is saved in multiple cache keys, there is the originally specified
  28. * cache key which is just pointing to result rows by key. The following things
  29. * have to be ensured:
  30. *
  31. * 1. lifetime of the original key has to be longer than that of all the individual rows keys
  32. * 2. if any one row key is missing the query has to be re-executed.
  33. *
  34. * Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
  35. * This means that the memory usage for cached results might increase by using this feature.
  36. */
  37. class ResultCacheStatement implements \IteratorAggregate, ResultStatement
  38. {
  39. /**
  40. * @var \Doctrine\Common\Cache\Cache
  41. */
  42. private $resultCache;
  43. /**
  44. *
  45. * @var string
  46. */
  47. private $cacheKey;
  48. /**
  49. * @var string
  50. */
  51. private $realKey;
  52. /**
  53. * @var integer
  54. */
  55. private $lifetime;
  56. /**
  57. * @var \Doctrine\DBAL\Driver\Statement
  58. */
  59. private $statement;
  60. /**
  61. * Did we reach the end of the statement?
  62. *
  63. * @var boolean
  64. */
  65. private $emptied = false;
  66. /**
  67. * @var array
  68. */
  69. private $data;
  70. /**
  71. * @var integer
  72. */
  73. private $defaultFetchMode = PDO::FETCH_BOTH;
  74. /**
  75. * @param \Doctrine\DBAL\Driver\Statement $stmt
  76. * @param \Doctrine\Common\Cache\Cache $resultCache
  77. * @param string $cacheKey
  78. * @param string $realKey
  79. * @param integer $lifetime
  80. */
  81. public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
  82. {
  83. $this->statement = $stmt;
  84. $this->resultCache = $resultCache;
  85. $this->cacheKey = $cacheKey;
  86. $this->realKey = $realKey;
  87. $this->lifetime = $lifetime;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function closeCursor()
  93. {
  94. $this->statement->closeCursor();
  95. if ($this->emptied && $this->data !== null) {
  96. $data = $this->resultCache->fetch($this->cacheKey);
  97. if ( ! $data) {
  98. $data = array();
  99. }
  100. $data[$this->realKey] = $this->data;
  101. $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
  102. unset($this->data);
  103. }
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function columnCount()
  109. {
  110. return $this->statement->columnCount();
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  116. {
  117. $this->defaultFetchMode = $fetchMode;
  118. return true;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getIterator()
  124. {
  125. $data = $this->fetchAll();
  126. return new \ArrayIterator($data);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function fetch($fetchMode = null)
  132. {
  133. if ($this->data === null) {
  134. $this->data = array();
  135. }
  136. $row = $this->statement->fetch(PDO::FETCH_ASSOC);
  137. if ($row) {
  138. $this->data[] = $row;
  139. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  140. if ($fetchMode == PDO::FETCH_ASSOC) {
  141. return $row;
  142. } else if ($fetchMode == PDO::FETCH_NUM) {
  143. return array_values($row);
  144. } else if ($fetchMode == PDO::FETCH_BOTH) {
  145. return array_merge($row, array_values($row));
  146. } else if ($fetchMode == PDO::FETCH_COLUMN) {
  147. return reset($row);
  148. } else {
  149. throw new \InvalidArgumentException("Invalid fetch-style given for caching result.");
  150. }
  151. }
  152. $this->emptied = true;
  153. return false;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function fetchAll($fetchMode = null)
  159. {
  160. $rows = array();
  161. while ($row = $this->fetch($fetchMode)) {
  162. $rows[] = $row;
  163. }
  164. return $rows;
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function fetchColumn($columnIndex = 0)
  170. {
  171. $row = $this->fetch(PDO::FETCH_NUM);
  172. if (!isset($row[$columnIndex])) {
  173. // TODO: verify this is correct behavior
  174. return false;
  175. }
  176. return $row[$columnIndex];
  177. }
  178. /**
  179. * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
  180. * executed by the corresponding object.
  181. *
  182. * If the last SQL statement executed by the associated Statement object was a SELECT statement,
  183. * some databases may return the number of rows returned by that statement. However,
  184. * this behaviour is not guaranteed for all databases and should not be
  185. * relied on for portable applications.
  186. *
  187. * @return integer The number of rows.
  188. */
  189. public function rowCount()
  190. {
  191. return $this->statement->rowCount();
  192. }
  193. }