[345] | 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 | |
---|
| 20 | namespace Doctrine\DBAL\Cache; |
---|
| 21 | |
---|
| 22 | use Doctrine\DBAL\Driver\Statement; |
---|
| 23 | use Doctrine\DBAL\Driver\ResultStatement; |
---|
| 24 | use Doctrine\DBAL\Connection; |
---|
| 25 | use Doctrine\Common\Cache\Cache; |
---|
| 26 | use PDO; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Cache statement for SQL results. |
---|
| 30 | * |
---|
| 31 | * A result is saved in multiple cache keys, there is the originally specified |
---|
| 32 | * cache key which is just pointing to result rows by key. The following things |
---|
| 33 | * have to be ensured: |
---|
| 34 | * |
---|
| 35 | * 1. lifetime of the original key has to be longer than that of all the individual rows keys |
---|
| 36 | * 2. if any one row key is missing the query has to be re-executed. |
---|
| 37 | * |
---|
| 38 | * Also you have to realize that the cache will load the whole result into memory at once to ensure 2. |
---|
| 39 | * This means that the memory usage for cached results might increase by using this feature. |
---|
| 40 | */ |
---|
| 41 | class ResultCacheStatement implements \IteratorAggregate, ResultStatement |
---|
| 42 | { |
---|
| 43 | /** |
---|
| 44 | * @var \Doctrine\Common\Cache\Cache |
---|
| 45 | */ |
---|
| 46 | private $resultCache; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * |
---|
| 50 | * @var string |
---|
| 51 | */ |
---|
| 52 | private $cacheKey; |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * @var string |
---|
| 56 | */ |
---|
| 57 | private $realKey; |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * @var int |
---|
| 61 | */ |
---|
| 62 | private $lifetime; |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * @var Doctrine\DBAL\Driver\Statement |
---|
| 66 | */ |
---|
| 67 | private $statement; |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * Did we reach the end of the statement? |
---|
| 71 | * |
---|
| 72 | * @var bool |
---|
| 73 | */ |
---|
| 74 | private $emptied = false; |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * @var array |
---|
| 78 | */ |
---|
| 79 | private $data; |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * @var int |
---|
| 83 | */ |
---|
| 84 | private $defaultFetchStyle = PDO::FETCH_BOTH; |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * @param Statement $stmt |
---|
| 88 | * @param Cache $resultCache |
---|
| 89 | * @param string $cacheKey |
---|
| 90 | * @param string $realKey |
---|
| 91 | * @param int $lifetime |
---|
| 92 | */ |
---|
| 93 | public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime) |
---|
| 94 | { |
---|
| 95 | $this->statement = $stmt; |
---|
| 96 | $this->resultCache = $resultCache; |
---|
| 97 | $this->cacheKey = $cacheKey; |
---|
| 98 | $this->realKey = $realKey; |
---|
| 99 | $this->lifetime = $lifetime; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * Closes the cursor, enabling the statement to be executed again. |
---|
| 104 | * |
---|
| 105 | * @return boolean Returns TRUE on success or FALSE on failure. |
---|
| 106 | */ |
---|
| 107 | public function closeCursor() |
---|
| 108 | { |
---|
| 109 | $this->statement->closeCursor(); |
---|
| 110 | if ($this->emptied && $this->data !== null) { |
---|
| 111 | $data = $this->resultCache->fetch($this->cacheKey); |
---|
| 112 | if (!$data) { |
---|
| 113 | $data = array(); |
---|
| 114 | } |
---|
| 115 | $data[$this->realKey] = $this->data; |
---|
| 116 | |
---|
| 117 | $this->resultCache->save($this->cacheKey, $data, $this->lifetime); |
---|
| 118 | unset($this->data); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * columnCount |
---|
| 124 | * Returns the number of columns in the result set |
---|
| 125 | * |
---|
| 126 | * @return integer Returns the number of columns in the result set represented |
---|
| 127 | * by the PDOStatement object. If there is no result set, |
---|
| 128 | * this method should return 0. |
---|
| 129 | */ |
---|
| 130 | public function columnCount() |
---|
| 131 | { |
---|
| 132 | return $this->statement->columnCount(); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | public function setFetchMode($fetchStyle) |
---|
| 136 | { |
---|
| 137 | $this->defaultFetchStyle = $fetchStyle; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | public function getIterator() |
---|
| 141 | { |
---|
| 142 | $data = $this->fetchAll($this->defaultFetchStyle); |
---|
| 143 | return new \ArrayIterator($data); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * fetch |
---|
| 148 | * |
---|
| 149 | * @see Query::HYDRATE_* constants |
---|
| 150 | * @param integer $fetchStyle Controls how the next row will be returned to the caller. |
---|
| 151 | * This value must be one of the Query::HYDRATE_* constants, |
---|
| 152 | * defaulting to Query::HYDRATE_BOTH |
---|
| 153 | * |
---|
| 154 | * @param integer $cursorOrientation For a PDOStatement object representing a scrollable cursor, |
---|
| 155 | * this value determines which row will be returned to the caller. |
---|
| 156 | * This value must be one of the Query::HYDRATE_ORI_* constants, defaulting to |
---|
| 157 | * Query::HYDRATE_ORI_NEXT. To request a scrollable cursor for your |
---|
| 158 | * PDOStatement object, |
---|
| 159 | * you must set the PDO::ATTR_CURSOR attribute to Doctrine::CURSOR_SCROLL when you |
---|
| 160 | * prepare the SQL statement with Doctrine_Adapter_Interface->prepare(). |
---|
| 161 | * |
---|
| 162 | * @param integer $cursorOffset For a PDOStatement object representing a scrollable cursor for which the |
---|
| 163 | * $cursorOrientation parameter is set to Query::HYDRATE_ORI_ABS, this value specifies |
---|
| 164 | * the absolute number of the row in the result set that shall be fetched. |
---|
| 165 | * |
---|
| 166 | * For a PDOStatement object representing a scrollable cursor for |
---|
| 167 | * which the $cursorOrientation parameter is set to Query::HYDRATE_ORI_REL, this value |
---|
| 168 | * specifies the row to fetch relative to the cursor position before |
---|
| 169 | * PDOStatement->fetch() was called. |
---|
| 170 | * |
---|
| 171 | * @return mixed |
---|
| 172 | */ |
---|
| 173 | public function fetch($fetchStyle = PDO::FETCH_BOTH) |
---|
| 174 | { |
---|
| 175 | if ($this->data === null) { |
---|
| 176 | $this->data = array(); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | $row = $this->statement->fetch(PDO::FETCH_ASSOC); |
---|
| 180 | if ($row) { |
---|
| 181 | $this->data[] = $row; |
---|
| 182 | |
---|
| 183 | if ($fetchStyle == PDO::FETCH_ASSOC) { |
---|
| 184 | return $row; |
---|
| 185 | } else if ($fetchStyle == PDO::FETCH_NUM) { |
---|
| 186 | return array_values($row); |
---|
| 187 | } else if ($fetchStyle == PDO::FETCH_BOTH) { |
---|
| 188 | return array_merge($row, array_values($row)); |
---|
| 189 | } else { |
---|
| 190 | throw new \InvalidArgumentException("Invalid fetch-style given for caching result."); |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | $this->emptied = true; |
---|
| 194 | return false; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * Returns an array containing all of the result set rows |
---|
| 199 | * |
---|
| 200 | * @param integer $fetchStyle Controls how the next row will be returned to the caller. |
---|
| 201 | * This value must be one of the Query::HYDRATE_* constants, |
---|
| 202 | * defaulting to Query::HYDRATE_BOTH |
---|
| 203 | * |
---|
| 204 | * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is |
---|
| 205 | * Query::HYDRATE_COLUMN. Defaults to 0. |
---|
| 206 | * |
---|
| 207 | * @return array |
---|
| 208 | */ |
---|
| 209 | public function fetchAll($fetchStyle = PDO::FETCH_BOTH) |
---|
| 210 | { |
---|
| 211 | $rows = array(); |
---|
| 212 | while ($row = $this->fetch($fetchStyle)) { |
---|
| 213 | $rows[] = $row; |
---|
| 214 | } |
---|
| 215 | return $rows; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /** |
---|
| 219 | * fetchColumn |
---|
| 220 | * Returns a single column from the next row of a |
---|
| 221 | * result set or FALSE if there are no more rows. |
---|
| 222 | * |
---|
| 223 | * @param integer $columnIndex 0-indexed number of the column you wish to retrieve from the row. If no |
---|
| 224 | * value is supplied, PDOStatement->fetchColumn() |
---|
| 225 | * fetches the first column. |
---|
| 226 | * |
---|
| 227 | * @return string returns a single column in the next row of a result set. |
---|
| 228 | */ |
---|
| 229 | public function fetchColumn($columnIndex = 0) |
---|
| 230 | { |
---|
| 231 | $row = $this->fetch(PDO::FETCH_NUM); |
---|
| 232 | if (!isset($row[$columnIndex])) { |
---|
| 233 | // TODO: verify this is correct behavior |
---|
| 234 | return false; |
---|
| 235 | } |
---|
| 236 | return $row[$columnIndex]; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * rowCount |
---|
| 241 | * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement |
---|
| 242 | * executed by the corresponding object. |
---|
| 243 | * |
---|
| 244 | * If the last SQL statement executed by the associated Statement object was a SELECT statement, |
---|
| 245 | * some databases may return the number of rows returned by that statement. However, |
---|
| 246 | * this behaviour is not guaranteed for all databases and should not be |
---|
| 247 | * relied on for portable applications. |
---|
| 248 | * |
---|
| 249 | * @return integer Returns the number of rows. |
---|
| 250 | */ |
---|
| 251 | public function rowCount() |
---|
| 252 | { |
---|
| 253 | return $this->statement->rowCount(); |
---|
| 254 | } |
---|
| 255 | } |
---|