Highlight line with error in SQL command

This commit is contained in:
Jakub Vrana 2012-04-15 23:37:05 -07:00
parent 4815c7d020
commit 047ce184f5
4 changed files with 23 additions and 7 deletions

View file

@ -510,6 +510,16 @@ if (!defined("DRIVER")) {
return h(preg_replace('~^You have an error.*syntax to use~U', "Syntax error", $connection->error)); return h(preg_replace('~^You have an error.*syntax to use~U', "Syntax error", $connection->error));
} }
/** Get line of error
* @return int 0 for first line
*/
function error_line() {
global $connection;
if (ereg(' at line ([0-9]+)$', $connection->error, $regs)) {
return $regs[1] - 1;
}
}
/** Return expression for binary comparison /** Return expression for binary comparison
* @param string * @param string
* @return string * @return string

View file

@ -54,6 +54,8 @@ if (!$error && $_POST) {
} }
$commands = 0; $commands = 0;
$errors = array(); $errors = array();
$error_lines = array();
$line = 0;
$parse = '[\'"' . ($jush == "sql" ? '`#' : ($jush == "sqlite" ? '`[' : ($jush == "mssql" ? '[' : ''))) . ']|/\\*|-- |$' . ($jush == "pgsql" ? '|\\$[^$]*\\$' : ''); $parse = '[\'"' . ($jush == "sql" ? '`#' : ($jush == "sqlite" ? '`[' : ($jush == "mssql" ? '[' : ''))) . ']|/\\*|-- |$' . ($jush == "pgsql" ? '|\\$[^$]*\\$' : '');
$total_start = microtime(); $total_start = microtime();
parse_str($_COOKIE["adminer_export"], $adminer_export); parse_str($_COOKIE["adminer_export"], $adminer_export);
@ -64,16 +66,16 @@ if (!$error && $_POST) {
$delimiter = $match[1]; $delimiter = $match[1];
$query = substr($query, strlen($match[0])); $query = substr($query, strlen($match[0]));
} else { } else {
preg_match('(' . preg_quote($delimiter) . "|$parse)", $query, $match, PREG_OFFSET_CAPTURE, $offset); // should always match preg_match('(' . preg_quote($delimiter) . "\\s*|$parse)", $query, $match, PREG_OFFSET_CAPTURE, $offset); // should always match
$found = $match[0][0]; list($found, $pos) = $match[0];
if (!$found && $fp && !feof($fp)) { if (!$found && $fp && !feof($fp)) {
$query .= fread($fp, 1e5); $query .= fread($fp, 1e5);
} else { } else {
$offset = $match[0][1] + strlen($found); $offset = $pos + strlen($found);
if (!$found && rtrim($query) == "") { if (!$found && rtrim($query) == "") {
break; break;
} }
if ($found && $found != $delimiter) { // find matching quote or comment end if ($found && rtrim($found) != $delimiter) { // find matching quote or comment end
while (preg_match('(' . ($found == '/*' ? '\\*/' : ($found == '[' ? ']' : (ereg('^-- |^#', $found) ? "\n" : preg_quote($found) . "|\\\\."))) . '|$)s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES while (preg_match('(' . ($found == '/*' ? '\\*/' : ($found == '[' ? ']' : (ereg('^-- |^#', $found) ? "\n" : preg_quote($found) . "|\\\\."))) . '|$)s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES
$s = $match[0][0]; $s = $match[0][0];
if (!$s && $fp && !feof($fp)) { if (!$s && $fp && !feof($fp)) {
@ -87,7 +89,7 @@ if (!$error && $_POST) {
} }
} else { // end of a query } else { // end of a query
$empty = false; $empty = false;
$q = substr($query, 0, $match[0][1]); $q = substr($query, 0, $pos);
$commands++; $commands++;
$print = "<pre id='sql-$commands'><code class='jush-$jush'>" . shorten_utf8(trim($q), 1000) . "</code></pre>\n"; $print = "<pre id='sql-$commands'><code class='jush-$jush'>" . shorten_utf8(trim($q), 1000) . "</code></pre>\n";
if (!$_POST["only_errors"]) { if (!$_POST["only_errors"]) {
@ -107,6 +109,7 @@ if (!$error && $_POST) {
if ($connection->error) { if ($connection->error) {
echo ($_POST["only_errors"] ? $print : ""); echo ($_POST["only_errors"] ? $print : "");
echo "<p class='error'>" . lang('Error in query') . ": " . error() . "\n"; echo "<p class='error'>" . lang('Error in query') . ": " . error() . "\n";
$error_lines[] = $line + (function_exists('error_line') ? error_line() : 0);
$errors[] = " <a href='#sql-$commands'>$commands</a>"; $errors[] = " <a href='#sql-$commands'>$commands</a>";
if ($_POST["error_stops"]) { if ($_POST["error_stops"]) {
break 2; break 2;
@ -146,6 +149,7 @@ if (!$error && $_POST) {
} }
$start = $end; $start = $end;
} while ($connection->next_result()); } while ($connection->next_result());
$line += substr_count($q.$found, "\n");
$query = substr($query, $offset); $query = substr($query, $offset);
$offset = 0; $offset = 0;
} }
@ -220,6 +224,7 @@ if ($history) {
<script src="<?php echo "$codemirror_path/mode/$codemirror_mode/$codemirror_mode.js"; ?>"></script> <script src="<?php echo "$codemirror_path/mode/$codemirror_mode/$codemirror_mode.js"; ?>"></script>
<script type="text/javascript"> <script type="text/javascript">
if (window.CodeMirror) { if (window.CodeMirror) {
CodeMirror.fromTextArea(document.getElementById('query')); var codeMirror = CodeMirror.fromTextArea(document.getElementById('query'));
<?php foreach ($error_lines as $line) { ?>codeMirror.setLineClass(<?php echo $line; ?>, '', 'error');<?php } ?>
} }
</script> </script>

View file

@ -60,6 +60,7 @@ input[type=image] { vertical-align: middle; }
.rtl #lang, .rtl #menu { left: auto; right: 0; } .rtl #lang, .rtl #menu { left: auto; right: 0; }
.CodeMirror { border: 1px inset #777; } .CodeMirror { border: 1px inset #777; }
.CodeMirror .error { background: #fee; }
@media print { @media print {
#lang, #menu { display: none; } #lang, #menu { display: none; }

View file

@ -1,6 +1,6 @@
Adminer 3.4.0-dev: Adminer 3.4.0-dev:
Print current time next to executed SQL queries Print current time next to executed SQL queries
Highlight code in textarea by CodeMirror Highlight code in SQL command by CodeMirror
Link to original table in EXPLAIN of SELECT * FROM table t Link to original table in EXPLAIN of SELECT * FROM table t
Replace JSMin by better JavaScript minifier Replace JSMin by better JavaScript minifier
Don't use AJAX links and forms Don't use AJAX links and forms