ResultCacheStatement.php 4.9 KB

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