public function done($result = NULL)
 {
     $this->result = $result;
     try {
         $this->count = $result instanceof DibiResult ? count($result) : NULL;
     } catch (DibiException $e) {
         $this->count = NULL;
     }
     $this->time += microtime(TRUE);
     dibi::$elapsedTime = $this->time;
     dibi::$totalTime += $this->time;
     return $this;
 }
Example #2
0
 /**
  * After event notification.
  * @param  int
  * @param  DibiResult
  * @return void
  */
 public function after($ticket, $res = NULL)
 {
     if (!isset(self::$tickets[$ticket])) {
         throw new InvalidArgumentException('Bad ticket number.');
     }
     $ticket =& self::$tickets[$ticket];
     $ticket[3] += microtime(TRUE);
     list($connection, $event, $sql, $time) = $ticket;
     dibi::$elapsedTime = $time;
     dibi::$totalTime += $time;
     if (($event & $this->filter) === 0) {
         return;
     }
     if ($event & self::QUERY) {
         try {
             $ticket[4] = $count = $res instanceof DibiResult ? count($res) : '-';
         } catch (Exception $e) {
             $count = '?';
         }
         if (count(self::$fireTable) < self::$maxQueries) {
             self::$fireTable[] = array(sprintf('%0.3f', $time * 1000), strlen($sql) > self::$maxLength ? substr($sql, 0, self::$maxLength) . '...' : $sql, $count, $connection->getConfig('driver') . '/' . $connection->getConfig('name'));
             if ($this->explainQuery && $event === self::SELECT) {
                 try {
                     $ticket[5] = dibi::dump($connection->setProfiler(NULL)->nativeQuery('EXPLAIN ' . $sql), TRUE);
                 } catch (DibiException $e) {
                 }
                 $connection->setProfiler($this);
             }
             if ($this->useFirebug && !headers_sent()) {
                 header('X-Wf-Protocol-dibi: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
                 header('X-Wf-dibi-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.2.0');
                 header('X-Wf-dibi-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
                 $payload = json_encode(array(array('Type' => 'TABLE', 'Label' => 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)'), self::$fireTable));
                 foreach (str_split($payload, 4990) as $num => $s) {
                     $num++;
                     header("X-Wf-dibi-1-1-d{$num}: |{$s}|\\");
                     // protocol-, structure-, plugin-, message-index
                 }
                 header("X-Wf-dibi-1-1-d{$num}: |{$s}|");
             }
         }
         if ($this->file) {
             $this->writeFile("OK: " . $sql . ($res instanceof DibiResult ? ";\n-- rows: " . $count : '') . "\n-- takes: " . sprintf('%0.3f', $time * 1000) . ' ms' . "\n-- driver: " . $connection->getConfig('driver') . '/' . $connection->getConfig('name') . "\n-- " . date('Y-m-d H:i:s') . "\n\n");
         }
     }
 }
Example #3
0
 public final function nativeQuery($sql)
 {
     $this->connect();
     if ($this->profiler !== NULL) {
         $event = IDibiProfiler::QUERY;
         if (preg_match('#\\s*(SELECT|UPDATE|INSERT|DELETE)#i', $sql, $matches)) {
             static $events = array('SELECT' => IDibiProfiler::SELECT, 'UPDATE' => IDibiProfiler::UPDATE, 'INSERT' => IDibiProfiler::INSERT, 'DELETE' => IDibiProfiler::DELETE);
             $event = $events[strtoupper($matches[1])];
         }
         $ticket = $this->profiler->before($this, $event, $sql);
     }
     dibi::$numOfQueries++;
     dibi::$sql = $sql;
     dibi::$elapsedTime = FALSE;
     $time = -microtime(TRUE);
     if ($res = $this->driver->query($sql)) {
         $res = new DibiResult($res, $this->config);
     } else {
         $res = $this->driver->getAffectedRows();
     }
     $time += microtime(TRUE);
     dibi::$elapsedTime = $time;
     dibi::$totalTime += $time;
     if (isset($ticket)) {
         $this->profiler->after($ticket, $res);
     }
     return $res;
 }
Example #4
0
 /**
  * Executes the SQL query.
  *
  * @param  string           SQL statement.
  * @return DibiResult|NULL  result set object (if any)
  * @throws DibiException
  */
 public final function nativeQuery($sql)
 {
     $this->connect();
     dibi::$numOfQueries++;
     dibi::$sql = $sql;
     dibi::$elapsedTime = FALSE;
     $time = -microtime(TRUE);
     dibi::notify($this, 'beforeQuery', $sql);
     if ($res = $this->driver->query($sql)) {
         // intentionally =
         $res = new DibiResult($res, $this->config);
     }
     $time += microtime(TRUE);
     dibi::$elapsedTime = $time;
     dibi::$totalTime += $time;
     dibi::notify($this, 'afterQuery', $res);
     return $res;
 }