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