Completed
Push — develop ( 0c4aa7...b62acb )
by Sergei
55s queued 14s
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
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 275
    public function __construct(ResultStatement $stmt, Cache $resultCache, string $cacheKey, string $realKey, int $lifetime)
64
    {
65 275
        $this->statement   = $stmt;
66 275
        $this->resultCache = $resultCache;
67 275
        $this->cacheKey    = $cacheKey;
68 275
        $this->realKey     = $realKey;
69 275
        $this->lifetime    = $lifetime;
70 275
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 274
    public function closeCursor() : void
76
    {
77 274
        $this->statement->closeCursor();
78
79 274
        if (! $this->emptied || $this->data === null) {
80 97
            return;
81
        }
82
83 274
        $data = $this->resultCache->fetch($this->cacheKey);
84 274
        if (! $data) {
85 274
            $data = [];
86
        }
87 274
        $data[$this->realKey] = $this->data;
88
89 274
        $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
90 274
        unset($this->data);
91 274
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 268
    public function columnCount() : int
97
    {
98 268
        return $this->statement->columnCount();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 275
    public function setFetchMode(int $fetchMode, ...$args) : void
105
    {
106 275
        $this->defaultFetchMode = $fetchMode;
107 275
    }
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 274
    public function fetch(?int $fetchMode = null, ...$args)
123
    {
124 274
        if ($this->data === null) {
125 274
            $this->data = [];
126
        }
127
128 274
        $row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
129
130 274
        if ($row) {
131 272
            $this->data[] = $row;
132
133 272
            $fetchMode = $fetchMode ?: $this->defaultFetchMode;
134
135 272
            if ($fetchMode === FetchMode::ASSOCIATIVE) {
136 268
                return $row;
137
            }
138
139 246
            if ($fetchMode === FetchMode::NUMERIC) {
140 243
                return array_values($row);
141
            }
142
143 219
            if ($fetchMode === FetchMode::MIXED) {
144 218
                return array_merge($row, array_values($row));
145
            }
146
147 193
            if ($fetchMode === FetchMode::COLUMN) {
148 193
                return reset($row);
149
            }
150
151
            throw new InvalidArgumentException('Invalid fetch-style given for caching result.');
152
        }
153
154 274
        $this->emptied = true;
155
156 274
        return false;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 73
    public function fetchAll(?int $fetchMode = null, ...$args) : array
163
    {
164 73
        $this->data    = $this->statement->fetchAll($fetchMode, ...$args);
165 73
        $this->emptied = true;
166
167 73
        return $this->data;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function fetchColumn(int $columnIndex = 0)
174
    {
175
        $row = $this->fetch(FetchMode::NUMERIC);
176
177
        if ($row === false) {
178
            return false;
179
        }
180
181
        if (! array_key_exists($columnIndex, $row)) {
182
            throw InvalidColumnIndex::new($columnIndex, count($row));
183
        }
184
185
        return $row[$columnIndex];
186
    }
187
188
    /**
189
     * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
190
     * executed by the corresponding object.
191
     *
192
     * If the last SQL statement executed by the associated Statement object was a SELECT statement,
193
     * some databases may return the number of rows returned by that statement. However,
194
     * this behaviour is not guaranteed for all databases and should not be
195
     * relied on for portable applications.
196
     *
197
     * @return int The number of rows.
198
     */
199
    public function rowCount() : int
200
    {
201
        return $this->statement->rowCount();
202
    }
203
}
204