Skip to content

Commit

Permalink
Highlight line with error in SQL command
Browse files Browse the repository at this point in the history
  • Loading branch information
vrana committed Apr 17, 2012
1 parent 4815c7d commit 047ce18
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
10 changes: 10 additions & 0 deletions adminer/drivers/mysql.inc.php
Expand Up @@ -510,6 +510,16 @@ function 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
* @param string
* @return string
Expand Down
17 changes: 11 additions & 6 deletions adminer/sql.inc.php
Expand Up @@ -54,6 +54,8 @@
}
$commands = 0;
$errors = array();
$error_lines = array();
$line = 0;
$parse = '[\'"' . ($jush == "sql" ? '`#' : ($jush == "sqlite" ? '`[' : ($jush == "mssql" ? '[' : ''))) . ']|/\\*|-- |$' . ($jush == "pgsql" ? '|\\$[^$]*\\$' : '');
$total_start = microtime();
parse_str($_COOKIE["adminer_export"], $adminer_export);
Expand All @@ -64,16 +66,16 @@
$delimiter = $match[1];
$query = substr($query, strlen($match[0]));
} else {
preg_match('(' . preg_quote($delimiter) . "|$parse)", $query, $match, PREG_OFFSET_CAPTURE, $offset); // should always match
$found = $match[0][0];
preg_match('(' . preg_quote($delimiter) . "\\s*|$parse)", $query, $match, PREG_OFFSET_CAPTURE, $offset); // should always match
list($found, $pos) = $match[0];
if (!$found && $fp && !feof($fp)) {
$query .= fread($fp, 1e5);
} else {
$offset = $match[0][1] + strlen($found);
$offset = $pos + strlen($found);
if (!$found && rtrim($query) == "") {
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
$s = $match[0][0];
if (!$s && $fp && !feof($fp)) {
Expand All @@ -87,7 +89,7 @@
}
} else { // end of a query
$empty = false;
$q = substr($query, 0, $match[0][1]);
$q = substr($query, 0, $pos);
$commands++;
$print = "<pre id='sql-$commands'><code class='jush-$jush'>" . shorten_utf8(trim($q), 1000) . "</code></pre>\n";
if (!$_POST["only_errors"]) {
Expand All @@ -107,6 +109,7 @@
if ($connection->error) {
echo ($_POST["only_errors"] ? $print : "");
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>";
if ($_POST["error_stops"]) {
break 2;
Expand Down Expand Up @@ -146,6 +149,7 @@
}
$start = $end;
} while ($connection->next_result());
$line += substr_count($q.$found, "\n");
$query = substr($query, $offset);
$offset = 0;
}
Expand Down Expand Up @@ -220,6 +224,7 @@
<script src="<?php echo "$codemirror_path/mode/$codemirror_mode/$codemirror_mode.js"; ?>"></script>
<script type="text/javascript">
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>
1 change: 1 addition & 0 deletions adminer/static/default.css
Expand Up @@ -60,6 +60,7 @@ input[type=image] { vertical-align: middle; }
.rtl #lang, .rtl #menu { left: auto; right: 0; }

.CodeMirror { border: 1px inset #777; }
.CodeMirror .error { background: #fee; }

@media print {
#lang, #menu { display: none; }
Expand Down
2 changes: 1 addition & 1 deletion changes.txt
@@ -1,6 +1,6 @@
Adminer 3.4.0-dev:
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
Replace JSMin by better JavaScript minifier
Don't use AJAX links and forms
Expand Down

0 comments on commit 047ce18

Please sign in to comment.