* hl-line.el (hl-line): New face.
[bpt/emacs.git] / lisp / progmodes / sql.el
index 13f9e9e..ff5493d 100644 (file)
@@ -1,11 +1,13 @@
 ;;; sql.el --- specialized comint.el for SQL interpreters
 
-;; Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation, Inc.
+;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
+;; Free Software Foundation, Inc.
 
 ;; Author: Alex Schroeder <alex@gnu.org>
-;; Maintainer: Alex Schroeder <alex@gnu.org>
-;; Version: 1.6.2
+;; Maintainer: Michael Mauger <mmaug@yahoo.com>
+;; Version: 2.0.1
 ;; Keywords: comm languages processes
+;; URL: http://savannah.gnu.org/cgi-bin/viewcvs/emacs/emacs/lisp/progmodes/sql.el
 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlMode
 
 ;; This file is part of GNU Emacs.
@@ -22,8 +24,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
 
 ;;; Bugs:
 
-;; Using sql-ms (isql by Microsoft): When commands with syntax errors
-;; or execution errors are executed, there is no server feedback.
-;; This happens in stored procedures for example.  The server messages
-;; only appear after the process is exited.  This makes things
-;; somewhat unreliable.
+;; sql-ms now uses osql instead of isql.  Osql flushes its error
+;; stream more frequently than isql so that error messages are
+;; available.  There is no prompt and some output still is buffered.
+;; This improves the interaction under Emacs but it still is somewhat
+;; awkward.
+
+;; Quoted identifiers are not supported for hilighting.  Most
+;; databases support the use of double quoted strings in place of
+;; identifiers; ms (Microsoft SQLServer) also supports identifiers
+;; enclosed within brackets [].
 
 ;; ChangeLog available on request.
 
+;;; Product Support:
+
+;; To add support for additional SQL products the following steps
+;; must be followed ("xyz" is the name of the product in the examples
+;; below):
+
+;; 1) Add the product to `sql-product' choice list.
+
+;;     (const :tag "XyzDB" xyz)
+
+;; 2) Add an entry to the `sql-product-alist' list.
+
+;;     (xyz
+;;      :font-lock sql-mode-xyz-font-lock-keywords
+;;      :sqli-login (user password server database)
+;;      :sqli-connect sql-connect-xyz
+;;      :sqli-prompt-regexp "^xyzdb> "
+;;      :sqli-prompt-length 7
+;;      :sqli-input-sender nil
+;;      :syntax-alist ((?# . "w")))
+
+;; 3) Add customizable values for the product interpreter and options.
+
+;;     ;; Customization for XyzDB
+;;
+;;     (defcustom sql-xyz-program "ixyz"
+;;       "*Command to start ixyz by XyzDB."
+;;       :type 'file
+;;       :group 'SQL)
+;;
+;;     (defcustom sql-xyz-options '("-X" "-Y" "-Z")
+;;       "*List of additional options for `sql-xyz-program'."
+;;       :type '(repeat string)
+;;       :group 'SQL)
+
+;; 4) Add an entry to SQL->Product submenu.
+
+;;     ["XyzDB" sql-highlight-xyz-keywords
+;;      :style radio
+;;      :selected (eq sql-product 'xyz)]
+
+;; 5) Add the font-lock specifications.  At a minimum, default to
+;;    using ANSI keywords.  See sql-mode-oracle-font-lock-keywords for
+;;    a more complex example.
+
+;;     (defvar sql-mode-xyz-font-lock-keywords nil
+;;       "XyzDB SQL keywords used by font-lock.")
+
+;; 6) Add a product highlighting function.
+
+;;     (defun sql-highlight-xyz-keywords ()
+;;       "Highlight XyzDB keywords."
+;;       (interactive)
+;;       (sql-set-product 'xyz))
+
+;; 7) Add an autoloaded SQLi function.
+
+;;     ;;;###autoload
+;;     (defun sql-xyz ()
+;;       "Run ixyz by XyzDB as an inferior process."
+;;       (interactive)
+;;       (sql-product-interactive 'xyz))
+
+;; 8) Add a connect function which formats the command line arguments
+;;    and starts the product interpreter in a comint buffer.  See the
+;;    existing connect functions for examples of the types of
+;;    processing available.
+
+;;     (defun sql-connect-xyz ()
+;;       "Create comint buffer and connect to XyzDB using the login
+;;     parameters and command options."
+;;
+;;         ;; Do something with `sql-user', `sql-password',
+;;         ;; `sql-database', and `sql-server'.
+;;         (let ((params sql-xyz-options))
+;;           (if (not (string= "" sql-server))
+;;              (setq params (append (list "-S" sql-server) params)))
+;;           (if (not (string= "" sql-database))
+;;               (setq params (append (list "-D" sql-database) params)))
+;;           (if (not (string= "" sql-password))
+;;               (setq params (append (list "-P" sql-password) params)))
+;;           (if (not (string= "" sql-user))
+;;               (setq params (append (list "-U" sql-user) params)))
+;;           (set-buffer (apply 'make-comint "SQL" sql-xyz-program
+;;                              nil params))))
+
+;; 9) Save and compile sql.el.
+
 ;;; To Do:
 
 ;; Add better hilight support for other brands; there is a bias towards
 
 ;;; Thanks to all the people who helped me out:
 
+;; Alex Schroeder <alex@gnu.org>
 ;; Kai Blauberg <kai.blauberg@metla.fi>
 ;; <ibalaban@dalet.com>
 ;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
 ;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
 ;; nino <nino@inform.dk>
 ;; Berend de Boer <berend@pobox.com>
+;; Adam Jenkins <adam@thejenkins.org>
+;; Michael Mauger <mmaug@yahoo.com> -- improved product support
+;; Drew Adams <drew.adams@oracle.com> -- Emacs 20 support
+;; Harald Maier <maierh@myself.com> -- sql-send-string
+;; Stefan Monnier <monnier@iro.umontreal.ca> -- font-lock corrections
 
 \f
 
 
 (require 'comint)
 ;; Need the following to allow GNU Emacs 19 to compile the file.
-(require 'regexp-opt)
+(eval-when-compile
+  (require 'regexp-opt))
 (require 'custom)
+(eval-when-compile ;; needed in Emacs 19, 20
+  (setq max-specpdl-size 2000))
+
+(defvar font-lock-keyword-face)
+(defvar font-lock-set-defaults)
+(defvar font-lock-string-face)
 
 ;;; Allow customization
 
 (defgroup SQL nil
-  "Running a SQL interpreter from within Emacs buffers"
+  "Running a SQL interpreter from within Emacs buffers."
   :version "20.4"
   :group 'processes)
 
-;; These three variables will be used as defaults, if set.
+;; These four variables will be used as defaults, if set.
 
 (defcustom sql-user ""
   "*Default username."
@@ -147,6 +255,147 @@ Customizing your password will store it in your ~/.emacs file."
   :type 'string
   :group 'SQL)
 
+;; SQL Product support
+(defcustom sql-product 'ansi
+  "*Select the SQL database product used so that buffers can be
+highlighted properly when you open them."
+  :type '(choice (const :tag "ANSI" ansi)
+                (const :tag "DB2" db2)
+                (const :tag "Informix" informix)
+                (const :tag "Ingres" ingres)
+                (const :tag "Interbase" interbase)
+                (const :tag "Linter" linter)
+                (const :tag "Microsoft" ms)
+                (const :tag "MySQL" mysql)
+                (const :tag "Oracle" oracle)
+                (const :tag "PostGres" postgres)
+                (const :tag "Solid" solid)
+                (const :tag "SQLite" sqlite)
+                (const :tag "Sybase" sybase))
+  :group 'SQL)
+
+(defvar sql-interactive-product nil
+  "Product under `sql-interactive-mode'.")
+
+(defvar sql-product-alist
+  '((ansi
+     :font-lock sql-mode-ansi-font-lock-keywords)
+    (db2
+     :font-lock sql-mode-db2-font-lock-keywords
+     :sqli-login nil
+     :sqli-connect sql-connect-db2
+     :sqli-prompt-regexp "^db2 => "
+     :sqli-prompt-length 7)
+    (informix
+     :font-lock sql-mode-informix-font-lock-keywords
+     :sqli-login (database)
+     :sqli-connect sql-connect-informix
+     :sqli-prompt-regexp "^SQL> "
+     :sqli-prompt-length 5)
+    (ingres
+     :font-lock sql-mode-ingres-font-lock-keywords
+     :sqli-login (database)
+     :sqli-connect sql-connect-ingres
+     :sqli-prompt-regexp "^\* "
+     :sqli-prompt-length 2)
+    (interbase
+     :font-lock sql-mode-interbase-font-lock-keywords
+     :sqli-login (user password database)
+     :sqli-connect sql-connect-interbase
+     :sqli-prompt-regexp "^SQL> "
+     :sqli-prompt-length 5)
+    (linter
+     :font-lock sql-mode-linter-font-lock-keywords
+     :sqli-login (user password database server)
+     :sqli-connect sql-connect-linter
+     :sqli-prompt-regexp "^SQL>"
+     :sqli-prompt-length 4)
+    (ms
+     :font-lock sql-mode-ms-font-lock-keywords
+     :sqli-login (user password server database)
+     :sqli-connect sql-connect-ms
+     :sqli-prompt-regexp "^[0-9]*>"
+     :sqli-prompt-length 5
+     :syntax-alist ((?@ . "w")))
+    (mysql
+     :font-lock sql-mode-mysql-font-lock-keywords
+     :sqli-login (user password database server)
+     :sqli-connect sql-connect-mysql
+     :sqli-prompt-regexp "^mysql> "
+     :sqli-prompt-length 6)
+    (oracle
+     :font-lock sql-mode-oracle-font-lock-keywords
+     :sqli-login (user password database)
+     :sqli-connect sql-connect-oracle
+     :sqli-prompt-regexp "^SQL> "
+     :sqli-prompt-length 5
+     :syntax-alist ((?$ . "w") (?# . "w")))
+    (postgres
+     :font-lock sql-mode-postgres-font-lock-keywords
+     :sqli-login (user database server)
+     :sqli-connect sql-connect-postgres
+     :sqli-prompt-regexp "^.*[#>] *"
+     :sqli-prompt-length 5)
+    (solid
+     :font-lock sql-mode-solid-font-lock-keywords
+     :sqli-login (user password server)
+     :sqli-connect sql-connect-solid
+     :sqli-prompt-regexp "^"
+     :sqli-prompt-length 0)
+    (sqlite
+     :font-lock sql-mode-sqlite-font-lock-keywords
+     :sqli-login (user password server database)
+     :sqli-connect sql-connect-sqlite
+     :sqli-prompt-regexp "^sqlite> "
+     :sqli-prompt-length 8)
+    (sybase
+     :font-lock sql-mode-sybase-font-lock-keywords
+     :sqli-login (server user password database)
+     :sqli-connect sql-connect-sybase
+     :sqli-prompt-regexp "^SQL> "
+     :sqli-prompt-length 5
+     :syntax-alist ((?@ . "w")))
+    )
+  "This variable contains a list of product features for each of the
+SQL products handled by `sql-mode'.  Without an entry in this list a
+product will not be properly highlighted and will not support
+`sql-interactive-mode'.
+
+Each element in the list is in the following format:
+
+ \(PRODUCT FEATURE VALUE ...)
+
+where PRODUCT is the appropriate value of `sql-product'.  The product
+name is then followed by FEATURE-VALUE pairs.  If a FEATURE is not
+specified, its VALUE is treated as nil.  FEATURE must be one of the
+following:
+
+ :font-lock             name of the variable containing the product
+                        specific font lock highlighting patterns.
+
+ :sqli-login            a list of login parameters (i.e., user,
+                        password, database and server) needed to
+                        connect to the database.
+
+ :sqli-connect          the name of a function which accepts no
+                        parameters that will use the values of
+                        `sql-user', `sql-password',
+                        `sql-database' and `sql-server' to open a
+                        comint buffer and connect to the
+                        database.  Do product specific
+                        configuration of comint in this function.
+
+ :sqli-prompt-regexp    a regular expression string that matches
+                        the prompt issued by the product
+                        interpreter.  (Not needed in 21.3+)
+
+ :sqli-prompt-length    the length of the prompt on the line.(Not
+                        needed in 21.3+)
+
+ :syntax-alist          an alist of syntax table entries to enable
+                        special character treatment by font-lock and
+                        imenu. ")
+
 ;; misc customization of sql.el behaviour
 
 (defcustom sql-electric-stuff nil
@@ -177,8 +426,15 @@ buffer is shown using `display-buffer'."
 ;; imenu support for sql-mode.
 
 (defvar sql-imenu-generic-expression
-  '(("Tables" "^\\s-*create\\s-+table\\s-+\\(\\w+\\)" 1)
-    ("Indexes" "^\\s-*create\\s-+index\\s-+\\(\\w+\\)" 1))
+  ;; Items are in reverse order because they are rendered in reverse.
+  '(("Rules/Defaults" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*\\(rule\\|default\\)\\s-+\\(\\w+\\)" 3)
+    ("Sequences" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*sequence\\s-+\\(\\w+\\)" 2)
+    ("Triggers" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*trigger\\s-+\\(\\w+\\)" 2)
+    ("Functions" "^\\s-*\\(create\\s-+\\(\\w+\\s-+\\)*\\)?function\\s-+\\(\\w+\\)" 3)
+    ("Procedures" "^\\s-*\\(create\\s-+\\(\\w+\\s-+\\)*\\)?proc\\(edure\\)?\\s-+\\(\\w+\\)" 4)
+    ("Packages" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*package\\s-+\\(body\\s-+\\)?\\(\\w+\\)" 3)
+    ("Indexes" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*index\\s-+\\(\\w+\\)" 2)
+    ("Tables/Views" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*\\(table\\|view\\)\\s-+\\(\\w+\\)" 3))
   "Define interesting points in the SQL buffer for `imenu'.
 
 This is used to set `imenu-generic-expression' when SQL mode is
@@ -262,6 +518,25 @@ The program can also specify a TCP connection.  See `make-comint'."
   :version "20.8"
   :group 'SQL)
 
+;; Customization for SQLite
+
+(defcustom sql-sqlite-program "sqlite"
+  "*Command to start SQLite.
+
+Starts `sql-interactive-mode' after doing some setup.
+
+The program can also specify a TCP connection.  See `make-comint'."
+  :type 'file
+  :group 'SQL)
+
+(defcustom sql-sqlite-options nil
+  "*List of additional options for `sql-sqlite-program'.
+The following list of options is reported to make things work
+on Windows: \"-C\" \"-t\" \"-f\" \"-n\"."
+  :type '(repeat string)
+  :version "20.8"
+  :group 'SQL)
+
 ;; Customization for MySql
 
 (defcustom sql-mysql-program "mysql"
@@ -334,8 +609,8 @@ The program can also specify a TCP connection.  See `make-comint'."
 
 ;; Customization for Microsoft
 
-(defcustom sql-ms-program "isql"
-  "*Command to start isql by Microsoft.
+(defcustom sql-ms-program "osql"
+  "*Command to start osql by Microsoft.
 
 Starts `sql-interactive-mode' after doing some setup.
 
@@ -343,6 +618,13 @@ The program can also specify a TCP connection.  See `make-comint'."
   :type 'file
   :group 'SQL)
 
+(defcustom sql-ms-options '("-w" "300" "-n")
+  ;; -w is the linesize
+  "*List of additional options for `sql-ms-program'."
+  :type '(repeat string)
+  :version "22.1"
+  :group 'SQL)
+
 ;; Customization for Postgres
 
 (defcustom sql-postgres-program "psql"
@@ -400,6 +682,21 @@ The program can also specify a TCP connection.  See `make-comint'."
   :version "20.8"
   :group 'SQL)
 
+;; Customization for Linter
+
+(defcustom sql-linter-program "inl"
+  "*Command to start inl by RELEX.
+
+Starts `sql-interactive-mode' after doing some setup."
+  :type 'file
+  :group 'SQL)
+
+(defcustom sql-linter-options nil
+  "*List of additional options for `sql-linter-program'."
+  :type '(repeat string)
+  :version "21.3"
+  :group 'SQL)
+
 \f
 
 ;;; Variables which do not need customization
@@ -427,12 +724,12 @@ the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
 (defvar sql-prompt-regexp nil
   "Prompt used to initialize `comint-prompt-regexp'.
 
-You can change `comint-prompt-regexp' on `sql-interactive-mode-hook'.")
+You can change `sql-prompt-regexp' on `sql-interactive-mode-hook'.")
 
 (defvar sql-prompt-length 0
   "Prompt used to set `left-margin' in `sql-interactive-mode'.
 
-You can change it on `sql-interactive-mode-hook'.")
+You can change `sql-prompt-length' on `sql-interactive-mode-hook'.")
 
 (defvar sql-alternate-buffer-name nil
   "Buffer-local string used to possibly rename the SQLi buffer.
@@ -441,12 +738,13 @@ Used by `sql-rename-buffer'.")
 
 ;; Keymap for sql-interactive-mode.
 
-(defvar sql-interactive-mode-map 
+(defvar sql-interactive-mode-map
   (let ((map (make-sparse-keymap)))
-    (if (functionp 'set-keymap-parent)
+    (if (fboundp 'set-keymap-parent)
        (set-keymap-parent map comint-mode-map); Emacs
-      (set-keymap-parents map (list comint-mode-map))); XEmacs
-    (if (functionp 'set-keymap-name)
+      (if (fboundp 'set-keymap-parents)
+         (set-keymap-parents map (list comint-mode-map)))); XEmacs
+    (if (fboundp 'set-keymap-name)
        (set-keymap-name map 'sql-interactive-mode-map)); XEmacs
     (define-key map (kbd "C-j") 'sql-accumulate-and-indent)
     (define-key map (kbd "C-c C-w") 'sql-copy-column)
@@ -463,6 +761,7 @@ Based on `comint-mode-map'.")
   (let ((map (make-sparse-keymap)))
     (define-key map (kbd "C-c C-c") 'sql-send-paragraph)
     (define-key map (kbd "C-c C-r") 'sql-send-region)
+    (define-key map (kbd "C-c C-s") 'sql-send-string)
     (define-key map (kbd "C-c C-b") 'sql-send-buffer)
     map)
   "Mode map used for `sql-mode'.")
@@ -477,21 +776,62 @@ Based on `comint-mode-map'.")
                                             (get-buffer-process sql-buffer))]
    ["Send Region" sql-send-region (and (or (and (boundp 'mark-active); Emacs
                                                mark-active)
-                                          (mark)); XEmacs
+                                          (mark t)); XEmacs
                                       (buffer-live-p sql-buffer)
                                       (get-buffer-process sql-buffer))]
    ["Send Buffer" sql-send-buffer (and (buffer-live-p sql-buffer)
                                       (get-buffer-process sql-buffer))]
+   ["Send String" sql-send-string t]
+   ["--" nil nil]
+   ["Start SQLi session" sql-product-interactive (sql-product-feature :sqli-connect)]
    ["Show SQLi buffer" sql-show-sqli-buffer t]
    ["Set SQLi buffer" sql-set-sqli-buffer t]
    ["Pop to SQLi buffer after send"
     sql-toggle-pop-to-buffer-after-send-region
     :style toggle
     :selected sql-pop-to-buffer-after-send-region]
-   ("Highlighting"
-    ["ANSI SQL keywords" sql-highlight-ansi-keywords t]
-    ["Oracle keywords" sql-highlight-oracle-keywords t]
-    ["Postgres keywords" sql-highlight-postgres-keywords t])))
+   ["--" nil nil]
+   ("Product"
+    ["ANSI" sql-highlight-ansi-keywords
+     :style radio
+     :selected (eq sql-product 'ansi)]
+    ["DB2" sql-highlight-db2-keywords
+     :style radio
+     :selected (eq sql-product 'db2)]
+    ["Informix" sql-highlight-informix-keywords
+     :style radio
+     :selected (eq sql-product 'informix)]
+    ["Ingres" sql-highlight-ingres-keywords
+     :style radio
+     :selected (eq sql-product 'ingres)]
+    ["Interbase" sql-highlight-interbase-keywords
+     :style radio
+     :selected (eq sql-product 'interbase)]
+    ["Linter" sql-highlight-linter-keywords
+     :style radio
+     :selected (eq sql-product 'linter)]
+    ["MS SQLServer" sql-highlight-ms-keywords
+     :style radio
+     :selected (eq sql-product 'ms)]
+    ["MySQL" sql-highlight-mysql-keywords
+     :style radio
+     :selected (eq sql-product 'mysql)]
+    ["Oracle" sql-highlight-oracle-keywords
+     :style radio
+     :selected (eq sql-product 'oracle)]
+    ["Postgres" sql-highlight-postgres-keywords
+     :style radio
+     :selected (eq sql-product 'postgres)]
+    ["Solid" sql-highlight-solid-keywords
+     :style radio
+     :selected (eq sql-product 'solid)]
+    ["SQLite" sql-highlight-sqlite-keywords
+     :style radio
+     :selected (eq sql-product 'sqlite)]
+    ["Sybase" sql-highlight-sybase-keywords
+     :style radio
+     :selected (eq sql-product 'sybase)]
+    )))
 
 ;; easy menu for sql-interactive-mode.
 
@@ -506,14 +846,24 @@ Based on `comint-mode-map'.")
 
 (defvar sql-mode-abbrev-table nil
   "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
-(if sql-mode-abbrev-table
-    ()
-  (let ((wrapper))
-    (define-abbrev-table 'sql-mode-abbrev-table ())
-    (define-abbrev sql-mode-abbrev-table  "ins" "insert" nil)
-    (define-abbrev sql-mode-abbrev-table  "upd" "update" nil)
-    (define-abbrev sql-mode-abbrev-table  "del" "delete" nil)
-    (define-abbrev sql-mode-abbrev-table  "sel" "select" nil)))
+(unless sql-mode-abbrev-table
+  (define-abbrev-table 'sql-mode-abbrev-table nil)
+  (mapcar
+    ;; In Emacs 21.3+, provide SYSTEM-FLAG to define-abbrev.
+   '(lambda (abbrev)
+      (let ((name (car abbrev))
+           (expansion (cdr abbrev)))
+       (condition-case nil
+           (define-abbrev sql-mode-abbrev-table name expansion nil 0 t)
+         (error
+          (define-abbrev sql-mode-abbrev-table name expansion)))))
+   '(("ins" "insert")
+    ("upd" "update")
+    ("del" "delete")
+    ("sel" "select")
+    ("proc" "procedure")
+    ("func" "function")
+    ("cr" "create"))))
 
 ;; Syntax Table
 
@@ -523,20 +873,132 @@ Based on `comint-mode-map'.")
     (modify-syntax-entry ?/ ". 14" table)
     (modify-syntax-entry ?* ". 23" table)
     ;; double-dash starts comment
-    (if (string-match "XEmacs\\|Lucid" emacs-version)
-       (modify-syntax-entry ?- ". 56" table)
-      (modify-syntax-entry ?- ". 12b" table))
+    (modify-syntax-entry ?- ". 12b" table)
     ;; newline and formfeed end coments
     (modify-syntax-entry ?\n "> b" table)
     (modify-syntax-entry ?\f "> b" table)
     ;; single quotes (') quotes delimit strings
     (modify-syntax-entry ?' "\"" table)
+    ;; backslash is no escape character
+    (modify-syntax-entry ?\\ "." table)
     table)
   "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
 
 ;; Font lock support
 
-(defvar sql-mode-ansi-font-lock-keywords nil
+(defvar sql-mode-font-lock-object-name
+  (list (concat "^\\s-*\\(create\\|drop\\|alter\\)\\s-+" ;; lead off with CREATE, DROP or ALTER
+               "\\(\\w+\\s-+\\)*"  ;; optional intervening keywords
+               "\\(table\\|view\\|package\\(\\s-+body\\)?\\|proc\\(edure\\)?"
+               "\\|function\\|trigger\\|sequence\\|rule\\|default\\)\\s-+"
+               "\\(\\w+\\)")
+       6 'font-lock-function-name-face)
+
+  "Pattern to match the names of top-level objects.
+
+The pattern matches the name in a CREATE, DROP or ALTER
+statement.  The format of variable should be a valid
+`font-lock-keywords' entry.")
+
+(defmacro sql-keywords-re (&rest keywords)
+  "Compile-time generation of regexp matching any one of KEYWORDS."
+  `(eval-when-compile
+     (concat "\\b"
+            (regexp-opt ',keywords t)
+            "\\b")))
+
+(defvar sql-mode-ansi-font-lock-keywords
+  (let ((ansi-funcs (sql-keywords-re
+"abs" "avg" "bit_length" "cardinality" "cast" "char_length"
+"character_length" "coalesce" "convert" "count" "current_date"
+"current_path" "current_role" "current_time" "current_timestamp"
+"current_user" "extract" "localtime" "localtimestamp" "lower" "max"
+"min" "mod" "nullif" "octet_length" "overlay" "placing" "session_user"
+"substring" "sum" "system_user" "translate" "treat" "trim" "upper"
+"user"
+))
+
+       (ansi-non-reserved (sql-keywords-re
+"ada" "asensitive" "assignment" "asymmetric" "atomic" "between"
+"bitvar" "called" "catalog_name" "chain" "character_set_catalog"
+"character_set_name" "character_set_schema" "checked" "class_origin"
+"cobol" "collation_catalog" "collation_name" "collation_schema"
+"column_name" "command_function" "command_function_code" "committed"
+"condition_number" "connection_name" "constraint_catalog"
+"constraint_name" "constraint_schema" "contains" "cursor_name"
+"datetime_interval_code" "datetime_interval_precision" "defined"
+"definer" "dispatch" "dynamic_function" "dynamic_function_code"
+"existing" "exists" "final" "fortran" "generated" "granted"
+"hierarchy" "hold" "implementation" "infix" "insensitive" "instance"
+"instantiable" "invoker" "key_member" "key_type" "length" "m"
+"message_length" "message_octet_length" "message_text" "method" "more"
+"mumps" "name" "nullable" "number" "options" "overlaps" "overriding"
+"parameter_mode" "parameter_name" "parameter_ordinal_position"
+"parameter_specific_catalog" "parameter_specific_name"
+"parameter_specific_schema" "pascal" "pli" "position" "repeatable"
+"returned_length" "returned_octet_length" "returned_sqlstate"
+"routine_catalog" "routine_name" "routine_schema" "row_count" "scale"
+"schema_name" "security" "self" "sensitive" "serializable"
+"server_name" "similar" "simple" "source" "specific_name" "style"
+"subclass_origin" "sublist" "symmetric" "system" "table_name"
+"transaction_active" "transactions_committed"
+"transactions_rolled_back" "transform" "transforms" "trigger_catalog"
+"trigger_name" "trigger_schema" "type" "uncommitted" "unnamed"
+"user_defined_type_catalog" "user_defined_type_name"
+"user_defined_type_schema"
+))
+
+       (ansi-reserved (sql-keywords-re
+"absolute" "action" "add" "admin" "after" "aggregate" "alias" "all"
+"allocate" "alter" "and" "any" "are" "as" "asc" "assertion" "at"
+"authorization" "before" "begin" "both" "breadth" "by" "call"
+"cascade" "cascaded" "case" "catalog" "check" "class" "close"
+"collate" "collation" "column" "commit" "completion" "connect"
+"connection" "constraint" "constraints" "constructor" "continue"
+"corresponding" "create" "cross" "cube" "current" "cursor" "cycle"
+"data" "day" "deallocate" "declare" "default" "deferrable" "deferred"
+"delete" "depth" "deref" "desc" "describe" "descriptor" "destroy"
+"destructor" "deterministic" "diagnostics" "dictionary" "disconnect"
+"distinct" "domain" "drop" "dynamic" "each" "else" "end" "equals"
+"escape" "every" "except" "exception" "exec" "execute" "external"
+"false" "fetch" "first" "for" "foreign" "found" "free" "from" "full"
+"function" "general" "get" "global" "go" "goto" "grant" "group"
+"grouping" "having" "host" "hour" "identity" "ignore" "immediate" "in"
+"indicator" "initialize" "initially" "inner" "inout" "input" "insert"
+"intersect" "into" "is" "isolation" "iterate" "join" "key" "language"
+"last" "lateral" "leading" "left" "less" "level" "like" "limit"
+"local" "locator" "map" "match" "minute" "modifies" "modify" "module"
+"month" "names" "natural" "new" "next" "no" "none" "not" "null" "of"
+"off" "old" "on" "only" "open" "operation" "option" "or" "order"
+"ordinality" "out" "outer" "output" "pad" "parameter" "parameters"
+"partial" "path" "postfix" "prefix" "preorder" "prepare" "preserve"
+"primary" "prior" "privileges" "procedure" "public" "read" "reads"
+"recursive" "references" "referencing" "relative" "restrict" "result"
+"return" "returns" "revoke" "right" "role" "rollback" "rollup"
+"routine" "rows" "savepoint" "schema" "scroll" "search" "second"
+"section" "select" "sequence" "session" "set" "sets" "size" "some"
+"space" "specific" "specifictype" "sql" "sqlexception" "sqlstate"
+"sqlwarning" "start" "state" "statement" "static" "structure" "table"
+"temporary" "terminate" "than" "then" "timezone_hour"
+"timezone_minute" "to" "trailing" "transaction" "translation"
+"trigger" "true" "under" "union" "unique" "unknown" "unnest" "update"
+"usage" "using" "value" "values" "variable" "view" "when" "whenever"
+"where" "with" "without" "work" "write" "year"
+))
+
+       (ansi-types (sql-keywords-re
+"array" "binary" "bit" "blob" "boolean" "char" "character" "clob"
+"date" "dec" "decimal" "double" "float" "int" "integer" "interval"
+"large" "national" "nchar" "nclob" "numeric" "object" "precision"
+"real" "ref" "row" "scope" "smallint" "time" "timestamp" "varchar"
+"varying" "zone"
+)))
+
+    `((,ansi-non-reserved . font-lock-keyword-face)
+      (,ansi-reserved     . font-lock-keyword-face)
+      (,ansi-funcs        . font-lock-builtin-face)
+      (,ansi-types        . font-lock-type-face)))
+
   "ANSI SQL keywords used by font-lock.
 
 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
@@ -544,39 +1006,215 @@ regular expressions are created during compilation by calling the
 function `regexp-opt'.  Therefore, take a look at the source before
 you define your own sql-mode-ansi-font-lock-keywords.  You may want to
 add functions and PL/SQL keywords.")
-(if sql-mode-ansi-font-lock-keywords
-    ()
-  (let ((ansi-keywords (eval-when-compile
-                        (concat "\\b"
-                                (regexp-opt '(
-"authorization" "avg" "begin" "close" "cobol" "commit"
-"continue" "count" "declare" "double" "end" "escape"
-"exec" "fetch" "foreign" "fortran" "found" "go" "goto" "indicator"
-"key" "language" "max" "min" "module" "numeric" "open" "pascal" "pli"
-"precision" "primary" "procedure" "references" "rollback"
-"schema" "section" "some" "sqlcode" "sqlerror" "sum" "work") t) "\\b")))
-       (ansi-reserved-words (eval-when-compile
-                              (concat "\\b"
-                                      (regexp-opt '(
-"all" "and" "any" "as" "asc" "between" "by" "check" "create"
-"current" "default" "delete" "desc" "distinct" "exists" "float" "for"
-"from" "grant" "group" "having" "in" "insert" "into" "is"
-"like" "not" "null" "of" "on" "option" "or" "order" "privileges"
-"public" "select" "set" "table" "to" "union" "unique"
-"update" "user" "values" "view" "where" "with") t) "\\b")))
-       (ansi-types (eval-when-compile
-                     (concat "\\b"
-                             (regexp-opt '(
-;; ANSI Keywords that look like types
-"character" "cursor" "dec" "int" "real"
-;; ANSI Reserved Word that look like types
-"char" "integer" "smallint" ) t) "\\b"))))
-    (setq sql-mode-ansi-font-lock-keywords
-         (list (cons ansi-keywords 'font-lock-function-name-face)
-               (cons ansi-reserved-words 'font-lock-keyword-face)
-               (cons ansi-types 'font-lock-type-face)))))
-
-(defvar sql-mode-oracle-font-lock-keywords nil
+
+(defvar sql-mode-oracle-font-lock-keywords
+  (let ((oracle-functions (sql-keywords-re
+"abs" "acos" "add_months" "ascii" "asciistr" "asin" "atan" "atan2"
+"avg" "bfilename" "bin_to_num" "bitand" "cast" "ceil" "chartorowid"
+"chr" "coalesce" "compose" "concat" "convert" "corr" "cos" "cosh"
+"count" "covar_pop" "covar_samp" "cume_dist" "current_date"
+"current_timestamp" "current_user" "dbtimezone" "decode" "decompose"
+"dense_rank" "depth" "deref" "dump" "empty_clob" "existsnode" "exp"
+"extract" "extractvalue" "first" "first_value" "floor" "following"
+"from_tz" "greatest" "group_id" "grouping_id" "hextoraw" "initcap"
+"instr" "lag" "last" "last_day" "last_value" "lead" "least" "length"
+"ln" "localtimestamp" "lower" "lpad" "ltrim" "make_ref" "max" "min"
+"mod" "months_between" "new_time" "next_day" "nls_charset_decl_len"
+"nls_charset_id" "nls_charset_name" "nls_initcap" "nls_lower"
+"nls_upper" "nlssort" "ntile" "nullif" "numtodsinterval"
+"numtoyminterval" "nvl" "nvl2" "over" "path" "percent_rank"
+"percentile_cont" "percentile_disc" "power" "preceding" "rank"
+"ratio_to_report" "rawtohex" "rawtonhex" "reftohex" "regr_"
+"regr_avgx" "regr_avgy" "regr_count" "regr_intercept" "regr_r2"
+"regr_slope" "regr_sxx" "regr_sxy" "regr_syy" "replace" "round"
+"row_number" "rowidtochar" "rowidtonchar" "rpad" "rtrim"
+"sessiontimezone" "sign" "sin" "sinh" "soundex" "sqrt" "stddev"
+"stddev_pop" "stddev_samp" "substr" "sum" "sys_connect_by_path"
+"sys_context" "sys_dburigen" "sys_extract_utc" "sys_guid" "sys_typeid"
+"sys_xmlagg" "sys_xmlgen" "sysdate" "systimestamp" "tan" "tanh"
+"to_char" "to_clob" "to_date" "to_dsinterval" "to_lob" "to_multi_byte"
+"to_nchar" "to_nclob" "to_number" "to_single_byte" "to_timestamp"
+"to_timestamp_tz" "to_yminterval" "translate" "treat" "trim" "trunc"
+"tz_offset" "uid" "unbounded" "unistr" "updatexml" "upper" "user"
+"userenv" "var_pop" "var_samp" "variance" "vsize" "width_bucket" "xml"
+"xmlagg" "xmlattribute" "xmlcolattval" "xmlconcat" "xmlelement"
+"xmlforest" "xmlsequence" "xmltransform"
+))
+
+       (oracle-keywords (sql-keywords-re
+"abort" "access" "accessed" "account" "activate" "add" "admin"
+"advise" "after" "agent" "aggregate" "all" "allocate" "allow" "alter"
+"always" "analyze" "ancillary" "and" "any" "apply" "archive"
+"archivelog" "array" "as" "asc" "associate" "at" "attribute"
+"attributes" "audit" "authenticated" "authid" "authorization" "auto"
+"autoallocate" "automatic" "availability" "backup" "before" "begin"
+"behalf" "between" "binding" "bitmap" "block" "blocksize" "body"
+"both" "buffer_pool" "build" "by"  "cache" "call" "cancel"
+"cascade" "case" "category" "certificate" "chained" "change" "check"
+"checkpoint" "child" "chunk" "class" "clear" "clone" "close" "cluster"
+"column" "column_value" "columns" "comment" "commit" "committed"
+"compatibility" "compile" "complete" "composite_limit" "compress"
+"compute" "connect" "connect_time" "consider" "consistent"
+"constraint" "constraints" "constructor" "contents" "context"
+"continue" "controlfile" "corruption" "cost" "cpu_per_call"
+"cpu_per_session" "create" "cross" "cube" "current" "currval" "cycle"
+"dangling" "data" "database" "datafile" "datafiles" "day" "ddl"
+"deallocate" "debug" "default" "deferrable" "deferred" "definer"
+"delay" "delete" "demand" "desc" "determines" "deterministic"
+"dictionary" "dimension" "directory" "disable" "disassociate"
+"disconnect" "distinct" "distinguished" "distributed" "dml" "drop"
+"each" "element" "else" "enable" "end" "equals_path" "escape"
+"estimate" "except" "exceptions" "exchange" "excluding" "exists"
+"expire" "explain" "extent" "external" "externally"
+"failed_login_attempts" "fast" "file" "final" "finish" "flush" "for"
+"force" "foreign" "freelist" "freelists" "freepools" "fresh" "from"
+"full" "function" "functions" "generated" "global" "global_name"
+"globally" "grant" "group" "grouping" "groups" "guard" "hash"
+"hashkeys" "having" "heap" "hierarchy" "id" "identified" "identifier"
+"idle_time" "immediate" "in" "including" "increment" "index" "indexed"
+"indexes" "indextype" "indextypes" "indicator" "initial" "initialized"
+"initially" "initrans" "inner" "insert" "instance" "instantiable"
+"instead" "intersect" "into" "invalidate" "is" "isolation" "java"
+"join"  "keep" "key" "kill" "language" "left" "less" "level"
+"levels" "library" "like" "like2" "like4" "likec" "limit" "link"
+"list" "lob" "local" "location" "locator" "lock" "log" "logfile"
+"logging" "logical" "logical_reads_per_call"
+"logical_reads_per_session"  "managed" "management" "manual" "map"
+"mapping" "master" "matched" "materialized" "maxdatafiles"
+"maxextents" "maximize" "maxinstances" "maxlogfiles" "maxloghistory"
+"maxlogmembers" "maxsize" "maxtrans" "maxvalue" "member" "memory"
+"merge" "migrate" "minextents" "minimize" "minimum" "minus" "minvalue"
+"mode" "modify" "monitoring" "month" "mount" "move" "movement" "name"
+"named" "natural" "nested" "never" "new" "next" "nextval" "no"
+"noarchivelog" "noaudit" "nocache" "nocompress" "nocopy" "nocycle"
+"nodelay" "noforce" "nologging" "nomapping" "nomaxvalue" "nominimize"
+"nominvalue" "nomonitoring" "none" "noorder" "noparallel" "norely"
+"noresetlogs" "noreverse" "normal" "norowdependencies" "nosort"
+"noswitch" "not" "nothing" "notimeout" "novalidate" "nowait" "null"
+"nulls" "object" "of" "off" "offline" "oidindex" "old" "on" "online"
+"only" "open" "operator" "optimal" "option" "or" "order"
+"organization" "out" "outer" "outline" "overflow" "overriding"
+"package" "packages" "parallel" "parallel_enable" "parameters"
+"parent" "partition" "partitions" "password" "password_grace_time"
+"password_life_time" "password_lock_time" "password_reuse_max"
+"password_reuse_time" "password_verify_function" "pctfree"
+"pctincrease" "pctthreshold" "pctused" "pctversion" "percent"
+"performance" "permanent" "pfile" "physical" "pipelined" "plan"
+"post_transaction" "pragma" "prebuilt" "preserve" "primary" "private"
+"private_sga" "privileges" "procedure" "profile" "protection" "public"
+"purge" "query" "quiesce" "quota" "range" "read" "reads" "rebuild"
+"records_per_block" "recover" "recovery" "recycle" "reduced" "ref"
+"references" "referencing" "refresh" "register" "reject" "relational"
+"rely" "rename" "reset" "resetlogs" "resize" "resolve" "resolver"
+"resource" "restrict" "restrict_references" "restricted" "result"
+"resumable" "resume" "retention" "return" "returning" "reuse"
+"reverse" "revoke" "rewrite" "right" "rnds" "rnps" "role" "roles"
+"rollback" "rollup" "row" "rowdependencies" "rownum" "rows" "sample"
+"savepoint" "scan" "schema" "scn" "scope" "segment" "select"
+"selectivity" "self" "sequence" "serializable" "session"
+"sessions_per_user" "set" "sets" "settings" "shared" "shared_pool"
+"shrink" "shutdown" "siblings" "sid" "single" "size" "skip" "some"
+"sort" "source" "space" "specification" "spfile" "split" "standby"
+"start" "statement_id" "static" "statistics" "stop" "storage" "store"
+"structure" "subpartition" "subpartitions" "substitutable"
+"successful" "supplemental" "suspend" "switch" "switchover" "synonym"
+"sys" "system" "table" "tables" "tablespace" "tempfile" "template"
+"temporary" "test" "than" "then" "thread" "through" "time_zone"
+"timeout" "to" "trace" "transaction" "trigger" "triggers" "truncate"
+"trust" "type" "types" "unarchived" "under" "under_path" "undo"
+"uniform" "union" "unique" "unlimited" "unlock" "unquiesce"
+"unrecoverable" "until" "unusable" "unused" "update" "upgrade" "usage"
+"use" "using" "validate" "validation" "value" "values" "variable"
+"varray" "version" "view" "wait" "when" "whenever" "where" "with"
+"without" "wnds" "wnps" "work" "write" "xmldata" "xmlschema" "xmltype"
+))
+
+       (oracle-types (sql-keywords-re
+"bfile" "blob" "byte" "char" "character" "clob" "date" "dec" "decimal"
+"double" "float" "int" "integer" "interval" "long" "national" "nchar"
+"nclob" "number" "numeric" "nvarchar2" "precision" "raw" "real"
+"rowid" "second" "smallint" "time" "timestamp" "urowid" "varchar"
+"varchar2" "varying" "year" "zone"
+))
+
+       (plsql-functions (sql-keywords-re
+"%bulk_rowcount" "%found" "%isopen" "%notfound" "%rowcount" "%rowtype"
+"%type" "extend" "prior"
+))
+
+       (plsql-keywords (sql-keywords-re
+"autonomous_transaction" "bulk" "char_base" "collect" "constant"
+"cursor" "declare" "do" "elsif" "exception_init" "execute" "exit"
+"extends" "false" "fetch" "forall" "goto" "hour" "if" "interface"
+"loop" "minute" "number_base" "ocirowid" "opaque" "others" "rowtype"
+"separate" "serially_reusable" "sql" "sqlcode" "sqlerrm" "subtype"
+"the" "timezone_abbr" "timezone_hour" "timezone_minute"
+"timezone_region" "true" "varrying" "while"
+))
+
+       (plsql-type (sql-keywords-re
+"binary_integer" "boolean" "naturaln" "pls_integer" "positive"
+"positiven" "record" "signtype" "string"
+))
+
+       (plsql-warning (sql-keywords-re
+"access_into_null" "case_not_found" "collection_is_null"
+"cursor_already_open" "dup_val_on_index" "invalid_cursor"
+"invalid_number" "login_denied" "no_data_found" "not_logged_on"
+"program_error" "rowtype_mismatch" "self_is_null" "storage_error"
+"subscript_beyond_count" "subscript_outside_limit" "sys_invalid_rowid"
+"timeout_on_resource" "too_many_rows" "value_error" "zero_divide"
+"exception" "notfound"
+))
+
+       (sqlplus-commands
+        (eval-when-compile (concat "^\\(\\("
+                                   (regexp-opt '(
+"@" "@@" "accept" "append" "archive" "attribute" "break"
+"btitle" "change" "clear" "column" "connect" "copy" "define"
+"del" "describe" "disconnect" "edit" "execute" "exit" "get" "help"
+"host" "input" "list" "password" "pause" "print" "prompt" "recover"
+"remark" "repfooter" "repheader" "run" "save" "show" "shutdown"
+"spool" "start" "startup" "store" "timing" "ttitle" "undefine"
+"variable" "whenever"
+
+) t)
+
+   "\\)\\|"
+   "\\(compute\\s-+\\(avg\\|cou\\|min\\|max\\|num\\|sum\\|std\\|var\\)\\)\\|"
+   "\\(set\\s-+\\(appi\\(nfo\\)?\\|array\\(size\\)?\\|"
+   "auto\\(commit\\)?\\|autop\\(rint\\)?\\|autorecovery\\|"
+   "autot\\(race\\)?\\|blo\\(ckterminator\\)?\\|cmds\\(ep\\)?\\|"
+   "colsep\\|com\\(patibility\\)?\\|con\\(cat\\)?\\|"
+   "copyc\\(ommit\\)?\\|copytypecheck\\|def\\(ine\\)?\\|"
+   "describe\\|echo\\|editf\\(ile\\)?\\|emb\\(edded\\)?\\|"
+   "esc\\(ape\\)?\\|feed\\(back\\)?\\|flagger\\|"
+   "flu\\(sh\\)?\\|hea\\(ding\\)?\\|heads\\(ep\\)?\\|"
+   "instance\\|lin\\(esize\\)?\\|lobof\\(fset\\)?\\|"
+   "logsource\\|long\\|longc\\(hunksize\\)?\\|mark\\(up\\)?\\|"
+   "newp\\(age\\)?\\|null\\|numf\\(ormat\\)?\\|"
+   "num\\(width\\)?\\|pages\\(ize\\)?\\|pau\\(se\\)?\\|"
+   "recsep\\|recsepchar\\|serverout\\(put\\)?\\|"
+   "shift\\(inout\\)?\\|show\\(mode\\)?\\|"
+   "sqlbl\\(anklines\\)?\\|sqlc\\(ase\\)?\\|"
+   "sqlco\\(ntinue\\)?\\|sqln\\(umber\\)?\\|"
+   "sqlpluscompat\\(ibility\\)?\\|sqlpre\\(fix\\)?\\|"
+   "sqlp\\(rompt\\)?\\|sqlt\\(erminator\\)?\\|"
+   "suf\\(fix\\)?\\|tab\\|term\\(out\\)?\\|ti\\(me\\)?\\|"
+   "timi\\(ng\\)?\\|trim\\(out\\)?\\|trims\\(pool\\)?\\|"
+   "und\\(erline\\)?\\|ver\\(ify\\)?\\|wra\\(p\\)?\\)\\)\\)"
+   "\\b.*$"
+   ))))
+
+    `((,sqlplus-commands . font-lock-doc-face)
+      (,oracle-functions . font-lock-builtin-face)
+      (,oracle-keywords  . font-lock-keyword-face)
+      (,oracle-types     . font-lock-type-face)
+      (,plsql-functions  . font-lock-builtin-face)
+      (,plsql-keywords   . font-lock-keyword-face)
+      (,plsql-type       . font-lock-type-face)
+      (,plsql-warning    . font-lock-warning-face)))
+
   "Oracle SQL keywords used by font-lock.
 
 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
@@ -584,89 +1222,87 @@ regular expressions are created during compilation by calling the
 function `regexp-opt'.  Therefore, take a look at the source before
 you define your own sql-mode-oracle-font-lock-keywords.  You may want
 to add functions and PL/SQL keywords.")
-(if sql-mode-oracle-font-lock-keywords
-    ()
-  (let ((oracle-keywords (eval-when-compile
-                          (concat "\\b"
-                                  (regexp-opt '(
-"admin" "after" "allocate" "analyze" "archive" "archivelog" "backup"
-"become" "before" "block" "body" "cache" "cancel" "cascade" "change"
-"checkpoint" "compile" "constraint" "constraints" "contents"
-"controlfile" "cycle" "database" "datafile" "dba" "disable" "dismount"
-"dump" "each" "else" "elsif" "enable" "events" "except" "exceptions"
-"execute" "exit" "explain" "extent" "externally" "false" "flush" "force"
-"freelist" "freelists" "function" "groups" "if" "including" "initrans"
-"instance" "layer" "link" "lists" "logfile" "loop" "manage" "manual"
-"maxdatafiles" "maxinistances" "maxlogfiles" "maxloghistory"
-"maxlogmembers" "maxtrans" "maxvalue" "minextents" "minvalue" "mount"
-"new" "next" "noarchivelog" "nocache" "nocycle" "nomaxvalue"
-"nominvalue" "none" "noorder" "noresetlogs" "normal" "nosort" "off"
-"old" "only" "optimal" "others" "out" "own" "package" "parallel"
-"pctincrease" "pctused" "plan" "pragma" "private" "profile" "quota"
-"raise" "read" "recover" "referencing" "resetlogs" "restrict_references"
-"restricted" "return" "returning" "reuse" "rnds" "rnps" "role" "roles"
-"savepoint" "scn" "segment" "sequence" "shared" "snapshot" "sort"
-"statement_id" "statistics" "stop" "storage" "subtype" "switch" "system"
-"tables" "tablespace" "temporary" "thread" "time" "tracing"
-"transaction" "triggers" "true" "truncate" "type" "under" "unlimited"
-"until" "use" "using" "when" "while" "wnds" "wnps" "write") t) "\\b")))
-       (oracle-warning-words (eval-when-compile
-                                (concat "\\b"
-                                        (regexp-opt '(
-"cursor_already_open" "dup_val_on_index" "exception" "invalid_cursor"
-"invalid_number" "login_denied" "no_data_found" "not_logged_on"
-"notfound" "others" "pragma" "program_error" "storage_error"
-"timeout_on_resource" "too_many_rows" "transaction_backed_out"
-"value_error" "zero_divide") t) "\\b")))
-       (oracle-reserved-words (eval-when-compile
-                                (concat "\\b"
-                                        (regexp-opt '(
-"access" "add" "alter" "audit" "cluster" "column" "comment" "compress"
-"connect" "drop" "else" "exclusive" "file" "grant"
-"identified" "immediate" "increment" "index" "initial" "intersect"
-"level" "lock" "long" "maxextents" "minus" "mode" "modify" "noaudit"
-"nocompress" "nowait" "number" "offline" "online" "pctfree" "prior"
-"raw" "rename" "resource" "revoke" "row" "rowlabel" "rownum"
-"rows" "session" "share" "size" "start" "successful" "synonym" "sysdate"
-"then" "trigger" "uid" "validate" "whenever") t) "\\b")))
-       (oracle-types (eval-when-compile
-                       (concat "\\b"
-                               (regexp-opt '(
-;; Oracle Keywords that look like types
-;; Oracle Reserved Words that look like types
-"binary_integer" "blob" "boolean" "constant" "date" "decimal" "rowid"
-"varchar" "varchar2") t) "\\b")))
-       (oracle-builtin-functions (eval-when-compile
-                       (concat "\\b"
-                               (regexp-opt '(
-;; Misc Oracle builtin functions
-"abs" "add_months" "ascii" "avg" "ceil" "chartorowid" "chr" "concat"
-"convert" "cos" "cosh" "count" "currval" "decode" "dump" "exp" "floor"
-"glb" "greatest" "greatest_lb" "hextoraw" "initcap" "instr" "instrb"
-"last_day" "least" "least_ub" "length" "lengthb" "ln" "log" "lower"
-"lpad" "ltrim" "lub" "max" "min" "mod" "months_between" "new_time"
-"next_day" "nextval" "nls_initcap" "nls_lower" "nls_upper" "nlssort"
-"nvl" "power" "rawtohex" "replace" "round" "rowidtochar" "rpad"
-"rtrim" "sign" "sin" "sinh" "soundex" "sqlcode" "sqlerrm" "sqrt"
-"stddev" "sum" "substr" "substrb" "tan" "tanh" "to_char"
-"to_date" "to_label" "to_multi_byte" "to_number" "to_single_byte"
-"translate" "trim" "trunc" "uid" "upper" "userenv" "variance" "vsize") t) "\\b"))))
-    (setq sql-mode-oracle-font-lock-keywords
-         (append sql-mode-ansi-font-lock-keywords
-                 (list (cons oracle-keywords 'font-lock-function-name-face)
-                       (cons oracle-warning-words 'font-lock-warning-face)
-                       (cons oracle-reserved-words 'font-lock-keyword-face)
-                       ;; XEmacs doesn't have font-lock-builtin-face
-                       (if (string-match "XEmacs\\|Lucid" emacs-version)
-                           (cons oracle-builtin-functions 'font-lock-preprocessor-face)
-                         ;; GNU Emacs 19 doesn't have it either
-                         (if (string-match "GNU Emacs 19" emacs-version)
-                             (cons oracle-builtin-functions 'font-lock-function-name-face)
-                           ;; Emacs
-                           (cons oracle-builtin-functions 'font-lock-builtin-face)))
-                       (cons oracle-types 'font-lock-type-face))))))
-
-(defvar sql-mode-postgres-font-lock-keywords nil
+
+(defvar sql-mode-postgres-font-lock-keywords
+  (let ((pg-funcs (sql-keywords-re
+"abbrev" "abs" "acos" "age" "area" "ascii" "asin" "atab2" "atan"
+"atan2" "avg" "bit_length" "both" "broadcast" "btrim" "cbrt" "ceil"
+"center" "char_length" "chr" "coalesce" "col_description" "convert"
+"cos" "cot" "count" "current_database" "current_date" "current_schema"
+"current_schemas" "current_setting" "current_time" "current_timestamp"
+"current_user" "currval" "date_part" "date_trunc" "decode" "degrees"
+"diameter" "encode" "exp" "extract" "floor" "get_bit" "get_byte"
+"has_database_privilege" "has_function_privilege"
+"has_language_privilege" "has_schema_privilege" "has_table_privilege"
+"height" "host" "initcap" "isclosed" "isfinite" "isopen" "leading"
+"length" "ln" "localtime" "localtimestamp" "log" "lower" "lpad"
+"ltrim" "masklen" "max" "min" "mod" "netmask" "network" "nextval"
+"now" "npoints" "nullif" "obj_description" "octet_length" "overlay"
+"pclose" "pg_client_encoding" "pg_function_is_visible"
+"pg_get_constraintdef" "pg_get_indexdef" "pg_get_ruledef"
+"pg_get_userbyid" "pg_get_viewdef" "pg_opclass_is_visible"
+"pg_operator_is_visible" "pg_table_is_visible" "pg_type_is_visible"
+"pi" "popen" "position" "pow" "quote_ident" "quote_literal" "radians"
+"radius" "random" "repeat" "replace" "round" "rpad" "rtrim"
+"session_user" "set_bit" "set_byte" "set_config" "set_masklen"
+"setval" "sign" "sin" "split_part" "sqrt" "stddev" "strpos" "substr"
+"substring" "sum" "tan" "timeofday" "to_ascii" "to_char" "to_date"
+"to_hex" "to_number" "to_timestamp" "trailing" "translate" "trim"
+"trunc" "upper" "variance" "version" "width"
+))
+
+       (pg-reserved (sql-keywords-re
+"abort" "access" "add" "after" "aggregate" "alignment" "all" "alter"
+"analyze" "and" "any" "as" "asc" "assignment" "authorization"
+"backward" "basetype" "before" "begin" "between" "binary" "by" "cache"
+"called" "cascade" "case" "cast" "characteristics" "check"
+"checkpoint" "class" "close" "cluster" "column" "comment" "commit"
+"committed" "commutator" "constraint" "constraints" "conversion"
+"copy" "create" "createdb" "createuser" "cursor" "cycle" "database"
+"deallocate" "declare" "default" "deferrable" "deferred" "definer"
+"delete" "delimiter" "desc" "distinct" "do" "domain" "drop" "each"
+"element" "else" "encoding" "encrypted" "end" "escape" "except"
+"exclusive" "execute" "exists" "explain" "extended" "external" "false"
+"fetch" "finalfunc" "for" "force" "foreign" "forward" "freeze" "from"
+"full" "function" "grant" "group" "gtcmp" "handler" "hashes" "having"
+"immediate" "immutable" "implicit" "in" "increment" "index" "inherits"
+"initcond" "initially" "input" "insensitive" "insert" "instead"
+"internallength" "intersect" "into" "invoker" "is" "isnull"
+"isolation" "join" "key" "language" "leftarg" "level" "like" "limit"
+"listen" "load" "local" "location" "lock" "ltcmp" "main" "match"
+"maxvalue" "merges" "minvalue" "mode" "move" "natural" "negator"
+"next" "nocreatedb" "nocreateuser" "none" "not" "nothing" "notify"
+"notnull" "null" "of" "offset" "oids" "on" "only" "operator" "or"
+"order" "output" "owner" "partial" "passedbyvalue" "password" "plain"
+"prepare" "primary" "prior" "privileges" "procedural" "procedure"
+"public" "read" "recheck" "references" "reindex" "relative" "rename"
+"reset" "restrict" "returns" "revoke" "rightarg" "rollback" "row"
+"rule" "schema" "scroll" "security" "select" "sequence" "serializable"
+"session" "set" "sfunc" "share" "show" "similar" "some" "sort1"
+"sort2" "stable" "start" "statement" "statistics" "storage" "strict"
+"stype" "sysid" "table" "temp" "template" "temporary" "then" "to"
+"transaction" "trigger" "true" "truncate" "trusted" "type"
+"unencrypted" "union" "unique" "unknown" "unlisten" "until" "update"
+"usage" "user" "using" "vacuum" "valid" "validator" "values"
+"variable" "verbose" "view" "volatile" "when" "where" "with" "without"
+"work"
+))
+
+       (pg-types (sql-keywords-re
+"anyarray" "bigint" "bigserial" "bit" "boolean" "box" "bytea" "char"
+"character" "cidr" "circle" "cstring" "date" "decimal" "double"
+"float4" "float8" "inet" "int2" "int4" "int8" "integer" "internal"
+"interval" "language_handler" "line" "lseg" "macaddr" "money"
+"numeric" "oid" "opaque" "path" "point" "polygon" "precision" "real"
+"record" "regclass" "regoper" "regoperator" "regproc" "regprocedure"
+"regtype" "serial" "serial4" "serial8" "smallint" "text" "time"
+"timestamp" "varchar" "varying" "void" "zone"
+)))
+
+  `((,pg-funcs    . font-lock-builtin-face)
+    (,pg-reserved . font-lock-keyword-face)
+    (,pg-types    . font-lock-type-face)))
+
   "Postgres SQL keywords used by font-lock.
 
 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
@@ -674,74 +1310,514 @@ regular expressions are created during compilation by calling the
 function `regexp-opt'.  Therefore, take a look at the source before
 you define your own sql-mode-postgres-font-lock-keywords.")
 
-(if sql-mode-postgres-font-lock-keywords
-    ()
-  (let ((postgres-reserved-words (eval-when-compile
-                                (concat "\\b"
-                                        (regexp-opt '(
-"language"
-) t) "\\b")))
-       (postgres-types (eval-when-compile
-                         (concat "\\b"
-                                 (regexp-opt '(
-"bool" "box" "circle" "char" "char2" "char4" "char8" "char16" "date"
-"float4" "float8" "int2" "int4" "int8" "line" "lseg" "money" "path"
-"point" "polygon" "serial" "text" "time" "timespan" "timestamp" "varchar"
-) t)"\\b")))
-       (postgres-builtin-functions (eval-when-compile
-                       (concat "\\b"
-                               (regexp-opt '(
-;; Misc Postgres builtin functions
-"abstime" "age" "area" "box" "center" "date_part" "date_trunc"
-"datetime" "dexp" "diameter" "dpow" "float" "float4" "height"
-"initcap" "integer" "isclosed" "isfinite" "isoldpath" "isopen"
-"length" "lower" "lpad" "ltrim" "pclose" "point" "points" "popen"
-"position" "radius" "reltime" "revertpoly" "rpad" "rtrim" "substr"
-"substring" "text" "timespan" "translate" "trim" "upgradepath"
-"upgradepoly" "upper" "varchar" "width"
-) t) "\\b"))))
-    (setq sql-mode-postgres-font-lock-keywords
-         (append sql-mode-ansi-font-lock-keywords
-                 (list (cons postgres-reserved-words 'font-lock-keyword-face)
-                       ;; XEmacs doesn't have 'font-lock-builtin-face
-                       (if (string-match "XEmacs\\|Lucid" emacs-version)
-                           (cons postgres-builtin-functions 'font-lock-preprocessor-face)
-                         ;; Emacs
-                         (cons postgres-builtin-functions 'font-lock-builtin-face))
-                       (cons postgres-types 'font-lock-type-face))))))
-
-
-(defvar sql-mode-font-lock-keywords sql-mode-ansi-font-lock-keywords
+(defvar sql-mode-linter-font-lock-keywords
+  (let ((linter-keywords (sql-keywords-re
+"autocommit" "autoinc" "autorowid" "cancel" "cascade" "channel"
+"committed" "count" "countblob" "cross" "current" "data" "database"
+"datafile" "datafiles" "datesplit" "dba" "dbname" "default" "deferred"
+"denied" "description" "device" "difference" "directory" "error"
+"escape" "euc" "exclusive" "external" "extfile" "false" "file"
+"filename" "filesize" "filetime" "filter" "findblob" "first" "foreign"
+"full" "fuzzy" "global" "granted" "ignore" "immediate" "increment"
+"indexes" "indexfile" "indexfiles" "indextime" "initial" "integrity"
+"internal" "key" "last_autoinc" "last_rowid" "limit" "linter"
+"linter_file_device" "linter_file_size" "linter_name_length" "ln"
+"local" "login" "maxisn" "maxrow" "maxrowid" "maxvalue" "message"
+"minvalue" "module" "names" "national" "natural" "new" "new_table"
+"no" "node" "noneuc" "nulliferror" "numbers" "off" "old" "old_table"
+"only" "operation" "optimistic" "option" "page" "partially" "password"
+"phrase" "plan" "precision" "primary" "priority" "privileges"
+"proc_info_size" "proc_par_name_len" "protocol" "quant" "range" "raw"
+"read" "record" "records" "references" "remote" "rename" "replication"
+"restart" "rewrite" "root" "row" "rule" "savepoint" "security"
+"sensitive" "sequence" "serializable" "server" "since" "size" "some"
+"startup" "statement" "station" "success" "sys_guid" "tables" "test"
+"timeout" "trace" "transaction" "translation" "trigger"
+"trigger_info_size" "true" "trunc" "uncommitted" "unicode" "unknown"
+"unlimited" "unlisted" "user" "utf8" "value" "varying" "volumes"
+"wait" "windows_code" "workspace" "write" "xml"
+))
+
+       (linter-reserved (sql-keywords-re
+"access" "action" "add" "address" "after" "all" "alter" "always" "and"
+"any" "append" "as" "asc" "ascic" "async" "at_begin" "at_end" "audit"
+"aud_obj_name_len" "backup" "base" "before" "between" "blobfile"
+"blobfiles" "blobpct" "brief" "browse" "by" "case" "cast" "check"
+"clear" "close" "column" "comment" "commit" "connect" "contains"
+"correct" "create" "delete" "desc" "disable" "disconnect" "distinct"
+"drop" "each" "ef" "else" "enable" "end" "event" "except" "exclude"
+"execute" "exists" "extract" "fetch" "finish" "for" "from" "get"
+"grant" "group" "having" "identified" "in" "index" "inner" "insert"
+"instead" "intersect" "into" "is" "isolation" "join" "left" "level"
+"like" "lock" "mode" "modify" "not" "nowait" "null" "of" "on" "open"
+"or" "order" "outer" "owner" "press" "prior" "procedure" "public"
+"purge" "rebuild" "resource" "restrict" "revoke" "right" "role"
+"rollback" "rownum" "select" "session" "set" "share" "shutdown"
+"start" "stop" "sync" "synchronize" "synonym" "sysdate" "table" "then"
+"to" "union" "unique" "unlock" "until" "update" "using" "values"
+"view" "when" "where" "with" "without"
+))
+
+       (linter-types (sql-keywords-re
+"bigint" "bitmap" "blob" "boolean" "char" "character" "date"
+"datetime" "dec" "decimal" "double" "float" "int" "integer" "nchar"
+"number" "numeric" "real" "smallint" "varbyte" "varchar" "byte"
+"cursor" "long"
+))
+
+       (linter-functions (sql-keywords-re
+"abs" "acos" "asin" "atan" "atan2" "avg" "ceil" "cos" "cosh" "divtime"
+"exp" "floor" "getbits" "getblob" "getbyte" "getlong" "getraw"
+"getstr" "gettext" "getword" "hextoraw" "lenblob" "length" "log"
+"lower" "lpad" "ltrim" "max" "min" "mod" "monthname" "nvl"
+"octet_length" "power" "rand" "rawtohex" "repeat_string"
+"right_substr" "round" "rpad" "rtrim" "sign" "sin" "sinh" "soundex"
+"sqrt" "sum" "tan" "tanh" "timeint_to_days" "to_char" "to_date"
+"to_gmtime" "to_localtime" "to_number" "trim" "upper" "decode"
+"substr" "substring" "chr" "dayname" "days" "greatest" "hex" "initcap"
+"instr" "least" "multime" "replace" "width"
+)))
+
+    `((,linter-keywords  . font-lock-keyword-face)
+      (,linter-reserved  . font-lock-keyword-face)
+      (,linter-functions . font-lock-builtin-face)
+      (,linter-types     . font-lock-type-face)))
+
+  "Linter SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.")
+
+(defvar sql-mode-ms-font-lock-keywords
+  (let ((ms-reserved (sql-keywords-re
+"absolute" "add" "all" "alter" "and" "any" "as" "asc" "authorization"
+"avg" "backup" "begin" "between" "break" "browse" "bulk" "by"
+"cascade" "case" "check" "checkpoint" "close" "clustered" "coalesce"
+"column" "commit" "committed" "compute" "confirm" "constraint"
+"contains" "containstable" "continue" "controlrow" "convert" "count"
+"create" "cross" "current" "current_date" "current_time"
+"current_timestamp" "current_user" "database" "deallocate" "declare"
+"default" "delete" "deny" "desc" "disk" "distinct" "distributed"
+"double" "drop" "dummy" "dump" "else" "end" "errlvl" "errorexit"
+"escape" "except" "exec" "execute" "exists" "exit" "fetch" "file"
+"fillfactor" "first" "floppy" "for" "foreign" "freetext"
+"freetexttable" "from" "full" "goto" "grant" "group" "having"
+"holdlock" "identity" "identity_insert" "identitycol" "if" "in"
+"index" "inner" "insert" "intersect" "into" "is" "isolation" "join"
+"key" "kill" "last" "left" "level" "like" "lineno" "load" "max" "min"
+"mirrorexit" "national" "next" "nocheck" "nolock" "nonclustered" "not"
+"null" "nullif" "of" "off" "offsets" "on" "once" "only" "open"
+"opendatasource" "openquery" "openrowset" "option" "or" "order"
+"outer" "output" "over" "paglock" "percent" "perm" "permanent" "pipe"
+"plan" "precision" "prepare" "primary" "print" "prior" "privileges"
+"proc" "procedure" "processexit" "public" "raiserror" "read"
+"readcommitted" "readpast" "readtext" "readuncommitted" "reconfigure"
+"references" "relative" "repeatable" "repeatableread" "replication"
+"restore" "restrict" "return" "revoke" "right" "rollback" "rowcount"
+"rowguidcol" "rowlock" "rule" "save" "schema" "select" "serializable"
+"session_user" "set" "shutdown" "some" "statistics" "sum"
+"system_user" "table" "tablock" "tablockx" "tape" "temp" "temporary"
+"textsize" "then" "to" "top" "tran" "transaction" "trigger" "truncate"
+"tsequal" "uncommitted" "union" "unique" "update" "updatetext"
+"updlock" "use" "user" "values" "view" "waitfor" "when" "where"
+"while" "with" "work" "writetext" "collate" "function" "openxml"
+"returns"
+))
+
+       (ms-types (sql-keywords-re
+"binary" "bit" "char" "character" "cursor" "datetime" "dec" "decimal"
+"double" "float" "image" "int" "integer" "money" "national" "nchar"
+"ntext" "numeric" "numeric" "nvarchar" "precision" "real"
+"smalldatetime" "smallint" "smallmoney" "text" "timestamp" "tinyint"
+"uniqueidentifier" "varbinary" "varchar" "varying"
+))
+
+       (ms-vars "\\b@[a-zA-Z0-9_]*\\b")
+
+       (ms-functions (sql-keywords-re
+"@@connections" "@@cpu_busy" "@@cursor_rows" "@@datefirst" "@@dbts"
+"@@error" "@@fetch_status" "@@identity" "@@idle" "@@io_busy"
+"@@langid" "@@language" "@@lock_timeout" "@@max_connections"
+"@@max_precision" "@@nestlevel" "@@options" "@@pack_received"
+"@@pack_sent" "@@packet_errors" "@@procid" "@@remserver" "@@rowcount"
+"@@servername" "@@servicename" "@@spid" "@@textsize" "@@timeticks"
+"@@total_errors" "@@total_read" "@@total_write" "@@trancount"
+"@@version" "abs" "acos" "and" "app_name" "ascii" "asin" "atan" "atn2"
+"avg" "case" "cast" "ceiling" "char" "charindex" "coalesce"
+"col_length" "col_name" "columnproperty" "containstable" "convert"
+"cos" "cot" "count" "current_timestamp" "current_user" "cursor_status"
+"databaseproperty" "datalength" "dateadd" "datediff" "datename"
+"datepart" "day" "db_id" "db_name" "degrees" "difference" "exp"
+"file_id" "file_name" "filegroup_id" "filegroup_name"
+"filegroupproperty" "fileproperty" "floor" "formatmessage"
+"freetexttable" "fulltextcatalogproperty" "fulltextserviceproperty"
+"getansinull" "getdate" "grouping" "host_id" "host_name" "ident_incr"
+"ident_seed" "identity" "index_col" "indexproperty" "is_member"
+"is_srvrolemember" "isdate" "isnull" "isnumeric" "left" "len" "log"
+"log10" "lower" "ltrim" "max" "min" "month" "nchar" "newid" "nullif"
+"object_id" "object_name" "objectproperty" "openquery" "openrowset"
+"parsename" "patindex" "patindex" "permissions" "pi" "power"
+"quotename" "radians" "rand" "replace" "replicate" "reverse" "right"
+"round" "rtrim" "session_user" "sign" "sin" "soundex" "space" "sqrt"
+"square" "stats_date" "stdev" "stdevp" "str" "stuff" "substring" "sum"
+"suser_id" "suser_name" "suser_sid" "suser_sname" "system_user" "tan"
+"textptr" "textvalid" "typeproperty" "unicode" "upper" "user"
+"user_id" "user_name" "var" "varp" "year"
+))
+
+       (ms-commands
+        (eval-when-compile
+          (concat "^\\(\\(set\\s-+\\("
+                  (regexp-opt '(
+"datefirst" "dateformat" "deadlock_priority" "lock_timeout"
+"concat_null_yields_null" "cursor_close_on_commit"
+"disable_def_cnst_chk" "fips_flagger" "identity_insert" "language"
+"offsets" "quoted_identifier" "arithabort" "arithignore" "fmtonly"
+"nocount" "noexec" "numeric_roundabort" "parseonly"
+"query_governor_cost_limit" "rowcount" "textsize" "ansi_defaults"
+"ansi_null_dflt_off" "ansi_null_dflt_on" "ansi_nulls" "ansi_padding"
+"ansi_warnings" "forceplan" "showplan_all" "showplan_text"
+"statistics" "implicit_transactions" "remote_proc_transactions"
+"transaction" "xact_abort"
+) t)
+                  "\\)\\)\\|go\\s-*\\|use\\s-+\\|setuser\\s-+\\|dbcc\\s-+\\).*$"))))
+
+    `((,ms-commands  . font-lock-doc-face)
+      (,ms-reserved  . font-lock-keyword-face)
+      (,ms-functions . font-lock-builtin-face)
+      (,ms-vars      . font-lock-variable-name-face)
+      (,ms-types     . font-lock-type-face)))
+
+  "Microsoft SQLServer SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-ms-font-lock-keywords.")
+
+(defvar sql-mode-sybase-font-lock-keywords nil
+  "Sybase SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-sybase-font-lock-keywords.")
+
+(defvar sql-mode-informix-font-lock-keywords nil
+  "Informix SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-informix-font-lock-keywords.")
+
+(defvar sql-mode-interbase-font-lock-keywords nil
+  "Interbase SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-interbase-font-lock-keywords.")
+
+(defvar sql-mode-ingres-font-lock-keywords nil
+  "Ingres SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-interbase-font-lock-keywords.")
+
+(defvar sql-mode-solid-font-lock-keywords nil
+  "Solid SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-solid-font-lock-keywords.")
+
+(defvar sql-mode-mysql-font-lock-keywords
+  (let ((mysql-funcs (sql-keywords-re
+"ascii" "avg" "bdmpolyfromtext" "bdmpolyfromwkb" "bdpolyfromtext"
+"bdpolyfromwkb" "benchmark" "bin" "bit_and" "bit_length" "bit_or"
+"bit_xor" "both" "cast" "char_length" "character_length" "coalesce"
+"concat" "concat_ws" "connection_id" "conv" "convert" "count"
+"curdate" "current_date" "current_time" "current_timestamp" "curtime"
+"elt" "encrypt" "export_set" "field" "find_in_set" "found_rows" "from"
+"geomcollfromtext" "geomcollfromwkb" "geometrycollectionfromtext"
+"geometrycollectionfromwkb" "geometryfromtext" "geometryfromwkb"
+"geomfromtext" "geomfromwkb" "get_lock" "group_concat" "hex" "ifnull"
+"instr" "interval" "isnull" "last_insert_id" "lcase" "leading"
+"length" "linefromtext" "linefromwkb" "linestringfromtext"
+"linestringfromwkb" "load_file" "locate" "lower" "lpad" "ltrim"
+"make_set" "master_pos_wait" "max" "mid" "min" "mlinefromtext"
+"mlinefromwkb" "mpointfromtext" "mpointfromwkb" "mpolyfromtext"
+"mpolyfromwkb" "multilinestringfromtext" "multilinestringfromwkb"
+"multipointfromtext" "multipointfromwkb" "multipolygonfromtext"
+"multipolygonfromwkb" "now" "nullif" "oct" "octet_length" "ord"
+"pointfromtext" "pointfromwkb" "polyfromtext" "polyfromwkb"
+"polygonfromtext" "polygonfromwkb" "position" "quote" "rand"
+"release_lock" "repeat" "replace" "reverse" "rpad" "rtrim" "soundex"
+"space" "std" "stddev" "substring" "substring_index" "sum" "sysdate"
+"trailing" "trim" "ucase" "unix_timestamp" "upper" "user" "variance"
+))
+
+       (mysql-keywords (sql-keywords-re
+"action" "add" "after" "against" "all" "alter" "and" "as" "asc"
+"auto_increment" "avg_row_length" "bdb" "between" "by" "cascade"
+"case" "change" "character" "check" "checksum" "close" "collate"
+"collation" "column" "columns" "comment" "committed" "concurrent"
+"constraint" "create" "cross" "data" "database" "default"
+"delay_key_write" "delayed" "delete" "desc" "directory" "disable"
+"distinct" "distinctrow" "do" "drop" "dumpfile" "duplicate" "else"
+"enable" "enclosed" "end" "escaped" "exists" "fields" "first" "for"
+"force" "foreign" "from" "full" "fulltext" "global" "group" "handler"
+"having" "heap" "high_priority" "if" "ignore" "in" "index" "infile"
+"inner" "insert" "insert_method" "into" "is" "isam" "isolation" "join"
+"key" "keys" "last" "left" "level" "like" "limit" "lines" "load"
+"local" "lock" "low_priority" "match" "max_rows" "merge" "min_rows"
+"mode" "modify" "mrg_myisam" "myisam" "natural" "next" "no" "not"
+"null" "offset" "oj" "on" "open" "optionally" "or" "order" "outer"
+"outfile" "pack_keys" "partial" "password" "prev" "primary"
+"procedure" "quick" "raid0" "raid_type" "read" "references" "rename"
+"repeatable" "restrict" "right" "rollback" "rollup" "row_format"
+"savepoint" "select" "separator" "serializable" "session" "set"
+"share" "show" "sql_big_result" "sql_buffer_result" "sql_cache"
+"sql_calc_found_rows" "sql_no_cache" "sql_small_result" "starting"
+"straight_join" "striped" "table" "tables" "temporary" "terminated"
+"then" "to" "transaction" "truncate" "type" "uncommitted" "union"
+"unique" "unlock" "update" "use" "using" "values" "when" "where"
+"with" "write" "xor"
+))
+
+       (mysql-types (sql-keywords-re
+"bigint" "binary" "bit" "blob" "bool" "boolean" "char" "curve" "date"
+"datetime" "dec" "decimal" "double" "enum" "fixed" "float" "geometry"
+"geometrycollection" "int" "integer" "line" "linearring" "linestring"
+"longblob" "longtext" "mediumblob" "mediumint" "mediumtext"
+"multicurve" "multilinestring" "multipoint" "multipolygon"
+"multisurface" "national" "numeric" "point" "polygon" "precision"
+"real" "smallint" "surface" "text" "time" "timestamp" "tinyblob"
+"tinyint" "tinytext" "unsigned" "varchar" "year" "year2" "year4"
+"zerofill"
+)))
+
+    `((,mysql-funcs    . font-lock-builtin-face)
+      (,mysql-keywords . font-lock-keyword-face)
+      (,mysql-types    . font-lock-type-face)))
+
+  "MySQL SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-mysql-font-lock-keywords.")
+
+(defvar sql-mode-sqlite-font-lock-keywords nil
+  "SQLite SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-sqlite-font-lock-keywords.")
+
+(defvar sql-mode-db2-font-lock-keywords nil
+  "DB2 SQL keywords used by font-lock.
+
+This variable is used by `sql-mode' and `sql-interactive-mode'.  The
+regular expressions are created during compilation by calling the
+function `regexp-opt'.  Therefore, take a look at the source before
+you define your own sql-mode-db2-font-lock-keywords.")
+
+(defvar sql-mode-font-lock-keywords nil
   "SQL keywords used by font-lock.
 
-This variable defaults to `sql-mode-ansi-font-lock-keywords'.  This is
-used for the default `font-lock-defaults' value in `sql-mode'.  This
-can be changed by some entry functions to provide more hilighting.")
+Setting this variable directly no longer has any affect.  Use
+`sql-product' and `sql-add-product-keywords' to control the
+highlighting rules in sql-mode.")
+
+\f
+
+;;; SQL Product support functions
+
+(defun sql-product-feature (feature &optional product)
+  "Lookup `feature' needed to support the current SQL product.
+
+See \[sql-product-alist] for a list of products and supported features."
+  (plist-get
+   (cdr (assoc (or product sql-product)
+              sql-product-alist))
+   feature))
+
+(defun sql-product-font-lock (keywords-only imenu)
+  "Sets `font-lock-defaults' and `font-lock-keywords' based on
+the product-specific keywords and syntax-alists defined in
+`sql-product-alist'."
+  (let
+      ;; Get the product-specific syntax-alist.
+      ((syntax-alist
+       (append
+        (sql-product-feature :syntax-alist)
+        '((?_ . "w") (?. . "w")))))
+
+    ;; Get the product-specific keywords.
+    (setq sql-mode-font-lock-keywords
+         (append
+          (unless (eq sql-product 'ansi)
+            (eval (sql-product-feature :font-lock)))
+          ;; Always highlight ANSI keywords
+          (eval (sql-product-feature :font-lock 'ansi))
+          ;; Fontify object names in CREATE, DROP and ALTER DDL
+          ;; statements
+          (list sql-mode-font-lock-object-name)))
+
+    ;; Setup font-lock.  Force re-parsing of `font-lock-defaults'.
+    (set (make-local-variable 'font-lock-set-defaults) nil)
+    (setq font-lock-defaults (list 'sql-mode-font-lock-keywords
+                                  keywords-only t syntax-alist))
+
+    ;; Force font lock to reinitialize if it is already on
+    ;; Otherwise, we can wait until it can be started.
+    (when (and (fboundp 'font-lock-mode)
+              font-lock-mode)
+      (font-lock-mode-internal nil)
+      (font-lock-mode-internal t))
+
+    (add-hook 'font-lock-mode-hook
+             (lambda ()
+               ;; Provide defaults for new font-lock faces.
+               (defvar font-lock-builtin-face
+                 (if (boundp 'font-lock-preprocessor-face)
+                     font-lock-preprocessor-face
+                   font-lock-keyword-face))
+               (defvar font-lock-doc-face font-lock-string-face))
+             nil t)
+
+    ;; Setup imenu; it needs the same syntax-alist.
+    (when imenu
+       (setq imenu-syntax-alist syntax-alist))))
+
+;;;###autoload
+(defun sql-add-product-keywords (product keywords &optional append)
+  "Add highlighting KEYWORDS for SQL PRODUCT.
+
+PRODUCT should be a symbol, the name of a sql product, such as
+`oracle'.  KEYWORDS should be a list; see the variable
+`font-lock-keywords'.  By default they are added at the beginning
+of the current highlighting list.  If optional argument APPEND is
+`set', they are used to replace the current highlighting list.
+If APPEND is any other non-nil value, they are added at the end
+of the current highlighting list.
+
+For example:
+
+ (sql-add-product-keywords 'ms
+  '((\"\\\\b\\\\w+_t\\\\b\" . font-lock-type-face)))
+
+adds a fontification pattern to fontify identifiers ending in
+`_t' as data types."
+
+  (let ((font-lock (sql-product-feature :font-lock product))
+       old)
+    (setq old (eval font-lock))
+    (set font-lock
+        (if (eq append 'set)
+            keywords
+          (if append
+              (append old keywords)
+            (append keywords old))))))
 
 \f
 
 ;;; Functions to switch highlighting
 
+(defun sql-highlight-product ()
+  "Turns on the appropriate font highlighting for the SQL product
+selected."
+
+  (when (eq major-mode 'sql-mode)
+    ;; Setup font-lock
+    (sql-product-font-lock nil t)
+
+    ;; Set the mode name to include the product.
+    (setq mode-name (concat "SQL[" (prin1-to-string sql-product) "]"))))
+
+(defun sql-set-product (product)
+  "Set `sql-product' to product and enable appropriate
+highlighting."
+  (interactive "SEnter SQL product: ")
+  (when (not (assoc product sql-product-alist))
+    (error "SQL product %s is not supported; treated as ANSI" product)
+    (setq product 'ansi))
+
+  ;; Save product setting and fontify.
+  (setq sql-product product)
+  (sql-highlight-product))
+
 (defun sql-highlight-oracle-keywords ()
-  "Highlight Oracle keywords.
-Basically, this just sets `font-lock-keywords' appropriately."
+  "Highlight Oracle keywords."
   (interactive)
-  (setq font-lock-keywords sql-mode-oracle-font-lock-keywords)
-  (font-lock-fontify-buffer))
+  (sql-set-product 'oracle))
 
 (defun sql-highlight-postgres-keywords ()
-  "Highlight Postgres keywords.
-Basically, this just sets `font-lock-keywords' appropriately."
+  "Highlight Postgres keywords."
+  (interactive)
+  (sql-set-product 'postgres))
+
+(defun sql-highlight-linter-keywords ()
+  "Highlight LINTER keywords."
   (interactive)
-  (setq font-lock-keywords sql-mode-postgres-font-lock-keywords)
-  (font-lock-fontify-buffer))
+  (sql-set-product 'linter))
+
+(defun sql-highlight-ms-keywords ()
+  "Highlight Microsoft SQLServer keywords."
+  (interactive)
+  (sql-set-product 'ms))
 
 (defun sql-highlight-ansi-keywords ()
-  "Highlight ANSI SQL keywords.
-Basically, this just sets `font-lock-keywords' appropriately."
+  "Highlight ANSI SQL keywords."
+  (interactive)
+  (sql-set-product 'ansi))
+
+(defun sql-highlight-sybase-keywords ()
+  "Highlight Sybase SQL keywords."
   (interactive)
-  (setq font-lock-keywords sql-mode-ansi-font-lock-keywords)
-  (font-lock-fontify-buffer))
+  (sql-set-product 'sybase))
+
+(defun sql-highlight-informix-keywords ()
+  "Highlight Informix SQL keywords."
+  (interactive)
+  (sql-set-product 'informix))
+
+(defun sql-highlight-interbase-keywords ()
+  "Highlight Interbase SQL keywords."
+  (interactive)
+  (sql-set-product 'interbase))
+
+(defun sql-highlight-ingres-keywords ()
+  "Highlight Ingres SQL keywords."
+  (interactive)
+  (sql-set-product 'ingres))
+
+(defun sql-highlight-solid-keywords ()
+  "Highlight Solid SQL keywords."
+  (interactive)
+  (sql-set-product 'solid))
+
+(defun sql-highlight-mysql-keywords ()
+  "Highlight MySQL SQL keywords."
+  (interactive)
+  (sql-set-product 'mysql))
+
+(defun sql-highlight-sqlite-keywords ()
+  "Highlight SQLite SQL keywords."
+  (interactive)
+  (sql-set-product 'sqlite))
+
+(defun sql-highlight-db2-keywords ()
+  "Highlight DB2 SQL keywords."
+  (interactive)
+  (sql-set-product 'db2))
 
 \f
 
@@ -781,7 +1857,7 @@ the regular expression `comint-prompt-regexp', a buffer local variable."
 (defun sql-accumulate-and-indent ()
   "Continue SQL statement on the next line."
   (interactive)
-  (if (fboundp 'comint-accumulate) 
+  (if (fboundp 'comint-accumulate)
       (comint-accumulate)
     (newline))
   (indent-according-to-mode))
@@ -796,17 +1872,20 @@ usually named `*SQL*'.  The name of the major mode is SQLi.
 Use the following commands to start a specific SQL interpreter:
 
     PostGres: \\[sql-postgres]
+    MySQL: \\[sql-mysql]
+    SQLite: \\[sql-sqlite]
 
 Other non-free SQL implementations are also supported:
 
-    MySQL: \\[sql-mysql]
     Solid: \\[sql-solid]
     Oracle: \\[sql-oracle]
     Informix: \\[sql-informix]
     Sybase: \\[sql-sybase]
     Ingres: \\[sql-ingres]
     Microsoft: \\[sql-ms]
+    DB2: \\[sql-db2]
     Interbase: \\[sql-interbase]
+    Linter: \\[sql-linter]
 
 But we urge you to choose a free implementation instead of these.
 
@@ -827,16 +1906,8 @@ appended to the SQLi buffer without disturbing your SQL buffer."
   (describe-function 'sql-help))
 
 (defun sql-read-passwd (prompt &optional default)
-  "Read a password using PROMPT.
-Optional DEFAULT is password to start with.  This function calls
-`read-passwd' if it is available.  If not, function
-`ange-ftp-read-passwd' is called.  This should always be available,
-even in old versions of Emacs."
-  (if (fboundp 'read-passwd)
-      (read-passwd prompt nil default)
-    (unless (fboundp 'ange-ftp-read-passwd)
-      (autoload 'ange-ftp-read-passwd "ange-ftp"))
-    (ange-ftp-read-passwd prompt default)))
+  "Read a password using PROMPT.  Optional DEFAULT is password to start with."
+  (read-passwd prompt nil default))
 
 (defun sql-get-login (&rest what)
   "Get username, password and database from the user.
@@ -851,26 +1922,30 @@ Parameter WHAT is a list of the arguments passed to this function.
 The function asks for the username if WHAT contains symbol `user', for
 the password if it contains symbol `password', for the server if it
 contains symbol `server', and for the database if it contains symbol
-`database'.
+`database'.  The members of WHAT are processed in the order in which
+they are provided.
 
 In order to ask the user for username, password and database, call the
 function like this: (sql-get-login 'user 'password 'database)."
   (interactive)
-  (if (memq 'user what)
+  (while what
+    (cond
+     ((eq (car what) 'user)            ; user
       (setq sql-user
            (read-from-minibuffer "User: " sql-user nil nil
                                  sql-user-history)))
-  (if (memq 'password what)
+     ((eq (car what) 'password)                ; password
       (setq sql-password
            (sql-read-passwd "Password: " sql-password)))
-  (if (memq 'server what)
+     ((eq (car what) 'server)          ; server
       (setq sql-server
            (read-from-minibuffer "Server: " sql-server nil nil
                                  sql-server-history)))
-  (if (memq 'database what)
+     ((eq (car what) 'database)                ; database
       (setq sql-database
            (read-from-minibuffer "Database: " sql-database nil nil
                                  sql-database-history))))
+    (setq what (cdr what))))
 
 (defun sql-find-sqli-buffer ()
   "Return the current default SQLi buffer or nil.
@@ -894,7 +1969,7 @@ be in `sql-interactive-mode' and have a process."
          found)))))
 
 (defun sql-set-sqli-buffer-generally ()
-  "Set SQLi buffer for all SQL buffers that have none.  
+  "Set SQLi buffer for all SQL buffers that have none.
 This function checks all SQL buffers for their SQLi buffer.  If their
 SQLi buffer is nonexistent or has no process, it is set to the current
 default SQLi buffer.  The current default SQLi buffer is determined
@@ -1000,7 +2075,7 @@ Inserts SELECT or commas if appropriate."
          (insert ", "))
         ;; else insert a space
         (t
-         (if (eq (preceding-char) ? )
+         (if (eq (preceding-char) ?\s)
              nil
            (insert " ")))))
       ;; in any case, insert the column
@@ -1020,13 +2095,16 @@ Inserts SELECT or commas if appropriate."
 Placeholders are words starting with and ampersand like &this.
 This function is used for `comint-input-sender' if using `sql-oracle' on NT."
   (while (string-match "&\\(\\sw+\\)" string)
-    (setq string (replace-match 
+    (setq string (replace-match
                  (read-from-minibuffer
                   (format "Enter value for %s: " (match-string 1 string))
                   nil nil nil sql-placeholder-history)
                  t t string)))
   (comint-send-string proc string)
-  (comint-send-string proc "\n"))
+  (if comint-input-sender-no-newline
+      (if (not (string-equal string ""))
+         (process-send-eof))
+    (comint-send-string proc "\n")))
 
 ;; Using DB2 interactively, newlines must be escaped with " \".
 ;; The space before the backslash is relevant.
@@ -1044,7 +2122,10 @@ Every newline in STRING will be preceded with a space and a backslash."
       (setq start me))
     (setq result (concat result (substring string start)))
     (comint-send-string proc result)
-    (comint-send-string proc "\n")))
+    (if comint-input-sender-no-newline
+       (if (not (string-equal string ""))
+           (process-send-eof))
+      (comint-send-string proc "\n"))))
 
 \f
 
@@ -1081,6 +2162,19 @@ Every newline in STRING will be preceded with a space and a backslash."
   (interactive)
   (sql-send-region (point-min) (point-max)))
 
+(defun sql-send-string (str)
+  "Send a string to the SQL process."
+  (interactive "sSQL Text: ")
+  (if (buffer-live-p sql-buffer)
+      (save-excursion
+        (comint-send-string sql-buffer str)
+        (comint-send-string sql-buffer "\n")
+        (message "Sent string to buffer %s." (buffer-name sql-buffer))
+        (if sql-pop-to-buffer-after-send-region
+            (pop-to-buffer sql-buffer)
+          (display-buffer sql-buffer)))
+    (message "No SQL process started.")))
+
 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
   "Toggle `sql-pop-to-buffer-after-send-region'.
 
@@ -1115,7 +2209,15 @@ determine where the strings should be sent to.  You can set the
 value of `sql-buffer' using \\[sql-set-sqli-buffer].
 
 For information on how to create multiple SQLi buffers, see
-`sql-interactive-mode'."
+`sql-interactive-mode'.
+
+Note that SQL doesn't have an escape character unless you specify
+one.  If you specify backslash as escape character in SQL,
+you must tell Emacs.  Here's how to do that in your `~/.emacs' file:
+
+\(add-hook 'sql-mode-hook
+          (lambda ()
+           (modify-syntax-entry ?\\\\ \".\" sql-mode-syntax-table)))"
   (interactive)
   (kill-all-local-variables)
   (setq major-mode 'sql-mode)
@@ -1125,13 +2227,7 @@ For information on how to create multiple SQLi buffers, see
       (easy-menu-add sql-mode-menu)); XEmacs
   (set-syntax-table sql-mode-syntax-table)
   (make-local-variable 'font-lock-defaults)
-  ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
-  ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
-  ;; will have just one quote.  Therefore syntactic hilighting is
-  ;; disabled for interactive buffers.  `_' and `.' are considered part
-  ;; of words.
-  (setq font-lock-defaults '(sql-mode-font-lock-keywords
-                            nil t ((?_ . "w") (?. . "w"))))
+  (make-local-variable 'sql-mode-font-lock-keywords)
   (make-local-variable 'comment-start)
   (setq comment-start "--")
   ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
@@ -1139,11 +2235,8 @@ For information on how to create multiple SQLi buffers, see
   ;; Add imenu support for sql-mode.  Note that imenu-generic-expression
   ;; is buffer-local, so we don't need a local-variable for it.  SQL is
   ;; case-insensitive, that's why we have to set imenu-case-fold-search.
-  ;; imenu-syntax-alist makes sure that `_' is considered part of object
-  ;; names.
   (setq imenu-generic-expression sql-imenu-generic-expression
-       imenu-case-fold-search t
-       imenu-syntax-alist '(("_" . "w")))
+       imenu-case-fold-search t)
   ;; Make `sql-send-paragraph' work on paragraphs that contain indented
   ;; lines.
   (make-local-variable 'paragraph-separate)
@@ -1154,7 +2247,10 @@ For information on how to create multiple SQLi buffers, see
   (setq local-abbrev-table sql-mode-abbrev-table)
   (setq abbrev-all-caps 1)
   ;; Run hook
-  (run-hooks 'sql-mode-hook))
+  (run-mode-hooks 'sql-mode-hook)
+  ;; Catch changes to sql-product and highlight accordingly
+  (sql-highlight-product)
+  (add-hook 'hack-local-variables-hook 'sql-highlight-product t t))
 
 \f
 
@@ -1207,7 +2303,7 @@ hooks on `comint-input-filter-functions' are run.  After each SQL
 interpreter output, the hooks on `comint-output-filter-functions' are
 run.
 
-Variable `sql-input-ring-file-name' controls the initialisation of the
+Variable `sql-input-ring-file-name' controls the initialization of the
 input ring history.
 
 Variables `comint-output-filter-functions', a hook, and
@@ -1230,42 +2326,54 @@ you entered, right above the output it created.
 
 \(setq comint-output-filter-functions
        \(function (lambda (STR) (comint-show-output))))"
-  (comint-mode)
-  (setq comint-prompt-regexp sql-prompt-regexp)
-  (setq left-margin sql-prompt-length)
+  (delay-mode-hooks (comint-mode))
+  ;; Get the `sql-product' for this interactive session.
+  (set (make-local-variable 'sql-product)
+       (or sql-interactive-product
+          sql-product))
+  ;; Setup the mode.
   (setq major-mode 'sql-interactive-mode)
-  (setq mode-name "SQLi")
+  (setq mode-name (concat "SQLi[" (prin1-to-string sql-product) "]"))
   (use-local-map sql-interactive-mode-map)
   (if sql-interactive-mode-menu
-      (easy-menu-add sql-interactive-mode-menu)); XEmacs
+      (easy-menu-add sql-interactive-mode-menu)) ; XEmacs
   (set-syntax-table sql-mode-syntax-table)
+  (make-local-variable 'sql-mode-font-lock-keywords)
   (make-local-variable 'font-lock-defaults)
   ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
   ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
   ;; will have just one quote.  Therefore syntactic hilighting is
-  ;; disabled for interactive buffers.  `_' and `.' are considered part
-  ;; of words.
-  (setq font-lock-defaults '(sql-mode-font-lock-keywords
-                            t t ((?_ . "w") (?. . "w"))))
+  ;; disabled for interactive buffers.  No imenu support.
+  (sql-product-font-lock t nil)
   ;; Enable commenting and uncommenting of the region.
   (make-local-variable 'comment-start)
   (setq comment-start "--")
-  ;; Abbreviation table init and case-insensitive.  It is not activatet
+  ;; Abbreviation table init and case-insensitive.  It is not activated
   ;; by default.
   (setq local-abbrev-table sql-mode-abbrev-table)
   (setq abbrev-all-caps 1)
   ;; Exiting the process will call sql-stop.
   (set-process-sentinel (get-buffer-process sql-buffer) 'sql-stop)
+  ;; Create a usefull name for renaming this buffer later.
+  (make-local-variable 'sql-alternate-buffer-name)
+  (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
+  ;; User stuff.  Initialize before the hook.
+  (set (make-local-variable 'sql-prompt-regexp)
+       (sql-product-feature :sqli-prompt-regexp))
+  (set (make-local-variable 'sql-prompt-length)
+       (sql-product-feature :sqli-prompt-length))
+  (make-local-variable 'sql-input-ring-separator)
+  (make-local-variable 'sql-input-ring-file-name)
+  ;; Run hook.
+  (run-mode-hooks 'sql-interactive-mode-hook)
+  ;; Set comint based on user overrides.
+  (setq comint-prompt-regexp sql-prompt-regexp)
+  (setq left-margin sql-prompt-length)
   ;; People wanting a different history file for each
   ;; buffer/process/client/whatever can change separator and file-name
   ;; on the sql-interactive-mode-hook.
   (setq comint-input-ring-separator sql-input-ring-separator
        comint-input-ring-file-name sql-input-ring-file-name)
-  ;; Create a usefull name for renaming this buffer later.
-  (make-local-variable 'sql-alternate-buffer-name)
-  (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
-  ;; User stuff.
-  (run-hooks 'sql-interactive-mode-hook)
   ;; Calling the hook before calling comint-read-input-ring allows users
   ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
   (comint-read-input-ring t))
@@ -1288,6 +2396,33 @@ Sentinels will always get the two parameters PROCESS and EVENT."
 
 ;;; Entry functions for different SQL interpreters.
 
+;;;###autoload
+(defun sql-product-interactive (&optional product)
+  "Run product interpreter as an inferior process.
+
+If buffer `*SQL*' exists but no process is running, make a new process.
+If buffer exists and a process is running, just switch to buffer
+`*SQL*'.
+
+\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
+  (interactive)
+  (setq product (or product sql-product))
+  (when (sql-product-feature :sqli-connect product)
+    (if (comint-check-proc "*SQL*")
+       (pop-to-buffer "*SQL*")
+      ;; Get credentials.
+      (apply 'sql-get-login (sql-product-feature :sqli-login product))
+      ;; Connect to database.
+      (message "Login...")
+      (funcall (sql-product-feature :sqli-connect product))
+      ;; Set SQLi mode.
+      (setq sql-interactive-product product)
+      (setq sql-buffer (current-buffer))
+      (sql-interactive-mode)
+      ;; All done.
+      (message "Login...done")
+      (pop-to-buffer sql-buffer))))
+
 ;;;###autoload
 (defun sql-oracle ()
   "Run sqlplus by Oracle as an inferior process.
@@ -1313,42 +2448,32 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'user 'password 'database)
-    (message "Login...")
-    ;; Produce user/password@database construct.  Password without user
-    ;; is meaningless; database without user/password is meaningless,
-    ;; because "@param" will ask sqlplus to interpret the script
-    ;; "param".
-    (let ((parameter nil))
-      (if (not (string= "" sql-user))
-         (if (not (string= "" sql-password))
-             (setq parameter (concat sql-user "/" sql-password))
-           (setq parameter sql-user)))
-      (if (and parameter (not (string= "" sql-database)))
-         (setq parameter (concat parameter "@" sql-database)))
-      (if parameter
-         (setq parameter (nconc (list parameter) sql-oracle-options))
-       (setq parameter sql-oracle-options))
-      (if parameter
-         (set-buffer (apply 'make-comint "SQL" sql-oracle-program nil 
-                            parameter))
-       (set-buffer (make-comint "SQL" sql-oracle-program nil))))
-    (setq sql-prompt-regexp "^SQL> ")
-    (setq sql-prompt-length 5)
-    (setq sql-buffer (current-buffer))
-    ;; set sql-mode-font-lock-keywords to something different before
-    ;; calling sql-interactive-mode.
-    (setq sql-mode-font-lock-keywords sql-mode-oracle-font-lock-keywords)
-    (sql-interactive-mode)
-    ;; If running on NT, make sure we do placeholder replacement
-    ;; ourselves.  This must come after sql-interactive-mode because all
-    ;; local variables will be killed, there.
+  (sql-product-interactive 'oracle))
+
+(defun sql-connect-oracle ()
+  "Create comint buffer and connect to Oracle using the login
+parameters and command options."
+  ;; Produce user/password@database construct.  Password without user
+  ;; is meaningless; database without user/password is meaningless,
+  ;; because "@param" will ask sqlplus to interpret the script
+  ;; "param".
+  (let ((parameter nil))
+    (if (not (string= "" sql-user))
+       (if (not (string= "" sql-password))
+           (setq parameter (concat sql-user "/" sql-password))
+         (setq parameter sql-user)))
+    (if (and parameter (not (string= "" sql-database)))
+       (setq parameter (concat parameter "@" sql-database)))
+    (if parameter
+       (setq parameter (nconc (list parameter) sql-oracle-options))
+      (setq parameter sql-oracle-options))
+    (if parameter
+       (set-buffer (apply 'make-comint "SQL" sql-oracle-program nil
+                          parameter))
+      (set-buffer (make-comint "SQL" sql-oracle-program nil)))
+    ;; SQL*Plus is buffered on WindowsNT; this handles &placeholders.
     (if (eq window-system 'w32)
-       (setq comint-input-sender 'sql-query-placeholders-and-send))
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+       (setq comint-input-sender 'sql-query-placeholders-and-send))))
 
 \f
 
@@ -1377,29 +2502,24 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'server 'user 'password 'database)
-    (message "Login...")
-    ;; Put all parameters to the program (if defined) in a list and call
-    ;; make-comint.
-    (let ((params sql-sybase-options))
-      (if (not (string= "" sql-server))
-         (setq params (append (list "-S" sql-server) params)))
-      (if (not (string= "" sql-database))
-         (setq params (append (list "-D" sql-database) params)))
-      (if (not (string= "" sql-password))
-         (setq params (append (list "-P" sql-password) params)))
-      (if (not (string= "" sql-user))
-         (setq params (append (list "-U" sql-user) params)))
-      (set-buffer (apply 'make-comint "SQL" sql-sybase-program
-                        nil params)))
-    (setq sql-prompt-regexp "^SQL> ")
-    (setq sql-prompt-length 5)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+  (sql-product-interactive 'sybase))
+
+(defun sql-connect-sybase ()
+  "Create comint buffer and connect to Sybase using the login
+parameters and command options."
+  ;; Put all parameters to the program (if defined) in a list and call
+  ;; make-comint.
+  (let ((params sql-sybase-options))
+    (if (not (string= "" sql-server))
+       (setq params (append (list "-S" sql-server) params)))
+    (if (not (string= "" sql-database))
+       (setq params (append (list "-D" sql-database) params)))
+    (if (not (string= "" sql-password))
+       (setq params (append (list "-P" sql-password) params)))
+    (if (not (string= "" sql-user))
+       (setq params (append (list "-U" sql-user) params)))
+    (set-buffer (apply 'make-comint "SQL" sql-sybase-program
+                      nil params))))
 
 \f
 
@@ -1426,20 +2546,65 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'database)
-    (message "Login...")
-    ;; username and password are ignored.
-    (if (string= "" sql-database)
-       (set-buffer (make-comint "SQL" sql-informix-program nil))
-      (set-buffer (make-comint "SQL" sql-informix-program nil sql-database "-")))
-    (setq sql-prompt-regexp "^SQL> ")
-    (setq sql-prompt-length 5)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+  (sql-product-interactive 'informix))
+
+(defun sql-connect-informix ()
+  "Create comint buffer and connect to Informix using the login
+parameters and command options."
+  ;; username and password are ignored.
+  (if (string= "" sql-database)
+      (set-buffer (make-comint "SQL" sql-informix-program nil))
+    (set-buffer (make-comint "SQL" sql-informix-program nil sql-database "-"))))
+
+\f
+
+;;;###autoload
+(defun sql-sqlite ()
+  "Run sqlite as an inferior process.
+
+SQLite is free software.
+
+If buffer `*SQL*' exists but no process is running, make a new process.
+If buffer exists and a process is running, just switch to buffer
+`*SQL*'.
+
+Interpreter used comes from variable `sql-sqlite-program'.  Login uses
+the variables `sql-user', `sql-password', `sql-database', and
+`sql-server' as defaults, if set.  Additional command line parameters
+can be stored in the list `sql-sqlite-options'.
+
+The buffer is put in sql-interactive-mode, giving commands for sending
+input.  See `sql-interactive-mode'.
+
+To specify a coding system for converting non-ASCII characters
+in the input and output to the process, use \\[universal-coding-system-argument]
+before \\[sql-sqlite].  You can also specify this with \\[set-buffer-process-coding-system]
+in the SQL buffer, after you start the process.
+The default comes from `process-coding-system-alist' and
+`default-process-coding-system'.
+
+\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
+  (interactive)
+  (sql-product-interactive 'sqlite))
+
+(defun sql-connect-sqlite ()
+  "Create comint buffer and connect to SQLite using the login
+parameters and command options."
+  ;; Put all parameters to the program (if defined) in a list and call
+  ;; make-comint.
+  (let ((params))
+    (if (not (string= "" sql-database))
+       (setq params (append (list sql-database) params)))
+    (if (not (string= "" sql-server))
+       (setq params (append (list (concat "--host=" sql-server)) params)))
+    (if (not (string= "" sql-password))
+       (setq params (append (list (concat "--password=" sql-password)) params)))
+    (if (not (string= "" sql-user))
+       (setq params (append (list (concat "--user=" sql-user)) params)))
+    (if (not (null sql-sqlite-options))
+       (setq params (append sql-sqlite-options params)))
+    (set-buffer (apply 'make-comint "SQL" sql-sqlite-program
+                      nil params))))
 
 \f
 
@@ -1447,8 +2612,7 @@ The default comes from `process-coding-system-alist' and
 (defun sql-mysql ()
   "Run mysql by TcX as an inferior process.
 
-Note that the widespread idea that mysql is free software is inaccurate;
-its license is too restrictive.  We urge you to use PostGres instead.
+Mysql versions 3.23 and up are free software.
 
 If buffer `*SQL*' exists but no process is running, make a new process.
 If buffer exists and a process is running, just switch to buffer
@@ -1471,31 +2635,26 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'user 'password 'database 'server)
-    (message "Login...")
-    ;; Put all parameters to the program (if defined) in a list and call
-    ;; make-comint.
-    (let ((params))
-      (if (not (string= "" sql-database))
-         (setq params (append (list sql-database) params)))
-      (if (not (string= "" sql-server))
-         (setq params (append (list (concat "--host=" sql-server)) params)))
-      (if (not (string= "" sql-password))
-         (setq params (append (list (concat "--password=" sql-password)) params)))
-      (if (not (string= "" sql-user))
-         (setq params (append (list (concat "--user=" sql-user)) params)))
-      (if (not (null sql-mysql-options))
-          (setq params (append sql-mysql-options params)))
-      (set-buffer (apply 'make-comint "SQL" sql-mysql-program
-                        nil params)))
-    (setq sql-prompt-regexp "^mysql>")
-    (setq sql-prompt-length 6)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+  (sql-product-interactive 'mysql))
+
+(defun sql-connect-mysql ()
+  "Create comint buffer and connect to MySQL using the login
+parameters and command options."
+  ;; Put all parameters to the program (if defined) in a list and call
+  ;; make-comint.
+  (let ((params))
+    (if (not (string= "" sql-database))
+       (setq params (append (list sql-database) params)))
+    (if (not (string= "" sql-server))
+       (setq params (append (list (concat "--host=" sql-server)) params)))
+    (if (not (string= "" sql-password))
+       (setq params (append (list (concat "--password=" sql-password)) params)))
+    (if (not (string= "" sql-user))
+       (setq params (append (list (concat "--user=" sql-user)) params)))
+    (if (not (null sql-mysql-options))
+       (setq params (append sql-mysql-options params)))
+    (set-buffer (apply 'make-comint "SQL" sql-mysql-program
+                      nil params))))
 
 \f
 
@@ -1523,27 +2682,22 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'user 'password 'server)
-    (message "Login...")
-    ;; Put all parameters to the program (if defined) in a list and call
-    ;; make-comint.
-    (let ((params))
-      ;; It only makes sense if both username and password are there.
-      (if (not (or (string= "" sql-user)
-                  (string= "" sql-password)))
-         (setq params (append (list sql-user sql-password) params)))
-      (if (not (string= "" sql-server))
-         (setq params (append (list sql-server) params)))
-      (set-buffer (apply 'make-comint "SQL" sql-solid-program
-                        nil params)))
-    (setq sql-prompt-regexp "^")
-    (setq sql-prompt-length 0)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+  (sql-product-interactive 'solid))
+
+(defun sql-connect-solid ()
+  "Create comint buffer and connect to Solid using the login
+parameters and command options."
+  ;; Put all parameters to the program (if defined) in a list and call
+  ;; make-comint.
+  (let ((params))
+    ;; It only makes sense if both username and password are there.
+    (if (not (or (string= "" sql-user)
+                (string= "" sql-password)))
+       (setq params (append (list sql-user sql-password) params)))
+    (if (not (string= "" sql-server))
+       (setq params (append (list sql-server) params)))
+    (set-buffer (apply 'make-comint "SQL" sql-solid-program
+                      nil params))))
 
 \f
 
@@ -1570,26 +2724,21 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'database)
-    (message "Login...")
-    ;; username and password are ignored.
-    (if (string= "" sql-database)
-       (set-buffer (make-comint "SQL" sql-ingres-program nil))
-      (set-buffer (make-comint "SQL" sql-ingres-program nil sql-database)))
-    (setq sql-prompt-regexp "^\* ")
-    (setq sql-prompt-length 2)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+  (sql-product-interactive 'ingres))
+
+(defun sql-connect-ingres ()
+  "Create comint buffer and connect to Ingres using the login
+parameters and command options."
+  ;; username and password are ignored.
+  (if (string= "" sql-database)
+      (set-buffer (make-comint "SQL" sql-ingres-program nil))
+    (set-buffer (make-comint "SQL" sql-ingres-program nil sql-database))))
 
 \f
 
 ;;;###autoload
 (defun sql-ms ()
-  "Run isql by Microsoft as an inferior process.
+  "Run osql by Microsoft as an inferior process.
 
 If buffer `*SQL*' exists but no process is running, make a new process.
 If buffer exists and a process is running, just switch to buffer
@@ -1597,7 +2746,8 @@ If buffer exists and a process is running, just switch to buffer
 
 Interpreter used comes from variable `sql-ms-program'.  Login uses the
 variables `sql-user', `sql-password', `sql-database', and `sql-server'
-as defaults, if set.
+as defaults, if set.  Additional command line parameters can be stored
+in the list `sql-ms-options'.
 
 The buffer is put in sql-interactive-mode, giving commands for sending
 input.  See `sql-interactive-mode'.
@@ -1611,32 +2761,31 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'user 'password 'database 'server)
-    (message "Login...")
-    ;; Put all parameters to the program (if defined) in a list and call
-    ;; make-comint.
-    (let ((params '("-w 300")))
-      (if (not (string= "" sql-server))
+  (sql-product-interactive 'ms))
+
+(defun sql-connect-ms ()
+  "Create comint buffer and connect to Microsoft using the login
+parameters and command options."
+  ;; Put all parameters to the program (if defined) in a list and call
+  ;; make-comint.
+  (let ((params sql-ms-options))
+    (if (not (string= "" sql-server))
         (setq params (append (list "-S" sql-server) params)))
-      (if (not (string= "" sql-database))
+    (if (not (string= "" sql-database))
         (setq params (append (list "-d" sql-database) params)))
-      (if (not (string= "" sql-user))
-         (setq params (append (list "-U" sql-user) params)))
-      (if (not (string= "" sql-password))
-         (setq params (append (list "-P" sql-password) params))
-       ;; If -P is passed to ISQL as the last argument without a password,
-       ;; it's considered null.
-       (setq params (append params (list "-P"))))
-      (set-buffer (apply 'make-comint "SQL" sql-ms-program
-                        nil params)))
-    (setq sql-prompt-regexp "^[0-9]*>")
-    (setq sql-prompt-length 5)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+    (if (not (string= "" sql-user))
+       (setq params (append (list "-U" sql-user) params)))
+    (if (not (string= "" sql-password))
+       (setq params (append (list "-P" sql-password) params))
+      (if (string= "" sql-user)
+         ;; if neither user nor password is provided, use system
+         ;; credentials.
+         (setq params (append (list "-E") params))
+       ;; If -P is passed to ISQL as the last argument without a
+       ;; password, it's considered null.
+       (setq params (append params (list "-P")))))
+    (set-buffer (apply 'make-comint "SQL" sql-ms-program
+                      nil params))))
 
 \f
 
@@ -1670,34 +2819,25 @@ Try to set `comint-output-filter-functions' like this:
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'database 'server)
-    (message "Login...")
-    ;; username and password are ignored.  Mark Stosberg suggest to add
-    ;; the database at the end.  Jason Beegan suggest using --pset and
-    ;; pager=off instead of \\o|cat.  The later was the solution by
-    ;; Gregor Zych.  Jason's suggestion is the default value for
-    ;; sql-postgres-options.
-    (let ((params sql-postgres-options))
-      (if (not (string= "" sql-database))
-         (setq params (append params (list sql-database))))
-      (if (not (string= "" sql-server))
-         (setq params (append (list "-h" sql-server) params)))
-      (set-buffer (apply 'make-comint "SQL" sql-postgres-program
-                        nil params)))
-    (setq sql-prompt-regexp "^.*> *")
-    (setq sql-prompt-length 5)
-    ;; This is a lousy hack to prevent psql from truncating it's output
-    ;; and giving stupid warnings. If s.o. knows a way to prevent psql
-    ;; from acting this way, then I would be very thankful to
-    ;; incorporate this (Gregor Zych <zych@pool.informatik.rwth-aachen.de>)
-    ;; (comint-send-string "*SQL*" "\\o \| cat\n")
-    (setq sql-mode-font-lock-keywords sql-mode-postgres-font-lock-keywords)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+  (sql-product-interactive 'postgres))
+
+(defun sql-connect-postgres ()
+  "Create comint buffer and connect to Postgres using the login
+parameters and command options."
+  ;; username and password are ignored.  Mark Stosberg suggest to add
+  ;; the database at the end.  Jason Beegan suggest using --pset and
+  ;; pager=off instead of \\o|cat.  The later was the solution by
+  ;; Gregor Zych.  Jason's suggestion is the default value for
+  ;; sql-postgres-options.
+  (let ((params sql-postgres-options))
+    (if (not (string= "" sql-database))
+       (setq params (append params (list sql-database))))
+    (if (not (string= "" sql-server))
+       (setq params (append (list "-h" sql-server) params)))
+    (if (not (string= "" sql-user))
+       (setq params (append (list "-U" sql-user) params)))
+    (set-buffer (apply 'make-comint "SQL" sql-postgres-program
+                      nil params))))
 
 \f
 
@@ -1725,27 +2865,22 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (sql-get-login 'user 'password 'database)
-    (message "Login...")
-    ;; Put all parameters to the program (if defined) in a list and call
-    ;; make-comint.
-    (let ((params sql-interbase-options))
-      (if (not (string= "" sql-user))
-         (setq params (append (list "-u" sql-user) params)))
-      (if (not (string= "" sql-password))
-         (setq params (append (list "-p" sql-password) params)))
-      (if (not (string= "" sql-database))
-        (setq params (cons sql-database params))); add to the front!
-      (set-buffer (apply 'make-comint "SQL" sql-interbase-program
-                        nil params)))
-    (setq sql-prompt-regexp "^SQL> ")
-    (setq sql-prompt-length 5)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+  (sql-product-interactive 'interbase))
+
+(defun sql-connect-interbase ()
+  "Create comint buffer and connect to Interbase using the login
+parameters and command options."
+  ;; Put all parameters to the program (if defined) in a list and call
+  ;; make-comint.
+  (let ((params sql-interbase-options))
+    (if (not (string= "" sql-user))
+       (setq params (append (list "-u" sql-user) params)))
+    (if (not (string= "" sql-password))
+       (setq params (append (list "-p" sql-password) params)))
+    (if (not (string= "" sql-database))
+        (setq params (cons sql-database params))) ; add to the front!
+    (set-buffer (apply 'make-comint "SQL" sql-interbase-program
+                      nil params))))
 
 \f
 
@@ -1777,23 +2912,66 @@ The default comes from `process-coding-system-alist' and
 
 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
   (interactive)
-  (if (comint-check-proc "*SQL*")
-      (pop-to-buffer "*SQL*")
-    (message "Login...")
-    ;; Put all parameters to the program (if defined) in a list and call
-    ;; make-comint.
-    (set-buffer (apply 'make-comint "SQL" sql-db2-program
-                      nil sql-db2-options))
-    (setq sql-prompt-regexp "^db2 => ")
-    (setq sql-prompt-length 7)
-    (setq sql-buffer (current-buffer))
-    (sql-interactive-mode)
-    ;; Escape newlines.  This must come after sql-interactive-mode
-    ;; because all local variables will be killed, there.
-    (setq comint-input-sender 'sql-escape-newlines-and-send)
-    (message "Login...done")
-    (pop-to-buffer sql-buffer)))
+  (sql-product-interactive 'db2))
+
+(defun sql-connect-db2 ()
+  "Create comint buffer and connect to DB2 using the login
+parameters and command options."
+  ;; Put all parameters to the program (if defined) in a list and call
+  ;; make-comint.
+  (set-buffer (apply 'make-comint "SQL" sql-db2-program
+                    nil sql-db2-options))
+  ;; Properly escape newlines when DB2 is interactive.
+  (setq comint-input-sender 'sql-escape-newlines-and-send))
+
+;;;###autoload
+(defun sql-linter ()
+  "Run inl by RELEX as an inferior process.
+
+If buffer `*SQL*' exists but no process is running, make a new process.
+If buffer exists and a process is running, just switch to buffer
+`*SQL*'.
+
+Interpreter used comes from variable `sql-linter-program' - usually `inl'.
+Login uses the variables `sql-user', `sql-password', `sql-database' and
+`sql-server' as defaults, if set.  Additional command line parameters
+can be stored in the list `sql-linter-options'. Run inl -h to get help on
+parameters.
+
+`sql-database' is used to set the LINTER_MBX environment variable for
+local connections, `sql-server' refers to the server name from the
+`nodetab' file for the network connection (dbc_tcp or friends must run
+for this to work).  If `sql-password' is an empty string, inl will use
+an empty password.
+
+The buffer is put in sql-interactive-mode, giving commands for sending
+input.  See `sql-interactive-mode'.
+
+\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
+  (interactive)
+  (sql-product-interactive 'linter))
+
+(defun sql-connect-linter ()
+  "Create comint buffer and connect to Linter using the login
+parameters and command options."
+  ;; Put all parameters to the program (if defined) in a list and call
+  ;; make-comint.
+  (let ((params sql-linter-options) (login nil) (old-mbx (getenv "LINTER_MBX")))
+    (if (not (string= "" sql-user))
+       (setq login (concat sql-user "/" sql-password)))
+    (setq params (append (list "-u" login) params))
+    (if (not (string= "" sql-server))
+       (setq params (append (list "-n" sql-server) params)))
+    (if (string= "" sql-database)
+       (setenv "LINTER_MBX" nil)
+      (setenv "LINTER_MBX" sql-database))
+    (set-buffer (apply 'make-comint "SQL" sql-linter-program nil
+                      params))
+    (setenv "LINTER_MBX" old-mbx)))
+
+\f
 
 (provide 'sql)
 
+;;; arch-tag: 7e1fa1c4-9ca2-402e-87d2-83a5eccb7ac3
 ;;; sql.el ends here