Esempio n. 1
0
 /**
  * @param string $sql
  * @param array|null $params
  * @param array|null $types
  * @throws \Exception
  */
 public function startQuery($sql, array $params = null, array $types = null)
 {
     if ($this->backtraceEnabled) {
         if (class_exists('\\DumpBacktrace') === false) {
             throw new \Exception('You need to add steevanb/php-backtrace ^1.1 in your dependencies to activate query backtrace. ' . 'Example with composer : composer require steevanb/php-backtrace ^1.1');
         }
         $backtrace = \DumpBacktrace::getBacktraces();
     } else {
         $backtrace = null;
     }
     $this->start = microtime(true);
     $this->queries[++$this->currentQueryIndex] = ['sql' => $sql, 'params' => $params, 'types' => $types, 'time' => 0, 'backtrace' => $backtrace];
 }
 /**
  * @return array
  */
 public function getQueries()
 {
     static $return = false;
     if ($return === false) {
         $return = [];
         foreach ($this->data['queries'] as $index => $query) {
             if (array_key_exists($query['sql'], $return) === false) {
                 $return[$query['sql']] = ['queryTime' => 0, 'queryTimePercent' => 0, 'data' => [], 'lazyLoadedEntities' => [], 'hydrationTime' => 0, 'hydrationTimePercent' => 0, 'rows' => []];
             }
             $return[$query['sql']]['queryTime'] += $query['time'] * 1000;
             $return[$query['sql']]['data'][] = ['params' => $query['params']];
             $return[$query['sql']]['backtraces'][$index] = $query['backtrace'] === null ? null : \DumpBacktrace::getDump($query['backtrace']);
             $return[$query['sql']]['rows'][$index] = array_key_exists($index, $this->data['rows']) ? $this->data['rows'][$index] : [];
             foreach ($this->data['lazyLoadedEntities'] as $lazyLoadedEntity) {
                 if ($lazyLoadedEntity['queryIndex'] === $index) {
                     if (array_key_exists($index, $return[$query['sql']]['lazyLoadedEntities']) === false) {
                         $return[$query['sql']]['lazyLoadedEntities'][$index] = [];
                     }
                     $return[$query['sql']]['lazyLoadedEntities'][$index][] = $lazyLoadedEntity;
                 }
             }
             foreach ($this->data['hydrationTimes'] as $hydrationTimes) {
                 foreach ($hydrationTimes as $hydrationTime) {
                     if ($hydrationTime['queryIndex'] === $index) {
                         $return[$query['sql']]['hydrationTime'] += $hydrationTime['time'];
                     }
                 }
             }
         }
         foreach ($return as &$queryData) {
             $queryData['queryTimePercent'] = round($queryData['queryTime'] * 100 / ($queryData['hydrationTime'] + $queryData['queryTime']));
             $queryData['hydrationTimePercent'] = 100 - $queryData['queryTimePercent'];
         }
         uasort($return, function ($queryA, $queryB) {
             return count($queryA['data']) < count($queryB['data']) ? 1 : -1;
         });
     }
     return $return;
 }