Driver.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDOPgSql;
  3. use Doctrine\DBAL\Platforms;
  4. /**
  5. * Driver that connects through pdo_pgsql.
  6. *
  7. * @since 2.0
  8. */
  9. class Driver implements \Doctrine\DBAL\Driver
  10. {
  11. /**
  12. * Attempts to connect to the database and returns a driver connection on success.
  13. *
  14. * @return Doctrine\DBAL\Driver\Connection
  15. */
  16. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  17. {
  18. return new \Doctrine\DBAL\Driver\PDOConnection(
  19. $this->_constructPdoDsn($params),
  20. $username,
  21. $password,
  22. $driverOptions
  23. );
  24. }
  25. /**
  26. * Constructs the Postgres PDO DSN.
  27. *
  28. * @return string The DSN.
  29. */
  30. private function _constructPdoDsn(array $params)
  31. {
  32. $dsn = 'pgsql:';
  33. if (isset($params['host']) && $params['host'] != '') {
  34. $dsn .= 'host=' . $params['host'] . ' ';
  35. }
  36. if (isset($params['port']) && $params['port'] != '') {
  37. $dsn .= 'port=' . $params['port'] . ' ';
  38. }
  39. if (isset($params['dbname'])) {
  40. $dsn .= 'dbname=' . $params['dbname'] . ' ';
  41. }
  42. return $dsn;
  43. }
  44. public function getDatabasePlatform()
  45. {
  46. return new \Doctrine\DBAL\Platforms\PostgreSqlPlatform();
  47. }
  48. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  49. {
  50. return new \Doctrine\DBAL\Schema\PostgreSqlSchemaManager($conn);
  51. }
  52. public function getName()
  53. {
  54. return 'pdo_pgsql';
  55. }
  56. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  57. {
  58. $params = $conn->getParams();
  59. return $params['dbname'];
  60. }
  61. }