File indexing completed on 2025-01-19 05:21:02

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Db
0017  * @subpackage Profiler
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * @category   Zend
0026  * @package    Zend_Db
0027  * @subpackage Profiler
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  */
0031 class Zend_Db_Profiler_Query
0032 {
0033 
0034     /**
0035      * SQL query string or user comment, set by $query argument in constructor.
0036      *
0037      * @var string
0038      */
0039     protected $_query = '';
0040 
0041     /**
0042      * One of the Zend_Db_Profiler constants for query type, set by $queryType argument in constructor.
0043      *
0044      * @var integer
0045      */
0046     protected $_queryType = 0;
0047 
0048     /**
0049      * Unix timestamp with microseconds when instantiated.
0050      *
0051      * @var float
0052      */
0053     protected $_startedMicrotime = null;
0054 
0055     /**
0056      * Unix timestamp with microseconds when self::queryEnd() was called.
0057      *
0058      * @var integer
0059      */
0060     protected $_endedMicrotime = null;
0061 
0062     /**
0063      * @var array
0064      */
0065     protected $_boundParams = array();
0066 
0067     /**
0068      * @var array
0069      */
0070 
0071     /**
0072      * Class constructor.  A query is about to be started, save the query text ($query) and its
0073      * type (one of the Zend_Db_Profiler::* constants).
0074      *
0075      * @param  string  $query
0076      * @param  integer $queryType
0077      * @return void
0078      */
0079     public function __construct($query, $queryType)
0080     {
0081         $this->_query = $query;
0082         $this->_queryType = $queryType;
0083         // by default, and for backward-compatibility, start the click ticking
0084         $this->start();
0085     }
0086 
0087     /**
0088      * Clone handler for the query object.
0089      * @return void
0090      */
0091     public function __clone()
0092     {
0093         $this->_boundParams = array();
0094         $this->_endedMicrotime = null;
0095         $this->start();
0096     }
0097 
0098     /**
0099      * Starts the elapsed time click ticking.
0100      * This can be called subsequent to object creation,
0101      * to restart the clock.  For instance, this is useful
0102      * right before executing a prepared query.
0103      *
0104      * @return void
0105      */
0106     public function start()
0107     {
0108         $this->_startedMicrotime = microtime(true);
0109     }
0110 
0111     /**
0112      * Ends the query and records the time so that the elapsed time can be determined later.
0113      *
0114      * @return void
0115      */
0116     public function end()
0117     {
0118         $this->_endedMicrotime = microtime(true);
0119     }
0120 
0121     /**
0122      * Returns true if and only if the query has ended.
0123      *
0124      * @return boolean
0125      */
0126     public function hasEnded()
0127     {
0128         return $this->_endedMicrotime !== null;
0129     }
0130 
0131     /**
0132      * Get the original SQL text of the query.
0133      *
0134      * @return string
0135      */
0136     public function getQuery()
0137     {
0138         return $this->_query;
0139     }
0140 
0141     /**
0142      * Get the type of this query (one of the Zend_Db_Profiler::* constants)
0143      *
0144      * @return integer
0145      */
0146     public function getQueryType()
0147     {
0148         return $this->_queryType;
0149     }
0150 
0151     /**
0152      * @param string $param
0153      * @param mixed $variable
0154      * @return void
0155      */
0156     public function bindParam($param, $variable)
0157     {
0158         $this->_boundParams[$param] = $variable;
0159     }
0160 
0161     /**
0162      * @param array $param
0163      * @return void
0164      */
0165     public function bindParams(array $params)
0166     {
0167         if (array_key_exists(0, $params)) {
0168             array_unshift($params, null);
0169             unset($params[0]);
0170         }
0171         foreach ($params as $param => $value) {
0172             $this->bindParam($param, $value);
0173         }
0174     }
0175 
0176     /**
0177      * @return array
0178      */
0179     public function getQueryParams()
0180     {
0181         return $this->_boundParams;
0182     }
0183 
0184     /**
0185      * Get the elapsed time (in seconds) that the query ran.
0186      * If the query has not yet ended, false is returned.
0187      *
0188      * @return float|false
0189      */
0190     public function getElapsedSecs()
0191     {
0192         if (null === $this->_endedMicrotime) {
0193             return false;
0194         }
0195 
0196         return $this->_endedMicrotime - $this->_startedMicrotime;
0197     }
0198 
0199     /**
0200      * Get the time (in seconds) when the profiler started running.
0201      *
0202      * @return bool|float
0203      */
0204     public function getStartedMicrotime()
0205     {
0206         if(null === $this->_startedMicrotime) {
0207             return false;
0208         }
0209 
0210         return $this->_startedMicrotime;
0211     }
0212 }
0213