ResultCacheStatement.php 6.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Cache;

22
use Doctrine\DBAL\Driver\Statement;
23
use Doctrine\DBAL\Driver\ResultStatement;
24 25
use Doctrine\Common\Cache\Cache;
use PDO;
26 27 28 29 30 31 32 33 34 35 36 37 38 39

/**
 * Cache statement for SQL results.
 *
 * A result is saved in multiple cache keys, there is the originally specified
 * cache key which is just pointing to result rows by key. The following things
 * have to be ensured:
 *
 * 1. lifetime of the original key has to be longer than that of all the individual rows keys
 * 2. if any one row key is missing the query has to be re-executed.
 *
 * Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
 * This means that the memory usage for cached results might increase by using this feature.
 */
40
class ResultCacheStatement implements \IteratorAggregate, ResultStatement
41 42 43 44
{
    /**
     * @var \Doctrine\Common\Cache\Cache
     */
45
    private $resultCache;
46 47 48 49 50 51 52

    /**
     *
     * @var string
     */
    private $cacheKey;

53 54 55 56 57
    /**
     * @var string
     */
    private $realKey;

58
    /**
Benjamin Morel's avatar
Benjamin Morel committed
59
     * @var integer
60 61 62 63
     */
    private $lifetime;

    /**
Konstantin Kuklin's avatar
Konstantin Kuklin committed
64
     * @var \Doctrine\DBAL\Driver\Statement
65 66 67 68 69
     */
    private $statement;

    /**
     * Did we reach the end of the statement?
70
     *
Benjamin Morel's avatar
Benjamin Morel committed
71
     * @var boolean
72 73 74
     */
    private $emptied = false;

75 76 77 78 79
    /**
     * @var array
     */
    private $data;

80
    /**
Benjamin Morel's avatar
Benjamin Morel committed
81
     * @var integer
82
     */
83
    private $defaultFetchMode = PDO::FETCH_BOTH;
84

85
    /**
Benjamin Morel's avatar
Benjamin Morel committed
86 87 88 89 90
     * @param \Doctrine\DBAL\Driver\Statement $stmt
     * @param \Doctrine\Common\Cache\Cache    $resultCache
     * @param string                          $cacheKey
     * @param string                          $realKey
     * @param integer                         $lifetime
91
     */
92
    public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
93 94 95 96
    {
        $this->statement = $stmt;
        $this->resultCache = $resultCache;
        $this->cacheKey = $cacheKey;
97
        $this->realKey = $realKey;
98 99 100 101
        $this->lifetime = $lifetime;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
102
     * {@inheritdoc}
103 104 105
     */
    public function closeCursor()
    {
106
        $this->statement->closeCursor();
107
        if ($this->emptied && $this->data !== null) {
108
            $data = $this->resultCache->fetch($this->cacheKey);
109
            if ( ! $data) {
110 111 112 113 114 115
                $data = array();
            }
            $data[$this->realKey] = $this->data;

            $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
            unset($this->data);
116 117 118 119
        }
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
120
     * {@inheritdoc}
121 122 123 124 125 126
     */
    public function columnCount()
    {
        return $this->statement->columnCount();
    }

Benjamin Morel's avatar
Benjamin Morel committed
127 128 129
    /**
     * {@inheritdoc}
     */
130
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
131
    {
132
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
133 134

        return true;
135 136
    }

Benjamin Morel's avatar
Benjamin Morel committed
137 138 139
    /**
     * {@inheritdoc}
     */
140 141
    public function getIterator()
    {
142
        $data = $this->fetchAll();
Benjamin Morel's avatar
Benjamin Morel committed
143

144 145 146
        return new \ArrayIterator($data);
    }

147
    /**
Benjamin Morel's avatar
Benjamin Morel committed
148
     * {@inheritdoc}
149
     */
150
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
151
    {
152 153 154 155
        if ($this->data === null) {
            $this->data = array();
        }

156 157
        $row = $this->statement->fetch(PDO::FETCH_ASSOC);
        if ($row) {
158
            $this->data[] = $row;
159

160
            $fetchMode = $fetchMode ?: $this->defaultFetchMode;
161

162
            if ($fetchMode == PDO::FETCH_ASSOC) {
163
                return $row;
Steve Müller's avatar
Steve Müller committed
164
            } elseif ($fetchMode == PDO::FETCH_NUM) {
165
                return array_values($row);
Steve Müller's avatar
Steve Müller committed
166
            } elseif ($fetchMode == PDO::FETCH_BOTH) {
167
                return array_merge($row, array_values($row));
Steve Müller's avatar
Steve Müller committed
168
            } elseif ($fetchMode == PDO::FETCH_COLUMN) {
169
                return reset($row);
170 171 172 173 174
            } else {
                throw new \InvalidArgumentException("Invalid fetch-style given for caching result.");
            }
        }
        $this->emptied = true;
Benjamin Morel's avatar
Benjamin Morel committed
175

176 177 178 179
        return false;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
180
     * {@inheritdoc}
181
     */
182
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
183 184
    {
        $rows = array();
185
        while ($row = $this->fetch($fetchMode)) {
186 187
            $rows[] = $row;
        }
Benjamin Morel's avatar
Benjamin Morel committed
188

189 190 191 192
        return $rows;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
193
     * {@inheritdoc}
194 195 196 197 198 199 200 201
     */
    public function fetchColumn($columnIndex = 0)
    {
        $row = $this->fetch(PDO::FETCH_NUM);
        if (!isset($row[$columnIndex])) {
            // TODO: verify this is correct behavior
            return false;
        }
Benjamin Morel's avatar
Benjamin Morel committed
202

203 204 205 206
        return $row[$columnIndex];
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
207
     * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
208 209 210 211 212 213 214
     * executed by the corresponding object.
     *
     * If the last SQL statement executed by the associated Statement object was a SELECT statement,
     * some databases may return the number of rows returned by that statement. However,
     * this behaviour is not guaranteed for all databases and should not be
     * relied on for portable applications.
     *
Benjamin Morel's avatar
Benjamin Morel committed
215
     * @return integer The number of rows.
216 217 218 219 220
     */
    public function rowCount()
    {
        return $this->statement->rowCount();
    }
221
}