00001 <?php 00002 00027 class DebugConnection implements Connection { 00028 00030 private $childConnection = null; 00031 00033 private $numQueriesExecuted = 0; 00034 00036 private $lastExecutedQuery = ''; 00037 00041 private $logger; 00042 00048 public function setLogger($logger) 00049 { 00050 $this->logger = $logger; 00051 } 00052 00058 public function getNumQueriesExecuted() 00059 { 00060 return $this->numQueriesExecuted; 00061 } 00062 00068 public function getLastExecutedQuery() 00069 { 00070 return $this->lastExecutedQuery; 00071 } 00072 00076 public function connect($dsninfo, $flags = 0) 00077 { 00078 if (!($driver = Creole::getDriver($dsninfo['phptype']))) { 00079 throw new SQLException("No driver has been registered to handle connection type: $type"); 00080 } 00081 $connectionClass = Creole::import($driver); 00082 $this->childConnection = new $connectionClass(); 00083 $this->log("connect(): DSN: ". var_export($dsninfo, true) . ", FLAGS: " . var_export($flags, true)); 00084 return $this->childConnection->connect($dsninfo, $flags); 00085 } 00086 00090 public function getDatabaseInfo() 00091 { 00092 return $this->childConnection->getDatabaseInfo(); 00093 } 00094 00098 public function getIdGenerator() 00099 { 00100 return $this->childConnection->getIdGenerator(); 00101 } 00102 00106 public function isConnected() 00107 { 00108 return $this->childConnection->isConnected(); 00109 } 00110 00114 public function prepareStatement($sql) 00115 { 00116 $this->log("prepareStatement(): $sql"); 00117 $obj = $this->childConnection->prepareStatement($sql); 00118 $objClass = get_class($obj); 00119 return new $objClass($this, $sql); 00120 } 00121 00125 public function createStatement() 00126 { 00127 $obj = $this->childConnection->createStatement(); 00128 $objClass = get_class($obj); 00129 return new $objClass($this); 00130 } 00131 00135 public function applyLimit(&$sql, $offset, $limit) 00136 { 00137 $this->log("applyLimit(): $sql, offset: $offset, limit: $limit"); 00138 return $this->childConnection->applyLimit($sql, $offset, $limit); 00139 } 00140 00144 public function close() 00145 { 00146 $this->log("close(): Closing connection."); 00147 return $this->childConnection->close(); 00148 } 00149 00153 public function executeQuery($sql, $fetchmode = null) 00154 { 00155 $this->log("executeQuery(): $sql"); 00156 $this->lastExecutedQuery = $sql; 00157 $this->numQueriesExecuted++; 00158 return $this->childConnection->executeQuery($sql, $fetchmode); 00159 } 00160 00164 public function executeUpdate($sql) 00165 { 00166 $this->log("executeUpdate(): $sql"); 00167 $this->lastExecutedQuery = $sql; 00168 $this->numQueriesExecuted++; 00169 return $this->childConnection->executeUpdate($sql); 00170 } 00171 00175 public function getUpdateCount() 00176 { 00177 return $this->childConnection->getUpdateCount(); 00178 } 00179 00183 public function prepareCall($sql) 00184 { 00185 $this->log("prepareCall(): $sql"); 00186 return $this->childConnection->prepareCall($sql); 00187 } 00188 00192 public function getResource() 00193 { 00194 return $this->childConnection->getResource(); 00195 } 00196 00200 public function getDSN() 00201 { 00202 return $this->childConnection->getDSN(); 00203 } 00204 00208 public function getFlags() 00209 { 00210 return $this->childConnection->getFlags(); 00211 } 00212 00216 public function begin() 00217 { 00218 $this->log("Beginning transaction."); 00219 return $this->childConnection->begin(); 00220 } 00221 00225 public function commit() 00226 { 00227 $this->log("Committing transaction."); 00228 return $this->childConnection->commit(); 00229 } 00230 00234 public function rollback() 00235 { 00236 $this->log("Rolling back transaction."); 00237 return $this->childConnection->rollback(); 00238 } 00239 00243 public function setAutoCommit($bit) 00244 { 00245 $this->log("Setting autocommit to: " . var_export($bit, true)); 00246 return $this->childConnection->setAutoCommit($bit); 00247 } 00248 00252 public function getAutoCommit() 00253 { 00254 return $this->childConnection->getAutoCommit(); 00255 } 00256 00261 private function log($msg) 00262 { 00263 if ($this->logger) { 00264 $this->logger->log($msg); 00265 } 00266 } 00267 00268 }