diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/PDO/Connection.php')
| -rw-r--r-- | vendor/doctrine/dbal/src/Driver/PDO/Connection.php | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/PDO/Connection.php b/vendor/doctrine/dbal/src/Driver/PDO/Connection.php new file mode 100644 index 0000000..b1faca1 --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/PDO/Connection.php | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\DBAL\Driver\PDO; | ||
| 6 | |||
| 7 | use Doctrine\DBAL\Driver\Connection as ConnectionInterface; | ||
| 8 | use Doctrine\DBAL\Driver\Exception\IdentityColumnsNotSupported; | ||
| 9 | use Doctrine\DBAL\Driver\Exception\NoIdentityValue; | ||
| 10 | use PDO; | ||
| 11 | use PDOException; | ||
| 12 | use PDOStatement; | ||
| 13 | |||
| 14 | use function assert; | ||
| 15 | |||
| 16 | final class Connection implements ConnectionInterface | ||
| 17 | { | ||
| 18 | /** @internal The connection can be only instantiated by its driver. */ | ||
| 19 | public function __construct(private readonly PDO $connection) | ||
| 20 | { | ||
| 21 | $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
| 22 | } | ||
| 23 | |||
| 24 | public function exec(string $sql): int | ||
| 25 | { | ||
| 26 | try { | ||
| 27 | $result = $this->connection->exec($sql); | ||
| 28 | |||
| 29 | assert($result !== false); | ||
| 30 | |||
| 31 | return $result; | ||
| 32 | } catch (PDOException $exception) { | ||
| 33 | throw Exception::new($exception); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | public function getServerVersion(): string | ||
| 38 | { | ||
| 39 | return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); | ||
| 40 | } | ||
| 41 | |||
| 42 | public function prepare(string $sql): Statement | ||
| 43 | { | ||
| 44 | try { | ||
| 45 | $stmt = $this->connection->prepare($sql); | ||
| 46 | assert($stmt instanceof PDOStatement); | ||
| 47 | |||
| 48 | return new Statement($stmt); | ||
| 49 | } catch (PDOException $exception) { | ||
| 50 | throw Exception::new($exception); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | public function query(string $sql): Result | ||
| 55 | { | ||
| 56 | try { | ||
| 57 | $stmt = $this->connection->query($sql); | ||
| 58 | assert($stmt instanceof PDOStatement); | ||
| 59 | |||
| 60 | return new Result($stmt); | ||
| 61 | } catch (PDOException $exception) { | ||
| 62 | throw Exception::new($exception); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | public function quote(string $value): string | ||
| 67 | { | ||
| 68 | return $this->connection->quote($value); | ||
| 69 | } | ||
| 70 | |||
| 71 | public function lastInsertId(): int|string | ||
| 72 | { | ||
| 73 | try { | ||
| 74 | $value = $this->connection->lastInsertId(); | ||
| 75 | } catch (PDOException $exception) { | ||
| 76 | assert($exception->errorInfo !== null); | ||
| 77 | [$sqlState] = $exception->errorInfo; | ||
| 78 | |||
| 79 | // if the PDO driver does not support this capability, PDO::lastInsertId() triggers an IM001 SQLSTATE | ||
| 80 | // see https://www.php.net/manual/en/pdo.lastinsertid.php | ||
| 81 | if ($sqlState === 'IM001') { | ||
| 82 | throw IdentityColumnsNotSupported::new(); | ||
| 83 | } | ||
| 84 | |||
| 85 | // PDO PGSQL throws a 'lastval is not yet defined in this session' error when no identity value is | ||
| 86 | // available, with SQLSTATE 55000 'Object Not In Prerequisite State' | ||
| 87 | if ($sqlState === '55000' && $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME) === 'pgsql') { | ||
| 88 | throw NoIdentityValue::new($exception); | ||
| 89 | } | ||
| 90 | |||
| 91 | throw Exception::new($exception); | ||
| 92 | } | ||
| 93 | |||
| 94 | // pdo_mysql & pdo_sqlite return '0', pdo_sqlsrv returns '' or false depending on the PHP version | ||
| 95 | if ($value === '0' || $value === '' || $value === false) { | ||
| 96 | throw NoIdentityValue::new(); | ||
| 97 | } | ||
| 98 | |||
| 99 | return $value; | ||
| 100 | } | ||
| 101 | |||
| 102 | public function beginTransaction(): void | ||
| 103 | { | ||
| 104 | try { | ||
| 105 | $this->connection->beginTransaction(); | ||
| 106 | } catch (PDOException $exception) { | ||
| 107 | throw Exception::new($exception); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | public function commit(): void | ||
| 112 | { | ||
| 113 | try { | ||
| 114 | $this->connection->commit(); | ||
| 115 | } catch (PDOException $exception) { | ||
| 116 | throw Exception::new($exception); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | public function rollBack(): void | ||
| 121 | { | ||
| 122 | try { | ||
| 123 | $this->connection->rollBack(); | ||
| 124 | } catch (PDOException $exception) { | ||
| 125 | throw Exception::new($exception); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | public function getNativeConnection(): PDO | ||
| 130 | { | ||
| 131 | return $this->connection; | ||
| 132 | } | ||
| 133 | } | ||
