]> git.joonet.de Git - adminer.git/commitdiff
Highlight line with error in SQL command
authorJakub Vrana <jakub@vrana.cz>
Mon, 16 Apr 2012 06:37:05 +0000 (23:37 -0700)
committerJakub Vrana <jakub@vrana.cz>
Tue, 17 Apr 2012 20:51:20 +0000 (13:51 -0700)
adminer/drivers/mysql.inc.php
adminer/sql.inc.php
adminer/static/default.css
changes.txt

index daabae7a1519520864116c867aab49e446024580..681a4e139b6ad1218ebc6eedd2896d135ba66235 100644 (file)
@@ -510,6 +510,16 @@ if (!defined("DRIVER")) {
                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
index 61acae2f10cb2e570466225c15712a4f44d78c28..06a068ccee28b2d3c506cb5205ce7f251abd221a 100644 (file)
@@ -54,6 +54,8 @@ if (!$error && $_POST) {
                }
                $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);
@@ -64,16 +66,16 @@ if (!$error && $_POST) {
                                $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)) {
@@ -87,7 +89,7 @@ if (!$error && $_POST) {
                                                }
                                        } 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"]) {
@@ -107,6 +109,7 @@ if (!$error && $_POST) {
                                                        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;
@@ -146,6 +149,7 @@ if (!$error && $_POST) {
                                                        }
                                                        $start = $end;
                                                } while ($connection->next_result());
+                                               $line += substr_count($q.$found, "\n");
                                                $query = substr($query, $offset);
                                                $offset = 0;
                                        }
@@ -220,6 +224,7 @@ if ($history) {
 <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>
index cd42bae0582d24925cdf59b6bce08421039b3f1b..b9d6c251b41bf3e9c524f4b4c9384c6e418b30b9 100644 (file)
@@ -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; }
index 4e71f397945c28f902ba402da659e84e37159679..97a93b38e524034758f109c225d9d3ffff45bc7e 100644 (file)
@@ -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