Skip to content

Commit a008397

Browse files
committed
Fixing middleware order, dbal params and result proxy for cache middleware
1 parent 55d4aa8 commit a008397

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

src/RunOpenCode/Bundle/QueryResourcesLoader/Executor/Dbal/DbalParameters.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ final public function timeImmutable(string $name, ?\DateTimeInterface $value): s
203203
final public function enum(string $name, ?\UnitEnum $value): self
204204
{
205205
if (null === $value) {
206-
return $this->set($name, null, Types::STRING);
206+
return $this->set($name, null);
207207
}
208-
208+
209209
$reflection = new \ReflectionEnum($value::class);
210210

211211
if (!$reflection->isBacked()) {
@@ -235,13 +235,13 @@ final public function enum(string $name, ?\UnitEnum $value): self
235235
final public function integerArray(string $name, ?iterable $value): self
236236
{
237237
if (null === $value) {
238-
return $this->set($name, null, ArrayParameterType::INTEGER);
238+
return $this->set($name, null);
239239
}
240240

241241
$value = \is_array($value) ? $value : \iterator_to_array($value);
242242

243243
if (0 === \count($value)) {
244-
return $this->set($name, null, ArrayParameterType::INTEGER);
244+
return $this->set($name, null);
245245
}
246246

247247
return $this->set($name, $value, ArrayParameterType::INTEGER);
@@ -260,21 +260,18 @@ final public function integerArray(string $name, ?iterable $value): self
260260
final public function stringArray(string $name, ?iterable $value): self
261261
{
262262
if (null === $value) {
263-
return $this->set($name, null, ArrayParameterType::INTEGER);
263+
return $this->set($name, null);
264264
}
265265

266266
$value = \is_array($value) ? $value : \iterator_to_array($value);
267267

268268
if (0 === \count($value)) {
269-
return $this->set($name, null, ArrayParameterType::STRING);
269+
return $this->set($name, null);
270270
}
271271

272272
return $this->set(
273273
$name,
274-
\array_map(
275-
static fn(\Stringable|string $current): string => (string)$current,
276-
$value
277-
),
274+
\array_map('\strval', $value),
278275
ArrayParameterType::STRING
279276
);
280277
}
@@ -292,21 +289,18 @@ final public function stringArray(string $name, ?iterable $value): self
292289
final public function asciiArray(string $name, ?iterable $value): self
293290
{
294291
if (null === $value) {
295-
return $this->set($name, null, ArrayParameterType::ASCII);
292+
return $this->set($name, null);
296293
}
297294

298295
$value = \is_array($value) ? $value : \iterator_to_array($value);
299296

300297
if (0 === \count($value)) {
301-
return $this->set($name, null, ArrayParameterType::ASCII);
298+
return $this->set($name, null);
302299
}
303300

304301
return $this->set(
305302
$name,
306-
\array_map(
307-
static fn(\Stringable|string $current): string => (string)$current,
308-
$value
309-
),
303+
\array_map('\strval', $value),
310304
ArrayParameterType::ASCII
311305
);
312306
}
@@ -324,13 +318,13 @@ final public function asciiArray(string $name, ?iterable $value): self
324318
final public function binaryArray(string $name, ?iterable $value): self
325319
{
326320
if (null === $value) {
327-
return $this->set($name, null, ArrayParameterType::BINARY);
321+
return $this->set($name, null);
328322
}
329323

330324
$value = \is_array($value) ? $value : \iterator_to_array($value);
331325

332326
if (0 === \count($value)) {
333-
return $this->set($name, null, ArrayParameterType::BINARY);
327+
return $this->set($name, null);
334328
}
335329

336330
return $this->set($name, $value, ArrayParameterType::BINARY);
@@ -355,13 +349,13 @@ final public function binaryArray(string $name, ?iterable $value): self
355349
final public function enumArray(string $name, ?iterable $value): self
356350
{
357351
if (null === $value) {
358-
return $this->set($name, null, ArrayParameterType::STRING);
352+
return $this->set($name, null);
359353
}
360354

361355
$value = \is_array($value) ? $value : \iterator_to_array($value);
362356

363357
if (0 === \count($value)) {
364-
return $this->set($name, null, ArrayParameterType::BINARY);
358+
return $this->set($name, null);
365359
}
366360

367361
$hasString = false;

src/RunOpenCode/Bundle/QueryResourcesLoader/Executor/Dbal/ResultProxy.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,7 @@ public function count(): int
160160
return $count;
161161
}
162162

163-
if ($this->result instanceof ArrayResult) {
164-
return $this->rowCount();
165-
}
166-
167-
// If rowCount() returns 0, we need to fetch all rows to count them,
168-
// and we need to replace result with array result to be able to fetch all rows.
169-
$rows = $this->fetchAllNumeric();
170-
$columnNames = [];
171-
172-
for ($index = 0; $index < $this->columnCount(); ++$index) {
173-
$columnNames[] = $this->result->getColumnName($index);
174-
}
175-
176-
$this->result = new ArrayResult($columnNames, $rows);
163+
$this->toArrayResult();
177164

178165
return $this->rowCount();
179166
}
@@ -186,20 +173,7 @@ public function count(): int
186173
*/
187174
public function __serialize(): array
188175
{
189-
$columnNames = [];
190-
$rows = $this->result->fetchAllNumeric();
191-
192-
for ($index = 0; $index < $this->columnCount(); ++$index) {
193-
$columnNames[] = $this->result->getColumnName($index);
194-
}
195-
196-
// Replace result with array result so this resultset can be reused.
197-
$this->result = new ArrayResult($columnNames, $rows);
198-
199-
return [
200-
'columnNames' => $columnNames,
201-
'rows' => $rows,
202-
];
176+
return $this->toArrayResult()->__serialize(); // @phpstan-ignore-line
203177
}
204178

205179
/**
@@ -213,6 +187,34 @@ public function __unserialize(array $data): void
213187
$this->result = new ArrayResult($data['columnNames'], $data['rows']);
214188
}
215189

190+
private function toArrayResult(): ArrayResult
191+
{
192+
// Already an array result, nothing to do.
193+
if ($this->result instanceof ArrayResult) {
194+
return $this->result;
195+
}
196+
197+
// A new implementation of Dbal, let's leverage it.
198+
if (\method_exists($this->result, 'getColumnName')) {
199+
$rows = $this->result->fetchAllNumeric();
200+
$columnNames = \array_map(fn(int $index): string => $this->result->getColumnName($index), \range(0, $this->columnCount() - 1));
201+
202+
return ($this->result = new ArrayResult($columnNames, $rows));
203+
}
204+
205+
// A legacy implementation of Dbal, let's use the old way.
206+
$data = $this->result->fetchAllAssociative();
207+
208+
if (0 === \count($data)) {
209+
return ($this->result = new ArrayResult([], []));
210+
}
211+
212+
$columnNames = \array_keys($data[0]);
213+
$rows = \array_map(fn(array $row): array => \array_values($row), $data);
214+
215+
return ($this->result = new ArrayResult($columnNames, $rows));
216+
}
217+
216218
private function createException(\Throwable $inner, string $method): DriverException
217219
{
218220
return new DriverException(\sprintf(

src/RunOpenCode/Bundle/QueryResourcesLoader/Executor/QueryExecutor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function __construct(
3838
// Last middleware is the executor.
3939
$executor = static fn(string $query, Parameters $parameters, Options $options): ExecutionResultInterface => $registry->get($options->executor)->execute($query, $parameters, $options);
4040

41+
// Reverse middlewares to build chain in correct order.
42+
$middlewares = \array_reverse([...$middlewares]);
43+
4144
// Build middleware chain.
4245
foreach ($middlewares as $middleware) {
4346
$executor = static fn(string $query, Parameters $parameters, Options $options): ExecutionResultInterface => $middleware($query, $parameters, $options, $executor);

0 commit comments

Comments
 (0)