vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php line 48

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDOPgSql;
  3. use Doctrine\DBAL\DBALException;
  4. use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
  5. use Doctrine\DBAL\Driver\PDOConnection;
  6. use PDO;
  7. use PDOException;
  8. use function defined;
  9. /**
  10.  * Driver that connects through pdo_pgsql.
  11.  */
  12. class Driver extends AbstractPostgreSQLDriver
  13. {
  14.     /**
  15.      * {@inheritdoc}
  16.      */
  17.     public function connect(array $params$username null$password null, array $driverOptions = [])
  18.     {
  19.         try {
  20.             $pdo = new PDOConnection(
  21.                 $this->_constructPdoDsn($params),
  22.                 $username,
  23.                 $password,
  24.                 $driverOptions
  25.             );
  26.             if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
  27.                 && (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
  28.                     || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
  29.                 )
  30.             ) {
  31.                 $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPAREStrue);
  32.             }
  33.             /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
  34.              * - the 'client_encoding' connection param only works with postgres >= 9.1
  35.              * - passing client_encoding via the 'options' param breaks pgbouncer support
  36.              */
  37.             if (isset($params['charset'])) {
  38.                 $pdo->exec('SET NAMES \'' $params['charset'] . '\'');
  39.             }
  40.             return $pdo;
  41.         } catch (PDOException $e) {
  42.             throw DBALException::driverException($this$e);
  43.         }
  44.     }
  45.     /**
  46.      * Constructs the Postgres PDO DSN.
  47.      *
  48.      * @param mixed[] $params
  49.      *
  50.      * @return string The DSN.
  51.      */
  52.     private function _constructPdoDsn(array $params)
  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.             $dsn .= 'dbname=' $params['default_dbname'] . ';';
  65.         } else {
  66.             // Used for temporary connections to allow operations like dropping the database currently connected to.
  67.             // Connecting without an explicit database does not work, therefore "postgres" database is used
  68.             // as it is mostly present in every server setup.
  69.             $dsn .= 'dbname=postgres;';
  70.         }
  71.         if (isset($params['sslmode'])) {
  72.             $dsn .= 'sslmode=' $params['sslmode'] . ';';
  73.         }
  74.         if (isset($params['sslrootcert'])) {
  75.             $dsn .= 'sslrootcert=' $params['sslrootcert'] . ';';
  76.         }
  77.         if (isset($params['sslcert'])) {
  78.             $dsn .= 'sslcert=' $params['sslcert'] . ';';
  79.         }
  80.         if (isset($params['sslkey'])) {
  81.             $dsn .= 'sslkey=' $params['sslkey'] . ';';
  82.         }
  83.         if (isset($params['sslcrl'])) {
  84.             $dsn .= 'sslcrl=' $params['sslcrl'] . ';';
  85.         }
  86.         if (isset($params['application_name'])) {
  87.             $dsn .= 'application_name=' $params['application_name'] . ';';
  88.         }
  89.         return $dsn;
  90.     }
  91.     /**
  92.      * {@inheritdoc}
  93.      */
  94.     public function getName()
  95.     {
  96.         return 'pdo_pgsql';
  97.     }
  98. }