@@ -102,10 +102,7 @@ public function __construct(#[\SensitiveParameter] \PDO|string $connOrDsn, strin
102
102
*/
103
103
public function createTable ()
104
104
{
105
- // connect if we are not yet
106
- $ conn = $ this ->getConnection ();
107
-
108
- $ sql = match ($ this ->driver ) {
105
+ $ sql = match ($ driver = $ this ->getDriver ()) {
109
106
// We use varbinary for the ID column because it prevents unwanted conversions:
110
107
// - character set conversions between server and client
111
108
// - trailing space removal
@@ -116,10 +113,10 @@ public function createTable()
116
113
'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) " ,
117
114
'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) " ,
118
115
'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 )),
120
117
};
121
118
122
- $ conn ->exec ($ sql );
119
+ $ this -> getConnection () ->exec ($ sql );
123
120
}
124
121
125
122
public function prune (): bool
@@ -211,7 +208,7 @@ protected function doClear(string $namespace): bool
211
208
$ conn = $ this ->getConnection ();
212
209
213
210
if ('' === $ namespace ) {
214
- if ('sqlite ' === $ this ->driver ) {
211
+ if ('sqlite ' === $ this ->getDriver () ) {
215
212
$ sql = "DELETE FROM $ this ->table " ;
216
213
} else {
217
214
$ sql = "TRUNCATE TABLE $ this ->table " ;
@@ -249,7 +246,7 @@ protected function doSave(array $values, int $lifetime): array|bool
249
246
250
247
$ conn = $ this ->getConnection ();
251
248
252
- $ driver = $ this ->driver ;
249
+ $ driver = $ this ->getDriver () ;
253
250
$ insertSql = "INSERT INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->lifetimeCol , $ this ->timeCol ) VALUES (:id, :data, :lifetime, :time) " ;
254
251
255
252
switch (true ) {
@@ -286,7 +283,7 @@ protected function doSave(array $values, int $lifetime): array|bool
286
283
try {
287
284
$ stmt = $ conn ->prepare ($ sql );
288
285
} 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 ))) {
290
287
$ this ->createTable ();
291
288
}
292
289
$ stmt = $ conn ->prepare ($ sql );
@@ -321,7 +318,7 @@ protected function doSave(array $values, int $lifetime): array|bool
321
318
try {
322
319
$ stmt ->execute ();
323
320
} 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 ))) {
325
322
$ this ->createTable ();
326
323
}
327
324
$ stmt ->execute ();
@@ -343,7 +340,7 @@ protected function doSave(array $values, int $lifetime): array|bool
343
340
*/
344
341
protected function getId (mixed $ key ): string
345
342
{
346
- if ('pgsql ' !== $ this ->driver ??= ( $ this -> getConnection () ? $ this -> driver : null )) {
343
+ if ('pgsql ' !== $ this ->getDriver ( )) {
347
344
return parent ::getId ($ key );
348
345
}
349
346
@@ -360,30 +357,32 @@ private function getConnection(): \PDO
360
357
$ this ->conn = new \PDO ($ this ->dsn , $ this ->username , $ this ->password , $ this ->connectionOptions );
361
358
$ this ->conn ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION );
362
359
}
363
- $ this ->driver ??= $ this ->conn ->getAttribute (\PDO ::ATTR_DRIVER_NAME );
364
360
365
361
return $ this ->conn ;
366
362
}
367
363
364
+ private function getDriver (): string
365
+ {
366
+ return $ this ->driver ??= $ this ->getConnection ()->getAttribute (\PDO ::ATTR_DRIVER_NAME );
367
+ }
368
+
368
369
private function getServerVersion (): string
369
370
{
370
- return $ this ->serverVersion ??= $ this ->conn ->getAttribute (\PDO ::ATTR_SERVER_VERSION );
371
+ return $ this ->serverVersion ??= $ this ->getConnection () ->getAttribute (\PDO ::ATTR_SERVER_VERSION );
371
372
}
372
373
373
374
private function isTableMissing (\PDOException $ exception ): bool
374
375
{
375
- $ driver = $ this ->driver ;
376
+ $ driver = $ this ->getDriver () ;
376
377
$ code = $ exception ->getCode ();
377
378
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
+ };
388
387
}
389
388
}
0 commit comments