|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Database; |
| 4 | + |
| 5 | +use Utopia\Console; |
| 6 | + |
| 7 | +/** |
| 8 | + * Wraps a \PDOStatement so a connection lost during execution is recovered |
| 9 | + * transparently: the owning PDO reconnects, the statement is re-prepared |
| 10 | + * against the fresh connection, previously bound parameters/columns/attributes |
| 11 | + * are replayed, and the failed execute() is retried. |
| 12 | + * |
| 13 | + * Recovery is attempted only for execute(), and only outside a transaction: |
| 14 | + * re-running any other method (fetch, rowCount, ...) without a fresh execute |
| 15 | + * would return data from an unexecuted statement, and a connection cannot be |
| 16 | + * healed in place mid-transaction (the uncommitted state is gone, so the call |
| 17 | + * is rethrown for Adapter::withTransaction to roll back and replay). |
| 18 | + * |
| 19 | + * @mixin \PDOStatement |
| 20 | + * @implements \IteratorAggregate<int, mixed> |
| 21 | + */ |
| 22 | +class PDOStatement implements \IteratorAggregate |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var array<int|string, array{mixed, int}> |
| 26 | + */ |
| 27 | + private array $values = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var array<int|string, array{mixed, int, int, mixed}> |
| 31 | + */ |
| 32 | + private array $params = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * The order bindValue()/bindParam() were called, so a placeholder rebound |
| 36 | + * across methods replays with the last binding winning, as PDO applies it. |
| 37 | + * |
| 38 | + * @var array<int, array{string, int|string}> |
| 39 | + */ |
| 40 | + private array $bindOrder = []; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var array<int|string, array{mixed, int, ?int, ?int, mixed}> |
| 44 | + */ |
| 45 | + private array $columns = []; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var array<int, mixed> |
| 49 | + */ |
| 50 | + private array $attributes = []; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var array<int|string, mixed>|null |
| 54 | + */ |
| 55 | + private ?array $fetchMode = null; |
| 56 | + |
| 57 | + /** |
| 58 | + * @param array<mixed> $options |
| 59 | + */ |
| 60 | + public function __construct( |
| 61 | + private readonly PDO $pdo, |
| 62 | + private \PDOStatement $statement, |
| 63 | + private readonly string $query, |
| 64 | + private readonly array $options = [], |
| 65 | + ) { |
| 66 | + } |
| 67 | + |
| 68 | + public function __get(string $name): mixed |
| 69 | + { |
| 70 | + return $this->statement->{$name}; |
| 71 | + } |
| 72 | + |
| 73 | + public function __set(string $name, mixed $value): void |
| 74 | + { |
| 75 | + $this->statement->{$name} = $value; |
| 76 | + } |
| 77 | + |
| 78 | + public function __isset(string $name): bool |
| 79 | + { |
| 80 | + return isset($this->statement->{$name}); |
| 81 | + } |
| 82 | + |
| 83 | + public function __unset(string $name): void |
| 84 | + { |
| 85 | + unset($this->statement->{$name}); |
| 86 | + } |
| 87 | + |
| 88 | + public function __clone(): void |
| 89 | + { |
| 90 | + throw new \Error('Trying to clone an uncloneable PDOStatement'); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Preserve \PDOStatement's native iterability (foreach over rows), which |
| 95 | + * does not route through __call(). |
| 96 | + */ |
| 97 | + public function getIterator(): \Traversable |
| 98 | + { |
| 99 | + return $this->statement; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param array<mixed> $args |
| 104 | + * @throws \Throwable |
| 105 | + */ |
| 106 | + public function __call(string $method, array $args): mixed |
| 107 | + { |
| 108 | + try { |
| 109 | + return $this->statement->{$method}(...$args); |
| 110 | + } catch (\Throwable $e) { |
| 111 | + if ( |
| 112 | + \strcasecmp($method, 'execute') !== 0 |
| 113 | + || $this->pdo->inTransaction() |
| 114 | + || !Connection::hasError($e) |
| 115 | + ) { |
| 116 | + throw $e; |
| 117 | + } |
| 118 | + |
| 119 | + Console::warning('[Database] ' . $e->getMessage()); |
| 120 | + Console::warning('[Database] Lost connection detected. Re-preparing statement...'); |
| 121 | + |
| 122 | + $this->reprepare(); |
| 123 | + |
| 124 | + return $this->statement->{$method}(...$args); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public function getStatement(): \PDOStatement |
| 129 | + { |
| 130 | + return $this->statement; |
| 131 | + } |
| 132 | + |
| 133 | + public function setAttribute(int $attribute, mixed $value): bool |
| 134 | + { |
| 135 | + $this->attributes[$attribute] = $value; |
| 136 | + |
| 137 | + return $this->statement->setAttribute($attribute, $value); |
| 138 | + } |
| 139 | + |
| 140 | + public function setFetchMode(int $mode, mixed ...$args): bool |
| 141 | + { |
| 142 | + $this->fetchMode = [$mode, ...$args]; |
| 143 | + |
| 144 | + return $this->statement->setFetchMode($mode, ...$args); |
| 145 | + } |
| 146 | + |
| 147 | + public function bindValue(int|string $param, mixed $value, int $type = \PDO::PARAM_STR): bool |
| 148 | + { |
| 149 | + $this->values[$param] = [$value, $type]; |
| 150 | + $this->bindOrder[] = ['value', $param]; |
| 151 | + |
| 152 | + return $this->statement->bindValue($param, $value, $type); |
| 153 | + } |
| 154 | + |
| 155 | + public function bindParam(int|string $param, mixed &$variable, int $type = \PDO::PARAM_STR, int $maxLength = 0, mixed $driverOptions = null): bool |
| 156 | + { |
| 157 | + // Store the variable by reference so a value changed between bind and |
| 158 | + // execute is the value replayed after a reconnect (PDO binds late). |
| 159 | + $this->params[$param] = [&$variable, $type, $maxLength, $driverOptions]; |
| 160 | + $this->bindOrder[] = ['param', $param]; |
| 161 | + |
| 162 | + return $this->statement->bindParam($param, $variable, $type, $maxLength, $driverOptions); |
| 163 | + } |
| 164 | + |
| 165 | + public function bindColumn(int|string $column, mixed &$variable, ?int $type = null, ?int $maxLength = null, mixed $driverOptions = null): bool |
| 166 | + { |
| 167 | + // Record how many optional arguments were actually supplied so omitted |
| 168 | + // ones keep PDO's real defaults instead of being replayed as explicit |
| 169 | + // nulls (which would change the call contract / emit deprecations). |
| 170 | + $arity = \func_num_args(); |
| 171 | + $this->columns[$column] = [&$variable, $arity, $type, $maxLength, $driverOptions]; |
| 172 | + |
| 173 | + return $this->bindColumnTo($this->statement, $column, $variable, $arity, $type, $maxLength, $driverOptions); |
| 174 | + } |
| 175 | + |
| 176 | + private function reprepare(): void |
| 177 | + { |
| 178 | + $this->pdo->reconnect(); |
| 179 | + $this->statement = $this->pdo->prepareNative($this->query, $this->options); |
| 180 | + |
| 181 | + foreach ($this->attributes as $attribute => $value) { |
| 182 | + $this->statement->setAttribute($attribute, $value); |
| 183 | + } |
| 184 | + |
| 185 | + if ($this->fetchMode !== null) { |
| 186 | + $this->statement->setFetchMode(...$this->fetchMode); |
| 187 | + } |
| 188 | + |
| 189 | + // Replay value/param bindings in the original call order so a placeholder |
| 190 | + // rebound across methods ends up with the binding the caller applied last. |
| 191 | + foreach ($this->bindOrder as [$kind, $key]) { |
| 192 | + if ($kind === 'value') { |
| 193 | + [$value, $type] = $this->values[$key]; |
| 194 | + $this->statement->bindValue($key, $value, $type); |
| 195 | + } else { |
| 196 | + $bind = $this->params[$key]; |
| 197 | + $this->statement->bindParam($key, $bind[0], $bind[1], $bind[2], $bind[3]); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + foreach ($this->columns as $column => $bind) { |
| 202 | + $this->bindColumnTo($this->statement, $column, $bind[0], $bind[1], $bind[2], $bind[3], $bind[4]); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Forward bindColumn passing only the optional arguments the caller |
| 208 | + * supplied ($arity counts column + variable + supplied options). |
| 209 | + */ |
| 210 | + private function bindColumnTo(\PDOStatement $statement, int|string $column, mixed &$variable, int $arity, ?int $type = null, ?int $maxLength = null, mixed $driverOptions = null): bool |
| 211 | + { |
| 212 | + return match (true) { |
| 213 | + $arity <= 2 => $statement->bindColumn($column, $variable), |
| 214 | + $arity === 3 => $statement->bindColumn($column, $variable, $type ?? \PDO::PARAM_STR), |
| 215 | + $arity === 4 => $statement->bindColumn($column, $variable, $type ?? \PDO::PARAM_STR, $maxLength ?? 0), |
| 216 | + default => $statement->bindColumn($column, $variable, $type ?? \PDO::PARAM_STR, $maxLength ?? 0, $driverOptions), |
| 217 | + }; |
| 218 | + } |
| 219 | +} |
0 commit comments