Skip to content

Commit 5fae508

Browse files
committed
[Cache][Lock] PdoAdapter/PdoStore minor cleanup
1 parent adec730 commit 5fae508

File tree

2 files changed

+41
-55
lines changed

2 files changed

+41
-55
lines changed

src/Symfony/Component/Cache/Adapter/PdoAdapter.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ public function __construct(#[\SensitiveParameter] \PDO|string $connOrDsn, strin
102102
*/
103103
public function createTable()
104104
{
105-
// connect if we are not yet
106-
$conn = $this->getConnection();
107-
108-
$sql = match ($this->driver) {
105+
$sql = match ($driver = $this->getDriver()) {
109106
// We use varbinary for the ID column because it prevents unwanted conversions:
110107
// - character set conversions between server and client
111108
// - trailing space removal
@@ -116,10 +113,10 @@ public function createTable()
116113
'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)",
117114
'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)",
118115
'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)",
119-
default => throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $this->driver)),
116+
default => throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $driver)),
120117
};
121118

122-
$conn->exec($sql);
119+
$this->getConnection()->exec($sql);
123120
}
124121

125122
public function prune(): bool
@@ -211,7 +208,7 @@ protected function doClear(string $namespace): bool
211208
$conn = $this->getConnection();
212209

213210
if ('' === $namespace) {
214-
if ('sqlite' === $this->driver) {
211+
if ('sqlite' === $this->getDriver()) {
215212
$sql = "DELETE FROM $this->table";
216213
} else {
217214
$sql = "TRUNCATE TABLE $this->table";
@@ -249,7 +246,7 @@ protected function doSave(array $values, int $lifetime): array|bool
249246

250247
$conn = $this->getConnection();
251248

252-
$driver = $this->driver;
249+
$driver = $this->getDriver();
253250
$insertSql = "INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (:id, :data, :lifetime, :time)";
254251

255252
switch (true) {
@@ -285,8 +282,8 @@ protected function doSave(array $values, int $lifetime): array|bool
285282
$lifetime = $lifetime ?: null;
286283
try {
287284
$stmt = $conn->prepare($sql);
288-
} catch (\PDOException) {
289-
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
285+
} catch (\PDOException $e) {
286+
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
290287
$this->createTable();
291288
}
292289
$stmt = $conn->prepare($sql);
@@ -320,8 +317,8 @@ protected function doSave(array $values, int $lifetime): array|bool
320317
foreach ($values as $id => $data) {
321318
try {
322319
$stmt->execute();
323-
} catch (\PDOException) {
324-
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
320+
} catch (\PDOException $e) {
321+
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
325322
$this->createTable();
326323
}
327324
$stmt->execute();
@@ -343,7 +340,7 @@ protected function doSave(array $values, int $lifetime): array|bool
343340
*/
344341
protected function getId(mixed $key): string
345342
{
346-
if ('pgsql' !== $this->driver ??= ($this->getConnection() ? $this->driver : null)) {
343+
if ('pgsql' !== $this->getDriver()) {
347344
return parent::getId($key);
348345
}
349346

@@ -360,30 +357,32 @@ private function getConnection(): \PDO
360357
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
361358
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
362359
}
363-
$this->driver ??= $this->conn->getAttribute(\PDO::ATTR_DRIVER_NAME);
364360

365361
return $this->conn;
366362
}
367363

364+
private function getDriver(): string
365+
{
366+
return $this->driver ??= $this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME);
367+
}
368+
368369
private function getServerVersion(): string
369370
{
370-
return $this->serverVersion ??= $this->conn->getAttribute(\PDO::ATTR_SERVER_VERSION);
371+
return $this->serverVersion ??= $this->getConnection()->getAttribute(\PDO::ATTR_SERVER_VERSION);
371372
}
372373

373374
private function isTableMissing(\PDOException $exception): bool
374375
{
375-
$driver = $this->driver;
376+
$driver = $this->getDriver();
376377
$code = $exception->getCode();
377378

378-
switch (true) {
379-
case 'pgsql' === $driver && '42P01' === $code:
380-
case 'sqlite' === $driver && str_contains($exception->getMessage(), 'no such table:'):
381-
case 'oci' === $driver && 942 === $code:
382-
case 'sqlsrv' === $driver && 208 === $code:
383-
case 'mysql' === $driver && 1146 === $code:
384-
return true;
385-
default:
386-
return false;
387-
}
379+
return match (true) {
380+
'pgsql' === $driver && '42P01' === $code,
381+
'sqlite' === $driver && str_contains($exception->getMessage(), 'no such table:'),
382+
'oci' === $driver && 942 === $code,
383+
'sqlsrv' === $driver && 208 === $code,
384+
'mysql' === $driver && 1146 === $code => true,
385+
default => false,
386+
};
388387
}
389388
}

src/Symfony/Component/Lock/Store/PdoStore.php

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public function save(Key $key)
9494
$conn = $this->getConnection();
9595
try {
9696
$stmt = $conn->prepare($sql);
97-
} catch (\PDOException) {
98-
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
97+
} catch (\PDOException $e) {
98+
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->getDriver(), ['pgsql', 'sqlite', 'sqlsrv'], true))) {
9999
$this->createTable();
100100
}
101101
$stmt = $conn->prepare($sql);
@@ -106,13 +106,13 @@ public function save(Key $key)
106106

107107
try {
108108
$stmt->execute();
109-
} catch (\PDOException) {
110-
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
109+
} catch (\PDOException $e) {
110+
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->getDriver(), ['pgsql', 'sqlite', 'sqlsrv'], true))) {
111111
$this->createTable();
112112

113113
try {
114114
$stmt->execute();
115-
} catch (\PDOException $e) {
115+
} catch (\PDOException) {
116116
$this->putOffExpiration($key, $this->initialTtl);
117117
}
118118
} else {
@@ -196,11 +196,7 @@ private function getConnection(): \PDO
196196
*/
197197
public function createTable(): void
198198
{
199-
// connect if we are not yet
200-
$conn = $this->getConnection();
201-
$driver = $this->getDriver();
202-
203-
$sql = match ($driver) {
199+
$sql = match ($driver = $this->getDriver()) {
204200
'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",
205201
'sqlite' => "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->tokenCol TEXT NOT NULL, $this->expirationCol INTEGER)",
206202
'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
209205
default => throw new \DomainException(sprintf('Creating the lock table is currently not implemented for platform "%s".', $driver)),
210206
};
211207

212-
$conn->exec($sql);
208+
$this->getConnection()->exec($sql);
213209
}
214210

215211
/**
@@ -224,14 +220,7 @@ private function prune(): void
224220

225221
private function getDriver(): string
226222
{
227-
if (isset($this->driver)) {
228-
return $this->driver;
229-
}
230-
231-
$conn = $this->getConnection();
232-
$this->driver = $conn->getAttribute(\PDO::ATTR_DRIVER_NAME);
233-
234-
return $this->driver;
223+
return $this->driver ??= $this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME);
235224
}
236225

237226
/**
@@ -254,15 +243,13 @@ private function isTableMissing(\PDOException $exception): bool
254243
$driver = $this->getDriver();
255244
$code = $exception->getCode();
256245

257-
switch (true) {
258-
case 'pgsql' === $driver && '42P01' === $code:
259-
case 'sqlite' === $driver && str_contains($exception->getMessage(), 'no such table:'):
260-
case 'oci' === $driver && 942 === $code:
261-
case 'sqlsrv' === $driver && 208 === $code:
262-
case 'mysql' === $driver && 1146 === $code:
263-
return true;
264-
default:
265-
return false;
266-
}
246+
return match (true) {
247+
'pgsql' === $driver && '42P01' === $code,
248+
'sqlite' === $driver && str_contains($exception->getMessage(), 'no such table:'),
249+
'oci' === $driver && 942 === $code,
250+
'sqlsrv' === $driver && 208 === $code,
251+
'mysql' === $driver && 1146 === $code => true,
252+
default => false,
253+
};
267254
}
268255
}

0 commit comments

Comments
 (0)
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