Skip to content

[Cache][Lock] PdoAdapter/PdoStore minor cleanup #52667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/Symfony/Component/Cache/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}

Expand All @@ -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,
};
}
}
41 changes: 14 additions & 27 deletions src/Symfony/Component/Lock/Store/PdoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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)",
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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,
};
}
}