Query.php 4.8 KB

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