frameset.el (frameset--jump-to-register): Check that buffer is live (bug#16749).
[bpt/emacs.git] / lisp / progmodes / sql.el
index 3166abb..eb8e076 100644 (file)
@@ -1,10 +1,10 @@
-;;; sql.el --- specialized comint.el for SQL interpreters
+;;; sql.el --- specialized comint.el for SQL interpreters  -*- lexical-binding: t -*-
 
-;; Copyright (C) 1998-201 Free Software Foundation, Inc.
+;; Copyright (C) 1998-2014 Free Software Foundation, Inc.
 
 ;; Author: Alex Schroeder <alex@gnu.org>
-;; Maintainer: Michael Mauger <mmaug@yahoo.com>
-;; Version: 3.0
+;; Maintainer: Michael Mauger <michael@mauger.com>
+;; Version: 3.3
 ;; Keywords: comm languages processes
 ;; URL: http://savannah.gnu.org/projects/emacs/
 
 ;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
 ;; `imenu-add-menubar-index'.
 
-;;; Requirements for Emacs 19.34:
-
-;; If you are using Emacs 19.34, you will have to get and install
-;; the file regexp-opt.el
-;; <URL:ftp://ftp.ifi.uio.no/pub/emacs/emacs-20.3/lisp/emacs-lisp/regexp-opt.el>
-;; and the custom package
-;; <URL:http://www.dina.kvl.dk/~abraham/custom/>.
-
 ;;; Bugs:
 
 ;; sql-ms now uses osql instead of isql.  Osql flushes its error
@@ -96,7 +88,7 @@
 ;; This improves the interaction under Emacs but it still is somewhat
 ;; awkward.
 
-;; Quoted identifiers are not supported for hilighting.  Most
+;; Quoted identifiers are not supported for highlighting.  Most
 ;; databases support the use of double quoted strings in place of
 ;; identifiers; ms (Microsoft SQLServer) also supports identifiers
 ;; enclosed within brackets [].
 ;;
 ;;         ;; Do something with `sql-user', `sql-password',
 ;;         ;; `sql-database', and `sql-server'.
-;;         (let ((params 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)))
+;;         (let ((params
+;;                (append
 ;;           (if (not (string= "" sql-user))
-;;               (setq params (append (list "-U" sql-user) params)))
+;;                     (list "-U" sql-user))
+;;                 (if (not (string= "" sql-password))
+;;                     (list "-P" sql-password))
+;;                 (if (not (string= "" sql-database))
+;;                     (list "-D" sql-database))
+;;                 (if (not (string= "" sql-server))
+;;                     (list "-S" sql-server))
+;;                 options)))
 ;;           (sql-comint product params)))
 ;;
 ;;     (sql-set-product-feature 'xyz
 ;; nino <nino@inform.dk>
 ;; Berend de Boer <berend@pobox.com>
 ;; Adam Jenkins <adam@thejenkins.org>
-;; Michael Mauger <mmaug@yahoo.com> -- improved product support
+;; Michael Mauger <michael@mauger.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; code polish
+;; Stefan Monnier <monnier@iro.umontreal.ca> -- font-lock corrections; 
+;;      code polish
+;; Paul Sleigh <bat@flurf.net> -- MySQL keyword enhancement
+;; Andrew Schein <andrew@andrewschein.com> -- sql-port bug
+;; Ian Bjorhovde <idbjorh@dataproxy.com> -- db2 escape newlines 
+;;      incorrectly enabled by default
+;; Roman Scherer <roman.scherer@nugg.ad> -- Connection documentation
+;; Mark Wilkinson <wilkinsonmr@gmail.com> -- file-local variables ignored
+;;
 
 \f
 
 ;;; Code:
 
+(require 'cl-lib)
 (require 'comint)
 ;; Need the following to allow GNU Emacs 19 to compile the file.
 (eval-when-compile
   (require 'regexp-opt))
 (require 'custom)
 (require 'thingatpt)
-(eval-when-compile ;; needed in Emacs 19, 20
-  (setq max-specpdl-size (max max-specpdl-size 2000)))
-
-(defun sql-signum (n)
-  "Return 1, 0, or -1 to identify the sign of N."
-  (cond
-   ((not (numberp n)) nil)
-   ((< n 0) -1)
-   ((> n 0) 1)
-   (t 0)))
+(require 'view)
 
 (defvar font-lock-keyword-face)
 (defvar font-lock-set-defaults)
   :group 'languages
   :group 'processes)
 
-;; These four variables will be used as defaults, if set.
+;; These five variables will be used as defaults, if set.
 
 (defcustom sql-user ""
   "Default username."
 
 (defcustom sql-password ""
   "Default password.
-
-Storing your password in a textfile such as ~/.emacs could be dangerous.
-Customizing your password will store it in your ~/.emacs file."
+If you customize this, the value will be stored in your init
+file.  Since that is a plaintext file, this could be dangerous."
   :type 'string
   :group 'SQL
   :risky t)
@@ -283,7 +276,7 @@ Customizing your password will store it in your ~/.emacs file."
   :safe 'stringp)
 
 (defcustom sql-port 0
-  "Default port."
+  "Default port for connecting to a MySQL or Postgres server."
   :version "24.1"
   :type 'number
   :group 'SQL
@@ -293,36 +286,49 @@ Customizing your password will store it in your ~/.emacs file."
 
 (define-widget 'sql-login-params 'lazy
   "Widget definition of the login parameters list"
-  ;; FIXME: does not implement :default property for the user,
-  ;; database and server options.  Anybody have some guidance on how to
-  ;; do this.
   :tag "Login Parameters"
-  :type '(repeat (choice
-                  (const user)
-                  (const password)
-                  (choice :tag "server"
-                          (const server)
-                          (list :tag "file"
-                                (const :format "" server)
-                                (const :format "" :file)
-                                regexp)
-                          (list :tag "completion"
-                                (const :format "" server)
+  :type '(set :tag "Login Parameters"
+              (choice :tag "user"
+                      :value user
+                      (const user)
+                      (list :tag "Specify a default"
+                            (const user)
+                            (list :tag "Default"
+                                  :inline t (const :default) string)))
+              (const password)
+              (choice :tag "server"
+                      :value server
+                      (const server)
+                      (list :tag "Specify a default"
+                            (const server)
+                            (list :tag "Default"
+                                  :inline t (const :default) string))
+                      (list :tag "file"
+                            (const :format "" server)
+                            (const :format "" :file)
+                            regexp)
+                      (list :tag "completion"
+                            (const :format "" server)
+                            (const :format "" :completion)
+                            (restricted-sexp
+                             :match-alternatives (listp stringp))))
+              (choice :tag "database"
+                      :value database
+                      (const database)
+                      (list :tag "Specify a default"
+                            (const database)
+                            (list :tag "Default"
+                                  :inline t (const :default) string))
+                      (list :tag "file"
+                            (const :format "" database)
+                            (const :format "" :file)
+                            regexp)
+                      (list :tag "completion"
+                            (const :format "" database)
                                 (const :format "" :completion)
                                 (restricted-sexp
                                  :match-alternatives (listp stringp))))
-                  (choice :tag "database"
-                          (const database)
-                          (list :tag "file"
-                                (const :format "" database)
-                                (const :format "" :file)
-                                regexp)
-                          (list :tag "completion"
-                                (const :format "" database)
-                                (const :format "" :completion)
-                                (restricted-sexp
-                                 :match-alternatives (listp stringp))))
-                  (const port))))
+              (const port)))
 
 ;; SQL Product support
 
@@ -432,7 +438,7 @@ Customizing your password will store it in your ~/.emacs file."
      :completion-object sql-oracle-completion-object
      :prompt-regexp "^SQL> "
      :prompt-length 5
-     :prompt-cont-regexp "^\\s-*[[:digit:]]+  "
+     :prompt-cont-regexp "^\\(?:[ ][ ][1-9]\\|[ ][1-9][0-9]\\|[1-9][0-9]\\{2\\}\\)[ ]\\{2\\}"
      :statement sql-oracle-statement-starters
      :syntax-alist ((?$ . "_") (?# . "_"))
      :terminator ("\\(^/\\|;\\)$" . "/")
@@ -611,41 +617,36 @@ settings.")
   '(:font-lock :sqli-program :sqli-options :sqli-login :statement))
 
 (defcustom sql-connection-alist nil
-  "An alist of connection parameters for interacting with a SQL
-  product.
-
+  "An alist of connection parameters for interacting with a SQL product.
 Each element of the alist is as follows:
 
   \(CONNECTION \(SQL-VARIABLE VALUE) ...)
 
-Where CONNECTION is a symbol identifying the connection, SQL-VARIABLE
-is the symbol name of a SQL mode variable, and VALUE is the value to
-be assigned to the variable.
-
-The most common SQL-VARIABLE settings associated with a connection
-are:
-
-  `sql-product'
-  `sql-user'
-  `sql-password'
-  `sql-port'
-  `sql-server'
-  `sql-database'
+Where CONNECTION is a case-insensitive string identifying the
+connection, SQL-VARIABLE is the symbol name of a SQL mode
+variable, and VALUE is the value to be assigned to the variable.
+The most common SQL-VARIABLE settings associated with a
+connection are: `sql-product', `sql-user', `sql-password',
+`sql-port', `sql-server', and `sql-database'.
 
 If a SQL-VARIABLE is part of the connection, it will not be
-prompted for during login."
-
+prompted for during login.  The command `sql-connect' starts a
+predefined SQLi session using the parameters from this list.
+Connections defined here appear in the submenu SQL->Start...  for
+making new SQLi sessions."
   :type `(alist :key-type (string :tag "Connection")
                 :value-type
                 (set
                  (group (const :tag "Product"  sql-product)
                         (choice
-                         ,@(mapcar (lambda (prod-info)
-                                     `(const :tag
-                                             ,(or (plist-get (cdr prod-info) :name)
-                                                  (capitalize (symbol-name (car prod-info))))
-                                             (quote ,(car prod-info))))
-                                   sql-product-alist)))
+                         ,@(mapcar
+                            (lambda (prod-info)
+                              `(const :tag
+                                      ,(or (plist-get (cdr prod-info) :name)
+                                           (capitalize
+                                            (symbol-name (car prod-info))))
+                                      (quote ,(car prod-info))))
+                            sql-product-alist)))
                  (group (const :tag "Username" sql-user)     string)
                  (group (const :tag "Password" sql-password) string)
                  (group (const :tag "Server"   sql-server)   string)
@@ -659,8 +660,8 @@ prompted for during login."
   :group 'SQL)
 
 (defcustom sql-product 'ansi
-  "Select the SQL database product used so that buffers can be
-highlighted properly when you open them."
+  "Select the SQL database product used.
+This allows highlighting buffers properly when you open them."
   :type `(choice
           ,@(mapcar (lambda (prod-info)
                       `(const :tag
@@ -740,15 +741,15 @@ this variable is nil, that buffer is shown using
 
 (defvar sql-imenu-generic-expression
   ;; 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)
-    ("Types" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*type\\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))
+  '(("Rules/Defaults" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*\\(?:rule\\|default\\)\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\s-+\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
+    ("Sequences" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*sequence\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
+    ("Triggers" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*trigger\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
+    ("Functions" "^\\s-*\\(?:create\\s-+\\(?:\\w+\\s-+\\)*\\)?function\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
+    ("Procedures" "^\\s-*\\(?:create\\s-+\\(?:\\w+\\s-+\\)*\\)?proc\\(?:edure\\)?\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
+    ("Packages" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*package\\s-+\\(?:body\\s-+\\)?\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
+    ("Types" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*type\\s-+\\(?:body\\s-+\\)?\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
+    ("Indexes" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*index\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
+    ("Tables/Views" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*\\(?:table\\|view\\)\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1))
   "Define interesting points in the SQL buffer for `imenu'.
 
 This is used to set `imenu-generic-expression' when SQL mode is
@@ -811,18 +812,29 @@ is changed."
   :type 'hook
   :group 'SQL)
 
+(defcustom sql-login-hook '()
+  "Hook for interacting with a buffer in `sql-interactive-mode'.
+
+This hook is invoked in a buffer once it is ready to accept input
+for the first time."
+  :version "24.1"
+  :type 'hook
+  :group 'SQL)
+
 ;; Customization for ANSI
 
-(defcustom sql-ansi-statement-starters (regexp-opt '(
- "create" "alter" "drop"
- "select" "insert" "update" "delete" "merge"
- "grant" "revoke"
-))
-  "Regexp of keywords that start SQL commands
+(defcustom sql-ansi-statement-starters
+  (regexp-opt '("create" "alter" "drop"
+                "select" "insert" "update" "delete" "merge"
+                "grant" "revoke"))
+  "Regexp of keywords that start SQL commands.
 
 All products share this list; products should define a regexp to
 identify additional keywords in a variable defined by
-the :statement feature.")
+the :statement feature."
+  :version "24.1"
+  :type 'string
+  :group 'SQL)
 
 ;; Customization for Oracle
 
@@ -849,8 +861,12 @@ You will find the file in your Orant\\bin directory."
   :version "24.1"
   :group 'SQL)
 
-(defcustom sql-oracle-statement-starters (regexp-opt '("declare" "begin" "with"))
-  "Additional statement starting keywords in Oracle.")
+(defcustom sql-oracle-statement-starters
+  (regexp-opt '("declare" "begin" "with"))
+  "Additional statement starting keywords in Oracle."
+  :version "24.1"
+  :type 'string
+  :group 'SQL)
 
 (defcustom sql-oracle-scan-on t
   "Non-nil if placeholders should be replaced in Oracle SQLi.
@@ -865,6 +881,17 @@ You need to issue the following command in SQL*Plus to be safe:
     SET DEFINE OFF
 
 In older versions of SQL*Plus, this was the SET SCAN OFF command."
+  :version "24.1"
+  :type 'boolean
+  :group 'SQL)
+
+(defcustom sql-db2-escape-newlines nil
+  "Non-nil if newlines should be escaped by a backslash in DB2 SQLi.
+
+When non-nil, Emacs will automatically insert a space and
+backslash prior to every newline in multi-line SQL statements as
+they are submitted to an interactive DB2 session."
+  :version "24.3"
   :type 'boolean
   :group 'SQL)
 
@@ -1144,10 +1171,10 @@ You can change `sql-prompt-length' on `sql-interactive-mode-hook'.")
 Used by `sql-rename-buffer'.")
 
 (defun sql-buffer-live-p (buffer &optional product connection)
-  "Returns non-nil if the process associated with buffer is live.
+  "Return non-nil if the process associated with buffer is live.
 
 BUFFER can be a buffer object or a buffer name.  The buffer must
-be a live buffer, have an running process attached to it, be in
+be a live buffer, have a running process attached to it, be in
 `sql-interactive-mode', and, if PRODUCT or CONNECTION are
 specified, it's `sql-product' or `sql-connection' must match."
 
@@ -1155,7 +1182,6 @@ specified, it's `sql-product' or `sql-connection' must match."
     (setq buffer (get-buffer buffer))
     (and buffer
          (buffer-live-p buffer)
-         (get-buffer-process buffer)
          (comint-check-proc buffer)
          (with-current-buffer buffer
            (and (derived-mode-p 'sql-interactive-mode)
@@ -1261,30 +1287,18 @@ Based on `comint-mode-map'.")
    ["List all objects" sql-list-all (sql-get-product-feature sql-product :list-all)]
    ["List table details" sql-list-table (sql-get-product-feature sql-product :list-table)]))
 
-;; Abbreviations -- if you want more of them, define them in your
-;; ~/.emacs file.  Abbrevs have to be enabled in your ~/.emacs, too.
-
-(defvar sql-mode-abbrev-table nil
+;; Abbreviations -- if you want more of them, define them in your init
+;; file.  Abbrevs have to be enabled in your init file, too.
+
+(define-abbrev-table 'sql-mode-abbrev-table
+  '(("ins" "insert" nil nil t)
+    ("upd" "update" nil nil t)
+    ("del" "delete" nil nil t)
+    ("sel" "select" nil nil t)
+    ("proc" "procedure" nil nil t)
+    ("func" "function" nil nil t)
+    ("cr" "create" nil nil t))
   "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
-(unless sql-mode-abbrev-table
-  (define-abbrev-table 'sql-mode-abbrev-table nil))
-
-(mapc
- ;; In Emacs 22+, 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
 
@@ -1303,7 +1317,7 @@ Based on `comint-mode-map'.")
     ;; double quotes (") don't delimit strings
     (modify-syntax-entry ?\" "." table)
     ;; Make these all punctuation
-    (mapc (lambda (c) (modify-syntax-entry c "." table))
+    (mapc #'(lambda (c) (modify-syntax-entry c "." table))
           (string-to-list "!#$%&+,.:;<=>?@\\|"))
     table)
   "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
@@ -1316,7 +1330,8 @@ Based on `comint-mode-map'.")
                  "\\(?:\\w+\\s-+\\)*"  ;; optional intervening keywords
                  "\\(?:table\\|view\\|\\(?:package\\|type\\)\\(?:\\s-+body\\)?\\|proc\\(?:edure\\)?"
                  "\\|function\\|trigger\\|sequence\\|rule\\|default\\)\\s-+"
-                 "\\(\\w+\\)")
+                  "\\(?:if\\s-+not\\s-+exists\\s-+\\)?" ;; IF NOT EXISTS
+                 "\\(\\w+\\(?:\\s-*[.]\\s-*\\w+\\)*\\)")
          1 'font-lock-function-name-face))
 
   "Pattern to match the names of top-level objects.
@@ -1329,9 +1344,9 @@ statement.  The format of variable should be a valid
 ;; are not followed closely, and most vendors offer significant
 ;; capabilities beyond those defined in the standard specifications.
 
-;; SQL mode provides support for hilighting based on the product.  In
-;; addition to hilighting the product keywords, any ANSI keywords not
-;; used by the product are also hilighted.  This will help identify
+;; SQL mode provides support for highlighting based on the product.  In
+;; addition to highlighting the product keywords, any ANSI keywords not
+;; used by the product are also highlighted.  This will help identify
 ;; keywords that could be restricted in future versions of the product
 ;; or might be a problem if ported to another product.
 
@@ -1506,14 +1521,13 @@ 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.")
 
-(defun sql-oracle-show-reserved-words ()
+(defun sql--oracle-show-reserved-words ()
   ;; This function is for use by the maintainer of SQL.EL only.
-  (interactive)
   (if (or (and (not (derived-mode-p 'sql-mode))
                (not (derived-mode-p 'sql-interactive-mode)))
           (not sql-buffer)
           (not (eq sql-product 'oracle)))
-      (error "Not an Oracle buffer")
+      (user-error "Not an Oracle buffer")
 
     (let ((b "*RESERVED WORDS*"))
       (sql-execute sql-buffer b
@@ -1594,8 +1608,9 @@ to add functions and PL/SQL keywords.")
 "atan" "atan2" "avg" "bfilename" "bin_to_num" "bitand" "cardinality"
 "cast" "ceil" "chartorowid" "chr" "cluster_id" "cluster_probability"
 "cluster_set" "coalesce" "collect" "compose" "concat" "convert" "corr"
+"connect_by_root" "connect_by_iscycle" "connect_by_isleaf"
 "corr_k" "corr_s" "cos" "cosh" "count" "covar_pop" "covar_samp"
-"cube_table" "cume_dist" "currrent_date" "currrent_timestamp" "cv"
+"cube_table" "cume_dist" "current_date" "current_timestamp" "cv"
 "dataobj_to_partition" "dbtimezone" "decode" "decompose" "deletexml"
 "dense_rank" "depth" "deref" "dump" "empty_blob" "empty_clob"
 "existsnode" "exp" "extract" "extractvalue" "feature_id" "feature_set"
@@ -1695,7 +1710,7 @@ to add functions and PL/SQL keywords.")
 "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"
+"organization" "out" "outer" "outline" "over" "overflow" "overriding"
 "package" "packages" "parallel" "parallel_enable" "parameters"
 "parent" "partition" "partitions" "password" "password_grace_time"
 "password_life_time" "password_lock_time" "password_reuse_max"
@@ -1748,7 +1763,7 @@ to add functions and PL/SQL keywords.")
      ;; Oracle PL/SQL Functions
      (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
 "delete" "trim" "extend" "exists" "first" "last" "count" "limit"
-"prior" "next"
+"prior" "next" "sqlcode" "sqlerrm"
 )
 
      ;; Oracle PL/SQL Reserved words
@@ -1933,7 +1948,7 @@ to add functions and PL/SQL keywords.")
      ;; Postgres non-reserved words
      (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
 "abort" "absolute" "access" "action" "add" "admin" "after" "aggregate"
-"also" "alter" "always" "assertion" "assignment" "at" "backward"
+"also" "alter" "always" "assertion" "assignment" "at" "attribute" "backward"
 "before" "begin" "between" "by" "cache" "called" "cascade" "cascaded"
 "catalog" "chain" "characteristics" "checkpoint" "class" "close"
 "cluster" "coalesce" "comment" "comments" "commit" "committed"
@@ -1944,40 +1959,40 @@ to add functions and PL/SQL keywords.")
 "delimiters" "dictionary" "disable" "discard" "document" "domain"
 "drop" "each" "enable" "encoding" "encrypted" "enum" "escape"
 "exclude" "excluding" "exclusive" "execute" "exists" "explain"
-"external" "extract" "family" "first" "float" "following" "force"
+"extension" "external" "extract" "family" "first" "float" "following" "force"
 "forward" "function" "functions" "global" "granted" "greatest"
 "handler" "header" "hold" "hour" "identity" "if" "immediate"
 "immutable" "implicit" "including" "increment" "index" "indexes"
 "inherit" "inherits" "inline" "inout" "input" "insensitive" "insert"
-"instead" "invoker" "isolation" "key" "language" "large" "last"
-"lc_collate" "lc_ctype" "least" "level" "listen" "load" "local"
+"instead" "invoker" "isolation" "key" "label" "language" "large" "last"
+"lc_collate" "lc_ctype" "leakproof" "least" "level" "listen" "load" "local"
 "location" "lock" "login" "mapping" "match" "maxvalue" "minute"
-"minvalue" "mode" "month" "move" "name" "names" "national" "nchar"
+"minvalue" "mode" "month" "move" "names" "national" "nchar"
 "next" "no" "nocreatedb" "nocreaterole" "nocreateuser" "noinherit"
-"nologin" "none" "nosuperuser" "nothing" "notify" "nowait" "nullif"
-"nulls" "object" "of" "oids" "operator" "option" "options" "out"
-"overlay" "owned" "owner" "parser" "partial" "partition" "password"
-"plans" "position" "preceding" "prepare" "prepared" "preserve" "prior"
+"nologin" "none"  "noreplication" "nosuperuser" "nothing" "notify" "nowait" "nullif"
+"nulls" "object" "of" "off" "oids" "operator" "option" "options" "out"
+"overlay" "owned" "owner" "parser" "partial" "partition" "passing" "password"
+"plans" "position" "preceding" "precision" "prepare" "prepared" "preserve" "prior"
 "privileges" "procedural" "procedure" "quote" "range" "read"
-"reassign" "recheck" "recursive" "reindex" "relative" "release"
-"rename" "repeatable" "replace" "replica" "reset" "restart" "restrict"
+"reassign" "recheck" "recursive" "ref" "reindex" "relative" "release"
+"rename" "repeatable" "replace" "replica" "replication" "reset" "restart" "restrict"
 "returns" "revoke" "role" "rollback" "row" "rows" "rule" "savepoint"
-"schema" "scroll" "search" "second" "security" "sequence" "sequences"
+"schema" "scroll" "search" "second" "security" "sequence"
 "serializable" "server" "session" "set" "setof" "share" "show"
-"simple" "stable" "standalone" "start" "statement" "statistics"
+"simple" "snapshot" "stable" "standalone" "start" "statement" "statistics"
 "stdin" "stdout" "storage" "strict" "strip" "substring" "superuser"
 "sysid" "system" "tables" "tablespace" "temp" "template" "temporary"
-"transaction" "treat" "trigger" "trim" "truncate" "trusted" "type"
-"unbounded" "uncommitted" "unencrypted" "unknown" "unlisten" "until"
-"update" "vacuum" "valid" "validator" "value" "values" "version"
-"view" "volatile" "whitespace" "work" "wrapper" "write"
-"xmlattributes" "xmlconcat" "xmlelement" "xmlforest" "xmlparse"
-"xmlpi" "xmlroot" "xmlserialize" "year" "yes"
+"transaction" "treat" "trim" "truncate" "trusted" "type" "types"
+"unbounded" "uncommitted" "unencrypted" "unlisten" "unlogged" "until"
+"update" "vacuum" "valid" "validate" "validator" "value" "values" "varying" "version"
+"view" "volatile" "whitespace" "without" "work" "wrapper" "write"
+"xmlattributes" "xmlconcat" "xmlelement" "xmlexists" "xmlforest" "xmlparse"
+"xmlpi" "xmlroot" "xmlserialize" "year" "yes" "zone"
 )
 
      ;; Postgres Reserved
      (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
-"all" "analyse" "analyze" "and" "any" "array" "asc" "as" "asymmetric"
+"all" "analyse" "analyze" "and" "array" "asc" "as" "asymmetric"
 "authorization" "binary" "both" "case" "cast" "check" "collate"
 "column" "concurrently" "constraint" "create" "cross"
 "current_catalog" "current_date" "current_role" "current_schema"
@@ -1986,12 +2001,18 @@ to add functions and PL/SQL keywords.")
 "fetch" "foreign" "for" "freeze" "from" "full" "grant" "group"
 "having" "ilike" "initially" "inner" "in" "intersect" "into" "isnull"
 "is" "join" "leading" "left" "like" "limit" "localtime"
-"localtimestamp" "natural" "notnull" "not" "null" "off" "offset"
+"localtimestamp" "natural" "notnull" "not" "null" "offset"
 "only" "on" "order" "or" "outer" "overlaps" "over" "placing" "primary"
 "references" "returning" "right" "select" "session_user" "similar"
 "some" "symmetric" "table" "then" "to" "trailing" "true" "union"
 "unique" "user" "using" "variadic" "verbose" "when" "where" "window"
 "with"
+)
+
+     ;; Postgres PL/pgSQL
+     (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
+"assign" "if" "case" "loop" "while" "for" "foreach" "exit" "elsif" "return"
+"raise" "execsql" "dynexecute" "perform" "getdiag" "open" "fetch" "move" "close"
 )
 
      ;; Postgres Data Types
@@ -1999,10 +2020,10 @@ to add functions and PL/SQL keywords.")
 "bigint" "bigserial" "bit" "bool" "boolean" "box" "bytea" "char"
 "character" "cidr" "circle" "date" "decimal" "double" "float4"
 "float8" "inet" "int" "int2" "int4" "int8" "integer" "interval" "line"
-"lseg" "macaddr" "money" "numeric" "path" "point" "polygon"
-"precision" "real" "serial" "serial4" "serial8" "smallint" "text"
+"lseg" "macaddr" "money" "name" "numeric" "path" "point" "polygon"
+"precision" "real" "serial" "serial4" "serial8" "sequences" "smallint" "text"
 "time" "timestamp" "timestamptz" "timetz" "tsquery" "tsvector"
-"txid_snapshot" "uuid" "varbit" "varchar" "varying" "without"
+"txid_snapshot" "unknown" "uuid" "varbit" "varchar" "varying" "without"
 "xml" "zone"
 )))
 
@@ -2279,7 +2300,7 @@ you define your own `sql-mode-solid-font-lock-keywords'.")
 "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"
+"distinct" "distinctrow" "do" "drop" "dumpfile" "duplicate" "else" "elseif"
 "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"
@@ -2399,7 +2420,7 @@ highlighting rules in SQL mode.")
   (let ((init (or (and initial (symbol-name initial)) "ansi")))
     (intern (completing-read
              prompt
-             (mapcar (lambda (info) (symbol-name (car info)))
+             (mapcar #'(lambda (info) (symbol-name (car info)))
                      sql-product-alist)
              nil 'require-match
              init 'sql-product-history init))))
@@ -2415,10 +2436,10 @@ configuration."
 
   ;; Don't do anything if the product is already supported
   (if (assoc product sql-product-alist)
-      (message "Product `%s' is already defined" product)
+      (user-error "Product `%s' is already defined" product)
 
     ;; Add product to the alist
-    (add-to-list 'sql-product-alist `((,product :name ,display . ,plist)))
+    (add-to-list 'sql-product-alist `(,product :name ,display . ,plist))
     ;; Add a menu item to the SQL->Product menu
     (easy-menu-add-item sql-mode-menu '("Product")
                        ;; Each product is represented by a radio
@@ -2434,11 +2455,11 @@ configuration."
                        ;; after this product's name.
                        (let ((next-item)
                              (down-display (downcase display)))
-                         (map-keymap (lambda (k b)
-                                       (when (and (not next-item)
-                                                  (string-lessp down-display
-                                                                (downcase (cadr b))))
-                                         (setq next-item k)))
+                         (map-keymap #'(lambda (k b)
+                                          (when (and (not next-item)
+                                                     (string-lessp down-display
+                                                                   (downcase (cadr b))))
+                                            (setq next-item k)))
                                      (easy-menu-get-map sql-mode-menu '("Product")))
                          next-item))
     product))
@@ -2469,7 +2490,7 @@ argument must be a plist keyword accepted by
              (symbolp v))
             (set v newvalue)
           (setcdr p (plist-put (cdr p) feature newvalue)))
-      (message "`%s' is not a known product; use `sql-add-product' to add it first." product))))
+      (error "`%s' is not a known product; use `sql-add-product' to add it first." product))))
 
 (defun sql-get-product-feature (product feature &optional fallback not-indirect)
   "Lookup FEATURE associated with a SQL PRODUCT.
@@ -2499,14 +2520,14 @@ See `sql-product-alist' for a list of products and supported features."
                (symbolp v))
               (symbol-value v)
             v))
-      (message "`%s' is not a known product; use `sql-add-product' to add it first." product)
+      (error "`%s' is not a known product; use `sql-add-product' to add it first." product)
       nil)))
 
 (defun sql-product-font-lock (keywords-only imenu)
   "Configure font-lock and imenu with product-specific settings.
 
 The KEYWORDS-ONLY flag is passed to font-lock to specify whether
-only keywords should be hilighted and syntactic hilighting
+only keywords should be highlighted and syntactic highlighting
 skipped.  The IMENU flag indicates whether `imenu-mode' should
 also be configured."
 
@@ -2540,13 +2561,13 @@ also be configured."
       (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))
+             #'(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.
@@ -2586,15 +2607,13 @@ adds a fontification pattern to fontify identifiers ending in
             (append keywords old-val))))))
 
 (defun sql-for-each-login (login-params body)
-  "Iterates through login parameters and returns a list of results."
-
+  "Iterate through login parameters and return a list of results."
   (delq nil
         (mapcar
-         (lambda (param)
-           (let ((token (or (and (listp param) (car param)) param))
-                 (plist (or (and (listp param) (cdr param)) nil)))
-
-             (funcall body token plist)))
+         #'(lambda (param)
+             (let ((token (or (car-safe param) param))
+                   (plist (cdr-safe param)))
+               (funcall body token plist)))
          login-params)))
 
 \f
@@ -2603,8 +2622,8 @@ adds a fontification pattern to fontify identifiers ending in
 
 (defun sql-product-syntax-table ()
   (let ((table (copy-syntax-table sql-mode-syntax-table)))
-    (mapc (lambda (entry)
-            (modify-syntax-entry (car entry) (cdr entry) table))
+    (mapc #'(lambda (entry)
+              (modify-syntax-entry (car entry) (cdr entry) table))
           (sql-get-product-feature sql-product :syntax-alist))
     table))
 
@@ -2612,10 +2631,10 @@ adds a fontification pattern to fontify identifiers ending in
   (append
    ;; Change all symbol character to word characters
    (mapcar
-    (lambda (entry) (if (string= (substring (cdr entry) 0 1) "_")
-                        (cons (car entry)
-                              (concat "w" (substring (cdr entry) 1)))
-                      entry))
+    #'(lambda (entry) (if (string= (substring (cdr entry) 0 1) "_")
+                          (cons (car entry)
+                                (concat "w" (substring (cdr entry) 1)))
+                        entry))
     (sql-get-product-feature sql-product :syntax-alist))
    '((?_ . "w"))))
 
@@ -2638,7 +2657,7 @@ adds a fontification pattern to fontify identifiers ending in
    (list (sql-read-product "SQL product: ")))
   (if (stringp product) (setq product (intern product)))
   (when (not (assoc product sql-product-alist))
-    (error "SQL product %s is not supported; treated as ANSI" product)
+    (user-error "SQL product %s is not supported; treated as ANSI" product)
     (setq product 'ansi))
 
   ;; Save product setting and fontify.
@@ -2657,6 +2676,34 @@ matching the regular expression `comint-prompt-regexp', a buffer
 local variable."
       (save-excursion (comint-bol nil) (point))))
 
+;;; SMIE support
+
+;; Needs a lot more love than I can provide.  --Stef
+
+;; (require 'smie)
+
+;; (defconst sql-smie-grammar
+;;   (smie-prec2->grammar
+;;    (smie-bnf->prec2
+;;     ;; Partly based on http://www.h2database.com/html/grammar.html
+;;     '((cmd ("SELECT" select-exp "FROM" select-table-exp)
+;;            )
+;;       (select-exp ("*") (exp) (exp "AS" column-alias))
+;;       (column-alias)
+;;       (select-table-exp (table-exp "WHERE" exp) (table-exp))
+;;       (table-exp)
+;;       (exp ("CASE" exp "WHEN" exp "THEN" exp "ELSE" exp "END")
+;;            ("CASE" exp "WHEN" exp "THEN" exp "END"))
+;;       ;; Random ad-hoc additions.
+;;       (foo (foo "," foo))
+;;       )
+;;     '((assoc ",")))))
+
+;; (defun sql-smie-rules (kind token)
+;;   (pcase (cons kind token)
+;;     (`(:list-intro . ,_) t)
+;;     (`(:before . "(") (smie-rule-parent))))
+
 ;;; Motion Functions
 
 (defun sql-statement-regexp (prod)
@@ -2669,7 +2716,7 @@ local variable."
             "\\>")))
 
 (defun sql-beginning-of-statement (arg)
-  "Moves the cursor to the beginning of the current SQL statement."
+  "Move to the beginning of the current SQL statement."
   (interactive "p")
 
   (let ((here (point))
@@ -2696,10 +2743,10 @@ local variable."
     (beginning-of-line)
     ;; If we didn't move, try again
     (when (= here (point))
-      (sql-beginning-of-statement (* 2 (sql-signum arg))))))
+      (sql-beginning-of-statement (* 2 (cl-signum arg))))))
 
 (defun sql-end-of-statement (arg)
-  "Moves the cursor to the end of the current SQL statement."
+  "Move to the end of the current SQL statement."
   (interactive "p")
   (let ((term (sql-get-product-feature sql-product :terminator))
         (re-search (if (> 0 arg) 're-search-backward 're-search-forward))
@@ -2708,7 +2755,7 @@ local variable."
     (when (consp term)
       (setq term (car term)))
     ;; Iterate until we've moved the desired number of stmt ends
-    (while (not (= (sql-signum arg) 0))
+    (while (not (= (cl-signum arg) 0))
       ;; if we're looking at the terminator, jump by 2
       (if (or (and (> 0 arg) (looking-back term))
               (and (< 0 arg) (looking-at term)))
@@ -2719,7 +2766,7 @@ local variable."
           (setq arg 0)
         ;; count it if we're not in a comment
         (unless (nth 7 (syntax-ppss))
-          (setq arg (- arg (sql-signum arg))))))
+          (setq arg (- arg (cl-signum arg))))))
     (goto-char (if (match-data)
                    (match-end 0)
                  here))))
@@ -2736,6 +2783,7 @@ local variable."
             (comint-bol nil)
             (looking-at "go\\b")))
       (comint-send-input)))
+(put 'sql-magic-go 'delete-selection t)
 
 (defun sql-magic-semicolon (arg)
   "Insert semicolon and call `comint-send-input'.
@@ -2744,6 +2792,7 @@ local variable."
   (self-insert-command (prefix-numeric-value arg))
   (if (equal sql-electric-stuff 'semicolon)
        (comint-send-input)))
+(put 'sql-magic-semicolon 'delete-selection t)
 
 (defun sql-accumulate-and-indent ()
   "Continue SQL statement on the next line."
@@ -2777,10 +2826,14 @@ each line with INDENT."
                      "]\n"))))
     doc))
 
-;;;###autoload
 (defun sql-help ()
-  "Show short help for the SQL modes.
+  "Show short help for the SQL modes."
+  (interactive)
+  (describe-function 'sql-help))
+(put 'sql-help 'function-documentation '(sql--make-help-docstring))
 
+(defvar sql--help-docstring
+  "Show short help for the SQL modes.
 Use an entry function to open an interactive SQL buffer.  This buffer is
 usually named `*SQL*'.  The name of the major mode is SQLi.
 
@@ -2809,41 +2862,36 @@ anything.  The name of the major mode is SQL.
 
 In this SQL buffer (SQL mode), you can send the region or the entire
 buffer to the interactive SQL buffer (SQLi mode).  The results are
-appended to the SQLi buffer without disturbing your SQL buffer."
-  (interactive)
-
-  ;; Insert references to loaded products into the help buffer string
-  (let ((doc (documentation 'sql-help t))
-       changedp)
-    (setq changedp nil)
+appended to the SQLi buffer without disturbing your SQL buffer.")
 
+(defun sql--make-help-docstring ()
+  "Return a docstring for `sql-help' listing loaded SQL products."
+  (let ((doc sql--help-docstring))
     ;; Insert FREE software list
-    (when (string-match "^\\(\\s-*\\)[\\\\][\\\\]FREE\\s-*\n" doc 0)
+    (when (string-match "^\\(\\s-*\\)[\\\\][\\\\]FREE\\s-*$" doc 0)
       (setq doc (replace-match (sql-help-list-products (match-string 1 doc) t)
-                              t t doc 0)
-           changedp t))
-
+                              t t doc 0)))
     ;; Insert non-FREE software list
-    (when (string-match "^\\(\\s-*\\)[\\\\][\\\\]NONFREE\\s-*\n" doc 0)
+    (when (string-match "^\\(\\s-*\\)[\\\\][\\\\]NONFREE\\s-*$" doc 0)
       (setq doc (replace-match (sql-help-list-products (match-string 1 doc) nil)
-                              t t doc 0)
-           changedp t))
-
-    ;; If we changed the help text, save the change so that the help
-    ;; sub-system will see it
-    (when changedp
-      (put 'sql-help 'function-documentation doc)))
+                              t t doc 0)))
+    doc))
 
-  ;; Call help on this function
-  (describe-function 'sql-help))
+(defun sql-default-value (var)
+  "Fetch the value of a variable.
 
-(defun sql-read-passwd (prompt &optional default)
-  "Read a password using PROMPT.  Optional DEFAULT is password to start with."
-  (read-passwd prompt nil default))
+If the current buffer is in `sql-interactive-mode', then fetch
+the global value, otherwise use the buffer local value."
+  (if (derived-mode-p 'sql-interactive-mode)
+      (default-value var)
+    (buffer-local-value var (current-buffer))))
 
-(defun sql-get-login-ext (prompt last-value history-var plist)
+(defun sql-get-login-ext (symbol prompt history-var plist)
   "Prompt user with extended login parameters.
 
+The global value of SYMBOL is the last value and the global value
+of the SYMBOL is set based on the user's input.
+
 If PLIST is nil, then the user is simply prompted for a string
 value.
 
@@ -2856,38 +2904,40 @@ regexp pattern specified in its value.
 The `:completion' property prompts for a string specified by its
 value.  (The property value is used as the PREDICATE argument to
 `completing-read'.)"
-  (let* ((default (plist-get plist :default))
-         (prompt-def
-          (if default
-              (if (string-match "\\(\\):[ \t]*\\'" prompt)
-                  (replace-match (format " (default \"%s\")" default) t t prompt 1)
-                (replace-regexp-in-string "[ \t]*\\'"
-                                          (format " (default \"%s\") " default)
-                                          prompt t t))
-            prompt))
-         (use-dialog-box nil))
-    (cond
-     ((plist-member plist :file)
-      (expand-file-name
-       (read-file-name prompt
-                       (file-name-directory last-value) default t
-                       (file-name-nondirectory last-value)
-                       (when (plist-get plist :file)
-                         `(lambda (f)
-                            (string-match
-                             (concat "\\<" ,(plist-get plist :file) "\\>")
-                             (file-name-nondirectory f)))))))
-
-     ((plist-member plist :completion)
-      (completing-read prompt-def (plist-get plist :completion) nil t
-                       last-value history-var default))
-
-     ((plist-get plist :number)
-      (read-number prompt (or default last-value 0)))
-
-     (t
-      (let ((r (read-from-minibuffer prompt-def last-value nil nil history-var nil)))
-        (if (string= "" r) (or default "") r))))))
+  (set-default
+   symbol
+   (let* ((default (plist-get plist :default))
+          (last-value (sql-default-value symbol))
+          (prompt-def
+           (if default
+               (if (string-match "\\(\\):[ \t]*\\'" prompt)
+                   (replace-match (format " (default \"%s\")" default) t t prompt 1)
+                 (replace-regexp-in-string "[ \t]*\\'"
+                                           (format " (default \"%s\") " default)
+                                           prompt t t))
+             prompt))
+          (use-dialog-box nil))
+     (cond
+      ((plist-member plist :file)
+       (expand-file-name
+        (read-file-name prompt
+                        (file-name-directory last-value) default t
+                        (file-name-nondirectory last-value)
+                        (when (plist-get plist :file)
+                          `(lambda (f)
+                             (string-match
+                              (concat "\\<" ,(plist-get plist :file) "\\>")
+                              (file-name-nondirectory f)))))))
+
+      ((plist-member plist :completion)
+       (completing-read prompt-def (plist-get plist :completion) nil t
+                        last-value history-var default))
+
+      ((plist-get plist :number)
+       (read-number prompt (or default last-value 0)))
+
+      (t
+       (read-string prompt-def last-value history-var default))))))
 
 (defun sql-get-login (&rest what)
   "Get username, password and database from the user.
@@ -2917,40 +2967,29 @@ supported:
 
 In order to ask the user for username, password and database, call the
 function like this: (sql-get-login 'user 'password 'database)."
-  (interactive)
-  (mapcar
-   (lambda (w)
-     (let ((token (or (and (consp w) (car w)) w))
-           (plist (or (and (consp w) (cdr w)) nil)))
+  (dolist (w what)
+    (let ((plist (cdr-safe w)))
+      (pcase (or (car-safe w) w)
+        (`user
+         (sql-get-login-ext 'sql-user "User: " 'sql-user-history plist))
 
-     (cond
-      ((eq token 'user)                ; user
-       (setq sql-user
-             (sql-get-login-ext "User: " sql-user
-                                'sql-user-history plist)))
-
-      ((eq token 'password)            ; password
-       (setq sql-password
-             (sql-read-passwd "Password: " sql-password)))
-
-      ((eq token 'server)              ; server
-       (setq sql-server
-             (sql-get-login-ext "Server: " sql-server
-                                'sql-server-history plist)))
-
-      ((eq token 'database)            ; database
-       (setq sql-database
-             (sql-get-login-ext "Database: " sql-database
-                                'sql-database-history plist)))
-
-      ((eq token 'port)                ; port
-       (setq sql-port
-             (sql-get-login-ext "Port: " sql-port
-                                nil (append '(:number t) plist)))))))
-   what))
+        (`password
+         (setq-default sql-password
+                       (read-passwd "Password: " nil (sql-default-value 'sql-password))))
+
+        (`server
+         (sql-get-login-ext 'sql-server "Server: " 'sql-server-history plist))
+
+        (`database
+         (sql-get-login-ext 'sql-database "Database: "
+                            'sql-database-history plist))
+
+        (`port
+         (sql-get-login-ext 'sql-port "Port: "
+                            nil (append '(:number t) plist)))))))
 
 (defun sql-find-sqli-buffer (&optional product connection)
-  "Returns the name of the current default SQLi buffer or nil.
+  "Return the name of the current default SQLi buffer or nil.
 In order to qualify, the SQLi buffer must be alive, be in
 `sql-interactive-mode' and have a process."
   (let ((buf  sql-buffer)
@@ -2964,10 +3003,10 @@ In order to qualify, the SQLi buffer must be alive, be in
           (sql-buffer-live-p buf prod connection)
           buf)
      ;; Look thru each buffer
-     (car (apply 'append
-                 (mapcar (lambda (b)
-                           (and (sql-buffer-live-p b prod connection)
-                                (list (buffer-name b))))
+     (car (apply #'append
+                 (mapcar #'(lambda (b)
+                             (and (sql-buffer-live-p b prod connection)
+                                  (list (buffer-name b))))
                          (buffer-list)))))))
 
 (defun sql-set-sqli-buffer-generally ()
@@ -3008,10 +3047,10 @@ If you call it from anywhere else, it sets the global copy of
   (interactive)
   (let ((default-buffer (sql-find-sqli-buffer)))
     (if (null default-buffer)
-        (error "There is no suitable SQLi buffer")
+        (user-error "There is no suitable SQLi buffer")
       (let ((new-buffer (read-buffer "New SQLi buffer: " default-buffer t)))
         (if (null (sql-buffer-live-p new-buffer))
-            (error "Buffer %s is not a working SQLi buffer" new-buffer)
+            (user-error "Buffer %s is not a working SQLi buffer" new-buffer)
           (when new-buffer
             (setq sql-buffer new-buffer)
             (run-hooks 'sql-set-sqli-hook)))))))
@@ -3024,10 +3063,10 @@ variable `sql-buffer'.  See `sql-help' on how to create such a buffer."
   (interactive)
   (if (or (null sql-buffer)
           (null (buffer-live-p (get-buffer sql-buffer))))
-      (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
+      (user-error "%s has no SQLi buffer set" (buffer-name (current-buffer)))
     (if (null (get-buffer-process sql-buffer))
-       (message "Buffer %s has no process." sql-buffer)
-      (message "Current SQLi buffer is %s." sql-buffer))))
+       (user-error "Buffer %s has no process" sql-buffer)
+      (user-error "Current SQLi buffer is %s" sql-buffer))))
 
 (defun sql-make-alternate-buffer-name ()
   "Return a string that can be used to rename a SQLi buffer.
@@ -3048,35 +3087,35 @@ server/database name."
 
     ;; Build a name using the :sqli-login setting
     (setq name
-          (apply 'concat
+          (apply #'concat
                  (cdr
-                  (apply 'append nil
+                  (apply #'append nil
                          (sql-for-each-login
                           (sql-get-product-feature sql-product :sqli-login)
-                          (lambda (token plist)
-                            (cond
-                             ((eq token 'user)
-                              (unless (string= "" sql-user)
-                                (list "/" sql-user)))
-                             ((eq token 'port)
-                              (unless (or (not (numberp sql-port))
-                                          (= 0 sql-port))
-                                (list ":" (number-to-string sql-port))))
-                             ((eq token 'server)
-                              (unless (string= "" sql-server)
-                                (list "."
-                                      (if (plist-member plist :file)
-                                          (file-name-nondirectory sql-server)
-                                        sql-server))))
-                             ((eq token 'database)
-                              (unless (string= "" sql-database)
-                                (list "@"
-                                      (if (plist-member plist :file)
-                                         (file-name-nondirectory sql-database)
-                                        sql-database))))
-
-                             ((eq token 'password) nil)
-                             (t                    nil))))))))
+                          #'(lambda (token plist)
+                              (pcase token
+                                (`user
+                                 (unless (string= "" sql-user)
+                                   (list "/" sql-user)))
+                                (`port
+                                 (unless (or (not (numberp sql-port))
+                                             (= 0 sql-port))
+                                   (list ":" (number-to-string sql-port))))
+                                (`server
+                                 (unless (string= "" sql-server)
+                                   (list "."
+                                         (if (plist-member plist :file)
+                                             (file-name-nondirectory sql-server)
+                                           sql-server))))
+                                (`database
+                                 (unless (string= "" sql-database)
+                                   (list "@"
+                                         (if (plist-member plist :file)
+                                             (file-name-nondirectory sql-database)
+                                           sql-database))))
+
+                                ;; (`password nil)
+                                (_         nil))))))))
 
     ;; If there's a connection, use it and the name thus far
     (if sql-connection
@@ -3111,7 +3150,7 @@ NEW-NAME is empty, then the buffer name will be \"*SQL*\"."
   (interactive "P")
 
   (if (not (derived-mode-p 'sql-interactive-mode))
-      (message "Current buffer is not a SQL interactive buffer")
+      (user-error "Current buffer is not a SQL interactive buffer")
 
     (setq sql-alternate-buffer-name
           (cond
@@ -3121,6 +3160,7 @@ NEW-NAME is empty, then the buffer name will be \"*SQL*\"."
                          sql-alternate-buffer-name))
            (t                  sql-alternate-buffer-name)))
 
+    (setq sql-alternate-buffer-name (substring-no-properties sql-alternate-buffer-name))
     (rename-buffer (if (string= "" sql-alternate-buffer-name)
                        "*SQL*"
                      (format "*SQL: %s*" sql-alternate-buffer-name))
@@ -3178,20 +3218,23 @@ Placeholders are words starting with an ampersand like &this."
 
 ;; Using DB2 interactively, newlines must be escaped with " \".
 ;; The space before the backslash is relevant.
+
 (defun sql-escape-newlines-filter (string)
   "Escape newlines in STRING.
 Every newline in STRING will be preceded with a space and a backslash."
-  (let ((result "") (start 0) mb me)
-    (while (string-match "\n" string start)
-      (setq mb (match-beginning 0)
-           me (match-end 0)
-           result (concat result
-                          (substring string start mb)
-                          (if (and (> mb 1)
-                                   (string-equal " \\" (substring string (- mb 2) mb)))
-                              "" " \\\n"))
-           start me))
-    (concat result (substring string start))))
+  (if (not sql-db2-escape-newlines)
+      string
+    (let ((result "") (start 0) mb me)
+      (while (string-match "\n" string start)
+        (setq mb (match-beginning 0)
+              me (match-end 0)
+              result (concat result
+                             (substring string start mb)
+                             (if (and (> mb 1)
+                                      (string-equal " \\" (substring string (- mb 2) mb)))
+                                 "" " \\\n"))
+              start me))
+      (concat result (substring string start)))))
 
 \f
 
@@ -3202,13 +3245,10 @@ Every newline in STRING will be preceded with a space and a backslash."
 
 Allows the suppression of continuation prompts.")
 
-(defvar sql-output-by-send nil
-  "Non-nil if the command in the input was generated by `sql-send-string'.")
-
 (defun sql-input-sender (proc string)
   "Send STRING to PROC after applying filters."
 
-  (let* ((product (with-current-buffer (process-buffer proc) sql-product))
+  (let* ((product (buffer-local-value 'sql-product (process-buffer proc)))
         (filter  (sql-get-product-feature product :input-filter)))
 
     ;; Apply filter(s)
@@ -3218,15 +3258,13 @@ Allows the suppression of continuation prompts.")
      ((functionp filter)
       (setq string (funcall filter string)))
      ((listp filter)
-      (mapc (lambda (f) (setq string (funcall f string))) filter))
+      (mapc #'(lambda (f) (setq string (funcall f string))) filter))
      (t nil))
 
     ;; Count how many newlines in the string
-    (setq sql-output-newline-count 0)
-    (mapc (lambda (ch)
-            (when (eq ch ?\n)
-              (setq sql-output-newline-count (1+ sql-output-newline-count))))
-          string)
+    (setq sql-output-newline-count
+          (apply #'+ (mapcar #'(lambda (ch)
+                                (if (eq ch ?\n) 1 0)) string)))
 
     ;; Send the string
     (comint-simple-send proc string)))
@@ -3235,6 +3273,17 @@ Allows the suppression of continuation prompts.")
 
 (defvar sql-preoutput-hold nil)
 
+(defun sql-starts-with-prompt-re ()
+  "Anchor the prompt expression at the beginning of the output line.
+Remove the start of line regexp."
+  (replace-regexp-in-string "\\^" "\\\\`" comint-prompt-regexp))
+
+(defun sql-ends-with-prompt-re ()
+  "Anchor the prompt expression at the end of the output line.
+Remove the start of line regexp from the prompt expression since
+it may not follow newline characters in the output line."
+  (concat (replace-regexp-in-string "\\^" "" sql-prompt-regexp) "\\'"))
+
 (defun sql-interactive-remove-continuation-prompt (oline)
   "Strip out continuation prompts out of the OLINE.
 
@@ -3252,39 +3301,52 @@ to the next chunk to properly match the broken-up prompt.
 If the filter gets confused, it should reset and stop filtering
 to avoid deleting non-prompt output."
 
-  (let (did-filter)
-    (setq oline (concat (or sql-preoutput-hold "") oline)
-          sql-preoutput-hold nil)
+  (when comint-prompt-regexp
+    (save-match-data
+      (let (prompt-found last-nl)
 
-    (if (and comint-prompt-regexp
-             (integerp sql-output-newline-count)
-             (>= sql-output-newline-count 1))
-        (progn
-          (while (and (not (string= oline ""))
-                      (> sql-output-newline-count 0)
-                      (string-match comint-prompt-regexp oline)
-                      (= (match-beginning 0) 0))
-
-            (setq oline (replace-match "" nil nil oline)
-                  sql-output-newline-count (1- sql-output-newline-count)
-                  did-filter t))
+        ;; Add this text to what's left from the last pass
+        (setq oline (concat sql-preoutput-hold oline)
+              sql-preoutput-hold "")
 
+        ;; If we are looking for multiple prompts
+        (when (and (integerp sql-output-newline-count)
+                   (>= sql-output-newline-count 1))
+          ;; Loop thru each starting prompt and remove it
+          (let ((start-re (sql-starts-with-prompt-re)))
+            (while (and (not (string= oline ""))
+                      (> sql-output-newline-count 0)
+                      (string-match start-re oline))
+              (setq oline (replace-match "" nil nil oline)
+                    sql-output-newline-count (1- sql-output-newline-count)
+                    prompt-found t)))
+          
+          ;; If we've found all the expected prompts, stop looking
           (if (= sql-output-newline-count 0)
               (setq sql-output-newline-count nil
-                    oline (concat "\n" oline)
-                    sql-output-by-send nil)
+                    oline (concat "\n" oline))
 
+            ;; Still more possible prompts, leave them for the next pass
             (setq sql-preoutput-hold oline
-                  oline ""))
-
-          (unless did-filter
-            (setq oline (or sql-preoutput-hold "")
-                  sql-preoutput-hold nil
-                  sql-output-newline-count nil)))
-
-      (setq sql-output-newline-count nil))
-
-    oline))
+                  oline "")))
+
+        ;; If no prompts were found, stop looking
+        (unless prompt-found
+          (setq sql-output-newline-count nil
+                oline (concat oline sql-preoutput-hold)
+                sql-preoutput-hold ""))
+
+        ;; Break up output by physical lines if we haven't hit the final prompt
+        (unless (and (not (string= oline ""))
+                     (string-match (sql-ends-with-prompt-re) oline)
+                     (>= (match-end 0) (length oline)))
+          (setq last-nl 0)
+          (while (string-match "\n" oline last-nl)
+            (setq last-nl (match-end 0)))
+          (setq sql-preoutput-hold (concat (substring oline last-nl)
+                                           sql-preoutput-hold)
+                oline (substring oline 0 last-nl))))))
+   oline)
 
 ;;; Sending the region to the SQLi buffer.
 
@@ -3307,7 +3369,7 @@ to avoid deleting non-prompt output."
              (if sql-send-terminator
                  (sql-send-magic-terminator sql-buffer s sql-send-terminator))
 
-             (message "Sent string to buffer %s." sql-buffer)))
+             (message "Sent string to buffer %s" sql-buffer)))
 
          ;; Display the sql buffer
          (if sql-pop-to-buffer-after-send-region
@@ -3315,7 +3377,7 @@ to avoid deleting non-prompt output."
            (display-buffer sql-buffer)))
 
     ;; We don't have no stinkin' sql
-    (message "No SQL process started."))))
+    (user-error "No SQL process started"))))
 
 (defun sql-send-region (start end)
   "Send a region to the SQL process."
@@ -3366,8 +3428,7 @@ to avoid deleting non-prompt output."
       (setq sql-output-newline-count
             (if sql-output-newline-count
                 (1+ sql-output-newline-count)
-              1)))
-    (setq sql-output-by-send t)))
+              1)))))
 
 (defun sql-remove-tabs-filter (str)
   "Replace tab characters with spaces."
@@ -3409,7 +3470,7 @@ list of SQLi command strings."
     (when visible
       (message "Executing SQL command..."))
     (if (consp command)
-        (mapc (lambda (c) (sql-redirect-one sqlbuf c outbuf save-prior))
+        (mapc #'(lambda (c) (sql-redirect-one sqlbuf c outbuf save-prior))
               command)
       (sql-redirect-one sqlbuf command outbuf save-prior))
     (when visible
@@ -3423,7 +3484,8 @@ list of SQLi command strings."
                                                          :prompt-regexp))
           (start nil))
       (with-current-buffer buf
-        (toggle-read-only -1)
+        (setq-local view-no-disable-on-exit t)
+        (read-only-mode -1)
         (unless save-prior
           (erase-buffer))
         (goto-char (point-max))
@@ -3486,11 +3548,11 @@ for each match."
            (match-string regexp-groups))
           ;; list of numbers; return the specified matches only
           ((consp regexp-groups)
-           (mapcar (lambda (c)
-                     (cond
-                      ((numberp c) (match-string c))
-                      ((stringp c) (match-substitute-replacement c))
-                      (t (error "sql-redirect-value: unknown REGEXP-GROUPS value - %s" c))))
+           (mapcar #'(lambda (c)
+                       (cond
+                        ((numberp c) (match-string c))
+                        ((stringp c) (match-substitute-replacement c))
+                        (t (error "sql-redirect-value: unknown REGEXP-GROUPS value - %s" c))))
                    regexp-groups))
           ;; String is specified; return replacement string
           ((stringp regexp-groups)
@@ -3506,7 +3568,7 @@ for each match."
     (nreverse results)))
 
 (defun sql-execute (sqlbuf outbuf command enhanced arg)
-  "Executes a command in a SQL interactive buffer and captures the output.
+  "Execute a command in a SQL interactive buffer and capture the output.
 
 The commands are run in SQLBUF and the output saved in OUTBUF.
 COMMAND must be a string, a function or a list of such elements.
@@ -3514,17 +3576,17 @@ Functions are called with SQLBUF, OUTBUF and ARG as parameters;
 strings are formatted with ARG and executed.
 
 If the results are empty the OUTBUF is deleted, otherwise the
-buffer is popped into a view window. "
+buffer is popped into a view window."
   (mapc
-   (lambda (c)
-     (cond
-      ((stringp c)
-       (sql-redirect sqlbuf (if arg (format c arg) c) outbuf) t)
-      ((functionp c)
-       (apply c sqlbuf outbuf enhanced arg nil))
-      (t (error "Unknown sql-execute item %s" c))))
+   #'(lambda (c)
+       (cond
+        ((stringp c)
+         (sql-redirect sqlbuf (if arg (format c arg) c) outbuf) t)
+        ((functionp c)
+         (apply c sqlbuf outbuf enhanced arg nil))
+        (t (error "Unknown sql-execute item %s" c))))
    (if (consp command) command (cons command nil)))
-
+  
   (setq outbuf (get-buffer outbuf))
   (if (zerop (buffer-size outbuf))
       (kill-buffer outbuf)
@@ -3532,18 +3594,18 @@ buffer is popped into a view window. "
                        (get-lru-window))))
       (with-current-buffer outbuf
         (set-buffer-modified-p nil)
-        (toggle-read-only 1))
-      (view-buffer-other-window outbuf)
+        (read-only-mode +1))
+      (pop-to-buffer outbuf)
       (when one-win
         (shrink-window-if-larger-than-buffer)))))
 
 (defun sql-execute-feature (sqlbuf outbuf feature enhanced arg)
   "List objects or details in a separate display buffer."
-  (let (command)
-    (with-current-buffer sqlbuf
-      (setq command (sql-get-product-feature sql-product feature)))
+  (let (command
+        (product (buffer-local-value 'sql-product (get-buffer sqlbuf))))
+    (setq command (sql-get-product-feature product feature))
     (unless command
-      (error "%s does not support %s" sql-product feature))
+      (error "%s does not support %s" product feature))
     (when (consp command)
       (setq command (if enhanced
                         (cdr command)
@@ -3570,7 +3632,7 @@ The list is maintained in SQL interactive buffers.")
                                  (apply f (current-buffer) (cons schema nil)))
                          cl)
                 (unless (member e cl) (setq cl (cons e cl))))
-              (sort cl (function string<)))))))
+              (sort cl #'string<))))))
 
 (defun sql-build-completions (schema)
   "Generate a list of names in the database for use as completions."
@@ -3579,43 +3641,35 @@ The list is maintained in SQL interactive buffers.")
 
 (defvar sql-completion-sqlbuf nil)
 
-(defun sql-try-completion (string collection &optional predicate)
+(defun sql--completion-table (string pred action)
   (when sql-completion-sqlbuf
-      (with-current-buffer sql-completion-sqlbuf
-        (let ((schema (and (string-match "\\`\\(\\sw\\(:?\\sw\\|\\s_\\)*\\)[.]" string)
-                           (downcase (match-string 1 string)))))
-
-          ;; If we haven't loaded any object name yet, load local schema
-          (unless sql-completion-object
-            (sql-build-completions nil))
-
-          ;; If they want another schema, load it if we haven't yet
-          (when schema
-            (let ((schema-dot (concat schema "."))
-                  (schema-len (1+ (length schema)))
-                  (names sql-completion-object)
-                  has-schema)
-
-              (while (and (not has-schema) names)
-                (setq has-schema (and
-                                  (>= (length (car names)) schema-len)
-                                  (string= schema-dot
-                                           (downcase (substring (car names)
-                                                                0 schema-len))))
-                      names (cdr names)))
-              (unless has-schema
-                (sql-build-completions schema)))))
-
-        ;; Try to find the completion
-        (cond
-         ((not predicate)
-          (try-completion string sql-completion-object))
-         ((eq predicate t)
-          (all-completions string sql-completion-object))
-         ((eq predicate 'lambda)
-          (test-completion string sql-completion-object))
-         ((eq (car predicate) 'boundaries)
-          (completion-boundaries string sql-completion-object nil (cdr predicate)))))))
+    (with-current-buffer sql-completion-sqlbuf
+      (let ((schema (and (string-match "\\`\\(\\sw\\(:?\\sw\\|\\s_\\)*\\)[.]" string)
+                         (downcase (match-string 1 string)))))
+
+        ;; If we haven't loaded any object name yet, load local schema
+        (unless sql-completion-object
+          (sql-build-completions nil))
+
+        ;; If they want another schema, load it if we haven't yet
+        (when schema
+          (let ((schema-dot (concat schema "."))
+                (schema-len (1+ (length schema)))
+                (names sql-completion-object)
+                has-schema)
+
+            (while (and (not has-schema) names)
+              (setq has-schema (and
+                                (>= (length (car names)) schema-len)
+                                (string= schema-dot
+                                         (downcase (substring (car names)
+                                                              0 schema-len))))
+                    names (cdr names)))
+            (unless has-schema
+              (sql-build-completions schema)))))
+
+      ;; Try to find the completion
+      (complete-with-action action sql-completion-object string pred))))
 
 (defun sql-read-table-name (prompt)
   "Read the name of a database table."
@@ -3631,16 +3685,18 @@ The list is maintained in SQL interactive buffers.")
          (completion-ignore-case t))
 
     (if (sql-get-product-feature product :completion-object)
-        (completing-read prompt (function sql-try-completion)
+        (completing-read prompt #'sql--completion-table
                          nil nil tname)
       (read-from-minibuffer prompt tname))))
 
 (defun sql-list-all (&optional enhanced)
-  "List all database objects."
+  "List all database objects.
+With optional prefix argument ENHANCED, displays additional
+details or extends the listing to include other schemas objects."
   (interactive "P")
   (let ((sqlbuf (sql-find-sqli-buffer)))
     (unless sqlbuf
-      (error "No SQL interactive buffer found"))
+      (user-error "No SQL interactive buffer found"))
     (sql-execute-feature sqlbuf "*List All*" :list-all enhanced nil)
     (with-current-buffer sqlbuf
       ;; Contains the name of database objects
@@ -3648,15 +3704,17 @@ The list is maintained in SQL interactive buffers.")
       (set (make-local-variable 'sql-buffer) sqlbuf))))
 
 (defun sql-list-table (name &optional enhanced)
-  "List the details of a database table. "
+  "List the details of a database table named NAME.
+Displays the columns in the relation.  With optional prefix argument
+ENHANCED, displays additional details about each column."
   (interactive
    (list (sql-read-table-name "Table name: ")
          current-prefix-arg))
   (let ((sqlbuf (sql-find-sqli-buffer)))
     (unless sqlbuf
-      (error "No SQL interactive buffer found"))
+      (user-error "No SQL interactive buffer found"))
     (unless name
-      (error "No table name specified"))
+      (user-error "No table name specified"))
     (sql-execute-feature sqlbuf (format "*List %s*" name)
                          :list-table enhanced name)))
 \f
@@ -3685,8 +3743,8 @@ For information on how to create multiple SQLi buffers, see
 `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:
+one.  If you specify backslash as escape character in SQL, you
+must tell Emacs.  Here's how to do that in your init file:
 
 \(add-hook 'sql-mode-hook
           (lambda ()
@@ -3695,6 +3753,7 @@ you must tell Emacs.  Here's how to do that in your `~/.emacs' file:
   (if sql-mode-menu
       (easy-menu-add sql-mode-menu)); XEmacs
 
+  ;; (smie-setup sql-smie-grammar #'sql-smie-rules)
   (set (make-local-variable 'comment-start) "--")
   ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
   (make-local-variable 'sql-buffer)
@@ -3708,10 +3767,12 @@ you must tell Emacs.  Here's how to do that in your `~/.emacs' file:
   (set (make-local-variable 'paragraph-separate) "[\f]*$")
   (set (make-local-variable 'paragraph-start) "[\n\f]")
   ;; Abbrevs
-  (setq abbrev-all-caps 1)
+  (setq-local abbrev-all-caps 1)
   ;; Contains the name of database objects
   (set (make-local-variable 'sql-contains-names) t)
+  ;; Set syntax and font-face highlighting
   ;; Catch changes to sql-product and highlight accordingly
+  (sql-set-product (or sql-product 'ansi)) ; Fixes bug#13591
   (add-hook 'hack-local-variables-hook 'sql-highlight-product t t))
 
 \f
@@ -3776,7 +3837,7 @@ cause the window to scroll to the end of the buffer.
 If you want to make SQL buffers limited in length, add the function
 `comint-truncate-buffer' to `comint-output-filter-functions'.
 
-Here is an example for your .emacs file.  It keeps the SQLi buffer a
+Here is an example for your init file.  It keeps the SQLi buffer a
 certain length.
 
 \(add-hook 'sql-interactive-mode-hook
@@ -3807,7 +3868,7 @@ you entered, right above the output it created.
 
   ;; 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
+  ;; will have just one quote.  Therefore syntactic highlighting is
   ;; disabled for interactive buffers.  No imenu support.
   (sql-product-font-lock t nil)
 
@@ -3825,6 +3886,7 @@ you entered, right above the output it created.
   (set (make-local-variable 'sql-server)     sql-server)
   (set (make-local-variable 'sql-port)       sql-port)
   (set (make-local-variable 'sql-connection) sql-connection)
+  (setq-default sql-connection nil)
   ;; Contains the name of database objects
   (set (make-local-variable 'sql-contains-names) t)
   ;; Keep track of existing object names
@@ -3842,7 +3904,6 @@ you entered, right above the output it created.
        (sql-get-product-feature sql-product :prompt-cont-regexp))
   (make-local-variable 'sql-output-newline-count)
   (make-local-variable 'sql-preoutput-hold)
-  (make-local-variable 'sql-output-by-send)
   (add-hook 'comint-preoutput-filter-functions
             'sql-interactive-remove-continuation-prompt nil t)
   (make-local-variable 'sql-input-ring-separator)
@@ -3889,7 +3950,7 @@ Sentinels will always get the two parameters PROCESS and EVENT."
   "Read a connection name."
   (let ((completion-ignore-case t))
     (completing-read prompt
-                     (mapcar (lambda (c) (car c))
+                     (mapcar #'(lambda (c) (car c))
                              sql-connection-alist)
                      nil t initial 'sql-connection-history default)))
 
@@ -3908,55 +3969,62 @@ is specified in the connection settings."
    (if sql-connection-alist
        (list (sql-read-connection "Connection: " nil '(nil))
              current-prefix-arg)
-     nil))
+     (user-error "No SQL Connections defined")))
 
   ;; Are there connections defined
   (if sql-connection-alist
       ;; Was one selected
       (when connection
         ;; Get connection settings
-        (let ((connect-set  (assoc connection sql-connection-alist)))
+        (let ((connect-set (assoc-string connection sql-connection-alist t)))
           ;; Settings are defined
           (if connect-set
               ;; Set the desired parameters
-              (eval `(let*
-                         (,@(cdr connect-set)
-                          ;; :sqli-login params variable
-                          (param-var    (sql-get-product-feature sql-product
-                                                                 :sqli-login nil t))
-                          ;; :sqli-login params value
-                          (login-params (sql-get-product-feature sql-product
-                                                                 :sqli-login))
-                          ;; which params are in the connection
-                          (set-params   (mapcar
-                                         (lambda (v)
-                                           (cond
-                                            ((eq (car v) 'sql-user)     'user)
-                                            ((eq (car v) 'sql-password) 'password)
-                                            ((eq (car v) 'sql-server)   'server)
-                                            ((eq (car v) 'sql-database) 'database)
-                                            ((eq (car v) 'sql-port)     'port)
-                                            (t                          (car v))))
-                                         (cdr connect-set)))
-                          ;; the remaining params (w/o the connection params)
-                          (rem-params   (sql-for-each-login
-                                         login-params
-                                         (lambda (token plist)
-                                           (unless (member token set-params)
-                                                    (if plist
-                                                        (cons token plist)
-                                                      token))))))
-
-                       ;; Set the remaining parameters and start the
-                       ;; interactive session
-                       (eval `(let ((sql-connection ,connection)
-                                     (,param-var ',rem-params))
-                                (sql-product-interactive sql-product
-                                                         new-name)))))
-
-            (message "SQL Connection <%s> does not exist" connection)
+              (let (param-var login-params set-params rem-params)
+
+                ;; :sqli-login params variable
+                (setq param-var
+                      (sql-get-product-feature sql-product :sqli-login nil t))
+
+                ;; :sqli-login params value
+                (setq login-params
+                      (sql-get-product-feature sql-product :sqli-login))
+
+                ;; Params in the connection
+                (setq set-params
+                      (mapcar
+                       #'(lambda (v)
+                           (pcase (car v)
+                             (`sql-user     'user)
+                             (`sql-password 'password)
+                             (`sql-server   'server)
+                             (`sql-database 'database)
+                             (`sql-port     'port)
+                             (s             s)))
+                       (cdr connect-set)))
+
+                ;; the remaining params (w/o the connection params)
+                (setq rem-params
+                      (sql-for-each-login login-params
+                       #'(lambda (token plist)
+                           (unless (member token set-params)
+                             (if plist (cons token plist) token)))))
+
+                ;; Set the parameters and start the interactive session
+                (mapc
+                 #'(lambda (vv)
+                     (set-default (car vv) (eval (cadr vv))))
+                 (cdr connect-set))
+                (setq-default sql-connection connection)
+
+                ;; Start the SQLi session with revised list of login parameters
+                (eval `(let ((,param-var ',rem-params))
+                         (sql-product-interactive ',sql-product ',new-name))))
+
+            (user-error "SQL Connection <%s> does not exist" connection)
             nil)))
-    (message "No SQL Connections defined")
+
+    (user-error "No SQL Connections defined")
     nil))
 
 (defun sql-save-connection (name)
@@ -3968,7 +4036,7 @@ optionally is saved to the user's init file."
   (interactive "sNew connection name: ")
 
   (unless (derived-mode-p 'sql-interactive-mode)
-    (error "Not in a SQL interactive mode!"))
+    (user-error "Not in a SQL interactive mode!"))
 
   ;; Capture the buffer local settings
   (let* ((buf        (current-buffer))
@@ -3993,18 +4061,18 @@ optionally is saved to the user's init file."
 
         ;; Add the new connection if it doesn't exist
         (if (assoc name alist)
-            (message "Connection <%s> already exists" name)
+            (user-error "Connection <%s> already exists" name)
           (setq connect
-                (append (list name)
-                        (sql-for-each-login
-                         `(product ,@login)
-                         (lambda (token _plist)
-                           (cond
-                            ((eq token 'product)  `(sql-product  ',product))
-                            ((eq token 'user)     `(sql-user     ,user))
-                            ((eq token 'database) `(sql-database ,database))
-                            ((eq token 'server)   `(sql-server   ,server))
-                            ((eq token 'port)     `(sql-port     ,port)))))))
+                (cons name
+                      (sql-for-each-login
+                       `(product ,@login)
+                       #'(lambda (token _plist)
+                           (pcase token
+                             (`product  `(sql-product  ',product))
+                             (`user     `(sql-user     ,user))
+                             (`database `(sql-database ,database))
+                             (`server   `(sql-server   ,server))
+                             (`port     `(sql-port     ,port)))))))
 
           (setq alist (append alist (list connect)))
 
@@ -4014,24 +4082,23 @@ optionally is saved to the user's init file."
             (customize-set-variable 'sql-connection-alist alist)))))))
 
 (defun sql-connection-menu-filter (tail)
-  "Generates menu entries for using each connection."
+  "Generate menu entries for using each connection."
   (append
    (mapcar
-    (lambda (conn)
-      (vector
-       (format "Connection <%s>\t%s" (car conn)
-               (let ((sql-user "") (sql-database "")
-                     (sql-server "") (sql-port 0))
-                 (eval `(let ,(cdr conn) (sql-make-alternate-buffer-name)))))
-       (list 'sql-connect (car conn))
-       t))
+    #'(lambda (conn)
+        (vector
+         (format "Connection <%s>\t%s" (car conn)
+                 (let ((sql-user "") (sql-database "")
+                       (sql-server "") (sql-port 0))
+                   (eval `(let ,(cdr conn) (sql-make-alternate-buffer-name)))))
+         (list 'sql-connect (car conn))
+         t))
     sql-connection-alist)
    tail))
 
 \f
 
 ;;; Entry functions for different SQL interpreters.
-
 ;;;###autoload
 (defun sql-product-interactive (&optional product new-name)
   "Run PRODUCT interpreter as an inferior process.
@@ -4081,13 +4148,19 @@ the call to \\[sql-product-interactive] with
                   new-sqli-buffer)
 
               ;; Get credentials.
-              (apply 'sql-get-login (sql-get-product-feature product :sqli-login))
+              (apply #'sql-get-login
+                     (sql-get-product-feature product :sqli-login))
 
               ;; Connect to database.
               (message "Login...")
-              (funcall (sql-get-product-feature product :sqli-comint-func)
-                       product
-                       (sql-get-product-feature product :sqli-options))
+              (let ((sql-user       (default-value 'sql-user))
+                    (sql-password   (default-value 'sql-password))
+                    (sql-server     (default-value 'sql-server))
+                    (sql-database   (default-value 'sql-database))
+                    (sql-port       (default-value 'sql-port)))
+                (funcall (sql-get-product-feature product :sqli-comint-func)
+                         product
+                         (sql-get-product-feature product :sqli-options)))
 
               ;; Set SQLi mode.
               (let ((sql-interactive-product product))
@@ -4097,7 +4170,8 @@ the call to \\[sql-product-interactive] with
               (setq new-sqli-buffer (current-buffer))
               (when new-name
                 (sql-rename-buffer new-name))
-              (setq sql-buffer (buffer-name new-sqli-buffer))
+              (set (make-local-variable 'sql-buffer)
+                   (buffer-name new-sqli-buffer))
 
               ;; Set `sql-buffer' in the start buffer
               (with-current-buffer start-buffer
@@ -4105,10 +4179,19 @@ the call to \\[sql-product-interactive] with
                   (setq sql-buffer (buffer-name new-sqli-buffer))
                   (run-hooks 'sql-set-sqli-hook)))
 
+              ;; Make sure the connection is complete
+              ;; (Sometimes start up can be slow)
+              ;;  and call the login hook
+              (let ((proc (get-buffer-process new-sqli-buffer)))
+                (while (and (memq (process-status proc) '(open run))
+                            (accept-process-output proc 2.5)
+                            (progn (goto-char (point-max))
+                                   (not (looking-back sql-prompt-regexp))))))
+              (run-hooks 'sql-login-hook)
               ;; All done.
               (message "Login...done")
               (pop-to-buffer new-sqli-buffer)))))
-    (message "No default SQL product defined.  Set `sql-product'.")))
+    (user-error "No default SQL product defined.  Set `sql-product'.")))
 
 (defun sql-comint (product params)
   "Set up a comint buffer to run the SQL processor.
@@ -4117,10 +4200,12 @@ PRODUCT is the SQL product.  PARAMS is a list of strings which are
 passed as command line arguments."
   (let ((program (sql-get-product-feature product :sqli-program))
         (buf-name "SQL"))
-    ;; make sure we can find the program
-    (unless (executable-find program)
+    ;; Make sure we can find the program.  `executable-find' does not
+    ;; work for remote hosts; we suppress the check there.
+    (unless (or (file-remote-p default-directory)
+               (executable-find program))
       (error "Unable to locate SQL program \'%s\'" program))
-    ;; Make sure buffer name is unique
+    ;; Make sure buffer name is unique.
     (when (sql-buffer-live-p (format "*%s*" buf-name))
       (setq buf-name (format "SQL-%s" product))
       (when (sql-buffer-live-p (format "*%s*" buf-name))
@@ -4130,7 +4215,7 @@ passed as command line arguments."
                           (setq buf-name (format "SQL-%s%d" product i))))
             (setq i (1+ i))))))
     (set-buffer
-     (apply 'make-comint buf-name program nil params))))
+     (apply #'make-comint buf-name program nil params))))
 
 ;;;###autoload
 (defun sql-oracle (&optional buffer)
@@ -4170,7 +4255,7 @@ The default comes from `process-coding-system-alist' and
   ;; is meaningless; database without user/password is meaningless,
   ;; because "@param" will ask sqlplus to interpret the script
   ;; "param".
-  (let ((parameter nil))
+  (let (parameter nlslang coding)
     (if (not (string= "" sql-user))
        (if (not (string= "" sql-password))
            (setq parameter (concat sql-user "/" sql-password))
@@ -4180,10 +4265,32 @@ The default comes from `process-coding-system-alist' and
     (if parameter
        (setq parameter (nconc (list parameter) options))
       (setq parameter options))
-    (sql-comint product parameter)))
+    (sql-comint product parameter)
+    ;; Set process coding system to agree with the interpreter
+    (setq nlslang (or (getenv "NLS_LANG") "")
+          coding  (dolist (cs
+                           ;; Are we missing any common NLS character sets
+                           '(("US8PC437"  . cp437)
+                             ("EL8PC737"  . cp737)
+                             ("WE8PC850"  . cp850)
+                             ("EE8PC852"  . cp852)
+                             ("TR8PC857"  . cp857)
+                             ("WE8PC858"  . cp858)
+                             ("IS8PC861"  . cp861)
+                             ("IW8PC1507" . cp862)
+                             ("N8PC865"   . cp865)
+                             ("RU8PC866"  . cp866)
+                             ("US7ASCII"  . us-ascii)
+                             ("UTF8"      . utf-8)
+                             ("AL32UTF8"  . utf-8)
+                             ("AL16UTF16" . utf-16))
+                           (or coding 'utf-8))
+                    (when (string-match (format "\\.%s\\'" (car cs)) nlslang)
+                      (setq coding (cdr cs)))))
+    (set-buffer-process-coding-system coding coding)))
 
 (defun sql-oracle-save-settings (sqlbuf)
-  "Saves most SQL*Plus settings so they may be reset by \\[sql-redirect]."
+  "Save most SQL*Plus settings so they may be reset by \\[sql-redirect]."
   ;; Note: does not capture the following settings:
   ;;
   ;; APPINFO
@@ -4200,7 +4307,7 @@ The default comes from `process-coding-system-alist' and
   ;;
 
   (append
-  ;; (apply 'concat (append
+  ;; (apply #'concat (append
   ;;  '("SET")
 
    ;; option value...
@@ -4224,7 +4331,7 @@ The default comes from `process-coding-system-alist' and
     "^\\(.+\\) (hex ..)$"
     "SET \\1")
 
-   ;; FEDDBACK ON for 99 or more rows
+   ;; FEEDBACK ON for 99 or more rows
    ;; feedback OFF
    (sql-redirect-value
     sqlbuf
@@ -4248,14 +4355,14 @@ The default comes from `process-coding-system-alist' and
 
   ;; Remove any settings that haven't changed
   (mapc
-   (lambda (one-cur-setting)
-     (setq saved-settings (delete one-cur-setting saved-settings)))
+   #'(lambda (one-cur-setting)
+       (setq saved-settings (delete one-cur-setting saved-settings)))
    (sql-oracle-save-settings sqlbuf))
 
   ;; Restore the changed settings
   (sql-redirect sqlbuf saved-settings))
 
-(defun sql-oracle-list-all (sqlbuf outbuf enhanced table-name)
+(defun sql-oracle-list-all (sqlbuf outbuf enhanced _table-name)
   ;; Query from USER_OBJECTS or ALL_OBJECTS
   (let ((settings (sql-oracle-save-settings sqlbuf))
         (simple-sql
@@ -4294,7 +4401,7 @@ The default comes from `process-coding-system-alist' and
 
     (sql-oracle-restore-settings sqlbuf settings)))
 
-(defun sql-oracle-list-table (sqlbuf outbuf enhanced table-name)
+(defun sql-oracle-list-table (sqlbuf outbuf _enhanced table-name)
   "Implements :list-table under Oracle."
   (let ((settings (sql-oracle-save-settings sqlbuf)))
 
@@ -4371,15 +4478,17 @@ The default comes from `process-coding-system-alist' and
   "Create comint buffer and connect to Sybase."
   ;; Put all parameters to the program (if defined) in a list and call
   ;; make-comint.
-  (let ((params 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)))
+  (let ((params
+         (append
+          (if (not (string= "" sql-user))
+              (list "-U" sql-user))
+          (if (not (string= "" sql-password))
+              (list "-P" sql-password))
+          (if (not (string= "" sql-database))
+              (list "-D" sql-database))
+          (if (not (string= "" sql-server))
+              (list "-S" sql-server))
+          options)))
     (sql-comint product params)))
 
 \f
@@ -4464,14 +4573,13 @@ The default comes from `process-coding-system-alist' and
   "Create comint buffer and connect to SQLite."
   ;; 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 (expand-file-name sql-database))
-                             params)))
-    (setq params (append options params))
+  (let ((params
+         (append options
+                 (if (not (string= "" sql-database))
+                     `(,(expand-file-name sql-database))))))
     (sql-comint product params)))
 
-(defun sql-sqlite-completion-object (sqlbuf schema)
+(defun sql-sqlite-completion-object (sqlbuf _schema)
   (sql-redirect-value sqlbuf ".tables" "\\sw\\(?:\\sw\\|\\s_\\)*" 0))
 
 \f
@@ -4514,18 +4622,19 @@ The default comes from `process-coding-system-alist' and
   "Create comint buffer and connect to MySQL."
   ;; 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 (= 0 sql-port))
-       (setq params (append (list (concat "--port=" (number-to-string sql-port))) 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)))
-    (setq params (append options params))
+  (let ((params
+         (append
+          options
+          (if (not (string= "" sql-user))
+              (list (concat "--user=" sql-user)))
+          (if (not (string= "" sql-password))
+              (list (concat "--password=" sql-password)))
+          (if (not (= 0 sql-port))
+              (list (concat "--port=" (number-to-string sql-port))))
+          (if (not (string= "" sql-server))
+              (list (concat "--host=" sql-server)))
+          (if (not (string= "" sql-database))
+              (list sql-database)))))
     (sql-comint product params)))
 
 \f
@@ -4565,13 +4674,15 @@ The default comes from `process-coding-system-alist' and
   "Create comint buffer and connect to Solid."
   ;; Put all parameters to the program (if defined) in a list and call
   ;; make-comint.
-  (let ((params options))
-    ;; 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)))
+  (let ((params
+         (append
+          (if (not (string= "" sql-server))
+              (list sql-server))
+          ;; It only makes sense if both username and password are there.
+          (if (not (or (string= "" sql-user)
+                       (string= "" sql-password)))
+              (list sql-user sql-password))
+          options)))
     (sql-comint product params)))
 
 \f
@@ -4653,22 +4764,25 @@ The default comes from `process-coding-system-alist' and
   "Create comint buffer and connect to Microsoft SQL Server."
   ;; Put all parameters to the program (if defined) in a list and call
   ;; make-comint.
-  (let ((params 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-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")))))
+  (let ((params
+         (append
+          (if (not (string= "" sql-user))
+              (list "-U" sql-user))
+          (if (not (string= "" sql-database))
+              (list "-d" sql-database))
+          (if (not (string= "" sql-server))
+              (list "-S" sql-server))
+          options)))
+    (setq params
+          (if (not (string= "" sql-password))
+              `("-P" ,sql-password ,@params)
+            (if (string= "" sql-user)
+                ;; If neither user nor password is provided, use system
+                ;; credentials.
+                `("-E" ,@params)
+              ;; If -P is passed to ISQL as the last argument without a
+              ;; password, it's considered null.
+              `(,@params "-P"))))
     (sql-comint product params)))
 
 \f
@@ -4712,48 +4826,58 @@ Try to set `comint-output-filter-functions' like this:
 
 (defun sql-comint-postgres (product options)
   "Create comint buffer and connect to Postgres."
-  ;; username and password are ignored.  Mark Stosberg suggest to add
-  ;; the database at the end.  Jason Beegan suggest using --pset and
+  ;; username and password are ignored.  Mark Stosberg suggests to add
+  ;; the database at the end.  Jason Beegan suggests 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 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)))
-    (if (not (= 0 sql-port))
-       (setq params (append (list "-p" (number-to-string sql-port)) params)))
+  (let ((params
+         (append
+          (if (not (= 0 sql-port))
+              (list "-p" (number-to-string sql-port)))
+          (if (not (string= "" sql-user))
+              (list "-U" sql-user))
+          (if (not (string= "" sql-server))
+              (list "-h" sql-server))
+          options
+          (if (not (string= "" sql-database))
+              (list sql-database)))))
     (sql-comint product params)))
 
 (defun sql-postgres-completion-object (sqlbuf schema)
-  (let (cl re fs a r)
-    (sql-redirect sqlbuf "\\t on")
-    (setq a (car (sql-redirect-value sqlbuf "\\a" "Output format is \\(.*\\)[.]$" 1)))
-    (when (string= a "aligned")
+  (sql-redirect sqlbuf "\\t on")
+  (let ((aligned
+         (string= "aligned"
+                  (car (sql-redirect-value
+                        sqlbuf "\\a"
+                        "Output format is \\(.*\\)[.]$" 1)))))
+    (when aligned
       (sql-redirect sqlbuf "\\a"))
-    (setq fs (or (car (sql-redirect-value sqlbuf "\\f" "Field separator is \"\\(.\\)[.]$" 1)) "|"))
-
-    (setq re (concat "^\\([^" fs "]*\\)" fs "\\([^" fs "]*\\)" fs "[^" fs "]*" fs  "[^" fs "]*$"))
-    (setq cl (if (not schema)
-                 (sql-redirect-value sqlbuf "\\d" re '(1 2))
-               (append (sql-redirect-value sqlbuf (format "\\dt %s.*" schema) re '(1 2))
-                       (sql-redirect-value sqlbuf (format "\\dv %s.*" schema) re '(1 2))
-                       (sql-redirect-value sqlbuf (format "\\ds %s.*" schema) re '(1 2)))))
-
-    ;; Restore tuples and alignment to what they were
-    (sql-redirect sqlbuf "\\t off")
-    (when (not (string= a "aligned"))
-      (sql-redirect sqlbuf "\\a"))
-
-    ;; Return the list of table names (public schema name can be omitted)
-    (mapcar (lambda (tbl)
-              (if (string= (car tbl) "public")
-                  (cadr tbl)
-                (format "%s.%s" (car tbl) (cadr tbl))))
-            cl)))
+    (let* ((fs (or (car (sql-redirect-value
+                         sqlbuf "\\f" "Field separator is \"\\(.\\)[.]$" 1))
+                   "|"))
+           (re (concat "^\\([^" fs "]*\\)" fs "\\([^" fs "]*\\)"
+                       fs "[^" fs "]*" fs  "[^" fs "]*$"))
+           (cl (if (not schema)
+                   (sql-redirect-value sqlbuf "\\d" re '(1 2))
+                 (append (sql-redirect-value
+                          sqlbuf (format "\\dt %s.*" schema) re '(1 2))
+                         (sql-redirect-value
+                          sqlbuf (format "\\dv %s.*" schema) re '(1 2))
+                         (sql-redirect-value
+                          sqlbuf (format "\\ds %s.*" schema) re '(1 2))))))
+
+      ;; Restore tuples and alignment to what they were.
+      (sql-redirect sqlbuf "\\t off")
+      (when (not aligned)
+        (sql-redirect sqlbuf "\\a"))
+
+      ;; Return the list of table names (public schema name can be omitted)
+      (mapcar #'(lambda (tbl)
+                  (if (string= (car tbl) "public")
+                      (cadr tbl)
+                    (format "%s.%s" (car tbl) (cadr tbl))))
+              cl))))
 
 \f
 
@@ -4792,13 +4916,15 @@ The default comes from `process-coding-system-alist' and
   "Create comint buffer and connect to Interbase."
   ;; Put all parameters to the program (if defined) in a list and call
   ;; make-comint.
-  (let ((params 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!
+  (let ((params
+         (append
+          (if (not (string= "" sql-database))
+              (list sql-database))      ; Add to the front!
+          (if (not (string= "" sql-password))
+              (list "-p" sql-password))
+          (if (not (string= "" sql-user))
+              (list "-u" sql-user))
+          options)))
     (sql-comint product params)))
 
 \f
@@ -4880,19 +5006,18 @@ buffer.
   "Create comint buffer and connect to Linter."
   ;; Put all parameters to the program (if defined) in a list and call
   ;; make-comint.
-  (let ((params 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))
-    (sql-comint product params)
-    (setenv "LINTER_MBX" old-mbx)))
+  (let* ((login
+          (if (not (string= "" sql-user))
+              (concat sql-user "/" sql-password)))
+         (params
+          (append
+           (if (not (string= "" sql-server))
+               (list "-n" sql-server))
+           (list "-u" login)
+           options)))
+    (cl-letf (((getenv "LINTER_MBX")
+               (unless (string= "" sql-database) sql-database)))
+      (sql-comint product params))))
 
 \f