|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Database; |
| 4 | + |
| 5 | +use Utopia\CLI\Console; |
| 6 | + |
| 7 | +/** |
| 8 | + * Wraps a \PDOStatement so a connection lost at execution time is recovered |
| 9 | + * transparently: the owning PDO reconnects, the statement is re-prepared |
| 10 | + * against the fresh connection, previously bound parameters and attributes are |
| 11 | + * replayed, and the failed call is retried. |
| 12 | + * |
| 13 | + * Recovery is skipped while a transaction is open, because the uncommitted |
| 14 | + * state lives on the dead connection and cannot survive a reconnect. There the |
| 15 | + * call is rethrown so the transaction can be rolled back and replayed from the |
| 16 | + * start by the caller. |
| 17 | + * |
| 18 | + * @mixin \PDOStatement |
| 19 | + */ |
| 20 | +class PDOStatement |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var array<int|string, array{mixed, int}> |
| 24 | + */ |
| 25 | + private array $values = []; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var array<int|string, array{mixed, int, int, mixed}> |
| 29 | + */ |
| 30 | + private array $params = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var array<int|string, array{mixed, ?int, ?int, mixed}> |
| 34 | + */ |
| 35 | + private array $columns = []; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var array<int, mixed> |
| 39 | + */ |
| 40 | + private array $attributes = []; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var array<int|string, mixed>|null |
| 44 | + */ |
| 45 | + private ?array $fetchMode = null; |
| 46 | + |
| 47 | + public function __construct( |
| 48 | + private readonly PDO $pdo, |
| 49 | + private \PDOStatement $statement, |
| 50 | + private readonly string $query, |
| 51 | + ) { |
| 52 | + } |
| 53 | + |
| 54 | + public function __get(string $name): mixed |
| 55 | + { |
| 56 | + return $this->statement->{$name}; |
| 57 | + } |
| 58 | + |
| 59 | + public function __set(string $name, mixed $value): void |
| 60 | + { |
| 61 | + $this->statement->{$name} = $value; |
| 62 | + } |
| 63 | + |
| 64 | + public function __isset(string $name): bool |
| 65 | + { |
| 66 | + return isset($this->statement->{$name}); |
| 67 | + } |
| 68 | + |
| 69 | + public function __unset(string $name): void |
| 70 | + { |
| 71 | + unset($this->statement->{$name}); |
| 72 | + } |
| 73 | + |
| 74 | + public function __clone(): void |
| 75 | + { |
| 76 | + throw new \Error('Trying to clone an uncloneable PDOStatement'); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param array<mixed> $args |
| 81 | + * @throws \Throwable |
| 82 | + */ |
| 83 | + public function __call(string $method, array $args): mixed |
| 84 | + { |
| 85 | + try { |
| 86 | + return $this->statement->{$method}(...$args); |
| 87 | + } catch (\Throwable $e) { |
| 88 | + if ($this->pdo->inTransaction() || !Connection::hasError($e)) { |
| 89 | + throw $e; |
| 90 | + } |
| 91 | + |
| 92 | + Console::warning('[Database] ' . $e->getMessage()); |
| 93 | + Console::warning('[Database] Lost connection detected. Re-preparing statement...'); |
| 94 | + |
| 95 | + $this->reprepare(); |
| 96 | + |
| 97 | + return $this->statement->{$method}(...$args); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public function getStatement(): \PDOStatement |
| 102 | + { |
| 103 | + return $this->statement; |
| 104 | + } |
| 105 | + |
| 106 | + public function setAttribute(int $attribute, mixed $value): bool |
| 107 | + { |
| 108 | + $this->attributes[$attribute] = $value; |
| 109 | + |
| 110 | + return $this->statement->setAttribute($attribute, $value); |
| 111 | + } |
| 112 | + |
| 113 | + public function setFetchMode(int $mode, mixed ...$args): bool |
| 114 | + { |
| 115 | + $this->fetchMode = [$mode, ...$args]; |
| 116 | + |
| 117 | + return $this->statement->setFetchMode($mode, ...$args); |
| 118 | + } |
| 119 | + |
| 120 | + public function bindValue(int|string $param, mixed $value, int $type = \PDO::PARAM_STR): bool |
| 121 | + { |
| 122 | + $this->values[$param] = [$value, $type]; |
| 123 | + |
| 124 | + return $this->statement->bindValue($param, $value, $type); |
| 125 | + } |
| 126 | + |
| 127 | + public function bindParam(int|string $param, mixed &$variable, int $type = \PDO::PARAM_STR, int $maxLength = 0, mixed $driverOptions = null): bool |
| 128 | + { |
| 129 | + $this->params[$param] = [$variable, $type, $maxLength, $driverOptions]; |
| 130 | + |
| 131 | + return $this->statement->bindParam($param, $variable, $type, $maxLength, $driverOptions); |
| 132 | + } |
| 133 | + |
| 134 | + public function bindColumn(int|string $column, mixed &$variable, ?int $type = null, ?int $maxLength = null, mixed $driverOptions = null): bool |
| 135 | + { |
| 136 | + $this->columns[$column] = [$variable, $type, $maxLength, $driverOptions]; |
| 137 | + |
| 138 | + return $this->statement->bindColumn($column, $variable, $type, $maxLength, $driverOptions); |
| 139 | + } |
| 140 | + |
| 141 | + private function reprepare(): void |
| 142 | + { |
| 143 | + $this->pdo->reconnect(); |
| 144 | + $this->statement = $this->pdo->prepareNative($this->query); |
| 145 | + |
| 146 | + foreach ($this->attributes as $attribute => $value) { |
| 147 | + $this->statement->setAttribute($attribute, $value); |
| 148 | + } |
| 149 | + |
| 150 | + if ($this->fetchMode !== null) { |
| 151 | + $this->statement->setFetchMode(...$this->fetchMode); |
| 152 | + } |
| 153 | + |
| 154 | + foreach ($this->params as $param => [$variable, $type, $maxLength, $driverOptions]) { |
| 155 | + $this->statement->bindParam($param, $variable, $type, $maxLength, $driverOptions); |
| 156 | + } |
| 157 | + |
| 158 | + foreach ($this->columns as $column => [$variable, $type, $maxLength, $driverOptions]) { |
| 159 | + $this->statement->bindColumn($column, $variable, $type, $maxLength, $driverOptions); |
| 160 | + } |
| 161 | + |
| 162 | + foreach ($this->values as $param => [$value, $type]) { |
| 163 | + $this->statement->bindValue($param, $value, $type); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments