Completed
Push — develop ( 72ba3e...de019a )
by Marco
25s queued 12s
created

ResultCacheStatement::fetch()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.0109

Importance

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