function get_sql_log_html($db_obj, $log_name) { $log = ''; foreach ($db_obj->dbg as $i => $dbg) { $id = "sql_{$i}_" . mt_rand(); $sql = short_query($dbg['sql'], true); $time = sprintf('%.4f', $dbg['time']); $perc = @sprintf('[%2d]', $dbg['time'] * 100 / $db_obj->sql_timetotal); $info = !empty($dbg['info']) ? $dbg['info'] . ' [' . $dbg['src'] . ']' : $dbg['src']; $log .= '' . '<div class="sqlLogRow" title="' . $info . '">' . '<span style="letter-spacing: -1px;">' . $time . ' </span>' . '<span title="Copy to clipboard" onclick="$.copyToClipboard( $(\'#' . $id . '\').text() );" style="color: gray; letter-spacing: -1px;">' . $perc . '</span>' . ' ' . '<span style="letter-spacing: 0px;" id="' . $id . '">' . $sql . '</span>' . '<span style="color: gray"> # ' . $info . ' </span>' . '</div>' . "\n"; } return ' <div class="sqlLogTitle">' . $log_name . '</div> ' . $log . ' '; }
function debug($mode, $cur_query = null) { if (!$this->dbg_enabled) { return; } $id =& $this->dbg_id; $dbg =& $this->dbg[$id]; if ($mode == 'start') { $this->sql_starttime = utime(); $dbg['sql'] = isset($cur_query) ? short_query($cur_query) : short_query($this->cur_query); $dbg['src'] = $this->debug_find_source(); $dbg['file'] = $this->debug_find_source('file'); $dbg['line'] = $this->debug_find_source('line'); $dbg['time'] = ''; } else { if ($mode == 'stop') { $this->cur_query_time = utime() - $this->sql_starttime; $this->sql_timetotal += $this->cur_query_time; $dbg['time'] = $this->cur_query_time; $id++; } } }
/** * Explain queries (based on code from phpBB3) */ function explain($mode, $html_table = '', $row = '') { $query = str_compact($this->cur_query); // remove comments $query = preg_replace('#(\\s*)(/\\*)(.*)(\\*/)(\\s*)#', '', $query); switch ($mode) { case 'start': $this->explain_hold = ''; // TODO: добавить поддержку многотабличных запросов if (preg_match('#UPDATE ([a-z0-9_]+).*?WHERE(.*)/#', $query, $m)) { $query = "SELECT * FROM {$m['1']} WHERE {$m['2']}"; } else { if (preg_match('#DELETE FROM ([a-z0-9_]+).*?WHERE(.*)#s', $query, $m)) { $query = "SELECT * FROM {$m['1']} WHERE {$m['2']}"; } } if (preg_match('#^SELECT#', $query)) { $html_table = false; if ($result = @mysql_query("EXPLAIN {$query}", $this->link)) { while ($row = @mysql_fetch_assoc($result)) { $html_table = $this->explain('add_explain_row', $html_table, $row); } } if ($html_table) { $this->explain_hold .= '</table>'; } } break; case 'stop': if (!$this->explain_hold) { break; } $id = $this->dbg_id - 1; $htid = 'expl-' . intval($this->link) . '-' . $id; $dbg = $this->dbg[$id]; $this->explain_out .= ' <table width="98%" cellpadding="0" cellspacing="0" class="bodyline row2 bCenter" style="border-bottom: 0px;"> <tr> <th style="height: 22px; cursor: pointer;" align="left"> ' . $dbg['src'] . ' [' . sprintf('%.4f', $dbg['time']) . ' s] <i>' . $dbg['info'] . '</i></th> <th style="height: 22px; cursor: pointer;" align="right" title="Copy to clipboard" onclick="$.copyToClipboard( $(\'#' . $htid . '\').text() );">' . "{$this->db_server}.{$this->selected_db}" . ' :: Query #' . ($this->num_queries + 1) . ' </th> </tr> <tr><td colspan="2">' . $this->explain_hold . '</td></tr> </table> <div class="sqlLog"><div id="' . $htid . '" class="sqlLogRow sqlExplain" style="padding: 0px;">' . short_query($dbg['sql'], true) . ' </div></div> <br />'; break; case 'add_explain_row': if (!$html_table && $row) { $html_table = true; $this->explain_hold .= '<table width="100%" cellpadding="3" cellspacing="1" class="bodyline" style="border-width: 0;"><tr>'; foreach (array_keys($row) as $val) { $this->explain_hold .= '<td class="row3 gensmall" align="center"><b>' . $val . '</b></td>'; } $this->explain_hold .= '</tr>'; } $this->explain_hold .= '<tr>'; foreach (array_values($row) as $i => $val) { $class = !($i % 2) ? 'row1' : 'row2'; $this->explain_hold .= '<td class="' . $class . ' gen">' . str_replace(array("{$this->selected_db}.", ',', ';'), array('', ', ', ';<br />'), $val) . '</td>'; } $this->explain_hold .= '</tr>'; return $html_table; break; case 'display': echo '<a name="explain"></a><div class="med">' . $this->explain_out . '</div>'; break; } }