[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * (c) 2004-2006 Sean Kerr. 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 /** 13 * sfMySQLDatabase provides connectivity for the MySQL brand database. 14 * 15 * <b>Optional parameters:</b> 16 * 17 * # <b>database</b> - [none] - The database name. 18 * # <b>host</b> - [localhost] - The database host. 19 * # <b>method</b> - [normal] - How to read connection parameters. 20 * Possible values are normal, server, and 21 * env. The normal method reads them from 22 * the specified values. server reads them 23 * from $_SERVER where the keys to retrieve 24 * the values are what you specify the value 25 * as in the settings. env reads them from 26 * $_ENV and works like $_SERVER. 27 * # <b>password</b> - [none] - The database password. 28 * # <b>persistent</b> - [No] - Indicates that the connection should be 29 * persistent. 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 <skerr@mojavi.org> 36 * @version SVN: $Id: sfMySQLDatabase.class.php 3329 2007-01-23 08:29:34Z fabien $ 37 */ 38 class sfMySQLDatabase 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 48 // determine how to get our 49 $method = $this->getParameter('method', 'normal'); 50 51 switch ($method) 52 { 53 case 'normal': 54 // get parameters normally 55 $database = $this->getParameter('database'); 56 $host = $this->getParameter('host', 'localhost'); 57 $password = $this->getParameter('password'); 58 $username = $this->getParameter('username'); 59 60 break; 61 62 case 'server': 63 // construct a connection string from existing $_SERVER values 64 // and extract them to local scope 65 $parameters =& $this->loadParameters($_SERVER); 66 extract($parameters); 67 68 break; 69 70 case 'env': 71 // construct a connection string from existing $_ENV values 72 // and extract them to local scope 73 $string =& $this->loadParameters($_ENV); 74 extract($parameters); 75 76 break; 77 78 default: 79 // who knows what the user wants... 80 $error = 'Invalid MySQLDatabase parameter retrieval method "%s"'; 81 $error = sprintf($error, $method); 82 83 throw new sfDatabaseException($error); 84 } 85 86 // let's see if we need a persistent connection 87 $persistent = $this->getParameter('persistent', false); 88 $connect = ($persistent) ? 'mysql_pconnect' : 'mysql_connect'; 89 90 if ($password == null) 91 { 92 if ($username == null) 93 { 94 $this->connection = @$connect($host); 95 } 96 else 97 { 98 $this->connection = @$connect($host, $username); 99 } 100 } 101 else 102 { 103 $this->connection = @$connect($host, $username, $password); 104 } 105 106 // make sure the connection went through 107 if ($this->connection === false) 108 { 109 // the connection's foobar'd 110 $error = 'Failed to create a MySQLDatabase connection'; 111 112 throw new sfDatabaseException($error); 113 } 114 115 // select our database 116 if ($database != null && !@mysql_select_db($database, $this->connection)) 117 { 118 // can't select the database 119 $error = 'Failed to select MySQLDatabase "%s"'; 120 $error = sprintf($error, $database); 121 122 throw new sfDatabaseException($error); 123 } 124 125 // since we're not an abstraction layer, we copy the connection 126 // to the resource 127 $this->resource = $this->connection; 128 } 129 130 /** 131 * Loads connection parameters from an existing array. 132 * 133 * @return array An associative array of connection parameters 134 */ 135 protected function & loadParameters(&$array) 136 { 137 // list of available parameters 138 $available = array('database', 'host', 'password', 'user'); 139 140 $parameters = array(); 141 142 foreach ($available as $parameter) 143 { 144 $$parameter = $this->getParameter($parameter); 145 146 $parameters[$parameter] = ($$parameter != null) ? $array[$$parameter] : null; 147 } 148 149 return $parameters; 150 } 151 152 /** 153 * Execute the shutdown procedure 154 * 155 * @return void 156 * 157 * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database 158 */ 159 public function shutdown() 160 { 161 if ($this->connection != null) 162 { 163 @mysql_close($this->connection); 164 } 165 } 166 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |