public function __construct(DibiConnection $connection, $type, $sql = NULL)
 {
     $this->connection = $connection;
     $this->type = $type;
     $this->sql = trim($sql);
     $this->time = -microtime(TRUE);
     if ($type === self::QUERY && preg_match('#\\(?\\s*(SELECT|UPDATE|INSERT|DELETE)#iA', $this->sql, $matches)) {
         static $types = array('SELECT' => self::SELECT, 'UPDATE' => self::UPDATE, 'INSERT' => self::INSERT, 'DELETE' => self::DELETE);
         $this->type = $types[strtoupper($matches[1])];
     }
     $rc = new ReflectionClass('dibi');
     $dibiDir = dirname($rc->getFileName()) . DIRECTORY_SEPARATOR;
     foreach (debug_backtrace(FALSE) as $row) {
         if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
             $this->source = array($row['file'], (int) $row['line']);
             break;
         }
     }
     dibi::$elapsedTime = FALSE;
     dibi::$numOfQueries++;
     dibi::$sql = $sql;
 }
Пример #2
0
 /**
  * Executes the SQL query.
  * @param  string           SQL statement.
  * @return DibiResult|int   result set object (if any)
  * @throws DibiException
  */
 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::$sql = $sql;
     if ($res = $this->driver->query($sql)) {
         // intentionally =
         $res = new DibiResult($res, $this->config);
     } else {
         $res = $this->driver->getAffectedRows();
     }
     if (isset($ticket)) {
         $this->profiler->after($ticket, $res);
     }
     return $res;
 }
Пример #3
0
 /**
  * Executes the SQL query.
  * @param  string           SQL statement.
  * @return Result|int   result set object (if any)
  * @throws Exception
  */
 public final function nativeQuery($sql)
 {
     $this->connected || $this->connect();
     \dibi::$sql = $sql;
     $event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : NULL;
     try {
         $res = $this->driver->query($sql);
     } catch (Exception $e) {
         $event && $this->onEvent($event->done($e));
         throw $e;
     }
     if ($res) {
         $res = $this->createResultSet($res);
     } else {
         $res = $this->driver->getAffectedRows();
     }
     $event && $this->onEvent($event->done($res));
     return $res;
 }
Пример #4
0
 /**
  * Executes the SQL query.
  * @param  string           SQL statement.
  * @return DibiResult|int   result set object (if any)
  * @throws DibiException
  */
 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);
     }
     // TODO: move to profiler?
     dibi::$numOfQueries++;
     dibi::$sql = $sql;
     dibi::$elapsedTime = FALSE;
     $time = -microtime(TRUE);
     if ($res = $this->driver->query($sql)) {
         // intentionally =
         $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;
 }
Пример #5
0
 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) {
                 $tmpSql = dibi::$sql;
                 try {
                     $ticket[5] = dibi::dump($connection->setProfiler(NULL)->nativeQuery('EXPLAIN ' . $sql), TRUE);
                 } catch (DibiException $e) {
                 }
                 $connection->setProfiler($this);
                 dibi::$sql = $tmpSql;
             }
             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}|\\");
                 }
                 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");
         }
     }
 }
Пример #6
0
translateArgs($args){$this->connected||$this->connect();return$this->translator->translate($args);}final
function
nativeQuery($sql){$this->connected||$this->connect();if($this->profiler!==NULL){$event=IDibiProfiler::QUERY;if(preg_match('#\s*(SELECT|UPDATE|INSERT|DELETE)#iA',$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::$sql=$sql;if($res=$this->driver->query($sql)){$res=$this->createResultSet($res);}else{$res=$this->driver->getAffectedRows();}if(isset($ticket)){$this->profiler->after($ticket,$res);}return$res;}function
Пример #7
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;
 }