Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes
Zend_Db_Profiler Class Reference
Inheritance diagram for Zend_Db_Profiler:
Profiler Profiler

Public Member Functions

 __construct ($enabled=false)
 
 setEnabled ($enable)
 
 getEnabled ()
 
 setFilterElapsedSecs ($minimumSeconds=null)
 
 getFilterElapsedSecs ()
 
 setFilterQueryType ($queryTypes=null)
 
 getFilterQueryType ()
 
 clear ()
 
 queryClone (Zend_Db_Profiler_Query $query)
 
 queryStart ($queryText, $queryType=null)
 
 queryEnd ($queryId)
 
 getQueryProfile ($queryId)
 
 getQueryProfiles ($queryType=null, $showUnfinished=false)
 
 getTotalElapsedSecs ($queryType=null)
 
 getTotalNumQueries ($queryType=null)
 
 getLastQueryProfile ()
 

Data Fields

const CONNECT = 1
 
const QUERY = 2
 
const INSERT = 4
 
const UPDATE = 8
 
const DELETE = 16
 
const SELECT = 32
 
const TRANSACTION = 64
 
const STORED = 'stored'
 
const IGNORED = 'ignored'
 

Protected Attributes

 $_queryProfiles = array()
 
 $_enabled = false
 
 $_filterElapsedSecs = null
 
 $_filterTypes = null
 

Detailed Description

Definition at line 31 of file Profiler.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $enabled = false)

Class constructor. The profiler is disabled by default unless it is specifically enabled by passing in $enabled here or calling setEnabled().

Parameters
boolean$enabled
Returns
void

Definition at line 124 of file Profiler.php.

125  {
126  $this->setEnabled($enabled);
127  }
setEnabled($enable)
Definition: Profiler.php:136

Member Function Documentation

◆ clear()

clear ( )

Clears the history of any past query profiles. This is relentless and will even clear queries that were started and may not have been marked as ended.

Returns
Zend_Db_Profiler Provides a fluent interface

Definition at line 220 of file Profiler.php.

221  {
222  $this->_queryProfiles = array();
223 
224  return $this;
225  }

◆ getEnabled()

getEnabled ( )

Get the current state of enable. If True is returned, the profiler is enabled.

Returns
boolean

Definition at line 149 of file Profiler.php.

150  {
151  return $this->_enabled;
152  }

◆ getFilterElapsedSecs()

getFilterElapsedSecs ( )

Returns the minimum number of seconds for saving query profiles, or null if query profiles are saved regardless of elapsed time.

Returns
integer|null

Definition at line 180 of file Profiler.php.

181  {
183  }

◆ getFilterQueryType()

getFilterQueryType ( )

Returns the types of query profiles saved, or null if queries are saved regardless of their types.

Returns
integer|null
See also
Zend_Db_Profiler::setFilterQueryType()

Definition at line 208 of file Profiler.php.

209  {
210  return $this->_filterTypes;
211  }

◆ getLastQueryProfile()

getLastQueryProfile ( )

Get the Zend_Db_Profiler_Query object for the last query that was run, regardless if it has ended or not. If the query has not ended, its end time will be null. If no queries have been profiled, false is returned.

Returns
Zend_Db_Profiler_Query|false

Definition at line 461 of file Profiler.php.

462  {
463  if (empty($this->_queryProfiles)) {
464  return false;
465  }
466 
467  end($this->_queryProfiles);
468 
469  return current($this->_queryProfiles);
470  }

◆ getQueryProfile()

getQueryProfile (   $queryId)

Get a profile for a query. Pass it the same handle that was returned by queryStart() and it will return a Zend_Db_Profiler_Query object.

Parameters
integer$queryId
Exceptions
Zend_Db_Profiler_Exception
Returns
Zend_Db_Profiler_Query
See also
Zend_Db_Profiler_Exception

Definition at line 358 of file Profiler.php.

359  {
360  if (!array_key_exists($queryId, $this->_queryProfiles)) {
364  #require_once 'Zend/Db/Profiler/Exception.php';
365  throw new Zend_Db_Profiler_Exception("Query handle '$queryId' not found in profiler log.");
366  }
367 
368  return $this->_queryProfiles[$queryId];
369  }

◆ getQueryProfiles()

getQueryProfiles (   $queryType = null,
  $showUnfinished = false 
)

Get an array of query profiles (Zend_Db_Profiler_Query objects). If $queryType is set to one of the Zend_Db_Profiler::* constants then only queries of that type will be returned. Normally, queries that have not yet ended will not be returned unless $showUnfinished is set to True. If no queries were found, False is returned. The returned array is indexed by the query profile handles.

Parameters
integer$queryType
boolean$showUnfinished
Returns
array|false

Definition at line 383 of file Profiler.php.

384  {
385  $queryProfiles = array();
386  foreach ($this->_queryProfiles as $key => $qp) {
387  if ($queryType === null) {
388  $condition = true;
389  } else {
390  $condition = ($qp->getQueryType() & $queryType);
391  }
392 
393  if (($qp->hasEnded() || $showUnfinished) && $condition) {
394  $queryProfiles[$key] = $qp;
395  }
396  }
397 
398  if (empty($queryProfiles)) {
399  $queryProfiles = false;
400  }
401 
402  return $queryProfiles;
403  }

◆ getTotalElapsedSecs()

getTotalElapsedSecs (   $queryType = null)

Get the total elapsed time (in seconds) of all of the profiled queries. Only queries that have ended will be counted. If $queryType is set to one or more of the Zend_Db_Profiler::* constants, the elapsed time will be calculated only for queries of the given type(s).

Parameters
integer$queryTypeOPTIONAL
Returns
float

Definition at line 414 of file Profiler.php.

415  {
416  $elapsedSecs = 0;
417  foreach ($this->_queryProfiles as $key => $qp) {
418  if (null === $queryType) {
419  $condition = true;
420  } else {
421  $condition = ($qp->getQueryType() & $queryType);
422  }
423  if (($qp->hasEnded()) && $condition) {
424  $elapsedSecs += $qp->getElapsedSecs();
425  }
426  }
427  return $elapsedSecs;
428  }

◆ getTotalNumQueries()

getTotalNumQueries (   $queryType = null)

Get the total number of queries that have been profiled. Only queries that have ended will be counted. If $queryType is set to one of the Zend_Db_Profiler::* constants, only queries of that type will be counted.

Parameters
integer$queryTypeOPTIONAL
Returns
integer

Definition at line 438 of file Profiler.php.

439  {
440  if (null === $queryType) {
441  return count($this->_queryProfiles);
442  }
443 
444  $numQueries = 0;
445  foreach ($this->_queryProfiles as $qp) {
446  if ($qp->hasEnded() && ($qp->getQueryType() & $queryType)) {
447  $numQueries++;
448  }
449  }
450 
451  return $numQueries;
452  }

◆ queryClone()

queryClone ( Zend_Db_Profiler_Query  $query)

Clone a profiler query

Parameters
Zend_Db_Profiler_Query$query
Returns
integer or null

Definition at line 233 of file Profiler.php.

234  {
235  $this->_queryProfiles[] = clone $query;
236 
237  end($this->_queryProfiles);
238 
239  return key($this->_queryProfiles);
240  }

◆ queryEnd()

queryEnd (   $queryId)

Ends a query. Pass it the handle that was returned by queryStart(). This will mark the query as ended and save the time.

Parameters
integer$queryId
Exceptions
Zend_Db_Profiler_Exception
Returns
string Inform that a query is stored or ignored.
See also
Zend_Db_Profiler_Exception
Zend_Db_Profiler_Exception

If filtering by elapsed time is enabled, only keep the profile if it ran for the minimum time.

If filtering by query type is enabled, only keep the query if it was one of the allowed types.

Definition at line 299 of file Profiler.php.

300  {
301  // Don't do anything if the Zend_Db_Profiler is not enabled.
302  if (!$this->_enabled) {
303  return self::IGNORED;
304  }
305 
306  // Check for a valid query handle.
307  if (!isset($this->_queryProfiles[$queryId])) {
311  #require_once 'Zend/Db/Profiler/Exception.php';
312  throw new Zend_Db_Profiler_Exception("Profiler has no query with handle '$queryId'.");
313  }
314 
315  $qp = $this->_queryProfiles[$queryId];
316 
317  // Ensure that the query profile has not already ended
318  if ($qp->hasEnded()) {
322  #require_once 'Zend/Db/Profiler/Exception.php';
323  throw new Zend_Db_Profiler_Exception("Query with profiler handle '$queryId' has already ended.");
324  }
325 
326  // End the query profile so that the elapsed time can be calculated.
327  $qp->end();
328 
333  if (null !== $this->_filterElapsedSecs && $qp->getElapsedSecs() < $this->_filterElapsedSecs) {
334  unset($this->_queryProfiles[$queryId]);
335  return self::IGNORED;
336  }
337 
342  if (null !== $this->_filterTypes && !($qp->getQueryType() & $this->_filterTypes)) {
343  unset($this->_queryProfiles[$queryId]);
344  return self::IGNORED;
345  }
346 
347  return self::STORED;
348  }

◆ queryStart()

queryStart (   $queryText,
  $queryType = null 
)

Starts a query. Creates a new query profile object (Zend_Db_Profiler_Query) and returns the "query profiler handle". Run the query, then call queryEnd() and pass it this handle to make the query as ended and record the time. If the profiler is not enabled, this takes no action and immediately returns null.

Parameters
string$queryTextSQL statement
integer$queryTypeOPTIONAL Type of query, one of the Zend_Db_Profiler::* constants
Returns
integer|null
See also
Zend_Db_Profiler_Query

Definition at line 253 of file Profiler.php.

254  {
255  if (!$this->_enabled) {
256  return null;
257  }
258 
259  // make sure we have a query type
260  if (null === $queryType) {
261  switch (strtolower(substr(ltrim($queryText), 0, 6))) {
262  case 'insert':
263  $queryType = self::INSERT;
264  break;
265  case 'update':
266  $queryType = self::UPDATE;
267  break;
268  case 'delete':
269  $queryType = self::DELETE;
270  break;
271  case 'select':
272  $queryType = self::SELECT;
273  break;
274  default:
275  $queryType = self::QUERY;
276  break;
277  }
278  }
279 
283  #require_once 'Zend/Db/Profiler/Query.php';
284  $this->_queryProfiles[] = new Zend_Db_Profiler_Query($queryText, $queryType);
285 
286  end($this->_queryProfiles);
287 
288  return key($this->_queryProfiles);
289  }

◆ setEnabled()

setEnabled (   $enable)

Enable or disable the profiler. If $enable is false, the profiler is disabled and will not log any queries sent to it.

Parameters
boolean$enable
Returns
Zend_Db_Profiler Provides a fluent interface

Definition at line 136 of file Profiler.php.

137  {
138  $this->_enabled = (boolean) $enable;
139 
140  return $this;
141  }

◆ setFilterElapsedSecs()

setFilterElapsedSecs (   $minimumSeconds = null)

Sets a minimum number of seconds for saving query profiles. If this is set, only those queries whose elapsed time is equal or greater than $minimumSeconds will be saved. To save all queries regardless of elapsed time, set $minimumSeconds to null.

Parameters
integer$minimumSecondsOPTIONAL
Returns
Zend_Db_Profiler Provides a fluent interface

Definition at line 163 of file Profiler.php.

164  {
165  if (null === $minimumSeconds) {
166  $this->_filterElapsedSecs = null;
167  } else {
168  $this->_filterElapsedSecs = (integer) $minimumSeconds;
169  }
170 
171  return $this;
172  }

◆ setFilterQueryType()

setFilterQueryType (   $queryTypes = null)

Sets the types of query profiles to save. Set $queryType to one of the Zend_Db_Profiler::* constants to only save profiles for that type of query. To save more than one type, logical OR them together. To save all queries regardless of type, set $queryType to null.

Parameters
integer$queryTypesOPTIONAL
Returns
Zend_Db_Profiler Provides a fluent interface

Definition at line 194 of file Profiler.php.

195  {
196  $this->_filterTypes = $queryTypes;
197 
198  return $this;
199  }

Field Documentation

◆ $_enabled

$_enabled = false
protected

Definition at line 94 of file Profiler.php.

◆ $_filterElapsedSecs

$_filterElapsedSecs = null
protected

Definition at line 104 of file Profiler.php.

◆ $_filterTypes

$_filterTypes = null
protected

Definition at line 115 of file Profiler.php.

◆ $_queryProfiles

$_queryProfiles = array()
protected

Definition at line 86 of file Profiler.php.

◆ CONNECT

const CONNECT = 1

A connection operation or selecting a database.

Definition at line 37 of file Profiler.php.

◆ DELETE

const DELETE = 16

An operation related to deleting data in the database, such as SQL's DELETE.

Definition at line 59 of file Profiler.php.

◆ IGNORED

const IGNORED = 'ignored'

Inform that a query is ignored (in case of filtering)

Definition at line 79 of file Profiler.php.

◆ INSERT

const INSERT = 4

Adding new data to the database, such as SQL's INSERT.

Definition at line 47 of file Profiler.php.

◆ QUERY

const QUERY = 2

Any general database query that does not fit into the other constants.

Definition at line 42 of file Profiler.php.

◆ SELECT

const SELECT = 32

Retrieving information from the database, such as SQL's SELECT.

Definition at line 64 of file Profiler.php.

◆ STORED

const STORED = 'stored'

Inform that a query is stored (in case of filtering)

Definition at line 74 of file Profiler.php.

◆ TRANSACTION

const TRANSACTION = 64

Transactional operation, such as start transaction, commit, or rollback.

Definition at line 69 of file Profiler.php.

◆ UPDATE

const UPDATE = 8

Updating existing information in the database, such as SQL's UPDATE.

Definition at line 53 of file Profiler.php.


The documentation for this class was generated from the following file: