Driver.php 3.7 KB

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