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 ;
@@ -66,7 +70,28 @@ public function __construct(Connection|string $connOrDsn, string $namespace = ''
66
70
if (!class_exists (DriverManager::class)) {
67
71
throw new InvalidArgumentException (sprintf ('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal". ' , $ connOrDsn ));
68
72
}
69
- $ this ->conn = DriverManager::getConnection (['url ' => $ connOrDsn ]);
73
+ if (class_exists (DsnParser::class)) {
74
+ $ params = (new DsnParser ([
75
+ 'db2 ' => 'ibm_db2 ' ,
76
+ 'mssql ' => 'pdo_sqlsrv ' ,
77
+ 'mysql ' => 'pdo_mysql ' ,
78
+ 'mysql2 ' => 'pdo_mysql ' ,
79
+ 'postgres ' => 'pdo_pgsql ' ,
80
+ 'postgresql ' => 'pdo_pgsql ' ,
81
+ 'pgsql ' => 'pdo_pgsql ' ,
82
+ 'sqlite ' => 'pdo_sqlite ' ,
83
+ 'sqlite3 ' => 'pdo_sqlite ' ,
84
+ ]))->parse ($ connOrDsn );
85
+ } else {
86
+ $ params = ['url ' => $ connOrDsn ];
87
+ }
88
+
89
+ $ config = new Configuration ();
90
+ if (class_exists (DefaultSchemaManagerFactory::class)) {
91
+ $ config ->setSchemaManagerFactory (new DefaultSchemaManagerFactory ());
92
+ }
93
+
94
+ $ this ->conn = DriverManager::getConnection ($ params , $ config );
70
95
}
71
96
72
97
$ this ->table = $ options ['db_table ' ] ?? $ this ->table ;
@@ -143,7 +168,7 @@ protected function doFetch(array $ids): iterable
143
168
$ ids ,
144
169
], [
145
170
ParameterType::INTEGER ,
146
- Connection::PARAM_STR_ARRAY ,
171
+ class_exists (ArrayParameterType::class) ? ArrayParameterType:: STRING : Connection::PARAM_STR_ARRAY ,
147
172
])->iterateNumeric ();
148
173
149
174
foreach ($ result as $ row ) {
@@ -161,7 +186,7 @@ protected function doFetch(array $ids): iterable
161
186
$ expired ,
162
187
], [
163
188
ParameterType::INTEGER ,
164
- Connection::PARAM_STR_ARRAY ,
189
+ class_exists (ArrayParameterType::class) ? ArrayParameterType:: STRING : Connection::PARAM_STR_ARRAY ,
165
190
]);
166
191
}
167
192
}
@@ -204,7 +229,7 @@ protected function doDelete(array $ids): bool
204
229
{
205
230
$ sql = "DELETE FROM $ this ->table WHERE $ this ->idCol IN (?) " ;
206
231
try {
207
- $ this ->conn ->executeStatement ($ sql , [array_values ($ ids )], [Connection::PARAM_STR_ARRAY ]);
232
+ $ this ->conn ->executeStatement ($ sql , [array_values ($ ids )], [class_exists (ArrayParameterType::class) ? ArrayParameterType:: STRING : Connection::PARAM_STR_ARRAY ]);
208
233
} catch (TableNotFoundException ) {
209
234
}
210
235
@@ -260,35 +285,42 @@ protected function doSave(array $values, int $lifetime): array|bool
260
285
$ stmt = $ this ->conn ->prepare ($ sql );
261
286
}
262
287
263
- // $id and $data are defined later in the loop. Binding is done by reference, values are read on execution.
264
288
if ('sqlsrv ' === $ platformName || 'oci ' === $ platformName ) {
265
- $ stmt ->bindParam (1 , $ id );
266
- $ stmt ->bindParam (2 , $ id );
267
- $ stmt ->bindParam (3 , $ data , ParameterType::LARGE_OBJECT );
289
+ $ bind = static function ($ id , $ data ) use ($ stmt ) {
290
+ $ stmt ->bindValue (1 , $ id );
291
+ $ stmt ->bindValue (2 , $ id );
292
+ $ stmt ->bindValue (3 , $ data , ParameterType::LARGE_OBJECT );
293
+ $ stmt ->bindValue (6 , $ data , ParameterType::LARGE_OBJECT );
294
+ };
268
295
$ stmt ->bindValue (4 , $ lifetime , ParameterType::INTEGER );
269
296
$ stmt ->bindValue (5 , $ now , ParameterType::INTEGER );
270
- $ stmt ->bindParam (6 , $ data , ParameterType::LARGE_OBJECT );
271
297
$ stmt ->bindValue (7 , $ lifetime , ParameterType::INTEGER );
272
298
$ stmt ->bindValue (8 , $ now , ParameterType::INTEGER );
273
299
} elseif (null !== $ platformName ) {
274
- $ stmt ->bindParam (1 , $ id );
275
- $ stmt ->bindParam (2 , $ data , ParameterType::LARGE_OBJECT );
300
+ $ bind = static function ($ id , $ data ) use ($ stmt ) {
301
+ $ stmt ->bindValue (1 , $ id );
302
+ $ stmt ->bindValue (2 , $ data , ParameterType::LARGE_OBJECT );
303
+ };
276
304
$ stmt ->bindValue (3 , $ lifetime , ParameterType::INTEGER );
277
305
$ stmt ->bindValue (4 , $ now , ParameterType::INTEGER );
278
306
} else {
279
- $ stmt ->bindParam (1 , $ data , ParameterType::LARGE_OBJECT );
280
307
$ stmt ->bindValue (2 , $ lifetime , ParameterType::INTEGER );
281
308
$ stmt ->bindValue (3 , $ now , ParameterType::INTEGER );
282
- $ stmt ->bindParam (4 , $ id );
283
309
284
310
$ insertStmt = $ this ->conn ->prepare ($ insertSql );
285
- $ insertStmt ->bindParam (1 , $ id );
286
- $ insertStmt ->bindParam (2 , $ data , ParameterType::LARGE_OBJECT );
287
311
$ insertStmt ->bindValue (3 , $ lifetime , ParameterType::INTEGER );
288
312
$ insertStmt ->bindValue (4 , $ now , ParameterType::INTEGER );
313
+
314
+ $ bind = static function ($ id , $ data ) use ($ stmt , $ insertStmt ) {
315
+ $ stmt ->bindValue (1 , $ data , ParameterType::LARGE_OBJECT );
316
+ $ stmt ->bindValue (4 , $ id );
317
+ $ insertStmt ->bindValue (1 , $ id );
318
+ $ insertStmt ->bindValue (2 , $ data , ParameterType::LARGE_OBJECT );
319
+ };
289
320
}
290
321
291
322
foreach ($ values as $ id => $ data ) {
323
+ $ bind ($ id , $ data );
292
324
try {
293
325
$ rowCount = $ stmt ->executeStatement ();
294
326
} catch (TableNotFoundException ) {
0 commit comments