sfMySQLDatabase.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 12763 2008-11-08 11:27:24Z 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. $encoding = $this->getParameter('encoding');
  56. break;
  57. case 'server':
  58. // construct a connection string from existing $_SERVER values
  59. // and extract them to local scope
  60. $parameters =& $this->loadParameters($_SERVER);
  61. extract($parameters);
  62. break;
  63. case 'env':
  64. // construct a connection string from existing $_ENV values
  65. // and extract them to local scope
  66. $string =& $this->loadParameters($_ENV);
  67. extract($parameters);
  68. break;
  69. default:
  70. // who knows what the user wants...
  71. throw new sfDatabaseException(sprintf('Invalid MySQLDatabase parameter retrieval method "%s".', $method));
  72. }
  73. // let's see if we need a persistent connection
  74. $connect = $this->getConnectMethod($this->getParameter('persistent', false));
  75. if ($password == null)
  76. {
  77. if ($username == null)
  78. {
  79. $this->connection = @$connect($host);
  80. }
  81. else
  82. {
  83. $this->connection = @$connect($host, $username);
  84. }
  85. }
  86. else
  87. {
  88. $this->connection = @$connect($host, $username, $password);
  89. }
  90. // make sure the connection went through
  91. if ($this->connection === false)
  92. {
  93. // the connection's foobar'd
  94. throw new sfDatabaseException('Failed to create a MySQLDatabase connection.');
  95. }
  96. // select our database
  97. if ($this->selectDatabase($database))
  98. {
  99. // can't select the database
  100. throw new sfDatabaseException(sprintf('Failed to select MySQLDatabase "%s".', $database));
  101. }
  102. // set encoding if specified
  103. if($encoding)
  104. {
  105. @mysql_query("SET NAMES '".$encoding."'", $this->connection);
  106. }
  107. // since we're not an abstraction layer, we copy the connection
  108. // to the resource
  109. $this->resource = $this->connection;
  110. }
  111. /**
  112. * Returns the appropriate connect method.
  113. *
  114. * @param bool $persistent wether persistent connections are use or not
  115. * @return string name of connect method.
  116. */
  117. protected function getConnectMethod($persistent)
  118. {
  119. return $persistent ? 'mysql_pconnect' : 'mysql_connect';
  120. }
  121. /**
  122. * Selects the database to be used in this connection
  123. *
  124. * @param string $database Name of database to be connected
  125. *
  126. * @return bool true if this was successful
  127. */
  128. protected function selectDatabase($database)
  129. {
  130. return ($database != null && !@mysql_select_db($database, $this->connection));
  131. }
  132. /**
  133. * Loads connection parameters from an existing array.
  134. *
  135. * @return array An associative array of connection parameters
  136. */
  137. protected function & loadParameters(&$array)
  138. {
  139. // list of available parameters
  140. $available = array('database', 'host', 'password', 'user');
  141. $parameters = array();
  142. foreach ($available as $parameter)
  143. {
  144. $$parameter = $this->getParameter($parameter);
  145. $parameters[$parameter] = ($$parameter != null) ? $array[$$parameter] : null;
  146. }
  147. return $parameters;
  148. }
  149. /**
  150. * Execute the shutdown procedure
  151. *
  152. * @return void
  153. *
  154. * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
  155. */
  156. public function shutdown()
  157. {
  158. if ($this->connection != null)
  159. {
  160. @mysql_close($this->connection);
  161. }
  162. }
  163. }