sfMySQLDatabase.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * sfMySQLDatabase provides connectivity for the MySQL 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>username</b> - [none] - The database username.
  29. *
  30. * @package symfony
  31. * @subpackage database
  32. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  33. * @author Sean Kerr <sean@code-box.org>
  34. * @version SVN: $Id: sfMySQLDatabase.class.php 9144 2008-05-21 11:56:58Z FabianLange $
  35. */
  36. class sfMySQLDatabase extends sfDatabase
  37. {
  38. /**
  39. * Connects to the database.
  40. *
  41. * @throws <b>sfDatabaseException</b> If a connection could not be created
  42. */
  43. public function connect()
  44. {
  45. // determine how to get our
  46. $method = $this->getParameter('method', 'normal');
  47. switch ($method)
  48. {
  49. case 'normal':
  50. // get parameters normally
  51. $database = $this->getParameter('database');
  52. $host = $this->getParameter('host', 'localhost');
  53. $password = $this->getParameter('password');
  54. $username = $this->getParameter('username');
  55. break;
  56. case 'server':
  57. // construct a connection string from existing $_SERVER values
  58. // and extract them to local scope
  59. $parameters =& $this->loadParameters($_SERVER);
  60. extract($parameters);
  61. break;
  62. case 'env':
  63. // construct a connection string from existing $_ENV values
  64. // and extract them to local scope
  65. $string =& $this->loadParameters($_ENV);
  66. extract($parameters);
  67. break;
  68. default:
  69. // who knows what the user wants...
  70. throw new sfDatabaseException(sprintf('Invalid MySQLDatabase parameter retrieval method "%s".', $method));
  71. }
  72. // let's see if we need a persistent connection
  73. $connect = $this->getConnectMethod($this->getParameter('persistent', false));
  74. if ($password == null)
  75. {
  76. if ($username == null)
  77. {
  78. $this->connection = @$connect($host);
  79. }
  80. else
  81. {
  82. $this->connection = @$connect($host, $username);
  83. }
  84. }
  85. else
  86. {
  87. $this->connection = @$connect($host, $username, $password);
  88. }
  89. // make sure the connection went through
  90. if ($this->connection === false)
  91. {
  92. // the connection's foobar'd
  93. throw new sfDatabaseException('Failed to create a MySQLDatabase connection.');
  94. }
  95. // select our database
  96. if ($this->selectDatabase($database))
  97. {
  98. // can't select the database
  99. throw new sfDatabaseException(sprintf('Failed to select MySQLDatabase "%s".', $database));
  100. }
  101. // since we're not an abstraction layer, we copy the connection
  102. // to the resource
  103. $this->resource = $this->connection;
  104. }
  105. /**
  106. * Returns the appropriate connect method.
  107. *
  108. * @param bool $persistent wether persistent connections are use or not
  109. * @return string name of connect method.
  110. */
  111. protected function getConnectMethod($persistent)
  112. {
  113. return $persistent ? 'mysql_pconnect' : 'mysql_connect';
  114. }
  115. /**
  116. * Selects the database to be used in this connection
  117. *
  118. * @param string $database Name of database to be connected
  119. *
  120. * @return bool true if this was successful
  121. */
  122. protected function selectDatabase($database)
  123. {
  124. return ($database != null && !@mysql_select_db($database, $this->connection));
  125. }
  126. /**
  127. * Loads connection parameters from an existing array.
  128. *
  129. * @return array An associative array of connection parameters
  130. */
  131. protected function & loadParameters(&$array)
  132. {
  133. // list of available parameters
  134. $available = array('database', 'host', 'password', 'user');
  135. $parameters = array();
  136. foreach ($available as $parameter)
  137. {
  138. $$parameter = $this->getParameter($parameter);
  139. $parameters[$parameter] = ($$parameter != null) ? $array[$$parameter] : null;
  140. }
  141. return $parameters;
  142. }
  143. /**
  144. * Execute the shutdown procedure
  145. *
  146. * @return void
  147. *
  148. * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
  149. */
  150. public function shutdown()
  151. {
  152. if ($this->connection != null)
  153. {
  154. @mysql_close($this->connection);
  155. }
  156. }
  157. }