| 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 |
class sfDebugConnection implements Connection |
|---|
| 29 |
{ |
|---|
| 30 |
|
|---|
| 31 |
private $childConnection = null; |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
private $numQueriesExecuted = 0; |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
private $lastExecutedQuery = ''; |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
* Optional sfEventDispatcher object. |
|---|
| 41 |
* @var Log |
|---|
| 42 |
*/ |
|---|
| 43 |
private static $dispatcher; |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
* Sets a sfEventDispatcher object. |
|---|
| 47 |
* |
|---|
| 48 |
* @param sfEventDispatcher $dispatcher An event dispatcher. |
|---|
| 49 |
*/ |
|---|
| 50 |
public static function setDispatcher($dispatcher) |
|---|
| 51 |
{ |
|---|
| 52 |
self::$dispatcher = $dispatcher; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
* Returns the number of queries executed on this connection so far |
|---|
| 57 |
* |
|---|
| 58 |
* @return int |
|---|
| 59 |
*/ |
|---|
| 60 |
public function getNumQueriesExecuted() |
|---|
| 61 |
{ |
|---|
| 62 |
return $this->numQueriesExecuted; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* Returns the last query executed on this connection |
|---|
| 67 |
* |
|---|
| 68 |
* @return string |
|---|
| 69 |
*/ |
|---|
| 70 |
public function getLastExecutedQuery() |
|---|
| 71 |
{ |
|---|
| 72 |
return $this->lastExecutedQuery; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
* connect() |
|---|
| 77 |
*/ |
|---|
| 78 |
public function connect($dsninfo, $flags = 0) |
|---|
| 79 |
{ |
|---|
| 80 |
if (!($driver = Creole::getDriver($dsninfo['phptype']))) |
|---|
| 81 |
{ |
|---|
| 82 |
throw new SQLException(sprintf('No driver has been registered to handle connection type: %s.', $dsninfo['phptype'])); |
|---|
| 83 |
} |
|---|
| 84 |
$connectionClass = Creole::import($driver); |
|---|
| 85 |
$this->childConnection = new $connectionClass(); |
|---|
| 86 |
$this->log("connect(): DSN: ". var_export($dsninfo, true) . ", FLAGS: " . var_export($flags, true)); |
|---|
| 87 |
return $this->childConnection->connect($dsninfo, $flags); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
* @see Connection::getDatabaseInfo() |
|---|
| 92 |
*/ |
|---|
| 93 |
public function getDatabaseInfo() |
|---|
| 94 |
{ |
|---|
| 95 |
return $this->childConnection->getDatabaseInfo(); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
* @see Connection::getIdGenerator() |
|---|
| 100 |
*/ |
|---|
| 101 |
public function getIdGenerator() |
|---|
| 102 |
{ |
|---|
| 103 |
return $this->childConnection->getIdGenerator(); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
* @see Connection::isConnected() |
|---|
| 108 |
*/ |
|---|
| 109 |
public function isConnected() |
|---|
| 110 |
{ |
|---|
| 111 |
return $this->childConnection->isConnected(); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
* @see Connection::prepareStatement() |
|---|
| 116 |
*/ |
|---|
| 117 |
public function prepareStatement($sql) |
|---|
| 118 |
{ |
|---|
| 119 |
$this->log("prepareStatement(): $sql"); |
|---|
| 120 |
$obj = $this->childConnection->prepareStatement($sql); |
|---|
| 121 |
$objClass = get_class($obj); |
|---|
| 122 |
return new $objClass($this, $sql); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
* @see Connection::createStatement() |
|---|
| 127 |
*/ |
|---|
| 128 |
public function createStatement() |
|---|
| 129 |
{ |
|---|
| 130 |
$obj = $this->childConnection->createStatement(); |
|---|
| 131 |
$objClass = get_class($obj); |
|---|
| 132 |
return new $objClass($this); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
* @see Connection::applyLimit() |
|---|
| 137 |
*/ |
|---|
| 138 |
public function applyLimit(&$sql, $offset, $limit) |
|---|
| 139 |
{ |
|---|
| 140 |
$this->log("applyLimit(): $sql, offset: $offset, limit: $limit"); |
|---|
| 141 |
return $this->childConnection->applyLimit($sql, $offset, $limit); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
* @see Connection::close() |
|---|
| 146 |
*/ |
|---|
| 147 |
public function close() |
|---|
| 148 |
{ |
|---|
| 149 |
$this->log("close(): Closing connection."); |
|---|
| 150 |
return $this->childConnection->close(); |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
* @see Connection::executeQuery() |
|---|
| 155 |
*/ |
|---|
| 156 |
public function executeQuery($sql, $fetchmode = null) |
|---|
| 157 |
{ |
|---|
| 158 |
$this->lastExecutedQuery = $sql; |
|---|
| 159 |
$this->numQueriesExecuted++; |
|---|
| 160 |
|
|---|
| 161 |
$elapsedTime = 0; |
|---|
| 162 |
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) |
|---|
| 163 |
{ |
|---|
| 164 |
$sqlTimer = sfTimerManager::getTimer('Database'); |
|---|
| 165 |
$timer = new sfTimer(); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
$retval = $this->childConnection->executeQuery($sql, $fetchmode); |
|---|
| 169 |
|
|---|
| 170 |
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) |
|---|
| 171 |
{ |
|---|
| 172 |
$sqlTimer->addTime(); |
|---|
| 173 |
$elapsedTime = $timer->getElapsedTime(); |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
$this->log(sprintf("executeQuery(): [%.2f ms] %s", $elapsedTime * 1000, $sql)); |
|---|
| 177 |
|
|---|
| 178 |
return $retval; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
* @see Connection::executeUpdate() |
|---|
| 183 |
**/ |
|---|
| 184 |
public function executeUpdate($sql) |
|---|
| 185 |
{ |
|---|
| 186 |
$this->log("executeUpdate(): $sql"); |
|---|
| 187 |
$this->lastExecutedQuery = $sql; |
|---|
| 188 |
$this->numQueriesExecuted++; |
|---|
| 189 |
return $this->childConnection->executeUpdate($sql); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
* @see Connection::getUpdateCount() |
|---|
| 194 |
*/ |
|---|
| 195 |
public function getUpdateCount() |
|---|
| 196 |
{ |
|---|
| 197 |
return $this->childConnection->getUpdateCount(); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
* @see Connection::prepareCall() |
|---|
| 202 |
**/ |
|---|
| 203 |
public function prepareCall($sql) |
|---|
| 204 |
{ |
|---|
| 205 |
$this->log("prepareCall(): $sql"); |
|---|
| 206 |
return $this->childConnection->prepareCall($sql); |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
* @see Connection::getResource() |
|---|
| 211 |
*/ |
|---|
| 212 |
public function getResource() |
|---|
| 213 |
{ |
|---|
| 214 |
return $this->childConnection->getResource(); |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
* @see Connection::connect() |
|---|
| 219 |
*/ |
|---|
| 220 |
public function getDSN() |
|---|
| 221 |
{ |
|---|
| 222 |
return $this->childConnection->getDSN(); |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
* @see Connection::getFlags() |
|---|
| 227 |
*/ |
|---|
| 228 |
public function getFlags() |
|---|
| 229 |
{ |
|---|
| 230 |
return $this->childConnection->getFlags(); |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
* @see Connection::begin() |
|---|
| 235 |
*/ |
|---|
| 236 |
public function begin() |
|---|
| 237 |
{ |
|---|
| 238 |
$this->log("beginning transaction."); |
|---|
| 239 |
return $this->childConnection->begin(); |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
* @see Connection::commit() |
|---|
| 244 |
*/ |
|---|
| 245 |
public function commit() |
|---|
| 246 |
{ |
|---|
| 247 |
$this->log("committing transaction."); |
|---|
| 248 |
return $this->childConnection->commit(); |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
* @see Connection::rollback() |
|---|
| 253 |
*/ |
|---|
| 254 |
public function rollback() |
|---|
| 255 |
{ |
|---|
| 256 |
$this->log("rolling back transaction."); |
|---|
| 257 |
return $this->childConnection->rollback(); |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
* @see Connection::setAutoCommit() |
|---|
| 262 |
*/ |
|---|
| 263 |
public function setAutoCommit($bit) |
|---|
| 264 |
{ |
|---|
| 265 |
$this->log("setting autocommit to: ".var_export($bit, true)); |
|---|
| 266 |
return $this->childConnection->setAutoCommit($bit); |
|---|
| 267 |
} |
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
* @see Connection::getAutoCommit() |
|---|
| 271 |
*/ |
|---|
| 272 |
public function getAutoCommit() |
|---|
| 273 |
{ |
|---|
| 274 |
return $this->childConnection->getAutoCommit(); |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
* Private function that logs message using specified dispatcher (if provided). |
|---|
| 279 |
* @param string $msg Message to log. |
|---|
| 280 |
*/ |
|---|
| 281 |
private function log($msg) |
|---|
| 282 |
{ |
|---|
| 283 |
if (self::$dispatcher && sfConfig::get('sf_logging_enabled')) |
|---|
| 284 |
{ |
|---|
| 285 |
|
|---|
| 286 |
$msg = preg_replace("/\r?\n/", ' ', $msg); |
|---|
| 287 |
self::$dispatcher->notify(new sfEvent($this, 'application.log', array($msg))); |
|---|
| 288 |
} |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
public function __call($method, $arguments) |
|---|
| 292 |
{ |
|---|
| 293 |
return $this->childConnection->$method($arguments); |
|---|
| 294 |
} |
|---|
| 295 |
} |
|---|
| 296 |
|
|---|