Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb32b92

Browse files
bug #50507 [Cache] Fix DBAL deprecations (MatTheCat)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Cache] Fix DBAL deprecations | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Part of #50481 | License | MIT | Doc PR | N/A Unfortunately calling `Table::getColumns` will still produce deprecations 🤔 Commits ------- bec5302 [Cache] Fix DBAL deprecations
2 parents 221c9bb + bec5302 commit fb32b92

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
namespace Symfony\Component\Cache\Adapter;
1313

14+
use Doctrine\DBAL\ArrayParameterType;
15+
use Doctrine\DBAL\Configuration;
1416
use Doctrine\DBAL\Connection;
1517
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1618
use Doctrine\DBAL\DriverManager;
1719
use Doctrine\DBAL\Exception as DBALException;
1820
use Doctrine\DBAL\Exception\TableNotFoundException;
1921
use Doctrine\DBAL\ParameterType;
22+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
2023
use Doctrine\DBAL\Schema\Schema;
24+
use Doctrine\DBAL\Tools\DsnParser;
2125
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2226
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
2327
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
@@ -68,7 +72,28 @@ public function __construct($connOrDsn, string $namespace = '', int $defaultLife
6872
if (!class_exists(DriverManager::class)) {
6973
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrDsn));
7074
}
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);
7297
} else {
7398
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be "%s" or string, "%s" given.', __METHOD__, Connection::class, get_debug_type($connOrDsn)));
7499
}
@@ -156,7 +181,7 @@ protected function doFetch(array $ids): iterable
156181
$ids,
157182
], [
158183
ParameterType::INTEGER,
159-
Connection::PARAM_STR_ARRAY,
184+
class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY,
160185
])->iterateNumeric();
161186

162187
foreach ($result as $row) {
@@ -174,7 +199,7 @@ protected function doFetch(array $ids): iterable
174199
$expired,
175200
], [
176201
ParameterType::INTEGER,
177-
Connection::PARAM_STR_ARRAY,
202+
class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY,
178203
]);
179204
}
180205
}
@@ -226,7 +251,7 @@ protected function doDelete(array $ids): bool
226251
{
227252
$sql = "DELETE FROM $this->table WHERE $this->idCol IN (?)";
228253
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]);
230255
} catch (TableNotFoundException $e) {
231256
}
232257

@@ -285,35 +310,42 @@ protected function doSave(array $values, int $lifetime)
285310
$stmt = $this->conn->prepare($sql);
286311
}
287312

288-
// $id and $data are defined later in the loop. Binding is done by reference, values are read on execution.
289313
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+
};
293320
$stmt->bindValue(4, $lifetime, ParameterType::INTEGER);
294321
$stmt->bindValue(5, $now, ParameterType::INTEGER);
295-
$stmt->bindParam(6, $data, ParameterType::LARGE_OBJECT);
296322
$stmt->bindValue(7, $lifetime, ParameterType::INTEGER);
297323
$stmt->bindValue(8, $now, ParameterType::INTEGER);
298324
} 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+
};
301329
$stmt->bindValue(3, $lifetime, ParameterType::INTEGER);
302330
$stmt->bindValue(4, $now, ParameterType::INTEGER);
303331
} else {
304-
$stmt->bindParam(1, $data, ParameterType::LARGE_OBJECT);
305332
$stmt->bindValue(2, $lifetime, ParameterType::INTEGER);
306333
$stmt->bindValue(3, $now, ParameterType::INTEGER);
307-
$stmt->bindParam(4, $id);
308334

309335
$insertStmt = $this->conn->prepare($insertSql);
310-
$insertStmt->bindParam(1, $id);
311-
$insertStmt->bindParam(2, $data, ParameterType::LARGE_OBJECT);
312336
$insertStmt->bindValue(3, $lifetime, ParameterType::INTEGER);
313337
$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+
};
314345
}
315346

316347
foreach ($values as $id => $data) {
348+
$bind($id, $data);
317349
try {
318350
$rowCount = $stmt->executeStatement();
319351
} catch (TableNotFoundException $e) {

src/Symfony/Component/Cache/Tests/Adapter/DoctrineDbalAdapterTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
1717
use Doctrine\DBAL\Driver\Middleware;
1818
use Doctrine\DBAL\DriverManager;
19+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
1920
use Doctrine\DBAL\Schema\Schema;
2021
use PHPUnit\Framework\SkippedTestSuiteError;
2122
use Psr\Cache\CacheItemPoolInterface;
@@ -45,12 +46,12 @@ public static function tearDownAfterClass(): void
4546

4647
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
4748
{
48-
return new DoctrineDbalAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]), '', $defaultLifetime);
49+
return new DoctrineDbalAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $this->getDbalConfig()), '', $defaultLifetime);
4950
}
5051

5152
public function testConfigureSchemaDecoratedDbalDriver()
5253
{
53-
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]);
54+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $this->getDbalConfig());
5455
if (!interface_exists(Middleware::class)) {
5556
$this->markTestSkipped('doctrine/dbal v2 does not support custom drivers using middleware');
5657
}
@@ -60,7 +61,7 @@ public function testConfigureSchemaDecoratedDbalDriver()
6061
->method('wrap')
6162
->willReturn(new DriverWrapper($connection->getDriver()));
6263

63-
$config = new Configuration();
64+
$config = $this->getDbalConfig();
6465
$config->setMiddlewares([$middleware]);
6566

6667
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $config);
@@ -75,7 +76,7 @@ public function testConfigureSchemaDecoratedDbalDriver()
7576

7677
public function testConfigureSchema()
7778
{
78-
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]);
79+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $this->getDbalConfig());
7980
$schema = new Schema();
8081

8182
$adapter = new DoctrineDbalAdapter($connection);
@@ -95,7 +96,7 @@ public function testConfigureSchemaDifferentDbalConnection()
9596

9697
public function testConfigureSchemaTableExists()
9798
{
98-
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]);
99+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $this->getDbalConfig());
99100
$schema = new Schema();
100101
$schema->createTable('cache_items');
101102

@@ -155,4 +156,12 @@ private function createConnectionMock()
155156

156157
return $connection;
157158
}
159+
160+
private function getDbalConfig()
161+
{
162+
$config = new Configuration();
163+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
164+
165+
return $config;
166+
}
158167
}

0 commit comments

Comments
 (0)