Print elapsed time in HTML instead of SQL command comment

This commit is contained in:
Jakub Vrana 2014-03-07 09:33:37 -08:00
parent d9fe03e1b4
commit 6a3ede75f6
10 changed files with 46 additions and 35 deletions

View file

@ -139,10 +139,11 @@ if (isset($_GET["elastic"])) {
if ($data["query"] && !$data["query"]["filtered"]["query"]) {
$data["query"]["filtered"]["query"] = array("match_all" => array());
}
if ($print) {
echo $adminer->selectQuery("$query: " . print_r($data, true));
}
$start = microtime(true);
$search = $this->_conn->query($query, $data);
if ($print) {
echo $adminer->selectQuery("$query: " . print_r($data, true), format_time($start, microtime(true)));
}
if (!$search) {
return false;
}

View file

@ -177,11 +177,12 @@ username.form['auth[driver]'].onchange();
/** Query printed in select before execution
* @param string query to be executed
* @param string elapsed time
* @return string
*/
function selectQuery($query) {
function selectQuery($query, $time) {
global $jush;
return "<p><code class='jush-$jush'>" . h(str_replace("\n", " ", $query)) . "</code>"
return "<p><code class='jush-$jush'>" . h(str_replace("\n", " ", $query)) . "</code> <span class='time'>($time)</span>"
. (support("sql") ? " <a href='" . h(ME) . "sql=" . urlencode($query) . "'>" . lang('Edit') . "</a>" : "")
. "</p>" // </p> - required for IE9 inline edit
;
@ -500,9 +501,10 @@ username.form['auth[driver]'].onchange();
/** Query printed after execution in the message
* @param string executed query
* @param string elapsed time
* @return string
*/
function messageQuery($query) {
function messageQuery($query, $time) {
global $jush;
restart_session();
$history = &get_session("queries");
@ -510,9 +512,10 @@ username.form['auth[driver]'].onchange();
if (strlen($query) > 1e6) {
$query = preg_replace('~[\x80-\xFF]+$~', '', substr($query, 0, 1e6)) . "\n..."; // [\x80-\xFF] - valid UTF-8, \n - can end by one-line comment
}
$history[$_GET["db"]][] = array($query, time()); // not DB - $_GET["db"] is changed in database.inc.php //! respect $_GET["ns"]
$history[$_GET["db"]][] = array($query, time(), $time); // not DB - $_GET["db"] is changed in database.inc.php //! respect $_GET["ns"]
return " <span class='time'>" . @date("H:i:s") . "</span> <a href='#$id' onclick=\"return !toggle('$id');\">" . lang('SQL command') . "</a>" // @ - time zone may be not set
. "<div id='$id' class='hidden'><pre><code class='jush-$jush'>" . shorten_utf8($query, 1000) . '</code></pre>'
. ($time ? " <span class='time'>($time)</span>" : '')
. (support("sql") ? '<p><a href="' . h(str_replace("db=" . urlencode(DB), "db=" . urlencode($_GET["db"]), ME) . 'sql=&history=' . (count($history[$_GET["db"]]) - 1)) . '">' . lang('Edit') . '</a>' : '')
. '</div>'
;

View file

@ -45,7 +45,7 @@
$start = microtime(true);
$return = $this->_conn->query($query);
if ($print) {
echo $adminer->selectQuery($query . ";\n-- " . format_time($start, microtime(true)));
echo $adminer->selectQuery($query, format_time($start, microtime(true)));
}
return $return;
}

View file

@ -111,9 +111,9 @@ function referencable_primary($self) {
/** Print SQL <textarea> tag
* @param string
* @param string or array in which case [0] of every element is used
* @param int
* @param int
* @param string
* @return null
*/
function textarea($name, $value, $rows = 10, $cols = 80) {
@ -121,7 +121,7 @@ function textarea($name, $value, $rows = 10, $cols = 80) {
echo "<textarea name='$name' rows='$rows' cols='$cols' class='sqlarea jush-$jush' spellcheck='false' wrap='off'>";
if (is_array($value)) {
foreach ($value as $val) { // not implode() to save memory
echo h($val[0]) . "\n\n\n"; // $val == array($query, $time)
echo h($val[0]) . "\n\n\n"; // $val == array($query, $time, $elapsed)
}
} else {
echo h($value);

View file

@ -548,17 +548,16 @@ function redirect($location, $message = null) {
* @param bool
* @return bool
*/
function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false) {
function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false, $time = "") {
global $connection, $error, $adminer;
$time = "";
if ($execute) {
$start = microtime(true);
$failed = !$connection->query($query);
$time = "; -- " . format_time($start, microtime(true));
$time = format_time($start, microtime(true));
}
$sql = "";
if ($query) {
$sql = $adminer->messageQuery($query . $time);
$sql = $adminer->messageQuery($query, $time);
}
if ($failed) {
$error = error() . $sql;
@ -571,21 +570,22 @@ function query_redirect($query, $location, $message, $redirect = true, $execute
}
/** Execute and remember query
* @param string null to return remembered queries, end with ';' to use DELIMITER
* @return Min_Result
* @param string or null to return remembered queries, end with ';' to use DELIMITER
* @return Min_Result or string if $query = null
*/
function queries($query = null) {
function queries($query) {
global $connection;
static $queries = array();
if ($query === null) {
// return executed queries without parameter
return implode("\n", $queries);
static $start;
if (!$start) {
$start = microtime(true);
}
$start = microtime(true);
$return = $connection->query($query);
$queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query)
. "; -- " . format_time($start, microtime(true));
return $return;
if ($query === null) {
// return executed queries
return array(implode("\n", $queries), format_time($start, microtime(true)));
}
$queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query) . ";";
return $connection->query($query);
}
/** Apply command to all array items
@ -610,7 +610,8 @@ function apply_queries($query, $tables, $escape = 'table') {
* @return bool
*/
function queries_redirect($location, $message, $redirect) {
return query_redirect(queries(), $location, $message, $redirect, false, !$redirect);
list($queries, $time) = queries(null);
return query_redirect($queries, $location, $message, $redirect, false, !$redirect, $time);
}
/** Format time difference

View file

@ -39,7 +39,7 @@ if (!$error && $_POST) {
$q = $query . (preg_match("~;[ \t\r\n]*\$~", $query) ? "" : ";"); //! doesn't work with DELIMITER |
if (!$history || reset(end($history)) != $q) { // no repeated queries
restart_session();
$history[] = array($q, time());
$history[] = array($q, time()); //! add elapsed time
set_session("queries", $history_all); // required because reference is unlinked by stop_session()
stop_session();
}
@ -224,8 +224,13 @@ if (!isset($_GET["import"]) && $history) {
print_fieldset("history", lang('History'), $_GET["history"] != "");
for ($val = end($history); $val; $val = prev($history)) { // not array_reverse() to save memory
$key = key($history);
list($q, $time) = $val;
echo '<a href="' . h(ME . "sql=&history=$key") . '">' . lang('Edit') . "</a> <span class='time' title='" . @date('Y-m-d', $time) . "'>" . @date("H:i:s", $time) . "</span> <code class='jush-$jush'>" . shorten_utf8(ltrim(str_replace("\n", " ", str_replace("\r", "", preg_replace('~^(#|-- ).*~m', '', $q)))), 80, "</code>") . "<br>\n"; // @ - time zone may be not set
list($q, $time, $elapsed) = $val;
echo '<a href="' . h(ME . "sql=&history=$key") . '">' . lang('Edit') . "</a>"
. " <span class='time' title='" . @date('Y-m-d', $time) . "'>" . @date("H:i:s", $time) . "</span>" // @ - time zone may be not set
. " <code class='jush-$jush'>" . shorten_utf8(ltrim(str_replace("\n", " ", str_replace("\r", "", preg_replace('~^(#|-- ).*~m', '', $q)))), 80, "</code>")
. ($elapsed ? " <span class='time'>($elapsed)</span>" : "")
. "<br>\n"
;
}
echo "<input type='submit' name='clear' value='" . lang('Clear') . "'>\n";
echo "<a href='" . h(ME . "sql=&history=all") . "'>" . lang('Edit all') . "</a>\n";

View file

@ -3,6 +3,7 @@ Provide size of all databases in the overview
Compute number of tables in the overview explicitly
Display edit form after error in clone or multi-edit
Display time of the select command
Print elapsed time in HTML instead of SQL command comment
Improve gzip export ratio (bug #387)
MySQL: Fix editing rows by binary values, bug since Adminer 3.7.1
MySQL: Respect daylight saving time in dump, bug since Adminer 3.6.4

View file

@ -128,8 +128,8 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
}
}
function selectQuery($query) {
return "<!--\n" . str_replace("--", "--><!-- ", $query) . "\n-->\n";
function selectQuery($query, $time) {
return "<!--\n" . str_replace("--", "--><!-- ", $query) . "\n($time)\n-->\n";
}
function rowDescription($table) {
@ -425,8 +425,8 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
return "";
}
function messageQuery($query) {
return " <span class='time'>" . @date("H:i:s") . "</span><!--\n" . str_replace("--", "--><!-- ", $query) . "\n-->";
function messageQuery($query, $time) {
return " <span class='time'>" . @date("H:i:s") . "</span><!--\n" . str_replace("--", "--><!-- ", $query) . "\n" . ($time ? "($time)\n" : "") . "-->";
}
function editFunctions($field) {

View file

@ -30,7 +30,7 @@ class AdminerMasterSlave {
}
}
function messageQuery($query) {
function messageQuery($query, $time) {
//! doesn't work with sql.inc.php
$connection = connection();
$result = $connection->query('SHOW MASTER STATUS');

View file

@ -17,7 +17,7 @@ class AdminerSqlLog {
$this->filename = $filename;
}
function messageQuery($query) {
function messageQuery($query, $time) {
if ($this->filename == "") {
$adminer = adminer();
$this->filename = $adminer->database() . ".sql"; // no database goes to ".sql" to avoid collisions