[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/addon/creole/drivers/ -> sfDebugConnection.php (source)

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


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7