|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Cache; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
|
8
|
|
|
use Doctrine\DBAL\Driver\DriverException; |
|
9
|
|
|
use Doctrine\DBAL\Driver\FetchUtils; |
|
10
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
|
11
|
|
|
use Traversable; |
|
12
|
|
|
use function array_values; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Cache statement for SQL results. |
|
16
|
|
|
* |
|
17
|
|
|
* A result is saved in multiple cache keys, there is the originally specified |
|
18
|
|
|
* cache key which is just pointing to result rows by key. The following things |
|
19
|
|
|
* have to be ensured: |
|
20
|
|
|
* |
|
21
|
|
|
* 1. lifetime of the original key has to be longer than that of all the individual rows keys |
|
22
|
|
|
* 2. if any one row key is missing the query has to be re-executed. |
|
23
|
|
|
* |
|
24
|
|
|
* Also you have to realize that the cache will load the whole result into memory at once to ensure 2. |
|
25
|
|
|
* This means that the memory usage for cached results might increase by using this feature. |
|
26
|
|
|
*/ |
|
27
|
|
|
final class ResultCacheStatement implements ResultStatement |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var Cache */ |
|
30
|
|
|
private $resultCache; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $cacheKey; |
|
34
|
|
|
|
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
private $realKey; |
|
37
|
|
|
|
|
38
|
|
|
/** @var int */ |
|
39
|
|
|
private $lifetime; |
|
40
|
|
|
|
|
41
|
|
|
/** @var ResultStatement */ |
|
42
|
|
|
private $statement; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Did we reach the end of the statement? |
|
46
|
|
|
* |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
private $emptied = false; |
|
50
|
|
|
|
|
51
|
|
|
/** @var array<int,array<mixed>> */ |
|
52
|
|
|
private $data; |
|
53
|
|
|
|
|
54
|
264 |
|
public function __construct(ResultStatement $stmt, Cache $resultCache, string $cacheKey, string $realKey, int $lifetime) |
|
55
|
|
|
{ |
|
56
|
264 |
|
$this->statement = $stmt; |
|
57
|
264 |
|
$this->resultCache = $resultCache; |
|
58
|
264 |
|
$this->cacheKey = $cacheKey; |
|
59
|
264 |
|
$this->realKey = $realKey; |
|
60
|
264 |
|
$this->lifetime = $lifetime; |
|
61
|
264 |
|
} |
|
62
|
|
|
|
|
63
|
242 |
|
public function closeCursor() : void |
|
64
|
|
|
{ |
|
65
|
242 |
|
$this->statement->closeCursor(); |
|
66
|
|
|
|
|
67
|
242 |
|
if (! $this->emptied || $this->data === null) { |
|
68
|
22 |
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
242 |
|
$data = $this->resultCache->fetch($this->cacheKey); |
|
72
|
242 |
|
if ($data === false) { |
|
73
|
242 |
|
$data = []; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
242 |
|
$data[$this->realKey] = $this->data; |
|
77
|
|
|
|
|
78
|
242 |
|
$this->resultCache->save($this->cacheKey, $data, $this->lifetime); |
|
79
|
242 |
|
unset($this->data); |
|
80
|
242 |
|
} |
|
81
|
|
|
|
|
82
|
66 |
|
public function columnCount() : int |
|
83
|
|
|
{ |
|
84
|
66 |
|
return $this->statement->columnCount(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
110 |
|
public function fetchNumeric() |
|
91
|
|
|
{ |
|
92
|
110 |
|
$row = $this->fetch(); |
|
93
|
|
|
|
|
94
|
110 |
|
if ($row === false) { |
|
95
|
110 |
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
110 |
|
return array_values($row); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
154 |
|
public function fetchAssociative() |
|
105
|
|
|
{ |
|
106
|
154 |
|
return $this->fetch(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
22 |
|
public function fetchOne() |
|
113
|
|
|
{ |
|
114
|
22 |
|
return FetchUtils::fetchOneFromNumeric($this); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
|
|
public function fetchAllNumeric() : array |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->store( |
|
123
|
|
|
$this->statement->fetchAllAssociative(), |
|
124
|
|
|
true |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
22 |
|
public function fetchAllAssociative() : array |
|
132
|
|
|
{ |
|
133
|
22 |
|
return $this->store( |
|
134
|
22 |
|
$this->statement->fetchAllAssociative(), |
|
135
|
22 |
|
true |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
22 |
|
public function fetchColumn() : array |
|
143
|
|
|
{ |
|
144
|
22 |
|
return $this->store( |
|
145
|
22 |
|
$this->statement->fetchAllAssociative(), |
|
146
|
22 |
|
true |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return Traversable<int,array<int,mixed>> |
|
152
|
|
|
* |
|
153
|
|
|
* @throws DriverException |
|
154
|
|
|
*/ |
|
155
|
|
|
public function iterateNumeric() : Traversable |
|
156
|
|
|
{ |
|
157
|
|
|
return FetchUtils::iterateNumericByOne($this); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return Traversable<int,array<string,mixed>> |
|
162
|
|
|
* |
|
163
|
|
|
* @throws DriverException |
|
164
|
|
|
*/ |
|
165
|
|
|
public function iterateAssociative() : Traversable |
|
166
|
|
|
{ |
|
167
|
|
|
return FetchUtils::iterateAssociativeByOne($this); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return Traversable<int,mixed> |
|
172
|
|
|
* |
|
173
|
|
|
* @throws DriverException |
|
174
|
|
|
*/ |
|
175
|
|
|
public function iterateColumn() : Traversable |
|
176
|
|
|
{ |
|
177
|
|
|
return FetchUtils::iterateColumnByOne($this); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement |
|
182
|
|
|
* executed by the corresponding object. |
|
183
|
|
|
* |
|
184
|
|
|
* If the last SQL statement executed by the associated Statement object was a SELECT statement, |
|
185
|
|
|
* some databases may return the number of rows returned by that statement. However, |
|
186
|
|
|
* this behaviour is not guaranteed for all databases and should not be |
|
187
|
|
|
* relied on for portable applications. |
|
188
|
|
|
* |
|
189
|
|
|
* @return int The number of rows. |
|
190
|
|
|
*/ |
|
191
|
|
|
public function rowCount() : int |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->statement->rowCount(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return array<string,mixed>|false |
|
198
|
|
|
* |
|
199
|
|
|
* @throws DriverException |
|
200
|
|
|
*/ |
|
201
|
220 |
|
private function fetch() |
|
202
|
|
|
{ |
|
203
|
220 |
|
if ($this->data === null) { |
|
204
|
220 |
|
$this->data = []; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
220 |
|
$row = $this->statement->fetchAssociative(); |
|
208
|
|
|
|
|
209
|
220 |
|
if ($row !== false) { |
|
210
|
176 |
|
$this->data[] = $row; |
|
211
|
|
|
|
|
212
|
176 |
|
return $row; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
220 |
|
$this->emptied = true; |
|
216
|
|
|
|
|
217
|
220 |
|
return false; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param array<int,array<mixed>> $data |
|
222
|
|
|
* |
|
223
|
|
|
* @return array<int,array<mixed>> |
|
224
|
|
|
*/ |
|
225
|
44 |
|
private function store(array $data, bool $isArray) : array |
|
226
|
|
|
{ |
|
227
|
44 |
|
if (! $isArray) { |
|
228
|
|
|
foreach ($data as $key => $value) { |
|
229
|
|
|
$data[$key] = [$value]; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
44 |
|
$this->data = $data; |
|
234
|
44 |
|
$this->emptied = true; |
|
235
|
|
|
|
|
236
|
44 |
|
return $this->data; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|