/package/app/app/vendor/symfony/addon/creole/drivers/sfDebugConnection.php

https://github.com/richhl/kalturaCE · PHP · 294 lines · 149 code · 34 blank · 111 comment · 6 complexity · 11c6b38ad7148462c3c463002b6b33a5 MD5 · raw file

  1. <?php
  2. /**
  3. * Debug implementation of Connection.
  4. *
  5. * This is a Connection that implements the decorator pattern, wrapping around
  6. * the true Connection object (stored in $childConnection). This Connection
  7. * tracks information about queries executed and makes that information available
  8. * for debugging purposes. The information tracked is the last query executed
  9. * on the connection (getLastExecutedQuery()) and the total number of
  10. * queries executed on the connection thus far (getNumQueriesExecuted()).
  11. *
  12. * To use this debug connection, you need to register it as a new Creole
  13. * driver that handles all connection types. To do this, call the following
  14. * before calling Creole::getConnection():
  15. *
  16. * <code>
  17. * Creole::registerDriver('*', 'creole.drivers.debug.DebugConnection');
  18. * </code>
  19. *
  20. * The next call to Creole::getConnection() will return an instance of
  21. * DebugConnection.
  22. *
  23. * @author Michael Sims
  24. * @package creole.drivers.debug
  25. */
  26. class sfDebugConnection implements Connection
  27. {
  28. /** @var Connection */
  29. private $childConnection = null;
  30. /** @var int */
  31. private $numQueriesExecuted = 0;
  32. /** @var string */
  33. private $lastExecutedQuery = '';
  34. /**
  35. * Optional PEAR Log class; if set queries will be logged at PEAR_LOG_INFO level.
  36. * @var Log
  37. */
  38. private static $logger;
  39. /**
  40. * Sets a Logger class (e.g. PEAR Log) to use for logging.
  41. * The logger class must have a log() method. All messages are logged at default log level.
  42. * @param object $logger
  43. */
  44. public static function setLogger($logger)
  45. {
  46. self::$logger = $logger;
  47. }
  48. /**
  49. * Returns the number of queries executed on this connection so far
  50. *
  51. * @return int
  52. */
  53. public function getNumQueriesExecuted()
  54. {
  55. return $this->numQueriesExecuted;
  56. }
  57. /**
  58. * Returns the last query executed on this connection
  59. *
  60. * @return string
  61. */
  62. public function getLastExecutedQuery()
  63. {
  64. return $this->lastExecutedQuery;
  65. }
  66. /**
  67. * connect()
  68. */
  69. public function connect($dsninfo, $flags = 0)
  70. {
  71. if (!($driver = Creole::getDriver($dsninfo['phptype'])))
  72. {
  73. throw new SQLException("No driver has been registered to handle connection type: $type");
  74. }
  75. $connectionClass = Creole::import($driver);
  76. $this->childConnection = new $connectionClass();
  77. $this->log("{sfCreole} connect(): DSN: ". var_export($dsninfo, true) . ", FLAGS: " . var_export($flags, true));
  78. return $this->childConnection->connect($dsninfo, $flags);
  79. }
  80. /**
  81. * @see Connection::getDatabaseInfo()
  82. */
  83. public function getDatabaseInfo()
  84. {
  85. return $this->childConnection->getDatabaseInfo();
  86. }
  87. /**
  88. * @see Connection::getIdGenerator()
  89. */
  90. public function getIdGenerator()
  91. {
  92. return $this->childConnection->getIdGenerator();
  93. }
  94. /**
  95. * @see Connection::isConnected()
  96. */
  97. public function isConnected()
  98. {
  99. return $this->childConnection->isConnected();
  100. }
  101. /**
  102. * @see Connection::prepareStatement()
  103. */
  104. public function prepareStatement($sql)
  105. {
  106. $this->log("{sfCreole} prepareStatement(): $sql");
  107. $obj = $this->childConnection->prepareStatement($sql);
  108. $objClass = get_class($obj);
  109. return new $objClass($this, $sql);
  110. }
  111. /**
  112. * @see Connection::createStatement()
  113. */
  114. public function createStatement()
  115. {
  116. $obj = $this->childConnection->createStatement();
  117. $objClass = get_class($obj);
  118. return new $objClass($this);
  119. }
  120. /**
  121. * @see Connection::applyLimit()
  122. */
  123. public function applyLimit(&$sql, $offset, $limit)
  124. {
  125. $this->log("{sfCreole} applyLimit(): $sql, offset: $offset, limit: $limit");
  126. return $this->childConnection->applyLimit($sql, $offset, $limit);
  127. }
  128. /**
  129. * @see Connection::close()
  130. */
  131. public function close()
  132. {
  133. $this->log("{sfCreole} close(): Closing connection.");
  134. return $this->childConnection->close();
  135. }
  136. /**
  137. * @see Connection::executeQuery()
  138. */
  139. public function executeQuery($sql, $fetchmode = null)
  140. {
  141. $this->lastExecutedQuery = $sql;
  142. $this->numQueriesExecuted++;
  143. $elapsedTime = 0;
  144. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  145. {
  146. $sqlTimer = sfTimerManager::getTimer('Database');
  147. $timer = new sfTimer();
  148. }
  149. $retval = $this->childConnection->executeQuery($sql, $fetchmode);
  150. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  151. {
  152. $sqlTimer->addTime();
  153. $elapsedTime = $timer->getElapsedTime();
  154. }
  155. $this->log(sprintf("{sfCreole} executeQuery(): [%.2f ms] %s", $elapsedTime * 1000, $sql));
  156. return $retval;
  157. }
  158. /**
  159. * @see Connection::executeUpdate()
  160. **/
  161. public function executeUpdate($sql)
  162. {
  163. $this->log("{sfCreole} executeUpdate(): $sql");
  164. $this->lastExecutedQuery = $sql;
  165. $this->numQueriesExecuted++;
  166. return $this->childConnection->executeUpdate($sql);
  167. }
  168. /**
  169. * @see Connection::getUpdateCount()
  170. */
  171. public function getUpdateCount()
  172. {
  173. return $this->childConnection->getUpdateCount();
  174. }
  175. /**
  176. * @see Connection::prepareCall()
  177. **/
  178. public function prepareCall($sql)
  179. {
  180. $this->log("{sfCreole} prepareCall(): $sql");
  181. return $this->childConnection->prepareCall($sql);
  182. }
  183. /**
  184. * @see Connection::getResource()
  185. */
  186. public function getResource()
  187. {
  188. return $this->childConnection->getResource();
  189. }
  190. /**
  191. * @see Connection::connect()
  192. */
  193. public function getDSN()
  194. {
  195. return $this->childConnection->getDSN();
  196. }
  197. /**
  198. * @see Connection::getFlags()
  199. */
  200. public function getFlags()
  201. {
  202. return $this->childConnection->getFlags();
  203. }
  204. /**
  205. * @see Connection::begin()
  206. */
  207. public function begin()
  208. {
  209. $this->log("{sfCreole} beginning transaction.");
  210. return $this->childConnection->begin();
  211. }
  212. /**
  213. * @see Connection::commit()
  214. */
  215. public function commit()
  216. {
  217. $this->log("{sfCreole} committing transaction.");
  218. return $this->childConnection->commit();
  219. }
  220. /**
  221. * @see Connection::rollback()
  222. */
  223. public function rollback()
  224. {
  225. $this->log("{sfCreole} rolling back transaction.");
  226. return $this->childConnection->rollback();
  227. }
  228. /**
  229. * @see Connection::setAutoCommit()
  230. */
  231. public function setAutoCommit($bit)
  232. {
  233. $this->log("{sfCreole} setting autocommit to: ".var_export($bit, true));
  234. return $this->childConnection->setAutoCommit($bit);
  235. }
  236. /**
  237. * @see Connection::getAutoCommit()
  238. */
  239. public function getAutoCommit()
  240. {
  241. return $this->childConnection->getAutoCommit();
  242. }
  243. /**
  244. * Private function that logs message using specified logger (if provided).
  245. * @param string $msg Message to log.
  246. */
  247. private function log($msg)
  248. {
  249. if (self::$logger)
  250. {
  251. // message on one line
  252. $msg = preg_replace("/\r?\n/", ' ', $msg);
  253. self::$logger->log($msg);
  254. }
  255. }
  256. public function __call($method, $arguments)
  257. {
  258. return $this->childConnection->$method($arguments);
  259. }
  260. }