';
foreach ($_SESSION['debug'] as $q => $query_data)
{
// Fix the indentation....
$query_data['q'] = ltrim(str_replace("\r", '', $query_data['q']), "\n");
$query = explode("\n", $query_data['q']);
$min_indent = 0;
foreach ($query as $line)
{
preg_match('/^(\t*)/', $line, $temp);
if (strlen($temp[0]) < $min_indent || $min_indent == 0)
$min_indent = strlen($temp[0]);
}
foreach ($query as $l => $dummy)
$query[$l] = substr($dummy, $min_indent);
$query_data['q'] = implode("\n", $query);
// Make the filenames look a bit better.
if (isset($query_data['f']))
$query_data['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $query_data['f']);
$is_select_query = substr(trim($query_data['q']), 0, 6) == 'SELECT' || substr(trim($query_data['q']), 0, 4) == 'WITH';
if ($is_select_query)
$select = $query_data['q'];
elseif (preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
{
$is_select_query = true;
$select = $matches[1];
}
elseif (preg_match('~^CREATE TEMPORARY TABLE .+?(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
{
$is_select_query = true;
$select = $matches[1];
}
// Temporary tables created in earlier queries are not explainable.
if ($is_select_query)
{
foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp)
if (strpos($select, $tmp) !== false)
{
$is_select_query = false;
break;
}
}
echo '
', nl2br(str_replace("\t", ' ', $smcFunc['htmlspecialchars']($query_data['q']))), '
';
if (!empty($query_data['f']) && !empty($query_data['l']))
echo sprintf($txt['debug_query_in_line'], $query_data['f'], $query_data['l']);
if (isset($query_data['s'], $query_data['t']) && isset($txt['debug_query_which_took_at']))
echo sprintf($txt['debug_query_which_took_at'], round($query_data['t'], 8), round($query_data['s'], 8));
else
echo sprintf($txt['debug_query_which_took'], round($query_data['t'], 8));
echo '
';
// Explain the query.
if ($query_id == $q && $is_select_query)
{
$result = $smcFunc['db_query']('', '
EXPLAIN ' . ($smcFunc['db_title'] === POSTGRE_TITLE ? 'ANALYZE ' : '') . $select,
array(
)
);
if ($result === false)
{
echo '
', $smcFunc['db_error']($db_connection), ' |
';
continue;
}
echo '
';
$row = $smcFunc['db_fetch_assoc']($result);
echo '
' . implode(' |
', array_keys($row)) . ' |
';
$smcFunc['db_data_seek']($result, 0);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
echo '
' . implode(' |
', $row) . ' |
';
}
$smcFunc['db_free_result']($result);
echo '
';
}
}
echo '