From 18e158fcdf704725d9e20c1eeee04017f315c5ca Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Fri, 22 Oct 2010 14:31:46 +0200 Subject: [PATCH] Option to show only errors in SQL command --- adminer/lang/cs.inc.php | 3 ++ adminer/lang/en.inc.php | 1 + adminer/sql.inc.php | 62 +++++++++++++++++++++++------------------ 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/adminer/lang/cs.inc.php b/adminer/lang/cs.inc.php index ffc92cf2..6c7ce148 100644 --- a/adminer/lang/cs.inc.php +++ b/adminer/lang/cs.inc.php @@ -300,4 +300,7 @@ $translations = array( // function translation used in Editor 'now' => 'teď', + + '%d query(s) executed OK.' => array('%d příkaz proběhl v pořádku.', '%d příkazy proběhly v pořádku.', '%d příkazů proběhlo v pořádku.'), + 'Show only errors' => 'Zobrazit pouze chyby', ); diff --git a/adminer/lang/en.inc.php b/adminer/lang/en.inc.php index 3b0fca1e..1c2bdd75 100644 --- a/adminer/lang/en.inc.php +++ b/adminer/lang/en.inc.php @@ -9,4 +9,5 @@ $translations = array( '%d row(s) have been imported.' => array('%d row has been imported.', '%d rows have been imported.'), '%d e-mail(s) have been sent.' => array('%d e-mail has been sent.', '%d e-mails have been sent.'), '%d in total' => '%d in total', + '%d query(s) executed OK.' => array('%d query executed OK.', '%d queries executed OK.'), ); diff --git a/adminer/sql.inc.php b/adminer/sql.inc.php index c4f74574..160b1323 100644 --- a/adminer/sql.inc.php +++ b/adminer/sql.inc.php @@ -40,7 +40,7 @@ if (!$error && $_POST) { $connection2->select_db(DB); } $commands = 0; - $errors = ""; + $errors = array(); while ($query != "") { if (!$offset && preg_match('~^\\s*DELIMITER\\s+(.+)~i', $query, $match)) { $delimiter = $match[1]; @@ -55,18 +55,32 @@ if (!$error && $_POST) { if (!$found && rtrim($query) == "") { break; } - if (!$found || $found == $delimiter) { // end of a query + if ($found && $found != $delimiter) { // find matching quote or comment end + while (preg_match('~' . ($found == '/*' ? '\\*/' : (ereg('-- |#', $found) ? "\n" : "$found|\\\\.")) . '|$~s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES + $s = $match[0][0]; + $offset = $match[0][1] + strlen($s); + if (!$s && $fp && !feof($fp)) { + $query .= fread($fp, 1e6); + } elseif ($s[0] != "\\") { + break; + } + } + } else { // end of a query $empty = false; $q = substr($query, 0, $match[0][1]); $commands++; - echo "
" . shorten_utf8(trim($q), 1000) . "
\n"; - ob_flush(); - flush(); // can take a long time - show the running query + $print = "
" . shorten_utf8(trim($q), 1000) . "
\n"; + if (!$_POST["only_errors"]) { + echo $print; + ob_flush(); + flush(); // can take a long time - show the running query + } $start = explode(" ", microtime()); // microtime(true) is available since PHP 5 //! don't allow changing of character_set_results, convert encoding of displayed query if (!$connection->multi_query($q)) { + echo ($_POST["only_errors"] ? $print : ""); echo "

" . lang('Error in query') . ": " . error() . "\n"; - $errors .= " $commands"; + $errors[] = " $commands"; if ($_POST["error_stops"]) { break; } @@ -78,7 +92,16 @@ if (!$error && $_POST) { $result = $connection->store_result(); $end = explode(" ", microtime()); $time = " (" . lang('%.3f s', max(0, $end[0] - $start[0] + $end[1] - $start[1])) . ")"; - if (is_object($result)) { + if (!is_object($result)) { + if (preg_match("~^$space*(CREATE|DROP|ALTER)$space+(DATABASE|SCHEMA)\\b~isU", $q)) { + restart_session(); + set_session("dbs", null); // clear cache + session_write_close(); + } + if (!$_POST["only_errors"]) { + echo "

" . lang('Query executed OK, %d row(s) affected.', $connection->affected_rows) . "$time\n"; + } + } elseif (!$_POST["only_errors"]) { select($result, $connection2); echo "

" . ($result->num_rows ? lang('%d row(s)', $result->num_rows) : "") . $time; if ($connection2 && preg_match("~^($space|\\()*SELECT\\b~isU", $q)) { @@ -88,38 +111,22 @@ if (!$error && $_POST) { select(explain($connection2, $q)); echo "\n"; } - } else { - if (preg_match("~^$space*(CREATE|DROP|ALTER)$space+(DATABASE|SCHEMA)\\b~isU", $q)) { - restart_session(); - set_session("dbs", null); // clear cache - session_write_close(); - } - echo "

" . lang('Query executed OK, %d row(s) affected.', $connection->affected_rows) . "$time\n"; } $start = $end; } while ($connection->next_result()); } $query = substr($query, $offset); $offset = 0; - } else { // find matching quote or comment end - while (preg_match('~' . ($found == '/*' ? '\\*/' : (ereg('-- |#', $found) ? "\n" : "$found|\\\\.")) . '|$~s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES - $s = $match[0][0]; - $offset = $match[0][1] + strlen($s); - if (!$s && $fp && !feof($fp)) { - $query .= fread($fp, 1e6); - } elseif ($s[0] != "\\") { - break; - } - } } } } } - if ($errors && $commands > 1) { - echo "

" . lang('Error in query') . ": $errors\n"; - } if ($empty) { echo "

" . lang('No commands to execute.') . "\n"; + } elseif ($_POST["only_errors"]) { + echo "

" . lang('%d query(s) executed OK.', $commands - count($errors)) . "\n"; + } elseif ($errors && $commands > 1) { + echo "

" . lang('Error in query') . ": " . implode("", $errors) . "\n"; } //! MS SQL - SET SHOWPLAN_ALL OFF } else { @@ -146,6 +153,7 @@ echo "

" . (ini_bool("file_uploads") ? lang('File upload') . ':