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"]) { if ($data["query"] && !$data["query"]["filtered"]["query"]) {
$data["query"]["filtered"]["query"] = array("match_all" => array()); $data["query"]["filtered"]["query"] = array("match_all" => array());
} }
if ($print) { $start = microtime(true);
echo $adminer->selectQuery("$query: " . print_r($data, true));
}
$search = $this->_conn->query($query, $data); $search = $this->_conn->query($query, $data);
if ($print) {
echo $adminer->selectQuery("$query: " . print_r($data, true), format_time($start, microtime(true)));
}
if (!$search) { if (!$search) {
return false; return false;
} }

View file

@ -177,11 +177,12 @@ username.form['auth[driver]'].onchange();
/** Query printed in select before execution /** Query printed in select before execution
* @param string query to be executed * @param string query to be executed
* @param string elapsed time
* @return string * @return string
*/ */
function selectQuery($query) { function selectQuery($query, $time) {
global $jush; 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>" : "") . (support("sql") ? " <a href='" . h(ME) . "sql=" . urlencode($query) . "'>" . lang('Edit') . "</a>" : "")
. "</p>" // </p> - required for IE9 inline edit . "</p>" // </p> - required for IE9 inline edit
; ;
@ -500,9 +501,10 @@ username.form['auth[driver]'].onchange();
/** Query printed after execution in the message /** Query printed after execution in the message
* @param string executed query * @param string executed query
* @param string elapsed time
* @return string * @return string
*/ */
function messageQuery($query) { function messageQuery($query, $time) {
global $jush; global $jush;
restart_session(); restart_session();
$history = &get_session("queries"); $history = &get_session("queries");
@ -510,9 +512,10 @@ username.form['auth[driver]'].onchange();
if (strlen($query) > 1e6) { 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 $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 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>' . "<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>' : '') . (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>' . '</div>'
; ;

View file

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

View file

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

View file

@ -548,17 +548,16 @@ function redirect($location, $message = null) {
* @param bool * @param bool
* @return 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; global $connection, $error, $adminer;
$time = "";
if ($execute) { if ($execute) {
$start = microtime(true); $start = microtime(true);
$failed = !$connection->query($query); $failed = !$connection->query($query);
$time = "; -- " . format_time($start, microtime(true)); $time = format_time($start, microtime(true));
} }
$sql = ""; $sql = "";
if ($query) { if ($query) {
$sql = $adminer->messageQuery($query . $time); $sql = $adminer->messageQuery($query, $time);
} }
if ($failed) { if ($failed) {
$error = error() . $sql; $error = error() . $sql;
@ -571,21 +570,22 @@ function query_redirect($query, $location, $message, $redirect = true, $execute
} }
/** Execute and remember query /** Execute and remember query
* @param string null to return remembered queries, end with ';' to use DELIMITER * @param string or null to return remembered queries, end with ';' to use DELIMITER
* @return Min_Result * @return Min_Result or string if $query = null
*/ */
function queries($query = null) { function queries($query) {
global $connection; global $connection;
static $queries = array(); static $queries = array();
if ($query === null) { static $start;
// return executed queries without parameter if (!$start) {
return implode("\n", $queries); $start = microtime(true);
} }
$start = microtime(true); if ($query === null) {
$return = $connection->query($query); // return executed queries
$queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query) return array(implode("\n", $queries), format_time($start, microtime(true)));
. "; -- " . format_time($start, microtime(true)); }
return $return; $queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query) . ";";
return $connection->query($query);
} }
/** Apply command to all array items /** Apply command to all array items
@ -610,7 +610,8 @@ function apply_queries($query, $tables, $escape = 'table') {
* @return bool * @return bool
*/ */
function queries_redirect($location, $message, $redirect) { 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 /** 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 | $q = $query . (preg_match("~;[ \t\r\n]*\$~", $query) ? "" : ";"); //! doesn't work with DELIMITER |
if (!$history || reset(end($history)) != $q) { // no repeated queries if (!$history || reset(end($history)) != $q) { // no repeated queries
restart_session(); 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() set_session("queries", $history_all); // required because reference is unlinked by stop_session()
stop_session(); stop_session();
} }
@ -224,8 +224,13 @@ if (!isset($_GET["import"]) && $history) {
print_fieldset("history", lang('History'), $_GET["history"] != ""); print_fieldset("history", lang('History'), $_GET["history"] != "");
for ($val = end($history); $val; $val = prev($history)) { // not array_reverse() to save memory for ($val = end($history); $val; $val = prev($history)) { // not array_reverse() to save memory
$key = key($history); $key = key($history);
list($q, $time) = $val; 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> <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 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 "<input type='submit' name='clear' value='" . lang('Clear') . "'>\n";
echo "<a href='" . h(ME . "sql=&history=all") . "'>" . lang('Edit all') . "</a>\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 Compute number of tables in the overview explicitly
Display edit form after error in clone or multi-edit Display edit form after error in clone or multi-edit
Display time of the select command Display time of the select command
Print elapsed time in HTML instead of SQL command comment
Improve gzip export ratio (bug #387) Improve gzip export ratio (bug #387)
MySQL: Fix editing rows by binary values, bug since Adminer 3.7.1 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 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) { function selectQuery($query, $time) {
return "<!--\n" . str_replace("--", "--><!-- ", $query) . "\n-->\n"; return "<!--\n" . str_replace("--", "--><!-- ", $query) . "\n($time)\n-->\n";
} }
function rowDescription($table) { function rowDescription($table) {
@ -425,8 +425,8 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
return ""; return "";
} }
function messageQuery($query) { function messageQuery($query, $time) {
return " <span class='time'>" . @date("H:i:s") . "</span><!--\n" . str_replace("--", "--><!-- ", $query) . "\n-->"; return " <span class='time'>" . @date("H:i:s") . "</span><!--\n" . str_replace("--", "--><!-- ", $query) . "\n" . ($time ? "($time)\n" : "") . "-->";
} }
function editFunctions($field) { 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 //! doesn't work with sql.inc.php
$connection = connection(); $connection = connection();
$result = $connection->query('SHOW MASTER STATUS'); $result = $connection->query('SHOW MASTER STATUS');

View file

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