PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tine20/library/Zend/Db/Profiler.php

https://gitlab.com/israel.correa/Expresso
PHP | 460 lines | 176 code | 56 blank | 228 comment | 27 complexity | 9b34692450145036dc402fe4d0630b67 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, LGPL-2.1, LGPL-3.0, JSON, Apache-2.0
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Profiler.php 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Db
  25. * @subpackage Profiler
  26. * @copyright Copyright (c) 2005-2009 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
  30. {
  31. /**
  32. * A connection operation or selecting a database.
  33. */
  34. const CONNECT = 1;
  35. /**
  36. * Any general database query that does not fit into the other constants.
  37. */
  38. const QUERY = 2;
  39. /**
  40. * Adding new data to the database, such as SQL's INSERT.
  41. */
  42. const INSERT = 4;
  43. /**
  44. * Updating existing information in the database, such as SQL's UPDATE.
  45. *
  46. */
  47. const UPDATE = 8;
  48. /**
  49. * An operation related to deleting data in the database,
  50. * such as SQL's DELETE.
  51. */
  52. const DELETE = 16;
  53. /**
  54. * Retrieving information from the database, such as SQL's SELECT.
  55. */
  56. const SELECT = 32;
  57. /**
  58. * Transactional operation, such as start transaction, commit, or rollback.
  59. */
  60. const TRANSACTION = 64;
  61. /**
  62. * Array of Zend_Db_Profiler_Query objects.
  63. *
  64. * @var array
  65. */
  66. protected $_queryProfiles = array();
  67. /**
  68. * Stores enabled state of the profiler. If set to False, calls to
  69. * queryStart() will simply be ignored.
  70. *
  71. * @var boolean
  72. */
  73. protected $_enabled = false;
  74. /**
  75. * Stores the number of seconds to filter. NULL if filtering by time is
  76. * disabled. If an integer is stored here, profiles whose elapsed time
  77. * is less than this value in seconds will be unset from
  78. * the self::$_queryProfiles array.
  79. *
  80. * @var integer
  81. */
  82. protected $_filterElapsedSecs = null;
  83. /**
  84. * Logical OR of any of the filter constants. NULL if filtering by query
  85. * type is disable. If an integer is stored here, it is the logical OR of
  86. * any of the query type constants. When the query ends, if it is not
  87. * one of the types specified, it will be unset from the
  88. * self::$_queryProfiles array.
  89. *
  90. * @var integer
  91. */
  92. protected $_filterTypes = null;
  93. /**
  94. * Class constructor. The profiler is disabled by default unless it is
  95. * specifically enabled by passing in $enabled here or calling setEnabled().
  96. *
  97. * @param boolean $enabled
  98. * @return void
  99. */
  100. public function __construct($enabled = false)
  101. {
  102. $this->setEnabled($enabled);
  103. }
  104. /**
  105. * Enable or disable the profiler. If $enable is false, the profiler
  106. * is disabled and will not log any queries sent to it.
  107. *
  108. * @param boolean $enable
  109. * @return Zend_Db_Profiler Provides a fluent interface
  110. */
  111. public function setEnabled($enable)
  112. {
  113. $this->_enabled = (boolean) $enable;
  114. return $this;
  115. }
  116. /**
  117. * Get the current state of enable. If True is returned,
  118. * the profiler is enabled.
  119. *
  120. * @return boolean
  121. */
  122. public function getEnabled()
  123. {
  124. return $this->_enabled;
  125. }
  126. /**
  127. * Sets a minimum number of seconds for saving query profiles. If this
  128. * is set, only those queries whose elapsed time is equal or greater than
  129. * $minimumSeconds will be saved. To save all queries regardless of
  130. * elapsed time, set $minimumSeconds to null.
  131. *
  132. * @param integer $minimumSeconds OPTIONAL
  133. * @return Zend_Db_Profiler Provides a fluent interface
  134. */
  135. public function setFilterElapsedSecs($minimumSeconds = null)
  136. {
  137. if (null === $minimumSeconds) {
  138. $this->_filterElapsedSecs = null;
  139. } else {
  140. $this->_filterElapsedSecs = (integer) $minimumSeconds;
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Returns the minimum number of seconds for saving query profiles, or null if
  146. * query profiles are saved regardless of elapsed time.
  147. *
  148. * @return integer|null
  149. */
  150. public function getFilterElapsedSecs()
  151. {
  152. return $this->_filterElapsedSecs;
  153. }
  154. /**
  155. * Sets the types of query profiles to save. Set $queryType to one of
  156. * the Zend_Db_Profiler::* constants to only save profiles for that type of
  157. * query. To save more than one type, logical OR them together. To
  158. * save all queries regardless of type, set $queryType to null.
  159. *
  160. * @param integer $queryTypes OPTIONAL
  161. * @return Zend_Db_Profiler Provides a fluent interface
  162. */
  163. public function setFilterQueryType($queryTypes = null)
  164. {
  165. $this->_filterTypes = $queryTypes;
  166. return $this;
  167. }
  168. /**
  169. * Returns the types of query profiles saved, or null if queries are saved regardless
  170. * of their types.
  171. *
  172. * @return integer|null
  173. * @see Zend_Db_Profiler::setFilterQueryType()
  174. */
  175. public function getFilterQueryType()
  176. {
  177. return $this->_filterTypes;
  178. }
  179. /**
  180. * Clears the history of any past query profiles. This is relentless
  181. * and will even clear queries that were started and may not have
  182. * been marked as ended.
  183. *
  184. * @return Zend_Db_Profiler Provides a fluent interface
  185. */
  186. public function clear()
  187. {
  188. $this->_queryProfiles = array();
  189. return $this;
  190. }
  191. /**
  192. * @param integer $queryId
  193. * @return integer or null
  194. */
  195. public function queryClone(Zend_Db_Profiler_Query $query)
  196. {
  197. $this->_queryProfiles[] = clone $query;
  198. end($this->_queryProfiles);
  199. return key($this->_queryProfiles);
  200. }
  201. /**
  202. * Starts a query. Creates a new query profile object (Zend_Db_Profiler_Query)
  203. * and returns the "query profiler handle". Run the query, then call
  204. * queryEnd() and pass it this handle to make the query as ended and
  205. * record the time. If the profiler is not enabled, this takes no
  206. * action and immediately returns null.
  207. *
  208. * @param string $queryText SQL statement
  209. * @param integer $queryType OPTIONAL Type of query, one of the Zend_Db_Profiler::* constants
  210. * @return integer|null
  211. */
  212. public function queryStart($queryText, $queryType = null)
  213. {
  214. if (!$this->_enabled) {
  215. return null;
  216. }
  217. // make sure we have a query type
  218. if (null === $queryType) {
  219. switch (strtolower(substr(ltrim($queryText), 0, 6))) {
  220. case 'insert':
  221. $queryType = self::INSERT;
  222. break;
  223. case 'update':
  224. $queryType = self::UPDATE;
  225. break;
  226. case 'delete':
  227. $queryType = self::DELETE;
  228. break;
  229. case 'select':
  230. $queryType = self::SELECT;
  231. break;
  232. default:
  233. $queryType = self::QUERY;
  234. break;
  235. }
  236. }
  237. /**
  238. * @see Zend_Db_Profiler_Query
  239. */
  240. require_once 'Zend/Db/Profiler/Query.php';
  241. $this->_queryProfiles[] = new Zend_Db_Profiler_Query($queryText, $queryType);
  242. end($this->_queryProfiles);
  243. return key($this->_queryProfiles);
  244. }
  245. /**
  246. * Ends a query. Pass it the handle that was returned by queryStart().
  247. * This will mark the query as ended and save the time.
  248. *
  249. * @param integer $queryId
  250. * @throws Zend_Db_Profiler_Exception
  251. * @return void
  252. */
  253. public function queryEnd($queryId)
  254. {
  255. // Don't do anything if the Zend_Db_Profiler is not enabled.
  256. if (!$this->_enabled) {
  257. return;
  258. }
  259. // Check for a valid query handle.
  260. if (!isset($this->_queryProfiles[$queryId])) {
  261. /**
  262. * @see Zend_Db_Profiler_Exception
  263. */
  264. require_once 'Zend/Db/Profiler/Exception.php';
  265. throw new Zend_Db_Profiler_Exception("Profiler has no query with handle '$queryId'.");
  266. }
  267. $qp = $this->_queryProfiles[$queryId];
  268. // Ensure that the query profile has not already ended
  269. if ($qp->hasEnded()) {
  270. /**
  271. * @see Zend_Db_Profiler_Exception
  272. */
  273. require_once 'Zend/Db/Profiler/Exception.php';
  274. throw new Zend_Db_Profiler_Exception("Query with profiler handle '$queryId' has already ended.");
  275. }
  276. // End the query profile so that the elapsed time can be calculated.
  277. $qp->end();
  278. /**
  279. * If filtering by elapsed time is enabled, only keep the profile if
  280. * it ran for the minimum time.
  281. */
  282. if (null !== $this->_filterElapsedSecs && $qp->getElapsedSecs() < $this->_filterElapsedSecs) {
  283. unset($this->_queryProfiles[$queryId]);
  284. return;
  285. }
  286. /**
  287. * If filtering by query type is enabled, only keep the query if
  288. * it was one of the allowed types.
  289. */
  290. if (null !== $this->_filterTypes && !($qp->getQueryType() & $this->_filterTypes)) {
  291. unset($this->_queryProfiles[$queryId]);
  292. return;
  293. }
  294. }
  295. /**
  296. * Get a profile for a query. Pass it the same handle that was returned
  297. * by queryStart() and it will return a Zend_Db_Profiler_Query object.
  298. *
  299. * @param integer $queryId
  300. * @throws Zend_Db_Profiler_Exception
  301. * @return Zend_Db_Profiler_Query
  302. */
  303. public function getQueryProfile($queryId)
  304. {
  305. if (!array_key_exists($queryId, $this->_queryProfiles)) {
  306. /**
  307. * @see Zend_Db_Profiler_Exception
  308. */
  309. require_once 'Zend/Db/Profiler/Exception.php';
  310. throw new Zend_Db_Profiler_Exception("Query handle '$queryId' not found in profiler log.");
  311. }
  312. return $this->_queryProfiles[$queryId];
  313. }
  314. /**
  315. * Get an array of query profiles (Zend_Db_Profiler_Query objects). If $queryType
  316. * is set to one of the Zend_Db_Profiler::* constants then only queries of that
  317. * type will be returned. Normally, queries that have not yet ended will
  318. * not be returned unless $showUnfinished is set to True. If no
  319. * queries were found, False is returned. The returned array is indexed by the query
  320. * profile handles.
  321. *
  322. * @param integer $queryType
  323. * @param boolean $showUnfinished
  324. * @return array|false
  325. */
  326. public function getQueryProfiles($queryType = null, $showUnfinished = false)
  327. {
  328. $queryProfiles = array();
  329. foreach ($this->_queryProfiles as $key => $qp) {
  330. if ($queryType === null) {
  331. $condition = true;
  332. } else {
  333. $condition = ($qp->getQueryType() & $queryType);
  334. }
  335. if (($qp->hasEnded() || $showUnfinished) && $condition) {
  336. $queryProfiles[$key] = $qp;
  337. }
  338. }
  339. if (empty($queryProfiles)) {
  340. $queryProfiles = false;
  341. }
  342. return $queryProfiles;
  343. }
  344. /**
  345. * Get the total elapsed time (in seconds) of all of the profiled queries.
  346. * Only queries that have ended will be counted. If $queryType is set to
  347. * one or more of the Zend_Db_Profiler::* constants, the elapsed time will be calculated
  348. * only for queries of the given type(s).
  349. *
  350. * @param integer $queryType OPTIONAL
  351. * @return float
  352. */
  353. public function getTotalElapsedSecs($queryType = null)
  354. {
  355. $elapsedSecs = 0;
  356. foreach ($this->_queryProfiles as $key => $qp) {
  357. if (null === $queryType) {
  358. $condition = true;
  359. } else {
  360. $condition = ($qp->getQueryType() & $queryType);
  361. }
  362. if (($qp->hasEnded()) && $condition) {
  363. $elapsedSecs += $qp->getElapsedSecs();
  364. }
  365. }
  366. return $elapsedSecs;
  367. }
  368. /**
  369. * Get the total number of queries that have been profiled. Only queries that have ended will
  370. * be counted. If $queryType is set to one of the Zend_Db_Profiler::* constants, only queries of
  371. * that type will be counted.
  372. *
  373. * @param integer $queryType OPTIONAL
  374. * @return integer
  375. */
  376. public function getTotalNumQueries($queryType = null)
  377. {
  378. if (null === $queryType) {
  379. return count($this->_queryProfiles);
  380. }
  381. $numQueries = 0;
  382. foreach ($this->_queryProfiles as $qp) {
  383. if ($qp->hasEnded() && ($qp->getQueryType() & $queryType)) {
  384. $numQueries++;
  385. }
  386. }
  387. return $numQueries;
  388. }
  389. /**
  390. * Get the Zend_Db_Profiler_Query object for the last query that was run, regardless if it has
  391. * ended or not. If the query has not ended, its end time will be null. If no queries have
  392. * been profiled, false is returned.
  393. *
  394. * @return Zend_Db_Profiler_Query|false
  395. */
  396. public function getLastQueryProfile()
  397. {
  398. if (empty($this->_queryProfiles)) {
  399. return false;
  400. }
  401. end($this->_queryProfiles);
  402. return current($this->_queryProfiles);
  403. }
  404. }