17
17
use Doctrine \DBAL \DriverManager ;
18
18
use Doctrine \DBAL \Exception ;
19
19
use Doctrine \DBAL \Exception \TableNotFoundException ;
20
+ use Doctrine \DBAL \ParameterType ;
20
21
use Doctrine \DBAL \Schema \Schema ;
21
22
use Symfony \Component \Cache \Exception \InvalidArgumentException ;
22
23
use Symfony \Component \Cache \Marshaller \DefaultMarshaller ;
@@ -166,17 +167,20 @@ public function prune()
166
167
$ deleteSql .= " AND $ this ->idCol LIKE :namespace " ;
167
168
}
168
169
170
+ $ connection = $ this ->getConnection ();
171
+ $ useDbalConstants = $ connection instanceof Connection && class_exists (ParameterType::class);
172
+
169
173
try {
170
- $ delete = $ this -> getConnection () ->prepare ($ deleteSql );
174
+ $ delete = $ connection ->prepare ($ deleteSql );
171
175
} catch (TableNotFoundException $ e ) {
172
176
return true ;
173
177
} catch (\PDOException $ e ) {
174
178
return true ;
175
179
}
176
- $ delete ->bindValue (':time ' , time (), \PDO ::PARAM_INT );
180
+ $ delete ->bindValue (':time ' , time (), $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
177
181
178
182
if ('' !== $ this ->namespace ) {
179
- $ delete ->bindValue (':namespace ' , sprintf ('%s%% ' , $ this ->namespace ), \PDO ::PARAM_STR );
183
+ $ delete ->bindValue (':namespace ' , sprintf ('%s%% ' , $ this ->namespace ), $ useDbalConstants ? ParameterType:: STRING : \PDO ::PARAM_STR );
180
184
}
181
185
try {
182
186
return $ delete ->execute ();
@@ -192,13 +196,16 @@ public function prune()
192
196
*/
193
197
protected function doFetch (array $ ids )
194
198
{
199
+ $ connection = $ this ->getConnection ();
200
+ $ useDbalConstants = $ connection instanceof Connection && class_exists (ParameterType::class);
201
+
195
202
$ now = time ();
196
203
$ expired = [];
197
204
198
205
$ sql = str_pad ('' , (\count ($ ids ) << 1 ) - 1 , '?, ' );
199
206
$ sql = "SELECT $ this ->idCol , CASE WHEN $ this ->lifetimeCol IS NULL OR $ this ->lifetimeCol + $ this ->timeCol > ? THEN $ this ->dataCol ELSE NULL END FROM $ this ->table WHERE $ this ->idCol IN ( $ sql) " ;
200
- $ stmt = $ this -> getConnection () ->prepare ($ sql );
201
- $ stmt ->bindValue ($ i = 1 , $ now , \PDO ::PARAM_INT );
207
+ $ stmt = $ connection ->prepare ($ sql );
208
+ $ stmt ->bindValue ($ i = 1 , $ now , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
202
209
foreach ($ ids as $ id ) {
203
210
$ stmt ->bindValue (++$ i , $ id );
204
211
}
@@ -222,8 +229,8 @@ protected function doFetch(array $ids)
222
229
if ($ expired ) {
223
230
$ sql = str_pad ('' , (\count ($ expired ) << 1 ) - 1 , '?, ' );
224
231
$ sql = "DELETE FROM $ this ->table WHERE $ this ->lifetimeCol + $ this ->timeCol <= ? AND $ this ->idCol IN ( $ sql) " ;
225
- $ stmt = $ this -> getConnection () ->prepare ($ sql );
226
- $ stmt ->bindValue ($ i = 1 , $ now , \PDO ::PARAM_INT );
232
+ $ stmt = $ connection ->prepare ($ sql );
233
+ $ stmt ->bindValue ($ i = 1 , $ now , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
227
234
foreach ($ expired as $ id ) {
228
235
$ stmt ->bindValue (++$ i , $ id );
229
236
}
@@ -236,11 +243,14 @@ protected function doFetch(array $ids)
236
243
*/
237
244
protected function doHave ($ id )
238
245
{
246
+ $ connection = $ this ->getConnection ();
247
+ $ useDbalConstants = $ connection instanceof Connection && class_exists (ParameterType::class);
248
+
239
249
$ sql = "SELECT 1 FROM $ this ->table WHERE $ this ->idCol = :id AND ( $ this ->lifetimeCol IS NULL OR $ this ->lifetimeCol + $ this ->timeCol > :time) " ;
240
- $ stmt = $ this -> getConnection () ->prepare ($ sql );
250
+ $ stmt = $ connection ->prepare ($ sql );
241
251
242
252
$ stmt ->bindValue (':id ' , $ id );
243
- $ stmt ->bindValue (':time ' , time (), \PDO ::PARAM_INT );
253
+ $ stmt ->bindValue (':time ' , time (), $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
244
254
$ result = $ stmt ->execute ();
245
255
246
256
return (bool ) (\is_object ($ result ) ? $ result ->fetchOne () : $ stmt ->fetchColumn ());
@@ -303,6 +313,8 @@ protected function doSave(array $values, int $lifetime)
303
313
}
304
314
305
315
$ conn = $ this ->getConnection ();
316
+ $ useDbalConstants = $ conn instanceof Connection && class_exists (ParameterType::class);
317
+
306
318
$ driver = $ this ->driver ;
307
319
$ insertSql = "INSERT INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->lifetimeCol , $ this ->timeCol ) VALUES (:id, :data, :lifetime, :time) " ;
308
320
@@ -354,25 +366,25 @@ protected function doSave(array $values, int $lifetime)
354
366
if ('sqlsrv ' === $ driver || 'oci ' === $ driver ) {
355
367
$ stmt ->bindParam (1 , $ id );
356
368
$ stmt ->bindParam (2 , $ id );
357
- $ stmt ->bindParam (3 , $ data , \PDO ::PARAM_LOB );
358
- $ stmt ->bindValue (4 , $ lifetime , \PDO ::PARAM_INT );
359
- $ stmt ->bindValue (5 , $ now , \PDO ::PARAM_INT );
360
- $ stmt ->bindParam (6 , $ data , \PDO ::PARAM_LOB );
361
- $ stmt ->bindValue (7 , $ lifetime , \PDO ::PARAM_INT );
362
- $ stmt ->bindValue (8 , $ now , \PDO ::PARAM_INT );
369
+ $ stmt ->bindParam (3 , $ data , $ useDbalConstants ? ParameterType:: LARGE_OBJECT : \PDO ::PARAM_LOB );
370
+ $ stmt ->bindValue (4 , $ lifetime , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
371
+ $ stmt ->bindValue (5 , $ now , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
372
+ $ stmt ->bindParam (6 , $ data , $ useDbalConstants ? ParameterType:: LARGE_OBJECT : \PDO ::PARAM_LOB );
373
+ $ stmt ->bindValue (7 , $ lifetime , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
374
+ $ stmt ->bindValue (8 , $ now , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
363
375
} else {
364
376
$ stmt ->bindParam (':id ' , $ id );
365
- $ stmt ->bindParam (':data ' , $ data , \PDO ::PARAM_LOB );
366
- $ stmt ->bindValue (':lifetime ' , $ lifetime , \PDO ::PARAM_INT );
367
- $ stmt ->bindValue (':time ' , $ now , \PDO ::PARAM_INT );
377
+ $ stmt ->bindParam (':data ' , $ data , $ useDbalConstants ? ParameterType:: LARGE_OBJECT : \PDO ::PARAM_LOB );
378
+ $ stmt ->bindValue (':lifetime ' , $ lifetime , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
379
+ $ stmt ->bindValue (':time ' , $ now , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
368
380
}
369
381
if (null === $ driver ) {
370
382
$ insertStmt = $ conn ->prepare ($ insertSql );
371
383
372
384
$ insertStmt ->bindParam (':id ' , $ id );
373
- $ insertStmt ->bindParam (':data ' , $ data , \PDO ::PARAM_LOB );
374
- $ insertStmt ->bindValue (':lifetime ' , $ lifetime , \PDO ::PARAM_INT );
375
- $ insertStmt ->bindValue (':time ' , $ now , \PDO ::PARAM_INT );
385
+ $ insertStmt ->bindParam (':data ' , $ data , $ useDbalConstants ? ParameterType:: LARGE_OBJECT : \PDO ::PARAM_LOB );
386
+ $ insertStmt ->bindValue (':lifetime ' , $ lifetime , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
387
+ $ insertStmt ->bindValue (':time ' , $ now , $ useDbalConstants ? ParameterType:: INTEGER : \PDO ::PARAM_INT );
376
388
}
377
389
378
390
foreach ($ values as $ id => $ data ) {
0 commit comments