diff --git a/src/Symfony/Component/Cache/Adapter/PdoAdapter.php b/src/Symfony/Component/Cache/Adapter/PdoAdapter.php index 6582e2a442e32..815c622b03afb 100644 --- a/src/Symfony/Component/Cache/Adapter/PdoAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/PdoAdapter.php @@ -102,10 +102,7 @@ public function __construct(#[\SensitiveParameter] \PDO|string $connOrDsn, strin */ public function createTable() { - // connect if we are not yet - $conn = $this->getConnection(); - - $sql = match ($this->driver) { + $sql = match ($driver = $this->getDriver()) { // We use varbinary for the ID column because it prevents unwanted conversions: // - character set conversions between server and client // - trailing space removal @@ -116,10 +113,10 @@ public function createTable() 'pgsql' => "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol BYTEA NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)", 'oci' => "CREATE TABLE $this->table ($this->idCol VARCHAR2(255) NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)", 'sqlsrv' => "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol VARBINARY(MAX) NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)", - default => throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $this->driver)), + default => throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $driver)), }; - $conn->exec($sql); + $this->getConnection()->exec($sql); } public function prune(): bool @@ -211,7 +208,7 @@ protected function doClear(string $namespace): bool $conn = $this->getConnection(); if ('' === $namespace) { - if ('sqlite' === $this->driver) { + if ('sqlite' === $this->getDriver()) { $sql = "DELETE FROM $this->table"; } else { $sql = "TRUNCATE TABLE $this->table"; @@ -249,7 +246,7 @@ protected function doSave(array $values, int $lifetime): array|bool $conn = $this->getConnection(); - $driver = $this->driver; + $driver = $this->getDriver(); $insertSql = "INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (:id, :data, :lifetime, :time)"; switch (true) { @@ -286,7 +283,7 @@ protected function doSave(array $values, int $lifetime): array|bool try { $stmt = $conn->prepare($sql); } catch (\PDOException $e) { - if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) { + if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) { $this->createTable(); } $stmt = $conn->prepare($sql); @@ -321,7 +318,7 @@ protected function doSave(array $values, int $lifetime): array|bool try { $stmt->execute(); } catch (\PDOException $e) { - if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) { + if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) { $this->createTable(); } $stmt->execute(); @@ -343,7 +340,7 @@ protected function doSave(array $values, int $lifetime): array|bool */ protected function getId(mixed $key): string { - if ('pgsql' !== $this->driver ??= ($this->getConnection() ? $this->driver : null)) { + if ('pgsql' !== $this->getDriver()) { return parent::getId($key); } @@ -360,30 +357,32 @@ private function getConnection(): \PDO $this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions); $this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } - $this->driver ??= $this->conn->getAttribute(\PDO::ATTR_DRIVER_NAME); return $this->conn; } + private function getDriver(): string + { + return $this->driver ??= $this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME); + } + private function getServerVersion(): string { - return $this->serverVersion ??= $this->conn->getAttribute(\PDO::ATTR_SERVER_VERSION); + return $this->serverVersion ??= $this->getConnection()->getAttribute(\PDO::ATTR_SERVER_VERSION); } private function isTableMissing(\PDOException $exception): bool { - $driver = $this->driver; + $driver = $this->getDriver(); $code = $exception->getCode(); - switch (true) { - case 'pgsql' === $driver && '42P01' === $code: - case 'sqlite' === $driver && str_contains($exception->getMessage(), 'no such table:'): - case 'oci' === $driver && 942 === $code: - case 'sqlsrv' === $driver && 208 === $code: - case 'mysql' === $driver && 1146 === $code: - return true; - default: - return false; - } + return match ($driver) { + 'pgsql' => '42P01' === $code, + 'sqlite' => str_contains($exception->getMessage(), 'no such table:'), + 'oci' => 942 === $code, + 'sqlsrv' => 208 === $code, + 'mysql' => 1146 === $code, + default => false, + }; } } diff --git a/src/Symfony/Component/Lock/Store/PdoStore.php b/src/Symfony/Component/Lock/Store/PdoStore.php index 5582dc0278145..da3d968d2d930 100644 --- a/src/Symfony/Component/Lock/Store/PdoStore.php +++ b/src/Symfony/Component/Lock/Store/PdoStore.php @@ -95,7 +95,7 @@ public function save(Key $key) try { $stmt = $conn->prepare($sql); } catch (\PDOException $e) { - if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) { + if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->getDriver(), ['pgsql', 'sqlite', 'sqlsrv'], true))) { $this->createTable(); } $stmt = $conn->prepare($sql); @@ -107,12 +107,12 @@ public function save(Key $key) try { $stmt->execute(); } catch (\PDOException $e) { - if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) { + if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->getDriver(), ['pgsql', 'sqlite', 'sqlsrv'], true))) { $this->createTable(); try { $stmt->execute(); - } catch (\PDOException $e) { + } catch (\PDOException) { $this->putOffExpiration($key, $this->initialTtl); } } else { @@ -196,11 +196,7 @@ private function getConnection(): \PDO */ public function createTable(): void { - // connect if we are not yet - $conn = $this->getConnection(); - $driver = $this->getDriver(); - - $sql = match ($driver) { + $sql = match ($driver = $this->getDriver()) { 'mysql' => "CREATE TABLE $this->table ($this->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $this->tokenCol VARCHAR(44) NOT NULL, $this->expirationCol INTEGER UNSIGNED NOT NULL) COLLATE utf8mb4_bin, ENGINE = InnoDB", 'sqlite' => "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->tokenCol TEXT NOT NULL, $this->expirationCol INTEGER)", 'pgsql' => "CREATE TABLE $this->table ($this->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $this->tokenCol VARCHAR(64) NOT NULL, $this->expirationCol INTEGER)", @@ -209,7 +205,7 @@ public function createTable(): void default => throw new \DomainException(sprintf('Creating the lock table is currently not implemented for platform "%s".', $driver)), }; - $conn->exec($sql); + $this->getConnection()->exec($sql); } /** @@ -224,14 +220,7 @@ private function prune(): void private function getDriver(): string { - if (isset($this->driver)) { - return $this->driver; - } - - $conn = $this->getConnection(); - $this->driver = $conn->getAttribute(\PDO::ATTR_DRIVER_NAME); - - return $this->driver; + return $this->driver ??= $this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME); } /** @@ -254,15 +243,13 @@ private function isTableMissing(\PDOException $exception): bool $driver = $this->getDriver(); $code = $exception->getCode(); - switch (true) { - case 'pgsql' === $driver && '42P01' === $code: - case 'sqlite' === $driver && str_contains($exception->getMessage(), 'no such table:'): - case 'oci' === $driver && 942 === $code: - case 'sqlsrv' === $driver && 208 === $code: - case 'mysql' === $driver && 1146 === $code: - return true; - default: - return false; - } + return match ($driver) { + 'pgsql' => '42P01' === $code, + 'sqlite' => str_contains($exception->getMessage(), 'no such table:'), + 'oci' => 942 === $code, + 'sqlsrv' => 208 === $code, + 'mysql' => 1146 === $code, + default => false, + }; } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy