Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 45e17d5

Browse filesBrowse files
committed
[Cache][Lock] PdoAdapter/PdoStore minor cleanup
1 parent 36c718a commit 45e17d5
Copy full SHA for 45e17d5

File tree

2 files changed

+37
-51
lines changed
Filter options

2 files changed

+37
-51
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/PdoAdapter.php
+23-24Lines changed: 23 additions & 24 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) {
@@ -286,7 +283,7 @@ protected function doSave(array $values, int $lifetime): array|bool
286283
try {
287284
$stmt = $conn->prepare($sql);
288285
} catch (\PDOException $e) {
289-
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
286+
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
290287
$this->createTable();
291288
}
292289
$stmt = $conn->prepare($sql);
@@ -321,7 +318,7 @@ protected function doSave(array $values, int $lifetime): array|bool
321318
try {
322319
$stmt->execute();
323320
} catch (\PDOException $e) {
324-
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
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 ($driver) {
380+
'pgsql' => '42P01' === $code,
381+
'sqlite' => str_contains($exception->getMessage(), 'no such table:'),
382+
'oci' => 942 === $code,
383+
'sqlsrv' => 208 === $code,
384+
'mysql' => 1146 === $code,
385+
default => false,
386+
};
388387
}
389388
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/PdoStore.php
+14-27Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function save(Key $key)
9595
try {
9696
$stmt = $conn->prepare($sql);
9797
} catch (\PDOException $e) {
98-
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
98+
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->getDriver(), ['pgsql', 'sqlite', 'sqlsrv'], true))) {
9999
$this->createTable();
100100
}
101101
$stmt = $conn->prepare($sql);
@@ -107,12 +107,12 @@ public function save(Key $key)
107107
try {
108108
$stmt->execute();
109109
} catch (\PDOException $e) {
110-
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
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 ($driver) {
247+
'pgsql' => '42P01' === $code,
248+
'sqlite' => str_contains($exception->getMessage(), 'no such table:'),
249+
'oci' => 942 === $code,
250+
'sqlsrv' => 208 === $code,
251+
'mysql' => 1146 === $code,
252+
default => false,
253+
};
267254
}
268255
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.