00001 <?php
00031 class Zend_Db_Profiler
00032 {
00033
00037 const CONNECT = 1;
00038
00042 const QUERY = 2;
00043
00047 const INSERT = 4;
00048
00053 const UPDATE = 8;
00054
00059 const DELETE = 16;
00060
00064 const SELECT = 32;
00065
00069 const TRANSACTION = 64;
00070
00071
00077 protected $_queryProfiles = array();
00078
00085 protected $_enabled = false;
00086
00095 protected $_filterElapsedSecs = null;
00096
00106 protected $_filterTypes = null;
00107
00115 public function __construct($enabled = false)
00116 {
00117 $this->setEnabled($enabled);
00118 }
00119
00127 public function setEnabled($enable)
00128 {
00129 $this->_enabled = (boolean) $enable;
00130
00131 return $this;
00132 }
00133
00140 public function getEnabled()
00141 {
00142 return $this->_enabled;
00143 }
00144
00154 public function setFilterElapsedSecs($minimumSeconds = null)
00155 {
00156 if (null === $minimumSeconds) {
00157 $this->_filterElapsedSecs = null;
00158 } else {
00159 $this->_filterElapsedSecs = (integer) $minimumSeconds;
00160 }
00161
00162 return $this;
00163 }
00164
00171 public function getFilterElapsedSecs()
00172 {
00173 return $this->_filterElapsedSecs;
00174 }
00175
00185 public function setFilterQueryType($queryTypes = null)
00186 {
00187 $this->_filterTypes = $queryTypes;
00188
00189 return $this;
00190 }
00191
00199 public function getFilterQueryType()
00200 {
00201 return $this->_filterTypes;
00202 }
00203
00211 public function clear()
00212 {
00213 $this->_queryProfiles = array();
00214
00215 return $this;
00216 }
00217
00222 public function queryClone(Zend_Db_Profiler_Query $query)
00223 {
00224 $this->_queryProfiles[] = clone $query;
00225
00226 end($this->_queryProfiles);
00227
00228 return key($this->_queryProfiles);
00229 }
00230
00242 public function queryStart($queryText, $queryType = null)
00243 {
00244 if (!$this->_enabled) {
00245 return null;
00246 }
00247
00248
00249 if (null === $queryType) {
00250 switch (strtolower(substr(ltrim($queryText), 0, 6))) {
00251 case 'insert':
00252 $queryType = self::INSERT;
00253 break;
00254 case 'update':
00255 $queryType = self::UPDATE;
00256 break;
00257 case 'delete':
00258 $queryType = self::DELETE;
00259 break;
00260 case 'select':
00261 $queryType = self::SELECT;
00262 break;
00263 default:
00264 $queryType = self::QUERY;
00265 break;
00266 }
00267 }
00268
00272 require_once 'Zend/Db/Profiler/Query.php';
00273 $this->_queryProfiles[] = new Zend_Db_Profiler_Query($queryText, $queryType);
00274
00275 end($this->_queryProfiles);
00276
00277 return key($this->_queryProfiles);
00278 }
00279
00288 public function queryEnd($queryId)
00289 {
00290
00291 if (!$this->_enabled) {
00292 return;
00293 }
00294
00295
00296 if (!isset($this->_queryProfiles[$queryId])) {
00300 require_once 'Zend/Db/Profiler/Exception.php';
00301 throw new Zend_Db_Profiler_Exception("Profiler has no query with handle '$queryId'.");
00302 }
00303
00304 $qp = $this->_queryProfiles[$queryId];
00305
00306
00307 if ($qp->hasEnded()) {
00311 require_once 'Zend/Db/Profiler/Exception.php';
00312 throw new Zend_Db_Profiler_Exception("Query with profiler handle '$queryId' has already ended.");
00313 }
00314
00315
00316 $qp->end();
00317
00322 if (null !== $this->_filterElapsedSecs && $qp->getElapsedSecs() < $this->_filterElapsedSecs) {
00323 unset($this->_queryProfiles[$queryId]);
00324 return;
00325 }
00326
00331 if (null !== $this->_filterTypes && !($qp->getQueryType() & $this->_filterTypes)) {
00332 unset($this->_queryProfiles[$queryId]);
00333 return;
00334 }
00335 }
00336
00345 public function getQueryProfile($queryId)
00346 {
00347 if (!array_key_exists($queryId, $this->_queryProfiles)) {
00351 require_once 'Zend/Db/Profiler/Exception.php';
00352 throw new Zend_Db_Profiler_Exception("Query handle '$queryId' not found in profiler log.");
00353 }
00354
00355 return $this->_queryProfiles[$queryId];
00356 }
00357
00370 public function getQueryProfiles($queryType = null, $showUnfinished = false)
00371 {
00372 $queryProfiles = array();
00373 foreach ($this->_queryProfiles as $key => $qp) {
00374 if ($queryType === null) {
00375 $condition = true;
00376 } else {
00377 $condition = ($qp->getQueryType() & $queryType);
00378 }
00379
00380 if (($qp->hasEnded() || $showUnfinished) && $condition) {
00381 $queryProfiles[$key] = $qp;
00382 }
00383 }
00384
00385 if (empty($queryProfiles)) {
00386 $queryProfiles = false;
00387 }
00388
00389 return $queryProfiles;
00390 }
00391
00401 public function getTotalElapsedSecs($queryType = null)
00402 {
00403 $elapsedSecs = 0;
00404 foreach ($this->_queryProfiles as $key => $qp) {
00405 if (null === $queryType) {
00406 $condition = true;
00407 } else {
00408 $condition = ($qp->getQueryType() & $queryType);
00409 }
00410 if (($qp->hasEnded()) && $condition) {
00411 $elapsedSecs += $qp->getElapsedSecs();
00412 }
00413 }
00414 return $elapsedSecs;
00415 }
00416
00425 public function getTotalNumQueries($queryType = null)
00426 {
00427 if (null === $queryType) {
00428 return count($this->_queryProfiles);
00429 }
00430
00431 $numQueries = 0;
00432 foreach ($this->_queryProfiles as $qp) {
00433 if ($qp->hasEnded() && ($qp->getQueryType() & $queryType)) {
00434 $numQueries++;
00435 }
00436 }
00437
00438 return $numQueries;
00439 }
00440
00448 public function getLastQueryProfile()
00449 {
00450 if (empty($this->_queryProfiles)) {
00451 return false;
00452 }
00453
00454 end($this->_queryProfiles);
00455
00456 return current($this->_queryProfiles);
00457 }
00458
00459 }
00460