Passed
Pull Request — master (#3836)
by Benjamin
11:25
created

ResultCacheStatement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0028

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 1.0028
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Cache;
6
7
use ArrayIterator;
8
use Doctrine\Common\Cache\Cache;
9
use Doctrine\DBAL\Driver\ResultStatement;
10
use Doctrine\DBAL\Exception\InvalidColumnIndex;
11
use Doctrine\DBAL\FetchMode;
12
use InvalidArgumentException;
13
use IteratorAggregate;
14
use function array_key_exists;
15
use function array_merge;
16
use function array_values;
17
use function count;
18
use function reset;
19
20
/**
21
 * Cache statement for SQL results.
22
 *
23
 * A result is saved in multiple cache keys, there is the originally specified
24
 * cache key which is just pointing to result rows by key. The following things
25
 * have to be ensured:
26
 *
27
 * 1. lifetime of the original key has to be longer than that of all the individual rows keys
28
 * 2. if any one row key is missing the query has to be re-executed.
29
 *
30
 * Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
31
 * This means that the memory usage for cached results might increase by using this feature.
32
 */
33
final class ResultCacheStatement implements IteratorAggregate, ResultStatement
34
{
35
    /** @var Cache */
36
    private $resultCache;
37
38
    /** @var string */
39
    private $cacheKey;
40
41
    /** @var string */
42
    private $realKey;
43
44
    /** @var int */
45
    private $lifetime;
46
47
    /** @var ResultStatement */
48
    private $statement;
49
50
    /**
51
     * Did we reach the end of the statement?
52
     *
53
     * @var bool
54
     */
55
    private $emptied = false;
56
57
    /** @var mixed[] */
58
    private $data;
59
60
    /** @var int */
61
    private $defaultFetchMode = FetchMode::MIXED;
62
63 312
    public function __construct(ResultStatement $stmt, Cache $resultCache, string $cacheKey, string $realKey, int $lifetime)
64
    {
65 312
        $this->statement   = $stmt;
66 312
        $this->resultCache = $resultCache;
67 312
        $this->cacheKey    = $cacheKey;
68 312
        $this->realKey     = $realKey;
69 312
        $this->lifetime    = $lifetime;
70 312
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 286
    public function closeCursor() : void
76
    {
77 286
        $this->statement->closeCursor();
78
79 286
        if (! $this->emptied || $this->data === null) {
80 26
            return;
81
        }
82
83 286
        $data = $this->resultCache->fetch($this->cacheKey);
84 286
        if (! $data) {
85 286
            $data = [];
86
        }
87 286
        $data[$this->realKey] = $this->data;
88
89 286
        $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
90 286
        unset($this->data);
91 286
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 104
    public function columnCount() : int
97
    {
98 104
        return $this->statement->columnCount();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 312
    public function setFetchMode(int $fetchMode, ...$args) : void
105
    {
106 312
        $this->defaultFetchMode = $fetchMode;
107 312
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getIterator()
113
    {
114
        $data = $this->fetchAll();
115
116
        return new ArrayIterator($data);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 260
    public function fetch(?int $fetchMode = null, ...$args)
123
    {
124 260
        if ($this->data === null) {
125 260
            $this->data = [];
126
        }
127
128 260
        $row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
129
130 260
        if ($row) {
131 208
            $this->data[] = $row;
132
133 208
            $fetchMode = $fetchMode ?: $this->defaultFetchMode;
134
135 208
            if ($fetchMode === FetchMode::ASSOCIATIVE) {
136 104
                return $row;
137
            }
138
139 156
            if ($fetchMode === FetchMode::NUMERIC) {
140 78
                return array_values($row);
141
            }
142
143 78
            if ($fetchMode === FetchMode::MIXED) {
144 52
                return array_merge($row, array_values($row));
145
            }
146
147 26
            if ($fetchMode === FetchMode::COLUMN) {
148 26
                return reset($row);
149
            }
150
151
            throw new InvalidArgumentException('Invalid fetch-style given for caching result.');
152
        }
153
154 260
        $this->emptied = true;
155
156 260
        return false;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 52
    public function fetchAll(?int $fetchMode = null, ...$args) : array
163
    {
164 52
        $data = $this->statement->fetchAll($fetchMode, ...$args);
165
166 52
        if ($fetchMode === FetchMode::COLUMN) {
167 26
            foreach ($data as $key => $value) {
168 26
                $data[$key] = [$value];
169
            }
170
        }
171
172 52
        $this->data    = $data;
173 52
        $this->emptied = true;
174
175 52
        return $this->data;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function fetchColumn(int $columnIndex = 0)
182
    {
183
        $row = $this->fetch(FetchMode::NUMERIC);
184
185
        if ($row === false) {
186
            return false;
187
        }
188
189
        if (! array_key_exists($columnIndex, $row)) {
190
            throw InvalidColumnIndex::new($columnIndex, count($row));
191
        }
192
193
        return $row[$columnIndex];
194
    }
195
196
    /**
197
     * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
198
     * executed by the corresponding object.
199
     *
200
     * If the last SQL statement executed by the associated Statement object was a SELECT statement,
201
     * some databases may return the number of rows returned by that statement. However,
202
     * this behaviour is not guaranteed for all databases and should not be
203
     * relied on for portable applications.
204
     *
205
     * @return int The number of rows.
206
     */
207
    public function rowCount() : int
208
    {
209
        return $this->statement->rowCount();
210
    }
211
}
212