| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).'/../../vendor'); |
|---|
| 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 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
class sfCreoleDatabase extends sfDatabase |
|---|
| 54 |
{ |
|---|
| 55 |
|
|---|
| 56 |
* Connect to the database. |
|---|
| 57 |
* |
|---|
| 58 |
* @throws <b>sfDatabaseException</b> If a connection could not be created. |
|---|
| 59 |
*/ |
|---|
| 60 |
public function connect() |
|---|
| 61 |
{ |
|---|
| 62 |
try |
|---|
| 63 |
{ |
|---|
| 64 |
|
|---|
| 65 |
$method = $this->getParameter('method', 'normal'); |
|---|
| 66 |
|
|---|
| 67 |
switch ($method) |
|---|
| 68 |
{ |
|---|
| 69 |
case 'normal': |
|---|
| 70 |
|
|---|
| 71 |
$database = $this->getParameter('database', null); |
|---|
| 72 |
$hostspec = $this->getParameter('hostspec') ? $this->getParameter('hostspec') : ($this->getParameter('host') ? $this->getParameter('hostspec') : null); |
|---|
| 73 |
$password = $this->getParameter('password', null); |
|---|
| 74 |
$phptype = $this->getParameter('phptype', null); |
|---|
| 75 |
$username = $this->getParameter('username', null); |
|---|
| 76 |
$port = $this->getParameter('port', null); |
|---|
| 77 |
$encoding = $this->getParameter('encoding', null); |
|---|
| 78 |
|
|---|
| 79 |
$dsn = array('database' => $database, |
|---|
| 80 |
'hostspec' => $hostspec, |
|---|
| 81 |
'password' => $password, |
|---|
| 82 |
'phptype' => $phptype, |
|---|
| 83 |
'username' => $username, |
|---|
| 84 |
'port' => $port, |
|---|
| 85 |
'encoding' => $encoding); |
|---|
| 86 |
break; |
|---|
| 87 |
|
|---|
| 88 |
case 'dsn': |
|---|
| 89 |
$dsn = $this->getParameter('dsn'); |
|---|
| 90 |
|
|---|
| 91 |
if ($dsn == null) |
|---|
| 92 |
{ |
|---|
| 93 |
|
|---|
| 94 |
throw new sfDatabaseException('Database configuration specifies method "dsn", but is missing dsn parameter.'); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
break; |
|---|
| 98 |
|
|---|
| 99 |
case 'server': |
|---|
| 100 |
|
|---|
| 101 |
$dsn =& $this->loadDSN($_SERVER); |
|---|
| 102 |
|
|---|
| 103 |
break; |
|---|
| 104 |
|
|---|
| 105 |
case 'env': |
|---|
| 106 |
|
|---|
| 107 |
$dsn =& $this->loadDSN($_ENV); |
|---|
| 108 |
|
|---|
| 109 |
break; |
|---|
| 110 |
|
|---|
| 111 |
default: |
|---|
| 112 |
|
|---|
| 113 |
throw new sfDatabaseException(sprintf('Invalid CreoleDatabase parameter retrieval method "%s".', $method)); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
$classPath = $this->getParameter('classpath'); |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
if ($classPath == null) |
|---|
| 121 |
{ |
|---|
| 122 |
require_once('creole/Creole.php'); |
|---|
| 123 |
} |
|---|
| 124 |
else |
|---|
| 125 |
{ |
|---|
| 126 |
require_once($classPath); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
$noAssocLower = $this->getParameter('no_assoc_lower', false); |
|---|
| 131 |
$persistent = $this->getParameter('persistent', false); |
|---|
| 132 |
$compatAssocLower = $this->getParameter('compat_assoc_lower', false); |
|---|
| 133 |
$compatRtrimString = $this->getParameter('compat_rtrim_string', false); |
|---|
| 134 |
|
|---|
| 135 |
$flags = 0; |
|---|
| 136 |
$flags |= ($noAssocLower) ? Creole::NO_ASSOC_LOWER : 0; |
|---|
| 137 |
$flags |= ($persistent) ? Creole::PERSISTENT : 0; |
|---|
| 138 |
$flags |= ($compatAssocLower) ? Creole::COMPAT_ASSOC_LOWER : 0; |
|---|
| 139 |
$flags |= ($compatRtrimString) ? Creole::COMPAT_RTRIM_STRING : 0; |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
if ($flags > 0) |
|---|
| 143 |
{ |
|---|
| 144 |
$this->connection = Creole::getConnection($dsn, $flags); |
|---|
| 145 |
} |
|---|
| 146 |
else |
|---|
| 147 |
{ |
|---|
| 148 |
$this->connection = Creole::getConnection($dsn); |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
$this->resource = $this->connection->getResource(); |
|---|
| 153 |
} |
|---|
| 154 |
catch (SQLException $e) |
|---|
| 155 |
{ |
|---|
| 156 |
|
|---|
| 157 |
throw new sfDatabaseException($e->toString()); |
|---|
| 158 |
} |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
* Load a DSN connection string from an existing array. |
|---|
| 163 |
* |
|---|
| 164 |
* @return array An associative array of connection parameters. |
|---|
| 165 |
*/ |
|---|
| 166 |
protected function & loadDSN(&$array) |
|---|
| 167 |
{ |
|---|
| 168 |
|
|---|
| 169 |
$dsn = $this->getParameter('dsn'); |
|---|
| 170 |
|
|---|
| 171 |
if ($dsn == null) |
|---|
| 172 |
{ |
|---|
| 173 |
|
|---|
| 174 |
$available = array('database', 'hostspec', 'password', 'phptype', 'username', 'port'); |
|---|
| 175 |
|
|---|
| 176 |
$dsn = array(); |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
// an array for array's sake in this single spot in the source |
|---|
| 180 |
foreach ($available as $parameter) |
|---|
| 181 |
{ |
|---|
| 182 |
$$parameter = $this->getParameter($parameter); |
|---|
| 183 |
|
|---|
| 184 |
$dsn[$parameter] = ($$parameter != null) ? $array[$$parameter] : null; |
|---|
| 185 |
} |
|---|
| 186 |
} |
|---|
| 187 |
else |
|---|
| 188 |
{ |
|---|
| 189 |
$dsn = $array[$dsn]; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
return $dsn; |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
* Execute the shutdown procedure. |
|---|
| 197 |
* |
|---|
| 198 |
* @return void |
|---|
| 199 |
* |
|---|
| 200 |
* @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database. |
|---|
| 201 |
*/ |
|---|
| 202 |
public function shutdown() |
|---|
| 203 |
{ |
|---|
| 204 |
if ($this->connection !== null) |
|---|
| 205 |
{ |
|---|
| 206 |
@$this->connection->close(); |
|---|
| 207 |
} |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|