[ 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 * sfCreoleDatabase provides connectivity for the Creole database abstraction 14 * layer. 15 * 16 * <b>Optional parameters:</b> 17 * 18 * # <b>classpath</b> - [none] - An absolute filesystem path to the main 19 * Creole class file. 20 * # <b>database</b> - [none] - The database name. 21 * # <b>dsn</b> - [none] - The DSN formatted connection string. 22 * # <b>host</b> - [none] - The database host specifications. 23 * # <b>port</b> - [none] - The database port. 24 * # <b>encoding</b> - [none] - The database encoding. 25 * # <b>method</b> - [normal] - How to read connection parameters. 26 * Possible values are dsn, normal, 27 * server, and env. The dsn method reads 28 * them from the dsn parameter. The 29 * normal method reads them from the 30 * specified values. server reads them 31 * from $_SERVER where the keys to 32 * retrieve the values are what you 33 * specify the value as in the settings. 34 * env reads them from $_ENV and works 35 * like $_SERVER. 36 * # <b>no_assoc_lower</b> - [Off] - Turn off portabilty of resultset 37 * field names. 38 * # <b>password</b> - [none] - The database password. 39 * # <b>persistent</b> - [No] - Indicates that the connection should 40 * persistent. 41 * # <b>phptype</b> - [none] - The type of database (mysql, pgsql, 42 * etc). 43 * # <b>username</b> - [none] - The database username. 44 * 45 * @package symfony 46 * @subpackage database 47 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 48 * @author Sean Kerr <skerr@mojavi.org> 49 * @version SVN: $Id: sfCreoleDatabase.class.php 3329 2007-01-23 08:29:34Z fabien $ 50 */ 51 class sfCreoleDatabase extends sfDatabase 52 { 53 /** 54 * Connect to the database. 55 * 56 * @throws <b>sfDatabaseException</b> If a connection could not be created. 57 */ 58 public function connect() 59 { 60 try 61 { 62 // determine how to get our settings 63 $method = $this->getParameter('method', 'normal'); 64 65 switch ($method) 66 { 67 case 'normal': 68 // get parameters normally, and all are required 69 $database = $this->getParameter('database', null); 70 $hostspec = $this->getParameter('hostspec') ? $this->getParameter('hostspec') : ($this->getParameter('host') ? $this->getParameter('hostspec') : null); 71 $password = $this->getParameter('password', null); 72 $phptype = $this->getParameter('phptype', null); 73 $username = $this->getParameter('username', null); 74 $port = $this->getParameter('port', null); 75 $encoding = $this->getParameter('encoding', null); 76 77 $dsn = array('database' => $database, 78 'hostspec' => $hostspec, 79 'password' => $password, 80 'phptype' => $phptype, 81 'username' => $username, 82 'port' => $port, 83 'encoding' => $encoding); 84 break; 85 86 case 'dsn': 87 $dsn = $this->getParameter('dsn'); 88 89 if ($dsn == null) 90 { 91 // missing required dsn parameter 92 $error = 'Database configuration specifies method "dsn", but is missing dsn parameter'; 93 94 throw new sfDatabaseException($error); 95 } 96 97 break; 98 99 case 'server': 100 // construct a DSN connection string from existing $_SERVER values 101 $dsn =& $this->loadDSN($_SERVER); 102 103 break; 104 105 case 'env': 106 // construct a DSN connection string from existing $_ENV values 107 $dsn =& $this->loadDSN($_ENV); 108 109 break; 110 111 default: 112 // who knows what the user wants... 113 $error = 'Invalid CreoleDatabase parameter retrieval method "%s"'; 114 $error = sprintf($error, $method); 115 116 throw new sfDatabaseException($error); 117 } 118 119 // get creole class path 120 $classPath = $this->getParameter('classpath'); 121 122 // include the creole file 123 if ($classPath == null) 124 { 125 require_once('creole/Creole.php'); 126 } 127 else 128 { 129 require_once($classPath); 130 } 131 132 // set our flags 133 $noAssocLower = $this->getParameter('no_assoc_lower', false); 134 $persistent = $this->getParameter('persistent', false); 135 $compatAssocLower = $this->getParameter('compat_assoc_lower', false); 136 $compatRtrimString = $this->getParameter('compat_rtrim_string', false); 137 138 $flags = 0; 139 $flags |= ($noAssocLower) ? Creole::NO_ASSOC_LOWER : 0; 140 $flags |= ($persistent) ? Creole::PERSISTENT : 0; 141 $flags |= ($compatAssocLower) ? Creole::COMPAT_ASSOC_LOWER : 0; 142 $flags |= ($compatRtrimString) ? Creole::COMPAT_RTRIM_STRING : 0; 143 144 // do the duuuurtay work, right thurr 145 if ($flags > 0) 146 { 147 $this->connection = Creole::getConnection($dsn, $flags); 148 } 149 else 150 { 151 $this->connection = Creole::getConnection($dsn); 152 } 153 154 // get our resource 155 $this->resource = $this->connection->getResource(); 156 } 157 catch (SQLException $e) 158 { 159 // the connection's foobar'd 160 throw new sfDatabaseException($e->toString()); 161 } 162 } 163 164 /** 165 * Load a DSN connection string from an existing array. 166 * 167 * @return array An associative array of connection parameters. 168 */ 169 protected function & loadDSN(&$array) 170 { 171 // determine if a dsn is set, otherwise use separate parameters 172 $dsn = $this->getParameter('dsn'); 173 174 if ($dsn == null) 175 { 176 // list of available parameters 177 $available = array('database', 'hostspec', 'password', 'phptype', 'username', 'port'); 178 179 $dsn = array(); 180 181 // yes, i know variable variables are ugly, but let's avoid using 182 // an array for array's sake in this single spot in the source 183 foreach ($available as $parameter) 184 { 185 $$parameter = $this->getParameter($parameter); 186 187 $dsn[$parameter] = ($$parameter != null) ? $array[$$parameter] : null; 188 } 189 } 190 else 191 { 192 $dsn = $array[$dsn]; 193 } 194 195 return $dsn; 196 } 197 198 /** 199 * Execute the shutdown procedure. 200 * 201 * @return void 202 * 203 * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database. 204 */ 205 public function shutdown() 206 { 207 if ($this->connection !== null) 208 { 209 @$this->connection->close(); 210 } 211 } 212 }
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 |