|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Nettrine\DBAL\Middleware\Debug; |
| 4 | + |
| 5 | +use Doctrine\DBAL\ParameterType; |
| 6 | + |
| 7 | +/** |
| 8 | + * @see https://github.com/symfony/doctrine-bridge |
| 9 | + * @internal |
| 10 | + */ |
| 11 | +class DebugQuery |
| 12 | +{ |
| 13 | + |
| 14 | + /** @var array<int|string, mixed> */ |
| 15 | + private array $params = []; |
| 16 | + |
| 17 | + /** @var array<ParameterType|int> */ |
| 18 | + private array $types = []; |
| 19 | + |
| 20 | + private ?float $start = null; |
| 21 | + |
| 22 | + private ?float $duration = null; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private readonly string $sql, |
| 26 | + ) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public function start(): void |
| 31 | + { |
| 32 | + $this->start = microtime(true); |
| 33 | + } |
| 34 | + |
| 35 | + public function stop(): void |
| 36 | + { |
| 37 | + if ($this->start !== null) { |
| 38 | + $this->duration = microtime(true) - $this->start; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public function setValue(string|int $param, mixed $value, ParameterType|int $type): void |
| 43 | + { |
| 44 | + // Numeric indexes start at 0 in profiler |
| 45 | + $idx = is_int($param) ? $param - 1 : $param; |
| 46 | + |
| 47 | + $this->params[$idx] = $value; |
| 48 | + $this->types[$idx] = $type; |
| 49 | + } |
| 50 | + |
| 51 | + public function getSql(): string |
| 52 | + { |
| 53 | + return $this->sql; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return array<int|string, mixed> |
| 58 | + */ |
| 59 | + public function getParams(): array |
| 60 | + { |
| 61 | + return $this->params; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return array<int, int|ParameterType> |
| 66 | + */ |
| 67 | + public function getTypes(): array |
| 68 | + { |
| 69 | + return $this->types; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Query duration in seconds. |
| 74 | + */ |
| 75 | + public function getDuration(): ?float |
| 76 | + { |
| 77 | + return $this->duration; |
| 78 | + } |
| 79 | + |
| 80 | + public function __clone() |
| 81 | + { |
| 82 | + $copy = []; |
| 83 | + |
| 84 | + foreach ($this->params as $param => $valueOrVariable) { |
| 85 | + $copy[$param] = $valueOrVariable; |
| 86 | + } |
| 87 | + |
| 88 | + $this->params = $copy; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments