11
11
12
12
namespace Symfony \Component \Cache \Adapter ;
13
13
14
+ use Doctrine \DBAL \ArrayParameterType ;
15
+ use Doctrine \DBAL \Configuration ;
14
16
use Doctrine \DBAL \Connection ;
15
17
use Doctrine \DBAL \Driver \ServerInfoAwareConnection ;
16
18
use Doctrine \DBAL \DriverManager ;
17
19
use Doctrine \DBAL \Exception as DBALException ;
18
20
use Doctrine \DBAL \Exception \TableNotFoundException ;
19
21
use Doctrine \DBAL \ParameterType ;
22
+ use Doctrine \DBAL \Schema \DefaultSchemaManagerFactory ;
20
23
use Doctrine \DBAL \Schema \Schema ;
24
+ use Doctrine \DBAL \Tools \DsnParser ;
21
25
use Symfony \Component \Cache \Exception \InvalidArgumentException ;
22
26
use Symfony \Component \Cache \Marshaller \DefaultMarshaller ;
23
27
use Symfony \Component \Cache \Marshaller \MarshallerInterface ;
@@ -68,7 +72,28 @@ public function __construct($connOrDsn, string $namespace = '', int $defaultLife
68
72
if (!class_exists (DriverManager::class)) {
69
73
throw new InvalidArgumentException (sprintf ('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal". ' , $ connOrDsn ));
70
74
}
71
- $ this ->conn = DriverManager::getConnection (['url ' => $ connOrDsn ]);
75
+ if (class_exists (DsnParser::class)) {
76
+ $ params = (new DsnParser ([
77
+ 'db2 ' => 'ibm_db2 ' ,
78
+ 'mssql ' => 'pdo_sqlsrv ' ,
79
+ 'mysql ' => 'pdo_mysql ' ,
80
+ 'mysql2 ' => 'pdo_mysql ' ,
81
+ 'postgres ' => 'pdo_pgsql ' ,
82
+ 'postgresql ' => 'pdo_pgsql ' ,
83
+ 'pgsql ' => 'pdo_pgsql ' ,
84
+ 'sqlite ' => 'pdo_sqlite ' ,
85
+ 'sqlite3 ' => 'pdo_sqlite ' ,
86
+ ]))->parse ($ connOrDsn );
87
+ } else {
88
+ $ params = ['url ' => $ connOrDsn ];
89
+ }
90
+
91
+ $ config = new Configuration ();
92
+ if (class_exists (DefaultSchemaManagerFactory::class)) {
93
+ $ config ->setSchemaManagerFactory (new DefaultSchemaManagerFactory ());
94
+ }
95
+
96
+ $ this ->conn = DriverManager::getConnection ($ params , $ config );
72
97
} else {
73
98
throw new \TypeError (sprintf ('Argument 1 passed to "%s()" must be "%s" or string, "%s" given. ' , __METHOD__ , Connection::class, get_debug_type ($ connOrDsn )));
74
99
}
@@ -156,7 +181,7 @@ protected function doFetch(array $ids): iterable
156
181
$ ids ,
157
182
], [
158
183
ParameterType::INTEGER ,
159
- Connection::PARAM_STR_ARRAY ,
184
+ class_exists (ArrayParameterType::class) ? ArrayParameterType:: STRING : Connection::PARAM_STR_ARRAY ,
160
185
])->iterateNumeric ();
161
186
162
187
foreach ($ result as $ row ) {
@@ -174,7 +199,7 @@ protected function doFetch(array $ids): iterable
174
199
$ expired ,
175
200
], [
176
201
ParameterType::INTEGER ,
177
- Connection::PARAM_STR_ARRAY ,
202
+ class_exists (ArrayParameterType::class) ? ArrayParameterType:: STRING : Connection::PARAM_STR_ARRAY ,
178
203
]);
179
204
}
180
205
}
@@ -226,7 +251,7 @@ protected function doDelete(array $ids): bool
226
251
{
227
252
$ sql = "DELETE FROM $ this ->table WHERE $ this ->idCol IN (?) " ;
228
253
try {
229
- $ this ->conn ->executeStatement ($ sql , [array_values ($ ids )], [Connection::PARAM_STR_ARRAY ]);
254
+ $ this ->conn ->executeStatement ($ sql , [array_values ($ ids )], [class_exists (ArrayParameterType::class) ? ArrayParameterType:: STRING : Connection::PARAM_STR_ARRAY ]);
230
255
} catch (TableNotFoundException $ e ) {
231
256
}
232
257
@@ -285,35 +310,42 @@ protected function doSave(array $values, int $lifetime)
285
310
$ stmt = $ this ->conn ->prepare ($ sql );
286
311
}
287
312
288
- // $id and $data are defined later in the loop. Binding is done by reference, values are read on execution.
289
313
if ('sqlsrv ' === $ platformName || 'oci ' === $ platformName ) {
290
- $ stmt ->bindParam (1 , $ id );
291
- $ stmt ->bindParam (2 , $ id );
292
- $ stmt ->bindParam (3 , $ data , ParameterType::LARGE_OBJECT );
314
+ $ bind = static function ($ id , $ data ) use ($ stmt ) {
315
+ $ stmt ->bindValue (1 , $ id );
316
+ $ stmt ->bindValue (2 , $ id );
317
+ $ stmt ->bindValue (3 , $ data , ParameterType::LARGE_OBJECT );
318
+ $ stmt ->bindValue (6 , $ data , ParameterType::LARGE_OBJECT );
319
+ };
293
320
$ stmt ->bindValue (4 , $ lifetime , ParameterType::INTEGER );
294
321
$ stmt ->bindValue (5 , $ now , ParameterType::INTEGER );
295
- $ stmt ->bindParam (6 , $ data , ParameterType::LARGE_OBJECT );
296
322
$ stmt ->bindValue (7 , $ lifetime , ParameterType::INTEGER );
297
323
$ stmt ->bindValue (8 , $ now , ParameterType::INTEGER );
298
324
} elseif (null !== $ platformName ) {
299
- $ stmt ->bindParam (1 , $ id );
300
- $ stmt ->bindParam (2 , $ data , ParameterType::LARGE_OBJECT );
325
+ $ bind = static function ($ id , $ data ) use ($ stmt ) {
326
+ $ stmt ->bindValue (1 , $ id );
327
+ $ stmt ->bindValue (2 , $ data , ParameterType::LARGE_OBJECT );
328
+ };
301
329
$ stmt ->bindValue (3 , $ lifetime , ParameterType::INTEGER );
302
330
$ stmt ->bindValue (4 , $ now , ParameterType::INTEGER );
303
331
} else {
304
- $ stmt ->bindParam (1 , $ data , ParameterType::LARGE_OBJECT );
305
332
$ stmt ->bindValue (2 , $ lifetime , ParameterType::INTEGER );
306
333
$ stmt ->bindValue (3 , $ now , ParameterType::INTEGER );
307
- $ stmt ->bindParam (4 , $ id );
308
334
309
335
$ insertStmt = $ this ->conn ->prepare ($ insertSql );
310
- $ insertStmt ->bindParam (1 , $ id );
311
- $ insertStmt ->bindParam (2 , $ data , ParameterType::LARGE_OBJECT );
312
336
$ insertStmt ->bindValue (3 , $ lifetime , ParameterType::INTEGER );
313
337
$ insertStmt ->bindValue (4 , $ now , ParameterType::INTEGER );
338
+
339
+ $ bind = static function ($ id , $ data ) use ($ stmt , $ insertStmt ) {
340
+ $ stmt ->bindValue (1 , $ data , ParameterType::LARGE_OBJECT );
341
+ $ stmt ->bindValue (4 , $ id );
342
+ $ insertStmt ->bindValue (1 , $ id );
343
+ $ insertStmt ->bindValue (2 , $ data , ParameterType::LARGE_OBJECT );
344
+ };
314
345
}
315
346
316
347
foreach ($ values as $ id => $ data ) {
348
+ $ bind ($ id , $ data );
317
349
try {
318
350
$ rowCount = $ stmt ->executeStatement ();
319
351
} catch (TableNotFoundException $ e ) {
0 commit comments