Skip to content

Commit

Permalink
no issue - cleanup pdo dsn options in bridge factory prior connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Apr 26, 2024
1 parent e0022bb commit 6a66199
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
40 changes: 35 additions & 5 deletions src/BridgeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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';
Expand All @@ -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.
*/
Expand Down

0 comments on commit 6a66199

Please sign in to comment.