Exemplo n.º 1
0
 public function __destruct()
 {
     // Save all previous DB queries to analyzer if enabled (for debug and benchmarks)
     if (Settings::get('analyze_db_queries')) {
         QueryAnalyzer::getInstance()->store();
     }
 }
Exemplo n.º 2
0
 /**
  * Common DB query
  * @param string $q
  * @param bool $protect
  * @param bool $return_inserted_id
  * @return PDOStatement | int
  */
 public function sql_query($q, $protect = false, $return_inserted_id = false)
 {
     if (!$this->pdo_db) {
         $this->connect();
     }
     $q = trim($q);
     if ($protect) {
         $this->sqlQueryCheck($q);
     }
     // Set query start time if debug is enabled or if we analyze queries
     if (MODE === 'site' && (Settings::get('debug_panel') || Settings::get('analyze_db_queries'))) {
         $ts = microtime(1);
     }
     /** @var PDOStatement $pdo_query */
     $pdo_query = $this->pdo_db->query($q);
     if (!$pdo_query) {
         $err = $this->pdo_db->errorInfo()[2];
         trigger_error($err . '<br><br>Query:<br><br>' . $q);
     }
     if (isset($ts)) {
         // Start time exists - so we save query to analyze
         $tt = microtime(1) - $ts;
         if (Settings::get('debug_panel')) {
             Stats::addQuery(['query' => $q, 'backtrace' => debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT), 'time' => $tt]);
         }
         if (Settings::get('analyze_db_queries')) {
             QueryAnalyzer::getInstance()->addQuery($q, $tt);
         }
     }
     return $return_inserted_id ? $this->pdo_db->lastInsertId() : $pdo_query;
 }