From 6a661994c782e81a548873c284e11ea6dadbb729 Mon Sep 17 00:00:00 2001 From: Pierre Rineau Date: Fri, 26 Apr 2024 09:16:34 +0200 Subject: [PATCH] no issue - cleanup pdo dsn options in bridge factory prior connecting --- CHANGELOG.md | 4 ++++ src/BridgeFactory.php | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bcff74..37bbf8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Next + +* [fix] Cleanup PDO DSN options prior connecting in bridge factory. + ## 1.6.1 * [fix] Fix MySQL 5.7 `decimal` and `float` type cast. diff --git a/src/BridgeFactory.php b/src/BridgeFactory.php index edc0c4a..686db36 100644 --- a/src/BridgeFactory.php +++ b/src/BridgeFactory.php @@ -182,7 +182,9 @@ private static function pdoConnectionMySQL(#[\SensitiveParameter] Dsn $dsn): \PD if ($value = $dsn->getDatabase()) { $options['dbname'] = $value; } - $options += \array_diff_key($dsn->getOptions(), ['persistent' => 1]); + + // https://www.php.net/manual/en/ref.pdo-mysql.connection.php + $options = self::pdoConnectionOptionClean($options + $dsn->getOptions(), ['host', 'port', 'dbname', 'unix_socket', 'charset']); $flags = []; $persistent = $dsn->getOption('peristent'); @@ -209,7 +211,9 @@ private static function pdoConnectionPostgreSQL(#[\SensitiveParameter] Dsn $dsn) if ($value = $dsn->getDatabase()) { $options['dbname'] = $value; } - $options += \array_diff_key($dsn->getOptions(), ['persistent' => 1]); + + // https://www.php.net/manual/en/ref.pdo-pgsql.connection.php + $options = self::pdoConnectionOptionClean($options + $dsn->getOptions(), ['host', 'port', 'dbname', 'user', 'password', 'sslmode']); $flags = []; $persistent = $dsn->getOption('peristent'); @@ -228,15 +232,32 @@ private static function pdoConnectionSQLServer(#[\SensitiveParameter] Dsn $dsn): { $options = []; if ($value = $dsn->getHost()) { - $options['server'] = $value; + $options['Server'] = $value; if ($value = $dsn->getPort()) { - $options['server'] .= ',' . $value; + $options['Server'] .= ',' . $value; } } if ($value = $dsn->getDatabase()) { $options['Database'] = $value; } - $options += \array_diff_key($dsn->getOptions(), ['persistent' => 1]); + + // https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php + $options = self::pdoConnectionOptionClean($options + $dsn->getOptions(), [ + 'APP', + 'ConnectionPooling', + 'Database', + 'Encrypt', + 'Failover_Partner', + 'LoginTimeout', + 'MultipleActiveResultSets', + 'QuotedId', + 'Server', + 'TraceFile', + 'TraceOn', + 'TransactionIsolation', + 'TrustServerCertificate', + 'WSID', + ]); if (isset($options['MultipleActiveResultSets'])) { $options['MultipleActiveResultSets'] = $options['MultipleActiveResultSets'] ? 'true' : 'false'; @@ -262,9 +283,18 @@ private static function pdoConnectionSQLServer(#[\SensitiveParameter] Dsn $dsn): private static function pdoConnectionSQLite(#[\SensitiveParameter] Dsn $dsn): \PDO { // Dsn::getFilename() may return ":memory:" which is supported by PDO. + // https://www.php.net/manual/fr/ref.pdo-sqlite.connection.php return new \PDO('sqlite:' . $dsn->getFilename()); } + /** + * Strip some well known options that you may often encounter ensuitable for PDO. + */ + private static function pdoConnectionOptionClean(array $options, array $allowed): array + { + return \array_intersect_key($options, \array_flip($allowed)); + } + /** * Compute own PDO connection string. */