| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 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 |
$method = $this->getParameter('method', 'normal'); |
|---|
| 49 |
|
|---|
| 50 |
switch ($method) |
|---|
| 51 |
{ |
|---|
| 52 |
case 'normal': |
|---|
| 53 |
|
|---|
| 54 |
$database = $this->getParameter('database'); |
|---|
| 55 |
$host = $this->getParameter('host', 'localhost'); |
|---|
| 56 |
$password = $this->getParameter('password'); |
|---|
| 57 |
$username = $this->getParameter('username'); |
|---|
| 58 |
|
|---|
| 59 |
break; |
|---|
| 60 |
|
|---|
| 61 |
case 'server': |
|---|
| 62 |
|
|---|
| 63 |
// and extract them to local scope |
|---|
| 64 |
$parameters =& $this->loadParameters($_SERVER); |
|---|
| 65 |
extract($parameters); |
|---|
| 66 |
|
|---|
| 67 |
break; |
|---|
| 68 |
|
|---|
| 69 |
case 'env': |
|---|
| 70 |
|
|---|
| 71 |
// and extract them to local scope |
|---|
| 72 |
$string =& $this->loadParameters($_ENV); |
|---|
| 73 |
extract($parameters); |
|---|
| 74 |
|
|---|
| 75 |
break; |
|---|
| 76 |
|
|---|
| 77 |
default: |
|---|
| 78 |
|
|---|
| 79 |
throw new sfDatabaseException(sprintf('Invalid MySQLDatabase parameter retrieval method "%s".', $method)); |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
$connect = $this->getConnectMethod($this->getParameter('persistent', false)); |
|---|
| 84 |
if ($password == null) |
|---|
| 85 |
{ |
|---|
| 86 |
if ($username == null) |
|---|
| 87 |
{ |
|---|
| 88 |
$this->connection = @$connect($host); |
|---|
| 89 |
} |
|---|
| 90 |
else |
|---|
| 91 |
{ |
|---|
| 92 |
$this->connection = @$connect($host, $username); |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
else |
|---|
| 96 |
{ |
|---|
| 97 |
$this->connection = @$connect($host, $username, $password); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
if ($this->connection === false) |
|---|
| 102 |
{ |
|---|
| 103 |
|
|---|
| 104 |
throw new sfDatabaseException('Failed to create a MySQLDatabase connection.'); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
if ($this->selectDatabase($database)) |
|---|
| 109 |
{ |
|---|
| 110 |
|
|---|
| 111 |
throw new sfDatabaseException(sprintf('Failed to select MySQLDatabase "%s".', $database)); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
// to the resource |
|---|
| 116 |
$this->resource = $this->connection; |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
* Returns the appropriate connect method. |
|---|
| 121 |
* |
|---|
| 122 |
* @param bool $persistent wether persistent connections are use or not |
|---|
| 123 |
* @return string name of connect method. |
|---|
| 124 |
*/ |
|---|
| 125 |
protected function getConnectMethod($persistent) |
|---|
| 126 |
{ |
|---|
| 127 |
return $persistent ? 'mysql_pconnect' : 'mysql_connect'; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
* Selects the database to be used in this connection |
|---|
| 132 |
* |
|---|
| 133 |
* @param string $database Name of database to be connected |
|---|
| 134 |
* |
|---|
| 135 |
* @return bool true if this was successful |
|---|
| 136 |
*/ |
|---|
| 137 |
protected function selectDatabase($database) |
|---|
| 138 |
{ |
|---|
| 139 |
return ($database != null && !@mysql_select_db($database, $this->connection)); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
* Loads connection parameters from an existing array. |
|---|
| 144 |
* |
|---|
| 145 |
* @return array An associative array of connection parameters |
|---|
| 146 |
*/ |
|---|
| 147 |
protected function & loadParameters(&$array) |
|---|
| 148 |
{ |
|---|
| 149 |
|
|---|
| 150 |
$available = array('database', 'host', 'password', 'user'); |
|---|
| 151 |
|
|---|
| 152 |
$parameters = array(); |
|---|
| 153 |
|
|---|
| 154 |
foreach ($available as $parameter) |
|---|
| 155 |
{ |
|---|
| 156 |
$$parameter = $this->getParameter($parameter); |
|---|
| 157 |
|
|---|
| 158 |
$parameters[$parameter] = ($$parameter != null) ? $array[$$parameter] : null; |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
return $parameters; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
* Execute the shutdown procedure |
|---|
| 166 |
* |
|---|
| 167 |
* @return void |
|---|
| 168 |
* |
|---|
| 169 |
* @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database |
|---|
| 170 |
*/ |
|---|
| 171 |
public function shutdown() |
|---|
| 172 |
{ |
|---|
| 173 |
if ($this->connection != null) |
|---|
| 174 |
{ |
|---|
| 175 |
@mysql_close($this->connection); |
|---|
| 176 |
} |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|