Driver.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\PDOPgSql;
  20. use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
  21. use Doctrine\DBAL\Driver\PDOConnection;
  22. use Doctrine\DBAL\DBALException;
  23. use PDOException;
  24. use PDO;
  25. /**
  26. * Driver that connects through pdo_pgsql.
  27. *
  28. * @since 2.0
  29. */
  30. class Driver extends AbstractPostgreSQLDriver
  31. {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  36. {
  37. try {
  38. $pdo = new PDOConnection(
  39. $this->_constructPdoDsn($params),
  40. $username,
  41. $password,
  42. $driverOptions
  43. );
  44. if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
  45. && (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
  46. || true === $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES]
  47. )
  48. ) {
  49. $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
  50. }
  51. /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
  52. * - the 'client_encoding' connection param only works with postgres >= 9.1
  53. * - passing client_encoding via the 'options' param breaks pgbouncer support
  54. */
  55. if (isset($params['charset'])) {
  56. $pdo->query('SET NAMES \''.$params['charset'].'\'');
  57. }
  58. return $pdo;
  59. } catch (PDOException $e) {
  60. throw DBALException::driverException($this, $e);
  61. }
  62. }
  63. /**
  64. * Constructs the Postgres PDO DSN.
  65. *
  66. * @param array $params
  67. *
  68. * @return string The DSN.
  69. */
  70. private function _constructPdoDsn(array $params)
  71. {
  72. $dsn = 'pgsql:';
  73. if (isset($params['host']) && $params['host'] != '') {
  74. $dsn .= 'host=' . $params['host'] . ' ';
  75. }
  76. if (isset($params['port']) && $params['port'] != '') {
  77. $dsn .= 'port=' . $params['port'] . ' ';
  78. }
  79. if (isset($params['dbname'])) {
  80. $dsn .= 'dbname=' . $params['dbname'] . ' ';
  81. } else {
  82. // Used for temporary connections to allow operations like dropping the database currently connected to.
  83. // Connecting without an explicit database does not work, therefore "postgres" database is used
  84. // as it is certainly present in every server setup.
  85. $dsn .= 'dbname=postgres' . ' ';
  86. }
  87. if (isset($params['sslmode'])) {
  88. $dsn .= 'sslmode=' . $params['sslmode'] . ' ';
  89. }
  90. return $dsn;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function getName()
  96. {
  97. return 'pdo_pgsql';
  98. }
  99. }