function Analyze($query) { static $connected = false; if (!$connected) { $connected = true; $mysqlLink = Oops_Sql::Connect(); } $this->_trace = false; $this->_worktime = 0; list($t, $m) = explode(' ', microtime()); $start = (double) $t + $m; $r = Oops_Sql::Query($query); if (mysql_errno($mysqlLink)) { $this->_Log($query, 'mysqlerror'); return; } list($t, $m) = explode(' ', microtime()); $end = (double) $t + $m; $this->_worktime = $end - $start; if ($this->maxtime && $this->_worktime > $this->maxtime) { $this->_Log($query, 'querytime'); } if ($this->preg && preg_match($this->preg, $query)) { $this->_Log($query, 'preg'); } if (!preg_match('/\\s*select\\s+/i', $query)) { return $r; } if ($this->temporary || $this->filesort || $this->all || $this->maxrows || $this->registerKeys) { $tableKeys = array(); $rex = Oops_Sql::Query("Explain {$query}"); if (mysql_errno($mysqlLink)) { return $r; } $reasons = array(); while (($row = mysql_fetch_assoc($rex)) !== false) { if ($this->temporary && strpos($row['Extra'], 'temporary') !== false) { $reasons[] = 'temporary'; } if ($this->filesort && strpos($row['Extra'], 'filesort') !== false) { $reasons[] = 'filesort'; } if ($this->all && strtoupper($row['type']) == 'ALL') { $reasons[] = 'ALL'; } if ($this->maxrows && $row['rows'] > $this->maxrows) { $reasons[] = 'manyrows'; } if ($this->registerKeys && $row['table'] && substr($row['table'], 0, 6) != '<union') { $tableid = $row['table']; if (preg_match("/FROM\\s.*([\\w\\`\\.]+)\\s+AS\\s+{$tableid}\\b/siU", $query, $match) || preg_match("/FROM\\s.*([\\w\\`]+\\.\\`?{$tableid}\\b\\`?)/siU", $query, $match)) { $tbl = str_replace('`', '', $match[1]); } else { $tbl = DATABASE_NAME . '.' . $tableid; } if (!strpos($tbl, '.', 1)) { $tbl = DATABASE_NAME . '.' . $tbl; } $tableKeys[$tbl] = $row['key']; if (!strlen($row['key'])) { if (!strlen($row['possible_keys'])) { $reasons[] = 'nopossiblekeys'; } else { $reasons[] = 'nokeys'; } } } } if (sizeof($reasons)) { $reasons = array_unique($reasons); for ($i = 0, $c = sizeof($reasons); $i < $c; $i++) { $this->_Log($query, $reasons[$i]); } } if ($this->registerKeys) { foreach ($tableKeys as $t => $k) { mysql_query("INSERT IGNORE INTO moscow_service.querriesLogKeys (tbl,k) values ('{$t}','{$k}')"); } } } return $r; }
public static function Escape($s) { if (!isset(self::$_link)) { Oops_Sql::Connect(); } return mysql_real_escape_string($s, self::$_link); }