ResultCacheStatement.php 5.21 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Cache;

5
use ArrayIterator;
6
use Doctrine\Common\Cache\Cache;
7 8
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
9
use Doctrine\DBAL\FetchMode;
10 11 12
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
13 14
use function array_merge;
use function array_values;
Sergei Morozov's avatar
Sergei Morozov committed
15
use function assert;
16
use function reset;
17 18 19 20 21 22 23 24 25 26 27 28 29 30

/**
 * 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.
 */
31
class ResultCacheStatement implements IteratorAggregate, ResultStatement
32
{
33
    /** @var Cache */
34
    private $resultCache;
35

36
    /** @var string */
37 38
    private $cacheKey;

39
    /** @var string */
40 41
    private $realKey;

42
    /** @var int */
43 44
    private $lifetime;

Sergei Morozov's avatar
Sergei Morozov committed
45
    /** @var ResultStatement */
46 47 48 49
    private $statement;

    /**
     * Did we reach the end of the statement?
50
     *
51
     * @var bool
52 53 54
     */
    private $emptied = false;

55
    /** @var mixed[] */
56 57
    private $data;

58
    /** @var int */
59
    private $defaultFetchMode = FetchMode::MIXED;
60

61
    /**
62 63 64
     * @param string $cacheKey
     * @param string $realKey
     * @param int    $lifetime
65
     */
Sergei Morozov's avatar
Sergei Morozov committed
66
    public function __construct(ResultStatement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
67
    {
68
        $this->statement   = $stmt;
69
        $this->resultCache = $resultCache;
70 71 72
        $this->cacheKey    = $cacheKey;
        $this->realKey     = $realKey;
        $this->lifetime    = $lifetime;
73 74 75
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
76
     * {@inheritdoc}
77 78 79
     */
    public function closeCursor()
    {
80
        $this->statement->closeCursor();
81
        if (! $this->emptied || $this->data === null) {
82
            return true;
83
        }
84

85 86 87
        $data = $this->resultCache->fetch($this->cacheKey);
        if (! $data) {
            $data = [];
88
        }
Grégoire Paris's avatar
Grégoire Paris committed
89

90 91 92 93
        $data[$this->realKey] = $this->data;

        $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
        unset($this->data);
94 95

        return true;
96 97 98
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
99
     * {@inheritdoc}
100 101 102 103 104 105
     */
    public function columnCount()
    {
        return $this->statement->columnCount();
    }

Benjamin Morel's avatar
Benjamin Morel committed
106 107 108
    /**
     * {@inheritdoc}
     */
109
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
110
    {
111
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
112 113

        return true;
114 115
    }

Benjamin Morel's avatar
Benjamin Morel committed
116 117 118
    /**
     * {@inheritdoc}
     */
119 120
    public function getIterator()
    {
121
        $data = $this->fetchAll();
Benjamin Morel's avatar
Benjamin Morel committed
122

123
        return new ArrayIterator($data);
124 125
    }

126
    /**
Benjamin Morel's avatar
Benjamin Morel committed
127
     * {@inheritdoc}
128
     */
129
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
130
    {
131
        if ($this->data === null) {
132
            $this->data = [];
133 134
        }

135 136
        $row = $this->statement->fetch(FetchMode::ASSOCIATIVE);

137
        if ($row) {
138
            $this->data[] = $row;
139

140
            $fetchMode = $fetchMode ?: $this->defaultFetchMode;
141

Sergei Morozov's avatar
Sergei Morozov committed
142
            if ($fetchMode === FetchMode::ASSOCIATIVE) {
143
                return $row;
144 145
            }

Sergei Morozov's avatar
Sergei Morozov committed
146
            if ($fetchMode === FetchMode::NUMERIC) {
147
                return array_values($row);
148 149
            }

Sergei Morozov's avatar
Sergei Morozov committed
150
            if ($fetchMode === FetchMode::MIXED) {
151
                return array_merge($row, array_values($row));
152 153
            }

Sergei Morozov's avatar
Sergei Morozov committed
154
            if ($fetchMode === FetchMode::COLUMN) {
155
                return reset($row);
156
            }
157

158
            throw new InvalidArgumentException('Invalid fetch-style given for caching result.');
159
        }
160

161
        $this->emptied = true;
Benjamin Morel's avatar
Benjamin Morel committed
162

163 164 165 166
        return false;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
167
     * {@inheritdoc}
168
     */
169
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
170
    {
171 172 173 174 175 176 177 178 179
        $data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);

        if ($fetchMode === FetchMode::COLUMN) {
            foreach ($data as $key => $value) {
                $data[$key] = [$value];
            }
        }

        $this->data    = $data;
180 181 182
        $this->emptied = true;

        return $this->data;
183 184 185
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
186
     * {@inheritdoc}
187 188 189
     */
    public function fetchColumn($columnIndex = 0)
    {
190
        $row = $this->fetch(FetchMode::NUMERIC);
Benjamin Morel's avatar
Benjamin Morel committed
191

192
        // TODO: verify that return false is the correct behavior
193
        return $row[$columnIndex] ?? false;
194 195 196
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
197
     * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
198 199 200 201 202 203 204
     * 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.
     *
205
     * @return int The number of rows.
206 207 208
     */
    public function rowCount()
    {
Sergei Morozov's avatar
Sergei Morozov committed
209 210
        assert($this->statement instanceof Statement);

211 212
        return $this->statement->rowCount();
    }
213
}