web/lib/Zend/Db/Profiler/Query.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Db
       
    17  * @subpackage Profiler
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Query.php 23382 2010-11-18 22:50:50Z bittarman $
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * @category   Zend
       
    26  * @package    Zend_Db
       
    27  * @subpackage Profiler
       
    28  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    30  */
       
    31 class Zend_Db_Profiler_Query
       
    32 {
       
    33 
       
    34     /**
       
    35      * SQL query string or user comment, set by $query argument in constructor.
       
    36      *
       
    37      * @var string
       
    38      */
       
    39     protected $_query = '';
       
    40 
       
    41     /**
       
    42      * One of the Zend_Db_Profiler constants for query type, set by $queryType argument in constructor.
       
    43      *
       
    44      * @var integer
       
    45      */
       
    46     protected $_queryType = 0;
       
    47 
       
    48     /**
       
    49      * Unix timestamp with microseconds when instantiated.
       
    50      *
       
    51      * @var float
       
    52      */
       
    53     protected $_startedMicrotime = null;
       
    54 
       
    55     /**
       
    56      * Unix timestamp with microseconds when self::queryEnd() was called.
       
    57      *
       
    58      * @var integer
       
    59      */
       
    60     protected $_endedMicrotime = null;
       
    61 
       
    62     /**
       
    63      * @var array
       
    64      */
       
    65     protected $_boundParams = array();
       
    66 
       
    67     /**
       
    68      * @var array
       
    69      */
       
    70 
       
    71     /**
       
    72      * Class constructor.  A query is about to be started, save the query text ($query) and its
       
    73      * type (one of the Zend_Db_Profiler::* constants).
       
    74      *
       
    75      * @param  string  $query
       
    76      * @param  integer $queryType
       
    77      * @return void
       
    78      */
       
    79     public function __construct($query, $queryType)
       
    80     {
       
    81         $this->_query = $query;
       
    82         $this->_queryType = $queryType;
       
    83         // by default, and for backward-compatibility, start the click ticking
       
    84         $this->start();
       
    85     }
       
    86 
       
    87     /**
       
    88      * Clone handler for the query object.
       
    89      * @return void
       
    90      */
       
    91     public function __clone()
       
    92     {
       
    93         $this->_boundParams = array();
       
    94         $this->_endedMicrotime = null;
       
    95         $this->start();
       
    96     }
       
    97 
       
    98     /**
       
    99      * Starts the elapsed time click ticking.
       
   100      * This can be called subsequent to object creation,
       
   101      * to restart the clock.  For instance, this is useful
       
   102      * right before executing a prepared query.
       
   103      *
       
   104      * @return void
       
   105      */
       
   106     public function start()
       
   107     {
       
   108         $this->_startedMicrotime = microtime(true);
       
   109     }
       
   110 
       
   111     /**
       
   112      * Ends the query and records the time so that the elapsed time can be determined later.
       
   113      *
       
   114      * @return void
       
   115      */
       
   116     public function end()
       
   117     {
       
   118         $this->_endedMicrotime = microtime(true);
       
   119     }
       
   120 
       
   121     /**
       
   122      * Returns true if and only if the query has ended.
       
   123      *
       
   124      * @return boolean
       
   125      */
       
   126     public function hasEnded()
       
   127     {
       
   128         return $this->_endedMicrotime !== null;
       
   129     }
       
   130 
       
   131     /**
       
   132      * Get the original SQL text of the query.
       
   133      *
       
   134      * @return string
       
   135      */
       
   136     public function getQuery()
       
   137     {
       
   138         return $this->_query;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Get the type of this query (one of the Zend_Db_Profiler::* constants)
       
   143      *
       
   144      * @return integer
       
   145      */
       
   146     public function getQueryType()
       
   147     {
       
   148         return $this->_queryType;
       
   149     }
       
   150 
       
   151     /**
       
   152      * @param string $param
       
   153      * @param mixed $variable
       
   154      * @return void
       
   155      */
       
   156     public function bindParam($param, $variable)
       
   157     {
       
   158         $this->_boundParams[$param] = $variable;
       
   159     }
       
   160 
       
   161     /**
       
   162      * @param array $param
       
   163      * @return void
       
   164      */
       
   165     public function bindParams(array $params)
       
   166     {
       
   167         if (array_key_exists(0, $params)) {
       
   168             array_unshift($params, null);
       
   169             unset($params[0]);
       
   170         }
       
   171         foreach ($params as $param => $value) {
       
   172             $this->bindParam($param, $value);
       
   173         }
       
   174     }
       
   175 
       
   176     /**
       
   177      * @return array
       
   178      */
       
   179     public function getQueryParams()
       
   180     {
       
   181         return $this->_boundParams;
       
   182     }
       
   183 
       
   184     /**
       
   185      * Get the elapsed time (in seconds) that the query ran.
       
   186      * If the query has not yet ended, false is returned.
       
   187      *
       
   188      * @return float|false
       
   189      */
       
   190     public function getElapsedSecs()
       
   191     {
       
   192         if (null === $this->_endedMicrotime) {
       
   193             return false;
       
   194         }
       
   195 
       
   196         return $this->_endedMicrotime - $this->_startedMicrotime;
       
   197     }
       
   198 
       
   199     /**
       
   200      * Get the time (in seconds) when the profiler started running.
       
   201      *
       
   202      * @return bool|float
       
   203      */
       
   204     public function getStartedMicrotime()
       
   205     {
       
   206         if(null === $this->_startedMicrotime) {
       
   207             return false;
       
   208         }
       
   209 
       
   210         return $this->_startedMicrotime;
       
   211     }
       
   212 }
       
   213