sfPostgreSQLDatabase.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfPostgreSQLDatabase provides connectivity for the PostgreSQL brand database.
  12. *
  13. * <b>Optional parameters:</b>
  14. *
  15. * # <b>database</b> - [none] - The database name.
  16. * # <b>host</b> - [localhost] - The database host.
  17. * # <b>method</b> - [normal] - How to read connection parameters.
  18. * Possible values are normal, server, and
  19. * env. The normal method reads them from
  20. * the specified values. server reads them
  21. * from $_SERVER where the keys to retrieve
  22. * the values are what you specify the value
  23. * as in the settings. env reads them from
  24. * $_ENV and works like $_SERVER.
  25. * # <b>password</b> - [none] - The database password.
  26. * # <b>persistent</b> - [No] - Indicates that the connection should be
  27. * persistent.
  28. * # <b>port</b> - [none] - TCP/IP port on which PostgreSQL is
  29. * listening.
  30. * # <b>username</b> - [none] - The database username.
  31. *
  32. * @package symfony
  33. * @subpackage database
  34. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  35. * @author Sean Kerr <sean@code-box.org>
  36. * @version SVN: $Id: sfPostgreSQLDatabase.class.php 7792 2008-03-09 22:06:59Z fabien $
  37. */
  38. class sfPostgreSQLDatabase extends sfDatabase
  39. {
  40. /**
  41. * Connects to the database.
  42. *
  43. * @throws <b>sfDatabaseException</b> If a connection could not be created
  44. */
  45. public function connect()
  46. {
  47. // determine how to get our parameters
  48. $method = $this->getParameter('method', 'normal');
  49. // get parameters
  50. switch ($method)
  51. {
  52. case 'normal':
  53. // get parameters normally
  54. $database = $this->getParameter('database');
  55. $host = $this->getParameter('host');
  56. $password = $this->getParameter('password');
  57. $port = $this->getParameter('port');
  58. $username = $this->getParameter('username');
  59. // construct connection string
  60. $string = ($database != null ? (' dbname=' .$database) : '').
  61. ($host != null ? (' host=' .$host) : '').
  62. ($password != null ? (' password=' .$password) : '').
  63. ($port != null ? (' port=' .$port) : '').
  64. ($username != null ? (' user=' .$username) : '');
  65. break;
  66. case 'server':
  67. // construct a connection string from existing $_SERVER values
  68. $string = $this->loadParameters($_SERVER);
  69. break;
  70. case 'env':
  71. // construct a connection string from existing $_ENV values
  72. $string = $this->loadParameters($_ENV);
  73. break;
  74. default:
  75. // who knows what the user wants...
  76. throw new sfDatabaseException(sprintf('Invalid PostgreSQLDatabase parameter retrieval method "%s".', $method));
  77. }
  78. // let's see if we need a persistent connection
  79. $persistent = $this->getParameter('persistent', false);
  80. $connect = $persistent ? 'pg_pconnect' : 'pg_connect';
  81. $this->connection = @$connect($string);
  82. // make sure the connection went through
  83. if ($this->connection === false)
  84. {
  85. // the connection's foobar'd
  86. throw new sfDatabaseException('Failed to create a PostgreSQLDatabase connection.');
  87. }
  88. // since we're not an abstraction layer, we copy the connection
  89. // to the resource
  90. $this->resource = $this->connection;
  91. }
  92. /**
  93. * Loads connection parameters from an existing array.
  94. *
  95. * @return string A connection string
  96. */
  97. protected function loadParameters(&$array)
  98. {
  99. $database = $this->getParameter('database');
  100. $host = $this->getParameter('host');
  101. $password = $this->getParameter('password');
  102. $port = $this->getParameter('port');
  103. $username = $this->getParameter('username');
  104. // construct connection string
  105. $string = ($database != null ? (' dbname=' .$array[$database]) : '').
  106. ($host != null ? (' host=' .$array[$host]) : '').
  107. ($password != null ? (' password='.$array[$password]) : '').
  108. ($port != null ? (' port=' .$array[$port]) : '').
  109. ($username != null ? (' user=' .$array[$username]) : '');
  110. return $string;
  111. }
  112. /**
  113. * Executes the shutdown procedure.
  114. *
  115. * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
  116. */
  117. public function shutdown()
  118. {
  119. if ($this->connection != null)
  120. {
  121. @pg_close($this->connection);
  122. }
  123. }
  124. }