vendor/doctrine/dbal/src/Driver/PDO/PgSQL/Driver.php line 29

  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDO\PgSQL;
  3. use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
  4. use Doctrine\DBAL\Driver\PDO\Connection;
  5. use Doctrine\DBAL\Driver\PDO\Exception;
  6. use Doctrine\Deprecations\Deprecation;
  7. use PDO;
  8. use PDOException;
  9. final class Driver extends AbstractPostgreSQLDriver
  10. {
  11.     /**
  12.      * {@inheritdoc}
  13.      *
  14.      * @return Connection
  15.      */
  16.     public function connect(array $params)
  17.     {
  18.         $driverOptions $params['driverOptions'] ?? [];
  19.         if (! empty($params['persistent'])) {
  20.             $driverOptions[PDO::ATTR_PERSISTENT] = true;
  21.         }
  22.         try {
  23.             $pdo = new PDO(
  24.                 $this->constructPdoDsn($params),
  25.                 $params['user'] ?? '',
  26.                 $params['password'] ?? '',
  27.                 $driverOptions,
  28.             );
  29.         } catch (PDOException $exception) {
  30.             throw Exception::new($exception);
  31.         }
  32.         if (
  33.             ! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
  34.             || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
  35.         ) {
  36.             $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPAREStrue);
  37.         }
  38.         $connection = new Connection($pdo);
  39.         /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
  40.          * - passing client_encoding via the 'options' param breaks pgbouncer support
  41.          */
  42.         if (isset($params['charset'])) {
  43.             $connection->exec('SET NAMES \'' $params['charset'] . '\'');
  44.         }
  45.         return $connection;
  46.     }
  47.     /**
  48.      * Constructs the Postgres PDO DSN.
  49.      *
  50.      * @param mixed[] $params
  51.      */
  52.     private function constructPdoDsn(array $params): string
  53.     {
  54.         $dsn 'pgsql:';
  55.         if (isset($params['host']) && $params['host'] !== '') {
  56.             $dsn .= 'host=' $params['host'] . ';';
  57.         }
  58.         if (isset($params['port']) && $params['port'] !== '') {
  59.             $dsn .= 'port=' $params['port'] . ';';
  60.         }
  61.         if (isset($params['dbname'])) {
  62.             $dsn .= 'dbname=' $params['dbname'] . ';';
  63.         } elseif (isset($params['default_dbname'])) {
  64.             Deprecation::trigger(
  65.                 'doctrine/dbal',
  66.                 'https://github.com/doctrine/dbal/pull/5705',
  67.                 'The "default_dbname" connection parameter is deprecated. Use "dbname" instead.',
  68.             );
  69.             $dsn .= 'dbname=' $params['default_dbname'] . ';';
  70.         } else {
  71.             if (isset($params['user']) && $params['user'] !== 'postgres') {
  72.                 Deprecation::trigger(
  73.                     'doctrine/dbal',
  74.                     'https://github.com/doctrine/dbal/pull/5705',
  75.                     'Relying on the DBAL connecting to the "postgres" database by default is deprecated.'
  76.                         ' Unless you want to have the server determine the default database for the connection,'
  77.                         ' specify the database name explicitly.',
  78.                 );
  79.             }
  80.             // Used for temporary connections to allow operations like dropping the database currently connected to.
  81.             $dsn .= 'dbname=postgres;';
  82.         }
  83.         if (isset($params['sslmode'])) {
  84.             $dsn .= 'sslmode=' $params['sslmode'] . ';';
  85.         }
  86.         if (isset($params['sslrootcert'])) {
  87.             $dsn .= 'sslrootcert=' $params['sslrootcert'] . ';';
  88.         }
  89.         if (isset($params['sslcert'])) {
  90.             $dsn .= 'sslcert=' $params['sslcert'] . ';';
  91.         }
  92.         if (isset($params['sslkey'])) {
  93.             $dsn .= 'sslkey=' $params['sslkey'] . ';';
  94.         }
  95.         if (isset($params['sslcrl'])) {
  96.             $dsn .= 'sslcrl=' $params['sslcrl'] . ';';
  97.         }
  98.         if (isset($params['application_name'])) {
  99.             $dsn .= 'application_name=' $params['application_name'] . ';';
  100.         }
  101.         return $dsn;
  102.     }
  103. }