Initial Commit
[packages] / xemacs-packages / prog-modes / sql.el
1 ;;; sql.el --- specialized comint.el for SQL interpreters
2
3 ;; Copyright (C) 1998,99,2000,01,02,03,04  Free Software Foundation, Inc.
4
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Michael Mauger <mmaug@yahoo.com>
7 ;; Version: 2.0.1
8 ;; Keywords: comm languages processes
9 ;; URL: http://savannah.gnu.org/cgi-bin/viewcvs/emacs/emacs/lisp/progmodes/sql.el
10 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlMode
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; Please send bug reports and bug fixes to the mailing list at
32 ;; sql.el@gnu.org.  If you want to subscribe to the mailing list, send
33 ;; mail to sql.el-request@gnu.org with `subscribe sql.el FIRSTNAME
34 ;; LASTNAME' in the mail body.
35
36 ;; This file provides a sql-mode and a sql-interactive-mode.  My goals
37 ;; were two simple modes providing syntactic hilighting.  The
38 ;; interactive mode had to provide a command-line history; the other
39 ;; mode had to provide "send region/buffer to SQL interpreter"
40 ;; functions.  "simple" in this context means easy to use, easy to
41 ;; maintain and little or no bells and whistles.
42
43 ;; If anybody feels like extending this sql mode, take a look at the
44 ;; above mentioned modes and write a sqlx-mode on top of this one.  If
45 ;; this proves to be difficult, please suggest changes that will
46 ;; facilitate your plans.
47
48 ;; sql-interactive-mode is used to interact with a SQL interpreter
49 ;; process in a SQLi buffer (usually called `*SQL*').  The SQLi buffer
50 ;; is created by calling a SQL interpreter-specific entry function.  Do
51 ;; *not* call sql-interactive-mode by itself.
52
53 ;; The list of currently supported interpreters and the corresponding
54 ;; entry function used to create the SQLi buffers is shown with
55 ;; `sql-help' (M-x sql-help).
56
57 ;; Since sql-interactive-mode is built on top of the general
58 ;; command-interpreter-in-a-buffer mode (comint mode), it shares a
59 ;; common base functionality, and a common set of bindings, with all
60 ;; modes derived from comint mode.  This makes these modes easier to
61 ;; use.
62
63 ;; sql-mode can be used to keep editing SQL statements.  The SQL
64 ;; statements can be sent to the SQL process in the SQLi buffer.
65
66 ;; For documentation on the functionality provided by comint mode, and
67 ;; the hooks available for customizing it, see the file `comint.el'.
68
69 ;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
70 ;; `imenu-add-menubar-index'.
71
72 ;;; Requirements for Emacs 19.34:
73
74 ;; If you are using Emacs 19.34, you will have to get and install
75 ;; the file regexp-opt.el
76 ;; <URL:ftp://ftp.ifi.uio.no/pub/emacs/emacs-20.3/lisp/emacs-lisp/regexp-opt.el>
77 ;; and the custom package
78 ;; <URL:http://www.dina.kvl.dk/~abraham/custom/>.
79
80 ;;; Bugs:
81
82 ;; sql-ms now uses osql instead of isql.  Osql flushes its error
83 ;; stream more frequently than isql so that error messages are
84 ;; available.  There is no prompt and some output still is buffered.
85 ;; This improves the interaction under Emacs but it still is somewhat
86 ;; awkward.
87
88 ;; Quoted identifiers are not supported for hilighting.  Most
89 ;; databases support the use of double quoted strings in place of
90 ;; identifiers; ms (Microsoft SQLServer) also supports identifiers
91 ;; enclosed within brackets [].
92
93 ;; ChangeLog available on request.
94
95 ;;; Product Support:
96
97 ;; To add support for additional SQL products the following steps
98 ;; must be followed ("xyz" is the name of the product in the examples
99 ;; below):
100
101 ;; 1) Add the product to `sql-product' choice list.
102
103 ;;     (const :tag "XyzDB" xyz)
104
105 ;; 2) Add an entry to the `sql-product-alist' list.
106
107 ;;     (xyz
108 ;;      :font-lock sql-mode-xyz-font-lock-keywords
109 ;;      :sqli-login (user password server database)
110 ;;      :sqli-connect sql-connect-xyz
111 ;;      :sqli-prompt-regexp "^xyzdb> "
112 ;;      :sqli-prompt-length 7
113 ;;      :sqli-input-sender nil
114 ;;      :syntax-alist ((?# . "w")))
115
116 ;; 3) Add customizable values for the product interpreter and options.
117
118 ;;     ;; Customization for XyzDB
119 ;;
120 ;;     (defcustom sql-xyz-program "ixyz"
121 ;;       "*Command to start ixyz by XyzDB."
122 ;;       :type 'file
123 ;;       :group 'SQL)
124 ;;
125 ;;     (defcustom sql-xyz-options '("-X" "-Y" "-Z")
126 ;;       "*List of additional options for `sql-xyz-program'."
127 ;;       :type '(repeat string)
128 ;;       :group 'SQL)
129
130 ;; 4) Add an entry to SQL->Product submenu.
131
132 ;;     ["XyzDB" sql-highlight-xyz-keywords
133 ;;      :style radio
134 ;;      :selected (eq sql-product 'xyz)]
135
136 ;; 5) Add the font-lock specifications.  At a minimum, default to
137 ;;    using ANSI keywords.  See sql-mode-oracle-font-lock-keywords for
138 ;;    a more complex example.
139
140 ;;     (defvar sql-mode-xyz-font-lock-keywords nil
141 ;;       "XyzDB SQL keywords used by font-lock.")
142
143 ;; 6) Add a product highlighting function.
144
145 ;;     (defun sql-highlight-xyz-keywords ()
146 ;;       "Highlight XyzDB keywords."
147 ;;       (interactive)
148 ;;       (sql-set-product 'xyz))
149
150 ;; 7) Add an autoloaded SQLi function.
151
152 ;;     ;;;###autoload
153 ;;     (defun sql-xyz ()
154 ;;       "Run ixyz by XyzDB as an inferior process."
155 ;;       (interactive)
156 ;;       (sql-product-interactive 'xyz))
157
158 ;; 8) Add a connect function which formats the command line arguments
159 ;;    and starts the product interpreter in a comint buffer.  See the
160 ;;    existing connect functions for examples of the types of
161 ;;    processing available.
162
163 ;;     (defun sql-connect-xyz ()
164 ;;       "Create comint buffer and connect to XyzDB using the login
165 ;;     parameters and command options."
166 ;;
167 ;;         ;; Do something with `sql-user', `sql-password',
168 ;;         ;; `sql-database', and `sql-server'.
169 ;;         (let ((params sql-xyz-options))
170 ;;           (if (not (string= "" sql-server))
171 ;;              (setq params (append (list "-S" sql-server) params)))
172 ;;           (if (not (string= "" sql-database))
173 ;;               (setq params (append (list "-D" sql-database) params)))
174 ;;           (if (not (string= "" sql-password))
175 ;;               (setq params (append (list "-P" sql-password) params)))
176 ;;           (if (not (string= "" sql-user))
177 ;;               (setq params (append (list "-U" sql-user) params)))
178 ;;           (set-buffer (apply 'make-comint "SQL" sql-xyz-program
179 ;;                              nil params))))
180
181 ;; 9) Save and compile sql.el.
182
183 ;;; To Do:
184
185 ;; Add better hilight support for other brands; there is a bias towards
186 ;; Oracle because that's what I use at work.  Anybody else just send in
187 ;; your lists of reserved words, keywords and builtin functions!  As
188 ;; long as I don't receive any feedback, everything is hilighted with
189 ;; ANSI keywords only.  I received the list of ANSI keywords from a
190 ;; user; if you know of any changes, let me know.
191
192 ;; Add different hilighting levels.
193
194 ;;; Thanks to all the people who helped me out:
195
196 ;; Alex Schroeder <alex@gnu.org>
197 ;; Kai Blauberg <kai.blauberg@metla.fi>
198 ;; <ibalaban@dalet.com>
199 ;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
200 ;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
201 ;; nino <nino@inform.dk>
202 ;; Berend de Boer <berend@pobox.com>
203 ;; Adam Jenkins <adam@thejenkins.org>
204 ;; Michael Mauger <mmaug@yahoo.com> -- improved product support
205 ;; Drew Adams <drew.adams@oracle.com> -- Emacs 20 support
206 ;; Harald Maier <maierh@myself.com> -- sql-send-string
207 ;; Stefan Monnier <monnier@iro.umontreal.ca> -- font-lock corrections
208
209 \f
210
211 ;;; Code:
212
213 (require 'comint)
214 ;; Need the following to allow GNU Emacs 19 to compile the file.
215 (eval-when-compile
216   (require 'regexp-opt))
217 (require 'custom)
218
219 ;;; Allow customization
220
221 (defgroup SQL nil
222   "Running a SQL interpreter from within Emacs buffers"
223   :version "20.4"
224   :group 'processes)
225
226 ;; These four variables will be used as defaults, if set.
227
228 (defcustom sql-user ""
229   "*Default username."
230   :type 'string
231   :group 'SQL)
232
233 (defcustom sql-password ""
234   "*Default password.
235
236 Storing your password in a textfile such as ~/.emacs could be dangerous.
237 Customizing your password will store it in your ~/.emacs file."
238   :type 'string
239   :group 'SQL)
240
241 (defcustom sql-database ""
242   "*Default database."
243   :type 'string
244   :group 'SQL)
245
246 (defcustom sql-server ""
247   "*Default server or host."
248   :type 'string
249   :group 'SQL)
250
251 ;; SQL Product support
252 (defcustom sql-product 'ansi
253   "*Select the SQL database product used so that buffers can be
254 highlighted properly when you open them."
255   :type '(choice (const :tag "ANSI" ansi)
256                  (const :tag "DB2" db2)
257                  (const :tag "Informix" informix)
258                  (const :tag "Ingres" ingres)
259                  (const :tag "Interbase" interbase)
260                  (const :tag "Linter" linter)
261                  (const :tag "Microsoft" ms)
262                  (const :tag "MySQL" mysql)
263                  (const :tag "Oracle" oracle)
264                  (const :tag "PostGres" postgres)
265                  (const :tag "Solid" solid)
266                  (const :tag "SQLite" sqlite)
267                  (const :tag "Sybase" sybase))
268   :group 'SQL)
269
270 (defvar sql-interactive-product nil
271   "Product under `sql-interactive-mode'.")
272
273 (defvar sql-product-alist
274   '((ansi
275      :font-lock sql-mode-ansi-font-lock-keywords)
276     (db2
277      :font-lock sql-mode-db2-font-lock-keywords
278      :sqli-login nil
279      :sqli-connect sql-connect-db2
280      :sqli-prompt-regexp "^db2 => "
281      :sqli-prompt-length 7)
282     (informix
283      :font-lock sql-mode-informix-font-lock-keywords
284      :sqli-login (database)
285      :sqli-connect sql-connect-informix
286      :sqli-prompt-regexp "^SQL> "
287      :sqli-prompt-length 5)
288     (ingres
289      :font-lock sql-mode-ingres-font-lock-keywords
290      :sqli-login (database)
291      :sqli-connect sql-connect-ingres
292      :sqli-prompt-regexp "^\* "
293      :sqli-prompt-length 2)
294     (interbase
295      :font-lock sql-mode-interbase-font-lock-keywords
296      :sqli-login (user password database)
297      :sqli-connect sql-connect-interbase
298      :sqli-prompt-regexp "^SQL> "
299      :sqli-prompt-length 5)
300     (linter
301      :font-lock sql-mode-linter-font-lock-keywords
302      :sqli-login (user password database server)
303      :sqli-connect sql-connect-linter
304      :sqli-prompt-regexp "^SQL>"
305      :sqli-prompt-length 4)
306     (ms
307      :font-lock sql-mode-ms-font-lock-keywords
308      :sqli-login (user password server database)
309      :sqli-connect sql-connect-ms
310      :sqli-prompt-regexp "^[0-9]*>"
311      :sqli-prompt-length 5
312      :syntax-alist ((?@ . "w")))
313     (mysql
314      :font-lock sql-mode-mysql-font-lock-keywords
315      :sqli-login (user password database server)
316      :sqli-connect sql-connect-mysql
317      :sqli-prompt-regexp "^mysql> "
318      :sqli-prompt-length 6)
319     (oracle
320      :font-lock sql-mode-oracle-font-lock-keywords
321      :sqli-login (user password database)
322      :sqli-connect sql-connect-oracle
323      :sqli-prompt-regexp "^SQL> "
324      :sqli-prompt-length 5
325      :syntax-alist ((?$ . "w") (?# . "w")))
326     (postgres
327      :font-lock sql-mode-postgres-font-lock-keywords
328      :sqli-login (user database server)
329      :sqli-connect sql-connect-postgres
330      :sqli-prompt-regexp "^.*[#>] *"
331      :sqli-prompt-length 5)
332     (solid
333      :font-lock sql-mode-solid-font-lock-keywords
334      :sqli-login (user password server)
335      :sqli-connect sql-connect-solid
336      :sqli-prompt-regexp "^"
337      :sqli-prompt-length 0)
338     (sqlite
339      :font-lock sql-mode-sqlite-font-lock-keywords
340      :sqli-login (user password server database)
341      :sqli-connect sql-connect-sqlite
342      :sqli-prompt-regexp "^sqlite> "
343      :sqli-prompt-length 8)
344     (sybase
345      :font-lock sql-mode-sybase-font-lock-keywords
346      :sqli-login (server user password database)
347      :sqli-connect sql-connect-sybase
348      :sqli-prompt-regexp "^SQL> "
349      :sqli-prompt-length 5
350      :syntax-alist ((?@ . "w")))
351     )
352   "This variable contains a list of product features for each of the
353 SQL products handled by `sql-mode'.  Without an entry in this list a
354 product will not be properly highlighted and will not support
355 `sql-interactive-mode'.
356
357 Each element in the list is in the following format:
358
359  \(PRODUCT FEATURE VALUE ...)
360
361 where PRODUCT is the appropriate value of `sql-product'.  The product
362 name is then followed by FEATURE-VALUE pairs.  If a FEATURE is not
363 specified, its VALUE is treated as nil.  FEATURE must be one of the
364 following:
365
366  :font-lock             name of the variable containing the product
367                         specific font lock highlighting patterns.
368
369  :sqli-login            a list of login parameters (i.e., user,
370                         password, database and server) needed to
371                         connect to the database.
372
373  :sqli-connect          the name of a function which accepts no
374                         parameters that will use the values of
375                         `sql-user', `sql-password',
376                         `sql-database' and `sql-server' to open a
377                         comint buffer and connect to the
378                         database.  Do product specific
379                         configuration of comint in this function.
380
381  :sqli-prompt-regexp    a regular expression string that matches
382                         the prompt issued by the product
383                         interpreter.  (Not needed in 21.3+)
384
385  :sqli-prompt-length    the length of the prompt on the line.(Not
386                         needed in 21.3+)
387
388  :syntax-alist          an alist of syntax table entries to enable
389                         special character treatment by font-lock and
390                         imenu. ")
391
392 ;; misc customization of sql.el behaviour
393
394 (defcustom sql-electric-stuff nil
395   "Treat some input as electric.
396 If set to the symbol `semicolon', then hitting `;' will send current
397 input in the SQLi buffer to the process.
398 If set to the symbol `go', then hitting `go' on a line by itself will
399 send current input in the SQLi buffer to the process.
400 If set to nil, then you must use \\[comint-send-input] in order to send
401 current input in the SQLi buffer to the process."
402   :type '(choice (const :tag "Nothing" nil)
403                  (const :tag "The semikolon `;'" semicolon)
404                  (const :tag "The string `go' by itself" go))
405   :version "20.8"
406   :group 'SQL)
407
408 (defcustom sql-pop-to-buffer-after-send-region nil
409   "*If t, pop to the buffer SQL statements are sent to.
410
411 After a call to `sql-send-region' or `sql-send-buffer',
412 the window is split and the SQLi buffer is shown.  If this
413 variable is not nil, that buffer's window will be selected
414 by calling `pop-to-buffer'.  If this variable is nil, that
415 buffer is shown using `display-buffer'."
416   :type 'boolean
417   :group 'SQL)
418
419 ;; imenu support for sql-mode.
420
421 (defvar sql-imenu-generic-expression
422   ;; Items are in reverse order because they are rendered in reverse.
423   '(("Rules/Defaults" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*\\(rule\\|default\\)\\s-+\\(\\w+\\)" 3)
424     ("Sequences" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*sequence\\s-+\\(\\w+\\)" 2)
425     ("Triggers" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*trigger\\s-+\\(\\w+\\)" 2)
426     ("Functions" "^\\s-*\\(create\\s-+\\(\\w+\\s-+\\)*\\)?function\\s-+\\(\\w+\\)" 3)
427     ("Procedures" "^\\s-*\\(create\\s-+\\(\\w+\\s-+\\)*\\)?proc\\(edure\\)?\\s-+\\(\\w+\\)" 4)
428     ("Packages" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*package\\s-+\\(body\\s-+\\)?\\(\\w+\\)" 3)
429     ("Indexes" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*index\\s-+\\(\\w+\\)" 2)
430     ("Tables/Views" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*\\(table\\|view\\)\\s-+\\(\\w+\\)" 3))
431   "Define interesting points in the SQL buffer for `imenu'.
432
433 This is used to set `imenu-generic-expression' when SQL mode is
434 entered.  Subsequent changes to sql-imenu-generic-expression will not
435 affect existing SQL buffers because imenu-generic-expression is a
436 local variable.")
437
438 ;; history file
439
440 (defcustom sql-input-ring-file-name nil
441   "*If non-nil, name of the file to read/write input history.
442
443 You have to set this variable if you want the history of your commands
444 saved from one Emacs session to the next.  If this variable is set,
445 exiting the SQL interpreter in an SQLi buffer will write the input
446 history to the specified file.  Starting a new process in a SQLi buffer
447 will read the input history from the specified file.
448
449 This is used to initialize `comint-input-ring-file-name'.
450
451 Note that the size of the input history is determined by the variable
452 `comint-input-ring-size'."
453   :type '(choice (const :tag "none" nil)
454                  (file))
455   :group 'SQL)
456
457 (defcustom sql-input-ring-separator "\n--\n"
458   "*Separator between commands in the history file.
459
460 If set to \"\\n\", each line in the history file will be interpreted as
461 one command.  Multi-line commands are split into several commands when
462 the input ring is initialized from a history file.
463
464 This variable used to initialize `comint-input-ring-separator'.
465 `comint-input-ring-separator' is part of Emacs 21; if your Emacs
466 does not have it, setting `sql-input-ring-separator' will have no
467 effect.  In that case multiline commands will be split into several
468 commands when the input history is read, as if you had set
469 `sql-input-ring-separator' to \"\\n\"."
470   :type 'string
471   :group 'SQL)
472
473 ;; The usual hooks
474
475 (defcustom sql-interactive-mode-hook '()
476   "*Hook for customizing `sql-interactive-mode'."
477   :type 'hook
478   :group 'SQL)
479
480 (defcustom sql-mode-hook '()
481   "*Hook for customizing `sql-mode'."
482   :type 'hook
483   :group 'SQL)
484
485 (defcustom sql-set-sqli-hook '()
486   "*Hook for reacting to changes of `sql-buffer'.
487
488 This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
489 is changed."
490   :type 'hook
491   :group 'SQL)
492
493 ;; Customization for Oracle
494
495 (defcustom sql-oracle-program "sqlplus"
496   "*Command to start sqlplus by Oracle.
497
498 Starts `sql-interactive-mode' after doing some setup.
499
500 Under NT, \"sqlplus\" usually starts the sqlplus \"GUI\".  In order to
501 start the sqlplus console, use \"plus33\" or something similar.  You
502 will find the file in your Orant\\bin directory.
503
504 The program can also specify a TCP connection.  See `make-comint'."
505   :type 'file
506   :group 'SQL)
507
508 (defcustom sql-oracle-options nil
509   "*List of additional options for `sql-oracle-program'."
510   :type '(repeat string)
511   :version "20.8"
512   :group 'SQL)
513
514 ;; Customization for SQLite
515
516 (defcustom sql-sqlite-program "sqlite"
517   "*Command to start SQLite.
518
519 Starts `sql-interactive-mode' after doing some setup.
520
521 The program can also specify a TCP connection.  See `make-comint'."
522   :type 'file
523   :group 'SQL)
524
525 (defcustom sql-sqlite-options nil
526   "*List of additional options for `sql-sqlite-program'.
527 The following list of options is reported to make things work
528 on Windows: \"-C\" \"-t\" \"-f\" \"-n\"."
529   :type '(repeat string)
530   :version "20.8"
531   :group 'SQL)
532
533 ;; Customization for MySql
534
535 (defcustom sql-mysql-program "mysql"
536   "*Command to start mysql by TcX.
537
538 Starts `sql-interactive-mode' after doing some setup.
539
540 The program can also specify a TCP connection.  See `make-comint'."
541   :type 'file
542   :group 'SQL)
543
544 (defcustom sql-mysql-options nil
545   "*List of additional options for `sql-mysql-program'.
546 The following list of options is reported to make things work
547 on Windows: \"-C\" \"-t\" \"-f\" \"-n\"."
548   :type '(repeat string)
549   :version "20.8"
550   :group 'SQL)
551
552 ;; Customization for Solid
553
554 (defcustom sql-solid-program "solsql"
555   "*Command to start SOLID SQL Editor.
556
557 Starts `sql-interactive-mode' after doing some setup.
558
559 The program can also specify a TCP connection.  See `make-comint'."
560   :type 'file
561   :group 'SQL)
562
563 ;; Customization for SyBase
564
565 (defcustom sql-sybase-program "isql"
566   "*Command to start isql by SyBase.
567
568 Starts `sql-interactive-mode' after doing some setup.
569
570 The program can also specify a TCP connection.  See `make-comint'."
571   :type 'file
572   :group 'SQL)
573
574 (defcustom sql-sybase-options nil
575   "*List of additional options for `sql-sybase-program'.
576 Some versions of isql might require the -n option in order to work."
577   :type '(repeat string)
578   :version "20.8"
579   :group 'SQL)
580
581 ;; Customization for Informix
582
583 (defcustom sql-informix-program "dbaccess"
584   "*Command to start dbaccess by Informix.
585
586 Starts `sql-interactive-mode' after doing some setup.
587
588 The program can also specify a TCP connection.  See `make-comint'."
589   :type 'file
590   :group 'SQL)
591
592 ;; Customization for Ingres
593
594 (defcustom sql-ingres-program "sql"
595   "*Command to start sql by Ingres.
596
597 Starts `sql-interactive-mode' after doing some setup.
598
599 The program can also specify a TCP connection.  See `make-comint'."
600   :type 'file
601   :group 'SQL)
602
603 ;; Customization for Microsoft
604
605 (defcustom sql-ms-program "osql"
606   "*Command to start osql by Microsoft.
607
608 Starts `sql-interactive-mode' after doing some setup.
609
610 The program can also specify a TCP connection.  See `make-comint'."
611   :type 'file
612   :group 'SQL)
613
614 (defcustom sql-ms-options '("-w" "300" "-n")
615   ;; -w is the linesize
616   "*List of additional options for `sql-ms-program'."
617   :type '(repeat string)
618   :version "22.1"
619   :group 'SQL)
620
621 ;; Customization for Postgres
622
623 (defcustom sql-postgres-program "psql"
624   "Command to start psql by Postgres.
625
626 Starts `sql-interactive-mode' after doing some setup.
627
628 The program can also specify a TCP connection.  See `make-comint'."
629   :type 'file
630   :group 'SQL)
631
632 (defcustom sql-postgres-options '("-P" "pager=off")
633   "*List of additional options for `sql-postgres-program'.
634 The default setting includes the -P option which breaks older versions
635 of the psql client (such as version 6.5.3).  The -P option is equivalent
636 to the --pset option.  If you want the psql to prompt you for a user
637 name, add the string \"-u\" to the list of options.  If you want to
638 provide a user name on the command line (newer versions such as 7.1),
639 add your name with a \"-U\" prefix (such as \"-Umark\") to the list."
640   :type '(repeat string)
641   :version "20.8"
642   :group 'SQL)
643
644 ;; Customization for Interbase
645
646 (defcustom sql-interbase-program "isql"
647   "*Command to start isql by Interbase.
648
649 Starts `sql-interactive-mode' after doing some setup.
650
651 The program can also specify a TCP connection.  See `make-comint'."
652   :type 'file
653   :group 'SQL)
654
655 (defcustom sql-interbase-options nil
656   "*List of additional options for `sql-interbase-program'."
657   :type '(repeat string)
658   :version "20.8"
659   :group 'SQL)
660
661 ;; Customization for DB2
662
663 (defcustom sql-db2-program "db2"
664   "*Command to start db2 by IBM.
665
666 Starts `sql-interactive-mode' after doing some setup.
667
668 The program can also specify a TCP connection.  See `make-comint'."
669   :type 'file
670   :group 'SQL)
671
672 (defcustom sql-db2-options nil
673   "*List of additional options for `sql-db2-program'."
674   :type '(repeat string)
675   :version "20.8"
676   :group 'SQL)
677
678 ;; Customization for Linter
679
680 (defcustom sql-linter-program "inl"
681   "*Command to start inl by RELEX.
682
683 Starts `sql-interactive-mode' after doing some setup."
684   :type 'file
685   :group 'SQL)
686
687 (defcustom sql-linter-options nil
688   "*List of additional options for `sql-linter-program'."
689   :type '(repeat string)
690   :version "21.3"
691   :group 'SQL)
692
693 \f
694
695 ;;; Variables which do not need customization
696
697 (defvar sql-user-history nil
698   "History of usernames used.")
699
700 (defvar sql-database-history nil
701   "History of databases used.")
702
703 (defvar sql-server-history nil
704   "History of servers used.")
705
706 ;; Passwords are not kept in a history.
707
708 (defvar sql-buffer nil
709   "Current SQLi buffer.
710
711 The global value of sql-buffer is the name of the latest SQLi buffer
712 created.  Any SQL buffer created will make a local copy of this value.
713 See `sql-interactive-mode' for more on multiple sessions.  If you want
714 to change the SQLi buffer a SQL mode sends its SQL strings to, change
715 the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
716
717 (defvar sql-prompt-regexp nil
718   "Prompt used to initialize `comint-prompt-regexp'.
719
720 You can change `sql-prompt-regexp' on `sql-interactive-mode-hook'.")
721
722 (defvar sql-prompt-length 0
723   "Prompt used to set `left-margin' in `sql-interactive-mode'.
724
725 You can change `sql-prompt-length' on `sql-interactive-mode-hook'.")
726
727 (defvar sql-alternate-buffer-name nil
728   "Buffer-local string used to possibly rename the SQLi buffer.
729
730 Used by `sql-rename-buffer'.")
731
732 ;; Keymap for sql-interactive-mode.
733
734 (defvar sql-interactive-mode-map
735   (let ((map (make-sparse-keymap)))
736     (if (functionp 'set-keymap-parent)
737         (set-keymap-parent map comint-mode-map); Emacs
738       (set-keymap-parents map (list comint-mode-map))); XEmacs
739     (if (functionp 'set-keymap-name)
740         (set-keymap-name map 'sql-interactive-mode-map)); XEmacs
741     (define-key map (kbd "C-j") 'sql-accumulate-and-indent)
742     (define-key map (kbd "C-c C-w") 'sql-copy-column)
743     (define-key map (kbd "O") 'sql-magic-go)
744     (define-key map (kbd "o") 'sql-magic-go)
745     (define-key map (kbd ";") 'sql-magic-semicolon)
746     map)
747   "Mode map used for `sql-interactive-mode'.
748 Based on `comint-mode-map'.")
749
750 ;; Keymap for sql-mode.
751
752 (defvar sql-mode-map
753   (let ((map (make-sparse-keymap)))
754     (define-key map (kbd "C-c C-c") 'sql-send-paragraph)
755     (define-key map (kbd "C-c C-r") 'sql-send-region)
756     (define-key map (kbd "C-c C-s") 'sql-send-string)
757     (define-key map (kbd "C-c C-b") 'sql-send-buffer)
758     map)
759   "Mode map used for `sql-mode'.")
760
761 ;; easy menu for sql-mode.
762
763 (easy-menu-define
764  sql-mode-menu sql-mode-map
765  "Menu for `sql-mode'."
766  '("SQL"
767    ["Send Paragraph" sql-send-paragraph (and (buffer-live-p sql-buffer)
768                                              (get-buffer-process sql-buffer))]
769    ["Send Region" sql-send-region (and (or (and (boundp 'mark-active); Emacs
770                                                 mark-active)
771                                            (mark t)); XEmacs
772                                        (buffer-live-p sql-buffer)
773                                        (get-buffer-process sql-buffer))]
774    ["Send Buffer" sql-send-buffer (and (buffer-live-p sql-buffer)
775                                        (get-buffer-process sql-buffer))]
776    ["Send String" sql-send-string t]
777    "--"
778    ["Start SQLi session" sql-product-interactive (sql-product-feature :sqli-connect)]
779    ["Show SQLi buffer" sql-show-sqli-buffer t]
780    ["Set SQLi buffer" sql-set-sqli-buffer t]
781    ["Pop to SQLi buffer after send"
782     sql-toggle-pop-to-buffer-after-send-region
783     :style toggle
784     :selected sql-pop-to-buffer-after-send-region]
785    "--"
786    ("Product"
787     ["ANSI" sql-highlight-ansi-keywords
788      :style radio
789      :selected (eq sql-product 'ansi)]
790     ["DB2" sql-highlight-db2-keywords
791      :style radio
792      :selected (eq sql-product 'db2)]
793     ["Informix" sql-highlight-informix-keywords
794      :style radio
795      :selected (eq sql-product 'informix)]
796     ["Ingres" sql-highlight-ingres-keywords
797      :style radio
798      :selected (eq sql-product 'ingres)]
799     ["Interbase" sql-highlight-interbase-keywords
800      :style radio
801      :selected (eq sql-product 'interbase)]
802     ["Linter" sql-highlight-linter-keywords
803      :style radio
804      :selected (eq sql-product 'linter)]
805     ["MS SQLServer" sql-highlight-ms-keywords
806      :style radio
807      :selected (eq sql-product 'ms)]
808     ["MySQL" sql-highlight-mysql-keywords
809      :style radio
810      :selected (eq sql-product 'mysql)]
811     ["Oracle" sql-highlight-oracle-keywords
812      :style radio
813      :selected (eq sql-product 'oracle)]
814     ["Postgres" sql-highlight-postgres-keywords
815      :style radio
816      :selected (eq sql-product 'postgres)]
817     ["Solid" sql-highlight-solid-keywords
818      :style radio
819      :selected (eq sql-product 'solid)]
820     ["SQLite" sql-highlight-sqlite-keywords
821      :style radio
822      :selected (eq sql-product 'sqlite)]
823     ["Sybase" sql-highlight-sybase-keywords
824      :style radio
825      :selected (eq sql-product 'sybase)]
826     )))
827
828 ;; easy menu for sql-interactive-mode.
829
830 (easy-menu-define
831  sql-interactive-mode-menu sql-interactive-mode-map
832  "Menu for `sql-interactive-mode'."
833  '("SQL"
834    ["Rename Buffer" sql-rename-buffer t]))
835
836 ;; Abbreviations -- if you want more of them, define them in your
837 ;; ~/.emacs file.  Abbrevs have to be enabled in your ~/.emacs, too.
838
839 (defvar sql-mode-abbrev-table nil
840   "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
841 (unless sql-mode-abbrev-table
842   (define-abbrev-table 'sql-mode-abbrev-table nil)
843   (mapcar
844     ;; In Emacs 21.3+, provide SYSTEM-FLAG to define-abbrev.
845    '(lambda (abbrev)
846       (let ((name (car abbrev))
847             (expansion (cadr abbrev)))
848         (condition-case nil
849             (define-abbrev sql-mode-abbrev-table name expansion nil 0 t)
850           (error
851            (define-abbrev sql-mode-abbrev-table name expansion)))))
852    '(("ins" "insert")
853     ("upd" "update")
854     ("del" "delete")
855     ("sel" "select")
856     ("proc" "procedure")
857     ("func" "function")
858     ("cr" "create"))))
859
860 ;; Syntax Table
861
862 (defvar sql-mode-syntax-table
863   (let ((table (make-syntax-table)))
864     ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
865     (modify-syntax-entry ?/ ". 14" table)
866     (modify-syntax-entry ?* ". 23" table)
867     ;; double-dash starts comment
868     (if (string-match "XEmacs\\|Lucid" emacs-version)
869         (modify-syntax-entry ?- ". 56" table)
870       (modify-syntax-entry ?- ". 12b" table))
871     ;; newline and formfeed end coments
872     (modify-syntax-entry ?\n "> b" table)
873     (modify-syntax-entry ?\f "> b" table)
874     ;; single quotes (') quotes delimit strings
875     (modify-syntax-entry ?' "\"" table)
876     ;; backslash is no escape character
877     (modify-syntax-entry ?\\ "." table)
878     table)
879   "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
880
881 ;; Font lock support
882
883 (defvar sql-mode-font-lock-object-name
884   (list (concat "^\\s-*\\(create\\|drop\\|alter\\)\\s-+" ;; lead off with CREATE, DROP or ALTER
885                 "\\(\\w+\\s-+\\)*"  ;; optional intervening keywords
886                 "\\(table\\|view\\|package\\(\\s-+body\\)?\\|proc\\(edure\\)?"
887                 "\\|function\\|trigger\\|sequence\\|rule\\|default\\)\\s-+"
888                 "\\(\\w+\\)")
889         6 'font-lock-function-name-face)
890
891   "Pattern to match the names of top-level objects.
892
893 The pattern matches the name in a CREATE, DROP or ALTER
894 statement.  The format of variable should be a valid
895 `font-lock-keywords' entry.")
896
897 (defmacro sql-keywords-re (&rest keywords)
898   "Compile-time generation of regexp matching any one of KEYWORDS."
899   `(eval-when-compile
900      (concat "\\b"
901              (regexp-opt ',keywords t)
902              "\\b")))
903
904 (defvar sql-mode-ansi-font-lock-keywords
905   (let ((ansi-funcs (sql-keywords-re
906 "abs" "avg" "bit_length" "cardinality" "cast" "char_length"
907 "character_length" "coalesce" "convert" "count" "current_date"
908 "current_path" "current_role" "current_time" "current_timestamp"
909 "current_user" "extract" "localtime" "localtimestamp" "lower" "max"
910 "min" "mod" "nullif" "octet_length" "overlay" "placing" "session_user"
911 "substring" "sum" "system_user" "translate" "treat" "trim" "upper"
912 "user"
913 ))
914
915         (ansi-non-reserved (sql-keywords-re
916 "ada" "asensitive" "assignment" "asymmetric" "atomic" "between"
917 "bitvar" "called" "catalog_name" "chain" "character_set_catalog"
918 "character_set_name" "character_set_schema" "checked" "class_origin"
919 "cobol" "collation_catalog" "collation_name" "collation_schema"
920 "column_name" "command_function" "command_function_code" "committed"
921 "condition_number" "connection_name" "constraint_catalog"
922 "constraint_name" "constraint_schema" "contains" "cursor_name"
923 "datetime_interval_code" "datetime_interval_precision" "defined"
924 "definer" "dispatch" "dynamic_function" "dynamic_function_code"
925 "existing" "exists" "final" "fortran" "generated" "granted"
926 "hierarchy" "hold" "implementation" "infix" "insensitive" "instance"
927 "instantiable" "invoker" "key_member" "key_type" "length" "m"
928 "message_length" "message_octet_length" "message_text" "method" "more"
929 "mumps" "name" "nullable" "number" "options" "overlaps" "overriding"
930 "parameter_mode" "parameter_name" "parameter_ordinal_position"
931 "parameter_specific_catalog" "parameter_specific_name"
932 "parameter_specific_schema" "pascal" "pli" "position" "repeatable"
933 "returned_length" "returned_octet_length" "returned_sqlstate"
934 "routine_catalog" "routine_name" "routine_schema" "row_count" "scale"
935 "schema_name" "security" "self" "sensitive" "serializable"
936 "server_name" "similar" "simple" "source" "specific_name" "style"
937 "subclass_origin" "sublist" "symmetric" "system" "table_name"
938 "transaction_active" "transactions_committed"
939 "transactions_rolled_back" "transform" "transforms" "trigger_catalog"
940 "trigger_name" "trigger_schema" "type" "uncommitted" "unnamed"
941 "user_defined_type_catalog" "user_defined_type_name"
942 "user_defined_type_schema"
943 ))
944
945         (ansi-reserved (sql-keywords-re
946 "absolute" "action" "add" "admin" "after" "aggregate" "alias" "all"
947 "allocate" "alter" "and" "any" "are" "as" "asc" "assertion" "at"
948 "authorization" "before" "begin" "both" "breadth" "by" "call"
949 "cascade" "cascaded" "case" "catalog" "check" "class" "close"
950 "collate" "collation" "column" "commit" "completion" "connect"
951 "connection" "constraint" "constraints" "constructor" "continue"
952 "corresponding" "create" "cross" "cube" "current" "cursor" "cycle"
953 "data" "day" "deallocate" "declare" "default" "deferrable" "deferred"
954 "delete" "depth" "deref" "desc" "describe" "descriptor" "destroy"
955 "destructor" "deterministic" "diagnostics" "dictionary" "disconnect"
956 "distinct" "domain" "drop" "dynamic" "each" "else" "end" "equals"
957 "escape" "every" "except" "exception" "exec" "execute" "external"
958 "false" "fetch" "first" "for" "foreign" "found" "free" "from" "full"
959 "function" "general" "get" "global" "go" "goto" "grant" "group"
960 "grouping" "having" "host" "hour" "identity" "ignore" "immediate" "in"
961 "indicator" "initialize" "initially" "inner" "inout" "input" "insert"
962 "intersect" "into" "is" "isolation" "iterate" "join" "key" "language"
963 "last" "lateral" "leading" "left" "less" "level" "like" "limit"
964 "local" "locator" "map" "match" "minute" "modifies" "modify" "module"
965 "month" "names" "natural" "new" "next" "no" "none" "not" "null" "of"
966 "off" "old" "on" "only" "open" "operation" "option" "or" "order"
967 "ordinality" "out" "outer" "output" "pad" "parameter" "parameters"
968 "partial" "path" "postfix" "prefix" "preorder" "prepare" "preserve"
969 "primary" "prior" "privileges" "procedure" "public" "read" "reads"
970 "recursive" "references" "referencing" "relative" "restrict" "result"
971 "return" "returns" "revoke" "right" "role" "rollback" "rollup"
972 "routine" "rows" "savepoint" "schema" "scroll" "search" "second"
973 "section" "select" "sequence" "session" "set" "sets" "size" "some"
974 "space" "specific" "specifictype" "sql" "sqlexception" "sqlstate"
975 "sqlwarning" "start" "state" "statement" "static" "structure" "table"
976 "temporary" "terminate" "than" "then" "timezone_hour"
977 "timezone_minute" "to" "trailing" "transaction" "translation"
978 "trigger" "true" "under" "union" "unique" "unknown" "unnest" "update"
979 "usage" "using" "value" "values" "variable" "view" "when" "whenever"
980 "where" "with" "without" "work" "write" "year"
981 ))
982
983         (ansi-types (sql-keywords-re
984 "array" "binary" "bit" "blob" "boolean" "char" "character" "clob"
985 "date" "dec" "decimal" "double" "float" "int" "integer" "interval"
986 "large" "national" "nchar" "nclob" "numeric" "object" "precision"
987 "real" "ref" "row" "scope" "smallint" "time" "timestamp" "varchar"
988 "varying" "zone"
989 )))
990
991     `((,ansi-non-reserved . font-lock-keyword-face)
992       (,ansi-reserved     . font-lock-keyword-face)
993       (,ansi-funcs        . font-lock-builtin-face)
994       (,ansi-types        . font-lock-type-face)))
995
996   "ANSI SQL keywords used by font-lock.
997
998 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
999 regular expressions are created during compilation by calling the
1000 function `regexp-opt'.  Therefore, take a look at the source before
1001 you define your own sql-mode-ansi-font-lock-keywords.  You may want to
1002 add functions and PL/SQL keywords.")
1003
1004 (defvar sql-mode-oracle-font-lock-keywords
1005   (let ((oracle-functions (sql-keywords-re
1006 "abs" "acos" "add_months" "ascii" "asciistr" "asin" "atan" "atan2"
1007 "avg" "bfilename" "bin_to_num" "bitand" "cast" "ceil" "chartorowid"
1008 "chr" "coalesce" "compose" "concat" "convert" "corr" "cos" "cosh"
1009 "count" "covar_pop" "covar_samp" "cume_dist" "current_date"
1010 "current_timestamp" "current_user" "dbtimezone" "decode" "decompose"
1011 "dense_rank" "depth" "deref" "dump" "empty_clob" "existsnode" "exp"
1012 "extract" "extractvalue" "first" "first_value" "floor" "following"
1013 "from_tz" "greatest" "group_id" "grouping_id" "hextoraw" "initcap"
1014 "instr" "lag" "last" "last_day" "last_value" "lead" "least" "length"
1015 "ln" "localtimestamp" "lower" "lpad" "ltrim" "make_ref" "max" "min"
1016 "mod" "months_between" "new_time" "next_day" "nls_charset_decl_len"
1017 "nls_charset_id" "nls_charset_name" "nls_initcap" "nls_lower"
1018 "nls_upper" "nlssort" "ntile" "nullif" "numtodsinterval"
1019 "numtoyminterval" "nvl" "nvl2" "over" "path" "percent_rank"
1020 "percentile_cont" "percentile_disc" "power" "preceding" "rank"
1021 "ratio_to_report" "rawtohex" "rawtonhex" "reftohex" "regr_"
1022 "regr_avgx" "regr_avgy" "regr_count" "regr_intercept" "regr_r2"
1023 "regr_slope" "regr_sxx" "regr_sxy" "regr_syy" "replace" "round"
1024 "row_number" "rowidtochar" "rowidtonchar" "rpad" "rtrim"
1025 "sessiontimezone" "sign" "sin" "sinh" "soundex" "sqrt" "stddev"
1026 "stddev_pop" "stddev_samp" "substr" "sum" "sys_connect_by_path"
1027 "sys_context" "sys_dburigen" "sys_extract_utc" "sys_guid" "sys_typeid"
1028 "sys_xmlagg" "sys_xmlgen" "sysdate" "systimestamp" "tan" "tanh"
1029 "to_char" "to_clob" "to_date" "to_dsinterval" "to_lob" "to_multi_byte"
1030 "to_nchar" "to_nclob" "to_number" "to_single_byte" "to_timestamp"
1031 "to_timestamp_tz" "to_yminterval" "translate" "treat" "trim" "trunc"
1032 "tz_offset" "uid" "unbounded" "unistr" "updatexml" "upper" "user"
1033 "userenv" "var_pop" "var_samp" "variance" "vsize" "width_bucket" "xml"
1034 "xmlagg" "xmlattribute" "xmlcolattval" "xmlconcat" "xmlelement"
1035 "xmlforest" "xmlsequence" "xmltransform"
1036 ))
1037
1038         (oracle-keywords (sql-keywords-re
1039 "abort" "access" "accessed" "account" "activate" "add" "admin"
1040 "advise" "after" "agent" "aggregate" "all" "allocate" "allow" "alter"
1041 "always" "analyze" "ancillary" "and" "any" "apply" "archive"
1042 "archivelog" "array" "as" "asc" "associate" "at" "attribute"
1043 "attributes" "audit" "authenticated" "authid" "authorization" "auto"
1044 "autoallocate" "automatic" "availability" "backup" "before" "begin"
1045 "behalf" "between" "binding" "bitmap" "block" "blocksize" "body"
1046 "both" "buffer_pool" "build" "by"  "cache" "call" "cancel"
1047 "cascade" "case" "category" "certificate" "chained" "change" "check"
1048 "checkpoint" "child" "chunk" "class" "clear" "clone" "close" "cluster"
1049 "column" "column_value" "columns" "comment" "commit" "committed"
1050 "compatibility" "compile" "complete" "composite_limit" "compress"
1051 "compute" "connect" "connect_time" "consider" "consistent"
1052 "constraint" "constraints" "constructor" "contents" "context"
1053 "continue" "controlfile" "corruption" "cost" "cpu_per_call"
1054 "cpu_per_session" "create" "cross" "cube" "current" "currval" "cycle"
1055 "dangling" "data" "database" "datafile" "datafiles" "day" "ddl"
1056 "deallocate" "debug" "default" "deferrable" "deferred" "definer"
1057 "delay" "delete" "demand" "desc" "determines" "deterministic"
1058 "dictionary" "dimension" "directory" "disable" "disassociate"
1059 "disconnect" "distinct" "distinguished" "distributed" "dml" "drop"
1060 "each" "element" "else" "enable" "end" "equals_path" "escape"
1061 "estimate" "except" "exceptions" "exchange" "excluding" "exists"
1062 "expire" "explain" "extent" "external" "externally"
1063 "failed_login_attempts" "fast" "file" "final" "finish" "flush" "for"
1064 "force" "foreign" "freelist" "freelists" "freepools" "fresh" "from"
1065 "full" "function" "functions" "generated" "global" "global_name"
1066 "globally" "grant" "group" "grouping" "groups" "guard" "hash"
1067 "hashkeys" "having" "heap" "hierarchy" "id" "identified" "identifier"
1068 "idle_time" "immediate" "in" "including" "increment" "index" "indexed"
1069 "indexes" "indextype" "indextypes" "indicator" "initial" "initialized"
1070 "initially" "initrans" "inner" "insert" "instance" "instantiable"
1071 "instead" "intersect" "into" "invalidate" "is" "isolation" "java"
1072 "join"  "keep" "key" "kill" "language" "left" "less" "level"
1073 "levels" "library" "like" "like2" "like4" "likec" "limit" "link"
1074 "list" "lob" "local" "location" "locator" "lock" "log" "logfile"
1075 "logging" "logical" "logical_reads_per_call"
1076 "logical_reads_per_session"  "managed" "management" "manual" "map"
1077 "mapping" "master" "matched" "materialized" "maxdatafiles"
1078 "maxextents" "maximize" "maxinstances" "maxlogfiles" "maxloghistory"
1079 "maxlogmembers" "maxsize" "maxtrans" "maxvalue" "member" "memory"
1080 "merge" "migrate" "minextents" "minimize" "minimum" "minus" "minvalue"
1081 "mode" "modify" "monitoring" "month" "mount" "move" "movement" "name"
1082 "named" "natural" "nested" "never" "new" "next" "nextval" "no"
1083 "noarchivelog" "noaudit" "nocache" "nocompress" "nocopy" "nocycle"
1084 "nodelay" "noforce" "nologging" "nomapping" "nomaxvalue" "nominimize"
1085 "nominvalue" "nomonitoring" "none" "noorder" "noparallel" "norely"
1086 "noresetlogs" "noreverse" "normal" "norowdependencies" "nosort"
1087 "noswitch" "not" "nothing" "notimeout" "novalidate" "nowait" "null"
1088 "nulls" "object" "of" "off" "offline" "oidindex" "old" "on" "online"
1089 "only" "open" "operator" "optimal" "option" "or" "order"
1090 "organization" "out" "outer" "outline" "overflow" "overriding"
1091 "package" "packages" "parallel" "parallel_enable" "parameters"
1092 "parent" "partition" "partitions" "password" "password_grace_time"
1093 "password_life_time" "password_lock_time" "password_reuse_max"
1094 "password_reuse_time" "password_verify_function" "pctfree"
1095 "pctincrease" "pctthreshold" "pctused" "pctversion" "percent"
1096 "performance" "permanent" "pfile" "physical" "pipelined" "plan"
1097 "post_transaction" "pragma" "prebuilt" "preserve" "primary" "private"
1098 "private_sga" "privileges" "procedure" "profile" "protection" "public"
1099 "purge" "query" "quiesce" "quota" "range" "read" "reads" "rebuild"
1100 "records_per_block" "recover" "recovery" "recycle" "reduced" "ref"
1101 "references" "referencing" "refresh" "register" "reject" "relational"
1102 "rely" "rename" "reset" "resetlogs" "resize" "resolve" "resolver"
1103 "resource" "restrict" "restrict_references" "restricted" "result"
1104 "resumable" "resume" "retention" "return" "returning" "reuse"
1105 "reverse" "revoke" "rewrite" "right" "rnds" "rnps" "role" "roles"
1106 "rollback" "rollup" "row" "rowdependencies" "rownum" "rows" "sample"
1107 "savepoint" "scan" "schema" "scn" "scope" "segment" "select"
1108 "selectivity" "self" "sequence" "serializable" "session"
1109 "sessions_per_user" "set" "sets" "settings" "shared" "shared_pool"
1110 "shrink" "shutdown" "siblings" "sid" "single" "size" "skip" "some"
1111 "sort" "source" "space" "specification" "spfile" "split" "standby"
1112 "start" "statement_id" "static" "statistics" "stop" "storage" "store"
1113 "structure" "subpartition" "subpartitions" "substitutable"
1114 "successful" "supplemental" "suspend" "switch" "switchover" "synonym"
1115 "sys" "system" "table" "tables" "tablespace" "tempfile" "template"
1116 "temporary" "test" "than" "then" "thread" "through" "time_zone"
1117 "timeout" "to" "trace" "transaction" "trigger" "triggers" "truncate"
1118 "trust" "type" "types" "unarchived" "under" "under_path" "undo"
1119 "uniform" "union" "unique" "unlimited" "unlock" "unquiesce"
1120 "unrecoverable" "until" "unusable" "unused" "update" "upgrade" "usage"
1121 "use" "using" "validate" "validation" "value" "values" "variable"
1122 "varray" "version" "view" "wait" "when" "whenever" "where" "with"
1123 "without" "wnds" "wnps" "work" "write" "xmldata" "xmlschema" "xmltype"
1124 ))
1125
1126         (oracle-types (sql-keywords-re
1127 "bfile" "blob" "byte" "char" "character" "clob" "date" "dec" "decimal"
1128 "double" "float" "int" "integer" "interval" "long" "national" "nchar"
1129 "nclob" "number" "numeric" "nvarchar2" "precision" "raw" "real"
1130 "rowid" "second" "smallint" "time" "timestamp" "urowid" "varchar"
1131 "varchar2" "varying" "year" "zone"
1132 ))
1133
1134         (plsql-functions (sql-keywords-re
1135 "%bulk_rowcount" "%found" "%isopen" "%notfound" "%rowcount" "%rowtype"
1136 "%type" "extend" "prior"
1137 ))
1138
1139         (plsql-keywords (sql-keywords-re
1140 "autonomous_transaction" "bulk" "char_base" "collect" "constant"
1141 "cursor" "declare" "do" "elsif" "exception_init" "execute" "exit"
1142 "extends" "false" "fetch" "forall" "goto" "hour" "if" "interface"
1143 "loop" "minute" "number_base" "ocirowid" "opaque" "others" "rowtype"
1144 "separate" "serially_reusable" "sql" "sqlcode" "sqlerrm" "subtype"
1145 "the" "timezone_abbr" "timezone_hour" "timezone_minute"
1146 "timezone_region" "true" "varrying" "while"
1147 ))
1148
1149         (plsql-type (sql-keywords-re
1150 "binary_integer" "boolean" "naturaln" "pls_integer" "positive"
1151 "positiven" "record" "signtype" "string"
1152 ))
1153
1154         (plsql-warning (sql-keywords-re
1155 "access_into_null" "case_not_found" "collection_is_null"
1156 "cursor_already_open" "dup_val_on_index" "invalid_cursor"
1157 "invalid_number" "login_denied" "no_data_found" "not_logged_on"
1158 "program_error" "rowtype_mismatch" "self_is_null" "storage_error"
1159 "subscript_beyond_count" "subscript_outside_limit" "sys_invalid_rowid"
1160 "timeout_on_resource" "too_many_rows" "value_error" "zero_divide"
1161 "exception" "notfound"
1162 ))
1163
1164         (sqlplus-commands
1165          (eval-when-compile (concat "^\\(\\("
1166                                     (regexp-opt '(
1167 "@" "@@" "accept" "append" "archive" "attribute" "break"
1168 "btitle" "change" "clear" "column" "connect" "copy" "define"
1169 "del" "describe" "disconnect" "edit" "execute" "exit" "get" "help"
1170 "host" "input" "list" "password" "pause" "print" "prompt" "recover"
1171 "remark" "repfooter" "repheader" "run" "save" "show" "shutdown"
1172 "spool" "start" "startup" "store" "timing" "ttitle" "undefine"
1173 "variable" "whenever"
1174
1175 ) t)
1176
1177    "\\)\\|"
1178    "\\(compute\\s-+\\(avg\\|cou\\|min\\|max\\|num\\|sum\\|std\\|var\\)\\)\\|"
1179    "\\(set\\s-+\\(appi\\(nfo\\)?\\|array\\(size\\)?\\|"
1180    "auto\\(commit\\)?\\|autop\\(rint\\)?\\|autorecovery\\|"
1181    "autot\\(race\\)?\\|blo\\(ckterminator\\)?\\|cmds\\(ep\\)?\\|"
1182    "colsep\\|com\\(patibility\\)?\\|con\\(cat\\)?\\|"
1183    "copyc\\(ommit\\)?\\|copytypecheck\\|def\\(ine\\)?\\|"
1184    "describe\\|echo\\|editf\\(ile\\)?\\|emb\\(edded\\)?\\|"
1185    "esc\\(ape\\)?\\|feed\\(back\\)?\\|flagger\\|"
1186    "flu\\(sh\\)?\\|hea\\(ding\\)?\\|heads\\(ep\\)?\\|"
1187    "instance\\|lin\\(esize\\)?\\|lobof\\(fset\\)?\\|"
1188    "logsource\\|long\\|longc\\(hunksize\\)?\\|mark\\(up\\)?\\|"
1189    "newp\\(age\\)?\\|null\\|numf\\(ormat\\)?\\|"
1190    "num\\(width\\)?\\|pages\\(ize\\)?\\|pau\\(se\\)?\\|"
1191    "recsep\\|recsepchar\\|serverout\\(put\\)?\\|"
1192    "shift\\(inout\\)?\\|show\\(mode\\)?\\|"
1193    "sqlbl\\(anklines\\)?\\|sqlc\\(ase\\)?\\|"
1194    "sqlco\\(ntinue\\)?\\|sqln\\(umber\\)?\\|"
1195    "sqlpluscompat\\(ibility\\)?\\|sqlpre\\(fix\\)?\\|"
1196    "sqlp\\(rompt\\)?\\|sqlt\\(erminator\\)?\\|"
1197    "suf\\(fix\\)?\\|tab\\|term\\(out\\)?\\|ti\\(me\\)?\\|"
1198    "timi\\(ng\\)?\\|trim\\(out\\)?\\|trims\\(pool\\)?\\|"
1199    "und\\(erline\\)?\\|ver\\(ify\\)?\\|wra\\(p\\)?\\)\\)\\)"
1200    "\\b.*$"
1201    ))))
1202
1203     `((,sqlplus-commands . font-lock-doc-face)
1204       (,oracle-functions . font-lock-builtin-face)
1205       (,oracle-keywords  . font-lock-keyword-face)
1206       (,oracle-types     . font-lock-type-face)
1207       (,plsql-functions  . font-lock-builtin-face)
1208       (,plsql-keywords   . font-lock-keyword-face)
1209       (,plsql-type       . font-lock-type-face)
1210       (,plsql-warning    . font-lock-warning-face)))
1211
1212   "Oracle SQL keywords used by font-lock.
1213
1214 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1215 regular expressions are created during compilation by calling the
1216 function `regexp-opt'.  Therefore, take a look at the source before
1217 you define your own sql-mode-oracle-font-lock-keywords.  You may want
1218 to add functions and PL/SQL keywords.")
1219
1220 (defvar sql-mode-postgres-font-lock-keywords
1221   (let ((pg-funcs (sql-keywords-re
1222 "abbrev" "abs" "acos" "age" "area" "ascii" "asin" "atab2" "atan"
1223 "atan2" "avg" "bit_length" "both" "broadcast" "btrim" "cbrt" "ceil"
1224 "center" "char_length" "chr" "coalesce" "col_description" "convert"
1225 "cos" "cot" "count" "current_database" "current_date" "current_schema"
1226 "current_schemas" "current_setting" "current_time" "current_timestamp"
1227 "current_user" "currval" "date_part" "date_trunc" "decode" "degrees"
1228 "diameter" "encode" "exp" "extract" "floor" "get_bit" "get_byte"
1229 "has_database_privilege" "has_function_privilege"
1230 "has_language_privilege" "has_schema_privilege" "has_table_privilege"
1231 "height" "host" "initcap" "isclosed" "isfinite" "isopen" "leading"
1232 "length" "ln" "localtime" "localtimestamp" "log" "lower" "lpad"
1233 "ltrim" "masklen" "max" "min" "mod" "netmask" "network" "nextval"
1234 "now" "npoints" "nullif" "obj_description" "octet_length" "overlay"
1235 "pclose" "pg_client_encoding" "pg_function_is_visible"
1236 "pg_get_constraintdef" "pg_get_indexdef" "pg_get_ruledef"
1237 "pg_get_userbyid" "pg_get_viewdef" "pg_opclass_is_visible"
1238 "pg_operator_is_visible" "pg_table_is_visible" "pg_type_is_visible"
1239 "pi" "popen" "position" "pow" "quote_ident" "quote_literal" "radians"
1240 "radius" "random" "repeat" "replace" "round" "rpad" "rtrim"
1241 "session_user" "set_bit" "set_byte" "set_config" "set_masklen"
1242 "setval" "sign" "sin" "split_part" "sqrt" "stddev" "strpos" "substr"
1243 "substring" "sum" "tan" "timeofday" "to_ascii" "to_char" "to_date"
1244 "to_hex" "to_number" "to_timestamp" "trailing" "translate" "trim"
1245 "trunc" "upper" "variance" "version" "width"
1246 ))
1247
1248         (pg-reserved (sql-keywords-re
1249 "abort" "access" "add" "after" "aggregate" "alignment" "all" "alter"
1250 "analyze" "and" "any" "as" "asc" "assignment" "authorization"
1251 "backward" "basetype" "before" "begin" "between" "binary" "by" "cache"
1252 "called" "cascade" "case" "cast" "characteristics" "check"
1253 "checkpoint" "class" "close" "cluster" "column" "comment" "commit"
1254 "committed" "commutator" "constraint" "constraints" "conversion"
1255 "copy" "create" "createdb" "createuser" "cursor" "cycle" "database"
1256 "deallocate" "declare" "default" "deferrable" "deferred" "definer"
1257 "delete" "delimiter" "desc" "distinct" "do" "domain" "drop" "each"
1258 "element" "else" "encoding" "encrypted" "end" "escape" "except"
1259 "exclusive" "execute" "exists" "explain" "extended" "external" "false"
1260 "fetch" "finalfunc" "for" "force" "foreign" "forward" "freeze" "from"
1261 "full" "function" "grant" "group" "gtcmp" "handler" "hashes" "having"
1262 "immediate" "immutable" "implicit" "in" "increment" "index" "inherits"
1263 "initcond" "initially" "input" "insensitive" "insert" "instead"
1264 "internallength" "intersect" "into" "invoker" "is" "isnull"
1265 "isolation" "join" "key" "language" "leftarg" "level" "like" "limit"
1266 "listen" "load" "local" "location" "lock" "ltcmp" "main" "match"
1267 "maxvalue" "merges" "minvalue" "mode" "move" "natural" "negator"
1268 "next" "nocreatedb" "nocreateuser" "none" "not" "nothing" "notify"
1269 "notnull" "null" "of" "offset" "oids" "on" "only" "operator" "or"
1270 "order" "output" "owner" "partial" "passedbyvalue" "password" "plain"
1271 "prepare" "primary" "prior" "privileges" "procedural" "procedure"
1272 "public" "read" "recheck" "references" "reindex" "relative" "rename"
1273 "reset" "restrict" "returns" "revoke" "rightarg" "rollback" "row"
1274 "rule" "schema" "scroll" "security" "select" "sequence" "serializable"
1275 "session" "set" "sfunc" "share" "show" "similar" "some" "sort1"
1276 "sort2" "stable" "start" "statement" "statistics" "storage" "strict"
1277 "stype" "sysid" "table" "temp" "template" "temporary" "then" "to"
1278 "transaction" "trigger" "true" "truncate" "trusted" "type"
1279 "unencrypted" "union" "unique" "unknown" "unlisten" "until" "update"
1280 "usage" "user" "using" "vacuum" "valid" "validator" "values"
1281 "variable" "verbose" "view" "volatile" "when" "where" "with" "without"
1282 "work"
1283 ))
1284
1285         (pg-types (sql-keywords-re
1286 "anyarray" "bigint" "bigserial" "bit" "boolean" "box" "bytea" "char"
1287 "character" "cidr" "circle" "cstring" "date" "decimal" "double"
1288 "float4" "float8" "inet" "int2" "int4" "int8" "integer" "internal"
1289 "interval" "language_handler" "line" "lseg" "macaddr" "money"
1290 "numeric" "oid" "opaque" "path" "point" "polygon" "precision" "real"
1291 "record" "regclass" "regoper" "regoperator" "regproc" "regprocedure"
1292 "regtype" "serial" "serial4" "serial8" "smallint" "text" "time"
1293 "timestamp" "varchar" "varying" "void" "zone"
1294 )))
1295
1296   `((,pg-funcs    . font-lock-builtin-face)
1297     (,pg-reserved . font-lock-keyword-face)
1298     (,pg-types    . font-lock-type-face)))
1299
1300   "Postgres SQL keywords used by font-lock.
1301
1302 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1303 regular expressions are created during compilation by calling the
1304 function `regexp-opt'.  Therefore, take a look at the source before
1305 you define your own sql-mode-postgres-font-lock-keywords.")
1306
1307 (defvar sql-mode-linter-font-lock-keywords
1308   (let ((linter-keywords (sql-keywords-re
1309 "autocommit" "autoinc" "autorowid" "cancel" "cascade" "channel"
1310 "committed" "count" "countblob" "cross" "current" "data" "database"
1311 "datafile" "datafiles" "datesplit" "dba" "dbname" "default" "deferred"
1312 "denied" "description" "device" "difference" "directory" "error"
1313 "escape" "euc" "exclusive" "external" "extfile" "false" "file"
1314 "filename" "filesize" "filetime" "filter" "findblob" "first" "foreign"
1315 "full" "fuzzy" "global" "granted" "ignore" "immediate" "increment"
1316 "indexes" "indexfile" "indexfiles" "indextime" "initial" "integrity"
1317 "internal" "key" "last_autoinc" "last_rowid" "limit" "linter"
1318 "linter_file_device" "linter_file_size" "linter_name_length" "ln"
1319 "local" "login" "maxisn" "maxrow" "maxrowid" "maxvalue" "message"
1320 "minvalue" "module" "names" "national" "natural" "new" "new_table"
1321 "no" "node" "noneuc" "nulliferror" "numbers" "off" "old" "old_table"
1322 "only" "operation" "optimistic" "option" "page" "partially" "password"
1323 "phrase" "plan" "precision" "primary" "priority" "privileges"
1324 "proc_info_size" "proc_par_name_len" "protocol" "quant" "range" "raw"
1325 "read" "record" "records" "references" "remote" "rename" "replication"
1326 "restart" "rewrite" "root" "row" "rule" "savepoint" "security"
1327 "sensitive" "sequence" "serializable" "server" "since" "size" "some"
1328 "startup" "statement" "station" "success" "sys_guid" "tables" "test"
1329 "timeout" "trace" "transaction" "translation" "trigger"
1330 "trigger_info_size" "true" "trunc" "uncommitted" "unicode" "unknown"
1331 "unlimited" "unlisted" "user" "utf8" "value" "varying" "volumes"
1332 "wait" "windows_code" "workspace" "write" "xml"
1333 ))
1334
1335         (linter-reserved (sql-keywords-re
1336 "access" "action" "add" "address" "after" "all" "alter" "always" "and"
1337 "any" "append" "as" "asc" "ascic" "async" "at_begin" "at_end" "audit"
1338 "aud_obj_name_len" "backup" "base" "before" "between" "blobfile"
1339 "blobfiles" "blobpct" "brief" "browse" "by" "case" "cast" "check"
1340 "clear" "close" "column" "comment" "commit" "connect" "contains"
1341 "correct" "create" "delete" "desc" "disable" "disconnect" "distinct"
1342 "drop" "each" "ef" "else" "enable" "end" "event" "except" "exclude"
1343 "execute" "exists" "extract" "fetch" "finish" "for" "from" "get"
1344 "grant" "group" "having" "identified" "in" "index" "inner" "insert"
1345 "instead" "intersect" "into" "is" "isolation" "join" "left" "level"
1346 "like" "lock" "mode" "modify" "not" "nowait" "null" "of" "on" "open"
1347 "or" "order" "outer" "owner" "press" "prior" "procedure" "public"
1348 "purge" "rebuild" "resource" "restrict" "revoke" "right" "role"
1349 "rollback" "rownum" "select" "session" "set" "share" "shutdown"
1350 "start" "stop" "sync" "synchronize" "synonym" "sysdate" "table" "then"
1351 "to" "union" "unique" "unlock" "until" "update" "using" "values"
1352 "view" "when" "where" "with" "without"
1353 ))
1354
1355         (linter-types (sql-keywords-re
1356 "bigint" "bitmap" "blob" "boolean" "char" "character" "date"
1357 "datetime" "dec" "decimal" "double" "float" "int" "integer" "nchar"
1358 "number" "numeric" "real" "smallint" "varbyte" "varchar" "byte"
1359 "cursor" "long"
1360 ))
1361
1362         (linter-functions (sql-keywords-re
1363 "abs" "acos" "asin" "atan" "atan2" "avg" "ceil" "cos" "cosh" "divtime"
1364 "exp" "floor" "getbits" "getblob" "getbyte" "getlong" "getraw"
1365 "getstr" "gettext" "getword" "hextoraw" "lenblob" "length" "log"
1366 "lower" "lpad" "ltrim" "max" "min" "mod" "monthname" "nvl"
1367 "octet_length" "power" "rand" "rawtohex" "repeat_string"
1368 "right_substr" "round" "rpad" "rtrim" "sign" "sin" "sinh" "soundex"
1369 "sqrt" "sum" "tan" "tanh" "timeint_to_days" "to_char" "to_date"
1370 "to_gmtime" "to_localtime" "to_number" "trim" "upper" "decode"
1371 "substr" "substring" "chr" "dayname" "days" "greatest" "hex" "initcap"
1372 "instr" "least" "multime" "replace" "width"
1373 )))
1374
1375     `((,linter-keywords  . font-lock-keyword-face)
1376       (,linter-reserved  . font-lock-keyword-face)
1377       (,linter-functions . font-lock-builtin-face)
1378       (,linter-types     . font-lock-type-face)))
1379
1380   "Linter SQL keywords used by font-lock.
1381
1382 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1383 regular expressions are created during compilation by calling the
1384 function `regexp-opt'.")
1385
1386 (defvar sql-mode-ms-font-lock-keywords
1387   (let ((ms-reserved (sql-keywords-re
1388 "absolute" "add" "all" "alter" "and" "any" "as" "asc" "authorization"
1389 "avg" "backup" "begin" "between" "break" "browse" "bulk" "by"
1390 "cascade" "case" "check" "checkpoint" "close" "clustered" "coalesce"
1391 "column" "commit" "committed" "compute" "confirm" "constraint"
1392 "contains" "containstable" "continue" "controlrow" "convert" "count"
1393 "create" "cross" "current" "current_date" "current_time"
1394 "current_timestamp" "current_user" "database" "deallocate" "declare"
1395 "default" "delete" "deny" "desc" "disk" "distinct" "distributed"
1396 "double" "drop" "dummy" "dump" "else" "end" "errlvl" "errorexit"
1397 "escape" "except" "exec" "execute" "exists" "exit" "fetch" "file"
1398 "fillfactor" "first" "floppy" "for" "foreign" "freetext"
1399 "freetexttable" "from" "full" "goto" "grant" "group" "having"
1400 "holdlock" "identity" "identity_insert" "identitycol" "if" "in"
1401 "index" "inner" "insert" "intersect" "into" "is" "isolation" "join"
1402 "key" "kill" "last" "left" "level" "like" "lineno" "load" "max" "min"
1403 "mirrorexit" "national" "next" "nocheck" "nolock" "nonclustered" "not"
1404 "null" "nullif" "of" "off" "offsets" "on" "once" "only" "open"
1405 "opendatasource" "openquery" "openrowset" "option" "or" "order"
1406 "outer" "output" "over" "paglock" "percent" "perm" "permanent" "pipe"
1407 "plan" "precision" "prepare" "primary" "print" "prior" "privileges"
1408 "proc" "procedure" "processexit" "public" "raiserror" "read"
1409 "readcommitted" "readpast" "readtext" "readuncommitted" "reconfigure"
1410 "references" "relative" "repeatable" "repeatableread" "replication"
1411 "restore" "restrict" "return" "revoke" "right" "rollback" "rowcount"
1412 "rowguidcol" "rowlock" "rule" "save" "schema" "select" "serializable"
1413 "session_user" "set" "shutdown" "some" "statistics" "sum"
1414 "system_user" "table" "tablock" "tablockx" "tape" "temp" "temporary"
1415 "textsize" "then" "to" "top" "tran" "transaction" "trigger" "truncate"
1416 "tsequal" "uncommitted" "union" "unique" "update" "updatetext"
1417 "updlock" "use" "user" "values" "view" "waitfor" "when" "where"
1418 "while" "with" "work" "writetext" "collate" "function" "openxml"
1419 "returns"
1420 ))
1421
1422         (ms-types (sql-keywords-re
1423 "binary" "bit" "char" "character" "cursor" "datetime" "dec" "decimal"
1424 "double" "float" "image" "int" "integer" "money" "national" "nchar"
1425 "ntext" "numeric" "numeric" "nvarchar" "precision" "real"
1426 "smalldatetime" "smallint" "smallmoney" "text" "timestamp" "tinyint"
1427 "uniqueidentifier" "varbinary" "varchar" "varying"
1428 ))
1429
1430         (ms-vars "\\b@[a-zA-Z0-9_]*\\b")
1431
1432         (ms-functions (sql-keywords-re
1433 "@@connections" "@@cpu_busy" "@@cursor_rows" "@@datefirst" "@@dbts"
1434 "@@error" "@@fetch_status" "@@identity" "@@idle" "@@io_busy"
1435 "@@langid" "@@language" "@@lock_timeout" "@@max_connections"
1436 "@@max_precision" "@@nestlevel" "@@options" "@@pack_received"
1437 "@@pack_sent" "@@packet_errors" "@@procid" "@@remserver" "@@rowcount"
1438 "@@servername" "@@servicename" "@@spid" "@@textsize" "@@timeticks"
1439 "@@total_errors" "@@total_read" "@@total_write" "@@trancount"
1440 "@@version" "abs" "acos" "and" "app_name" "ascii" "asin" "atan" "atn2"
1441 "avg" "case" "cast" "ceiling" "char" "charindex" "coalesce"
1442 "col_length" "col_name" "columnproperty" "containstable" "convert"
1443 "cos" "cot" "count" "current_timestamp" "current_user" "cursor_status"
1444 "databaseproperty" "datalength" "dateadd" "datediff" "datename"
1445 "datepart" "day" "db_id" "db_name" "degrees" "difference" "exp"
1446 "file_id" "file_name" "filegroup_id" "filegroup_name"
1447 "filegroupproperty" "fileproperty" "floor" "formatmessage"
1448 "freetexttable" "fulltextcatalogproperty" "fulltextserviceproperty"
1449 "getansinull" "getdate" "grouping" "host_id" "host_name" "ident_incr"
1450 "ident_seed" "identity" "index_col" "indexproperty" "is_member"
1451 "is_srvrolemember" "isdate" "isnull" "isnumeric" "left" "len" "log"
1452 "log10" "lower" "ltrim" "max" "min" "month" "nchar" "newid" "nullif"
1453 "object_id" "object_name" "objectproperty" "openquery" "openrowset"
1454 "parsename" "patindex" "patindex" "permissions" "pi" "power"
1455 "quotename" "radians" "rand" "replace" "replicate" "reverse" "right"
1456 "round" "rtrim" "session_user" "sign" "sin" "soundex" "space" "sqrt"
1457 "square" "stats_date" "stdev" "stdevp" "str" "stuff" "substring" "sum"
1458 "suser_id" "suser_name" "suser_sid" "suser_sname" "system_user" "tan"
1459 "textptr" "textvalid" "typeproperty" "unicode" "upper" "user"
1460 "user_id" "user_name" "var" "varp" "year"
1461 ))
1462
1463         (ms-commands
1464          (eval-when-compile
1465            (concat "^\\(\\(set\\s-+\\("
1466                    (regexp-opt '(
1467 "datefirst" "dateformat" "deadlock_priority" "lock_timeout"
1468 "concat_null_yields_null" "cursor_close_on_commit"
1469 "disable_def_cnst_chk" "fips_flagger" "identity_insert" "language"
1470 "offsets" "quoted_identifier" "arithabort" "arithignore" "fmtonly"
1471 "nocount" "noexec" "numeric_roundabort" "parseonly"
1472 "query_governor_cost_limit" "rowcount" "textsize" "ansi_defaults"
1473 "ansi_null_dflt_off" "ansi_null_dflt_on" "ansi_nulls" "ansi_padding"
1474 "ansi_warnings" "forceplan" "showplan_all" "showplan_text"
1475 "statistics" "implicit_transactions" "remote_proc_transactions"
1476 "transaction" "xact_abort"
1477 ) t)
1478                    "\\)\\)\\|go\\s-*\\|use\\s-+\\|setuser\\s-+\\|dbcc\\s-+\\).*$"))))
1479
1480     `((,ms-commands  . font-lock-doc-face)
1481       (,ms-reserved  . font-lock-keyword-face)
1482       (,ms-functions . font-lock-builtin-face)
1483       (,ms-vars      . font-lock-variable-name-face)
1484       (,ms-types     . font-lock-type-face)))
1485
1486   "Microsoft SQLServer SQL keywords used by font-lock.
1487
1488 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1489 regular expressions are created during compilation by calling the
1490 function `regexp-opt'.  Therefore, take a look at the source before
1491 you define your own sql-mode-ms-font-lock-keywords.")
1492
1493 (defvar sql-mode-sybase-font-lock-keywords nil
1494   "Sybase SQL keywords used by font-lock.
1495
1496 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1497 regular expressions are created during compilation by calling the
1498 function `regexp-opt'.  Therefore, take a look at the source before
1499 you define your own sql-mode-sybase-font-lock-keywords.")
1500
1501 (defvar sql-mode-informix-font-lock-keywords nil
1502   "Informix SQL keywords used by font-lock.
1503
1504 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1505 regular expressions are created during compilation by calling the
1506 function `regexp-opt'.  Therefore, take a look at the source before
1507 you define your own sql-mode-informix-font-lock-keywords.")
1508
1509 (defvar sql-mode-interbase-font-lock-keywords nil
1510   "Interbase SQL keywords used by font-lock.
1511
1512 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1513 regular expressions are created during compilation by calling the
1514 function `regexp-opt'.  Therefore, take a look at the source before
1515 you define your own sql-mode-interbase-font-lock-keywords.")
1516
1517 (defvar sql-mode-ingres-font-lock-keywords nil
1518   "Ingres SQL keywords used by font-lock.
1519
1520 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1521 regular expressions are created during compilation by calling the
1522 function `regexp-opt'.  Therefore, take a look at the source before
1523 you define your own sql-mode-interbase-font-lock-keywords.")
1524
1525 (defvar sql-mode-solid-font-lock-keywords nil
1526   "Solid SQL keywords used by font-lock.
1527
1528 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1529 regular expressions are created during compilation by calling the
1530 function `regexp-opt'.  Therefore, take a look at the source before
1531 you define your own sql-mode-solid-font-lock-keywords.")
1532
1533 (defvar sql-mode-mysql-font-lock-keywords
1534   (let ((mysql-funcs (sql-keywords-re
1535 "ascii" "avg" "bdmpolyfromtext" "bdmpolyfromwkb" "bdpolyfromtext"
1536 "bdpolyfromwkb" "benchmark" "bin" "bit_and" "bit_length" "bit_or"
1537 "bit_xor" "both" "cast" "char_length" "character_length" "coalesce"
1538 "concat" "concat_ws" "connection_id" "conv" "convert" "count"
1539 "curdate" "current_date" "current_time" "current_timestamp" "curtime"
1540 "elt" "encrypt" "export_set" "field" "find_in_set" "found_rows" "from"
1541 "geomcollfromtext" "geomcollfromwkb" "geometrycollectionfromtext"
1542 "geometrycollectionfromwkb" "geometryfromtext" "geometryfromwkb"
1543 "geomfromtext" "geomfromwkb" "get_lock" "group_concat" "hex" "ifnull"
1544 "instr" "interval" "isnull" "last_insert_id" "lcase" "leading"
1545 "length" "linefromtext" "linefromwkb" "linestringfromtext"
1546 "linestringfromwkb" "load_file" "locate" "lower" "lpad" "ltrim"
1547 "make_set" "master_pos_wait" "max" "mid" "min" "mlinefromtext"
1548 "mlinefromwkb" "mpointfromtext" "mpointfromwkb" "mpolyfromtext"
1549 "mpolyfromwkb" "multilinestringfromtext" "multilinestringfromwkb"
1550 "multipointfromtext" "multipointfromwkb" "multipolygonfromtext"
1551 "multipolygonfromwkb" "now" "nullif" "oct" "octet_length" "ord"
1552 "pointfromtext" "pointfromwkb" "polyfromtext" "polyfromwkb"
1553 "polygonfromtext" "polygonfromwkb" "position" "quote" "rand"
1554 "release_lock" "repeat" "replace" "reverse" "rpad" "rtrim" "soundex"
1555 "space" "std" "stddev" "substring" "substring_index" "sum" "sysdate"
1556 "trailing" "trim" "ucase" "unix_timestamp" "upper" "user" "variance"
1557 ))
1558
1559         (mysql-keywords (sql-keywords-re
1560 "action" "add" "after" "against" "all" "alter" "and" "as" "asc"
1561 "auto_increment" "avg_row_length" "bdb" "between" "by" "cascade"
1562 "case" "change" "character" "check" "checksum" "close" "collate"
1563 "collation" "column" "columns" "comment" "committed" "concurrent"
1564 "constraint" "create" "cross" "data" "database" "default"
1565 "delay_key_write" "delayed" "delete" "desc" "directory" "disable"
1566 "distinct" "distinctrow" "do" "drop" "dumpfile" "duplicate" "else"
1567 "enable" "enclosed" "end" "escaped" "exists" "fields" "first" "for"
1568 "force" "foreign" "from" "full" "fulltext" "global" "group" "handler"
1569 "having" "heap" "high_priority" "if" "ignore" "in" "index" "infile"
1570 "inner" "insert" "insert_method" "into" "is" "isam" "isolation" "join"
1571 "key" "keys" "last" "left" "level" "like" "limit" "lines" "load"
1572 "local" "lock" "low_priority" "match" "max_rows" "merge" "min_rows"
1573 "mode" "modify" "mrg_myisam" "myisam" "natural" "next" "no" "not"
1574 "null" "offset" "oj" "on" "open" "optionally" "or" "order" "outer"
1575 "outfile" "pack_keys" "partial" "password" "prev" "primary"
1576 "procedure" "quick" "raid0" "raid_type" "read" "references" "rename"
1577 "repeatable" "restrict" "right" "rollback" "rollup" "row_format"
1578 "savepoint" "select" "separator" "serializable" "session" "set"
1579 "share" "show" "sql_big_result" "sql_buffer_result" "sql_cache"
1580 "sql_calc_found_rows" "sql_no_cache" "sql_small_result" "starting"
1581 "straight_join" "striped" "table" "tables" "temporary" "terminated"
1582 "then" "to" "transaction" "truncate" "type" "uncommitted" "union"
1583 "unique" "unlock" "update" "use" "using" "values" "when" "where"
1584 "with" "write" "xor"
1585 ))
1586
1587         (mysql-types (sql-keywords-re
1588 "bigint" "binary" "bit" "blob" "bool" "boolean" "char" "curve" "date"
1589 "datetime" "dec" "decimal" "double" "enum" "fixed" "float" "geometry"
1590 "geometrycollection" "int" "integer" "line" "linearring" "linestring"
1591 "longblob" "longtext" "mediumblob" "mediumint" "mediumtext"
1592 "multicurve" "multilinestring" "multipoint" "multipolygon"
1593 "multisurface" "national" "numeric" "point" "polygon" "precision"
1594 "real" "smallint" "surface" "text" "time" "timestamp" "tinyblob"
1595 "tinyint" "tinytext" "unsigned" "varchar" "year" "year2" "year4"
1596 "zerofill"
1597 )))
1598
1599     `((,mysql-funcs    . font-lock-builtin-face)
1600       (,mysql-keywords . font-lock-keyword-face)
1601       (,mysql-types    . font-lock-type-face)))
1602
1603   "MySQL SQL keywords used by font-lock.
1604
1605 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1606 regular expressions are created during compilation by calling the
1607 function `regexp-opt'.  Therefore, take a look at the source before
1608 you define your own sql-mode-mysql-font-lock-keywords.")
1609
1610 (defvar sql-mode-sqlite-font-lock-keywords nil
1611   "SQLite SQL keywords used by font-lock.
1612
1613 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1614 regular expressions are created during compilation by calling the
1615 function `regexp-opt'.  Therefore, take a look at the source before
1616 you define your own sql-mode-sqlite-font-lock-keywords.")
1617
1618 (defvar sql-mode-db2-font-lock-keywords nil
1619   "DB2 SQL keywords used by font-lock.
1620
1621 This variable is used by `sql-mode' and `sql-interactive-mode'.  The
1622 regular expressions are created during compilation by calling the
1623 function `regexp-opt'.  Therefore, take a look at the source before
1624 you define your own sql-mode-db2-font-lock-keywords.")
1625
1626 (defvar sql-mode-font-lock-keywords nil
1627   "SQL keywords used by font-lock.
1628
1629 Setting this variable directly no longer has any affect.  Use
1630 `sql-product' and `sql-add-product-keywords' to control the
1631 highlighting rules in sql-mode.")
1632
1633 \f
1634
1635 ;;; SQL Product support functions
1636
1637 (defun sql-product-feature (feature &optional product)
1638   "Lookup `feature' needed to support the current SQL product.
1639
1640 See \[sql-product-alist] for a list of products and supported features."
1641   (plist-get
1642    (cdr (assoc (or product sql-product)
1643                sql-product-alist))
1644    feature))
1645
1646 (defun sql-product-font-lock (keywords-only imenu)
1647   "Sets `font-lock-defaults' and `font-lock-keywords' based on
1648 the product-specific keywords and syntax-alists defined in
1649 `sql-product-alist'."
1650   (let
1651       ;; Get the product-specific syntax-alist.
1652       ((syntax-alist
1653         (append
1654          (sql-product-feature :syntax-alist)
1655          '((?_ . "w") (?. . "w")))))
1656
1657     ;; Get the product-specific keywords.
1658     (setq sql-mode-font-lock-keywords
1659           (append
1660            (unless (eq sql-product 'ansi)
1661              (eval (sql-product-feature :font-lock)))
1662            ;; Always highlight ANSI keywords
1663            (eval (sql-product-feature :font-lock 'ansi))
1664            ;; Fontify object names in CREATE, DROP and ALTER DDL
1665            ;; statements
1666            (list sql-mode-font-lock-object-name)))
1667
1668     ;; Setup font-lock.  Force re-parsing of `font-lock-defaults'.
1669     (set (make-local-variable 'font-lock-set-defaults) nil)
1670     (setq font-lock-defaults (list 'sql-mode-font-lock-keywords
1671                                    keywords-only t syntax-alist))
1672
1673     ;; Force font lock to reinitialize if it is already on
1674     ;; Otherwise, we can wait until it can be started.
1675     (when (and (fboundp 'font-lock-mode)
1676                font-lock-mode)
1677       (turn-off-font-lock)
1678       (turn-on-font-lock))
1679
1680     (add-hook 'font-lock-mode-hook
1681               (lambda ()
1682                 ;; Provide defaults for new font-lock faces.
1683                 (defvar font-lock-builtin-face
1684                   (if (boundp 'font-lock-preprocessor-face)
1685                       font-lock-preprocessor-face
1686                     font-lock-keyword-face))
1687                 (defvar font-lock-doc-face font-lock-string-face))
1688               nil t)
1689
1690     ;; Setup imenu; it needs the same syntax-alist.
1691     (when imenu
1692         (setq imenu-syntax-alist syntax-alist))))
1693
1694 ;;;###autoload
1695 (defun sql-add-product-keywords (product keywords &optional append)
1696   "Add highlighting KEYWORDS for SQL PRODUCT.
1697
1698 PRODUCT should be a symbol, the name of a sql product, such as
1699 `oracle'.  KEYWORDS should be a list; see the variable
1700 `font-lock-keywords'.  By default they are added at the beginning
1701 of the current highlighting list.  If optional argument APPEND is
1702 `set', they are used to replace the current highlighting list.
1703 If APPEND is any other non-nil value, they are added at the end
1704 of the current highlighting list.
1705
1706 For example:
1707
1708  (sql-add-product-keywords 'ms
1709   '((\"\\\\b\\\\w+_t\\\\b\" . font-lock-type-face)))
1710
1711 adds a fontification pattern to fontify identifiers ending in
1712 `_t' as data types."
1713
1714   (let ((font-lock (sql-product-feature :font-lock product))
1715         old)
1716     (setq old (eval font-lock))
1717     (set font-lock
1718          (if (eq append 'set)
1719              keywords
1720            (if append
1721                (append old keywords)
1722              (append keywords old))))))
1723
1724 \f
1725
1726 ;;; Functions to switch highlighting
1727
1728 (defun sql-highlight-product ()
1729   "Turns on the appropriate font highlighting for the SQL product
1730 selected."
1731
1732   (when (eq major-mode 'sql-mode)
1733     ;; Setup font-lock
1734     (sql-product-font-lock nil t)
1735
1736     ;; Set the mode name to include the product.
1737     (setq mode-name (concat "SQL[" (prin1-to-string sql-product) "]"))))
1738
1739 (defun sql-set-product (product)
1740   "Set `sql-product' to product and enable appropriate
1741 highlighting."
1742   (interactive "SEnter SQL product: ")
1743   (when (not (assoc product sql-product-alist))
1744     (error "SQL product %s is not supported; treated as ANSI" product)
1745     (setq product 'ansi))
1746
1747   ;; Save product setting and fontify.
1748   (setq sql-product product)
1749   (sql-highlight-product))
1750
1751 (defun sql-highlight-oracle-keywords ()
1752   "Highlight Oracle keywords."
1753   (interactive)
1754   (sql-set-product 'oracle))
1755
1756 (defun sql-highlight-postgres-keywords ()
1757   "Highlight Postgres keywords."
1758   (interactive)
1759   (sql-set-product 'postgres))
1760
1761 (defun sql-highlight-linter-keywords ()
1762   "Highlight LINTER keywords."
1763   (interactive)
1764   (sql-set-product 'linter))
1765
1766 (defun sql-highlight-ms-keywords ()
1767   "Highlight Microsoft SQLServer keywords."
1768   (interactive)
1769   (sql-set-product 'ms))
1770
1771 (defun sql-highlight-ansi-keywords ()
1772   "Highlight ANSI SQL keywords."
1773   (interactive)
1774   (sql-set-product 'ansi))
1775
1776 (defun sql-highlight-sybase-keywords ()
1777   "Highlight Sybase SQL keywords."
1778   (interactive)
1779   (sql-set-product 'sybase))
1780
1781 (defun sql-highlight-informix-keywords ()
1782   "Highlight Informix SQL keywords."
1783   (interactive)
1784   (sql-set-product 'informix))
1785
1786 (defun sql-highlight-interbase-keywords ()
1787   "Highlight Interbase SQL keywords."
1788   (interactive)
1789   (sql-set-product 'interbase))
1790
1791 (defun sql-highlight-ingres-keywords ()
1792   "Highlight Ingres SQL keywords."
1793   (interactive)
1794   (sql-set-product 'ingres))
1795
1796 (defun sql-highlight-solid-keywords ()
1797   "Highlight Solid SQL keywords."
1798   (interactive)
1799   (sql-set-product 'solid))
1800
1801 (defun sql-highlight-mysql-keywords ()
1802   "Highlight MySQL SQL keywords."
1803   (interactive)
1804   (sql-set-product 'mysql))
1805
1806 (defun sql-highlight-sqlite-keywords ()
1807   "Highlight SQLite SQL keywords."
1808   (interactive)
1809   (sql-set-product 'sqlite))
1810
1811 (defun sql-highlight-db2-keywords ()
1812   "Highlight DB2 SQL keywords."
1813   (interactive)
1814   (sql-set-product 'db2))
1815
1816 \f
1817
1818 ;;; Compatibility functions
1819
1820 (if (not (fboundp 'comint-line-beginning-position))
1821     ;; comint-line-beginning-position is defined in Emacs 21
1822     (defun comint-line-beginning-position ()
1823       "Returns the buffer position of the beginning of the line, after any prompt.
1824 The prompt is assumed to be any text at the beginning of the line matching
1825 the regular expression `comint-prompt-regexp', a buffer local variable."
1826       (save-excursion (comint-bol nil) (point))))
1827
1828 \f
1829
1830 ;;; Small functions
1831
1832 (defun sql-magic-go (arg)
1833   "Insert \"o\" and call `comint-send-input'.
1834 `sql-electric-stuff' must be the symbol `go'."
1835   (interactive "P")
1836   (self-insert-command (prefix-numeric-value arg))
1837   (if (and (equal sql-electric-stuff 'go)
1838            (save-excursion
1839              (comint-bol nil)
1840              (looking-at "go\\b")))
1841       (comint-send-input)))
1842
1843 (defun sql-magic-semicolon (arg)
1844   "Insert semicolon and call `comint-send-input'.
1845 `sql-electric-stuff' must be the symbol `semicolon'."
1846   (interactive "P")
1847   (self-insert-command (prefix-numeric-value arg))
1848   (if (equal sql-electric-stuff 'semicolon)
1849        (comint-send-input)))
1850
1851 (defun sql-accumulate-and-indent ()
1852   "Continue SQL statement on the next line."
1853   (interactive)
1854   (if (fboundp 'comint-accumulate)
1855       (comint-accumulate)
1856     (newline))
1857   (indent-according-to-mode))
1858
1859 ;;;###autoload
1860 (defun sql-help ()
1861   "Show short help for the SQL modes.
1862
1863 Use an entry function to open an interactive SQL buffer.  This buffer is
1864 usually named `*SQL*'.  The name of the major mode is SQLi.
1865
1866 Use the following commands to start a specific SQL interpreter:
1867
1868     PostGres: \\[sql-postgres]
1869     MySQL: \\[sql-mysql]
1870     SQLite: \\[sql-sqlite]
1871
1872 Other non-free SQL implementations are also supported:
1873
1874     Solid: \\[sql-solid]
1875     Oracle: \\[sql-oracle]
1876     Informix: \\[sql-informix]
1877     Sybase: \\[sql-sybase]
1878     Ingres: \\[sql-ingres]
1879     Microsoft: \\[sql-ms]
1880     DB2: \\[sql-db2]
1881     Interbase: \\[sql-interbase]
1882     Linter: \\[sql-linter]
1883
1884 But we urge you to choose a free implementation instead of these.
1885
1886 Once you have the SQLi buffer, you can enter SQL statements in the
1887 buffer.  The output generated is appended to the buffer and a new prompt
1888 is generated.  See the In/Out menu in the SQLi buffer for some functions
1889 that help you navigate through the buffer, the input history, etc.
1890
1891 If you have a really complex SQL statement or if you are writing a
1892 procedure, you can do this in a separate buffer.  Put the new buffer in
1893 `sql-mode' by calling \\[sql-mode].  The name of this buffer can be
1894 anything.  The name of the major mode is SQL.
1895
1896 In this SQL buffer (SQL mode), you can send the region or the entire
1897 buffer to the interactive SQL buffer (SQLi mode).  The results are
1898 appended to the SQLi buffer without disturbing your SQL buffer."
1899   (interactive)
1900   (describe-function 'sql-help))
1901
1902 (defun sql-read-passwd (prompt &optional default)
1903   "Read a password using PROMPT.
1904 Optional DEFAULT is password to start with.  This function calls
1905 `read-passwd' if it is available.  If not, function
1906 `ange-ftp-read-passwd' is called.  This should always be available,
1907 even in old versions of Emacs."
1908   (if (fboundp 'read-passwd)
1909       (read-passwd prompt nil default)
1910     (unless (fboundp 'ange-ftp-read-passwd)
1911       (autoload 'ange-ftp-read-passwd "ange-ftp"))
1912     (ange-ftp-read-passwd prompt default)))
1913
1914 (defun sql-get-login (&rest what)
1915   "Get username, password and database from the user.
1916
1917 The variables `sql-user', `sql-password', `sql-server', and
1918 `sql-database' can be customized.  They are used as the default values.
1919 Usernames, servers and databases are stored in `sql-user-history',
1920 `sql-server-history' and `database-history'.  Passwords are not stored
1921 in a history.
1922
1923 Parameter WHAT is a list of the arguments passed to this function.
1924 The function asks for the username if WHAT contains symbol `user', for
1925 the password if it contains symbol `password', for the server if it
1926 contains symbol `server', and for the database if it contains symbol
1927 `database'.  The members of WHAT are processed in the order in which
1928 they are provided.
1929
1930 In order to ask the user for username, password and database, call the
1931 function like this: (sql-get-login 'user 'password 'database)."
1932   (interactive)
1933   (while what
1934     (cond
1935      ((eq (car what) 'user)             ; user
1936       (setq sql-user
1937             (read-from-minibuffer "User: " sql-user nil nil
1938                                   sql-user-history)))
1939      ((eq (car what) 'password)         ; password
1940       (setq sql-password
1941             (sql-read-passwd "Password: " sql-password)))
1942      ((eq (car what) 'server)           ; server
1943       (setq sql-server
1944             (read-from-minibuffer "Server: " sql-server nil nil
1945                                   sql-server-history)))
1946      ((eq (car what) 'database)         ; database
1947       (setq sql-database
1948             (read-from-minibuffer "Database: " sql-database nil nil
1949                                   sql-database-history))))
1950     (setq what (cdr what))))
1951
1952 (defun sql-find-sqli-buffer ()
1953   "Return the current default SQLi buffer or nil.
1954 In order to qualify, the SQLi buffer must be alive,
1955 be in `sql-interactive-mode' and have a process."
1956   (let ((default-buffer (default-value 'sql-buffer)))
1957     (if (and (buffer-live-p default-buffer)
1958              (get-buffer-process default-buffer))
1959         default-buffer
1960       (save-excursion
1961         (let ((buflist (buffer-list))
1962               (found))
1963           (while (not (or (null buflist)
1964                           found))
1965             (let ((candidate (car buflist)))
1966               (set-buffer candidate)
1967               (if (and (equal major-mode 'sql-interactive-mode)
1968                        (get-buffer-process candidate))
1969                   (setq found candidate))
1970               (setq buflist (cdr buflist))))
1971           found)))))
1972
1973 (defun sql-set-sqli-buffer-generally ()
1974   "Set SQLi buffer for all SQL buffers that have none.
1975 This function checks all SQL buffers for their SQLi buffer.  If their
1976 SQLi buffer is nonexistent or has no process, it is set to the current
1977 default SQLi buffer.  The current default SQLi buffer is determined
1978 using `sql-find-sqli-buffer'.  If `sql-buffer' is set,
1979 `sql-set-sqli-hook' is run."
1980   (interactive)
1981   (save-excursion
1982     (let ((buflist (buffer-list))
1983           (default-sqli-buffer (sql-find-sqli-buffer)))
1984       (setq-default sql-buffer default-sqli-buffer)
1985       (while (not (null buflist))
1986         (let ((candidate (car buflist)))
1987           (set-buffer candidate)
1988           (if (and (equal major-mode 'sql-mode)
1989                    (not (buffer-live-p sql-buffer)))
1990               (progn
1991                 (setq sql-buffer default-sqli-buffer)
1992                 (run-hooks 'sql-set-sqli-hook))))
1993         (setq buflist (cdr buflist))))))
1994
1995 (defun sql-set-sqli-buffer ()
1996   "Set the SQLi buffer SQL strings are sent to.
1997
1998 Call this function in a SQL buffer in order to set the SQLi buffer SQL
1999 strings are sent to.  Calling this function sets `sql-buffer' and runs
2000 `sql-set-sqli-hook'.
2001
2002 If you call it from a SQL buffer, this sets the local copy of
2003 `sql-buffer'.
2004
2005 If you call it from anywhere else, it sets the global copy of
2006 `sql-buffer'."
2007   (interactive)
2008   (let ((default-buffer (sql-find-sqli-buffer)))
2009     (if (null default-buffer)
2010         (error "There is no suitable SQLi buffer"))
2011     (let ((new-buffer
2012            (get-buffer
2013             (read-buffer "New SQLi buffer: " default-buffer t))))
2014       (if (null (get-buffer-process new-buffer))
2015           (error "Buffer %s has no process" (buffer-name new-buffer)))
2016       (if (null (save-excursion
2017                   (set-buffer new-buffer)
2018                   (equal major-mode 'sql-interactive-mode)))
2019           (error "Buffer %s is no SQLi buffer" (buffer-name new-buffer)))
2020       (if new-buffer
2021           (progn
2022             (setq sql-buffer new-buffer)
2023             (run-hooks 'sql-set-sqli-hook))))))
2024
2025 (defun sql-show-sqli-buffer ()
2026   "Show the name of current SQLi buffer.
2027
2028 This is the buffer SQL strings are sent to.  It is stored in the
2029 variable `sql-buffer'.  See `sql-help' on how to create such a buffer."
2030   (interactive)
2031   (if (null (buffer-live-p sql-buffer))
2032       (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
2033     (if (null (get-buffer-process sql-buffer))
2034         (message "Buffer %s has no process." (buffer-name sql-buffer))
2035       (message "Current SQLi buffer is %s." (buffer-name sql-buffer)))))
2036
2037 (defun sql-make-alternate-buffer-name ()
2038   "Return a string that can be used to rename a SQLi buffer.
2039
2040 This is used to set `sql-alternate-buffer-name' within
2041 `sql-interactive-mode'."
2042   (concat (if (string= "" sql-user)
2043               (if (string= "" (user-login-name))
2044                   ()
2045                 (concat (user-login-name) "/"))
2046             (concat sql-user "/"))
2047           (if (string= "" sql-database)
2048               (if (string= "" sql-server)
2049                   (system-name)
2050                 sql-server)
2051             sql-database)))
2052
2053 (defun sql-rename-buffer ()
2054   "Renames a SQLi buffer."
2055   (interactive)
2056   (rename-buffer (format "*SQL: %s*" sql-alternate-buffer-name) t))
2057
2058 (defun sql-copy-column ()
2059   "Copy current column to the end of buffer.
2060 Inserts SELECT or commas if appropriate."
2061   (interactive)
2062   (let ((column))
2063     (save-excursion
2064       (setq column (buffer-substring
2065                   (progn (forward-char 1) (backward-sexp 1) (point))
2066                   (progn (forward-sexp 1) (point))))
2067       (goto-char (point-max))
2068       (let ((bol (comint-line-beginning-position)))
2069         (cond
2070          ;; if empty command line, insert SELECT
2071          ((= bol (point))
2072           (insert "SELECT "))
2073          ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
2074          ((save-excursion
2075             (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
2076                                 bol t))
2077           (insert ", "))
2078          ;; else insert a space
2079          (t
2080           (if (eq (preceding-char) ? )
2081               nil
2082             (insert " ")))))
2083       ;; in any case, insert the column
2084       (insert column)
2085       (message "%s" column))))
2086
2087 ;; On NT, SQL*Plus for Oracle turns on full buffering for stdout if it
2088 ;; is not attached to a character device; therefore placeholder
2089 ;; replacement by SQL*Plus is fully buffered.  The workaround lets
2090 ;; Emacs query for the placeholders.
2091
2092 (defvar sql-placeholder-history nil
2093   "History of placeholder values used.")
2094
2095 (defun sql-query-placeholders-and-send (proc string)
2096   "Send to PROC input STRING, maybe replacing placeholders.
2097 Placeholders are words starting with and ampersand like &this.
2098 This function is used for `comint-input-sender' if using `sql-oracle' on NT."
2099   (while (string-match "&\\(\\sw+\\)" string)
2100     (setq string (replace-match
2101                   (read-from-minibuffer
2102                    (format "Enter value for %s: " (match-string 1 string))
2103                    nil nil nil sql-placeholder-history)
2104                   t t string)))
2105   (comint-send-string proc string)
2106   (if comint-input-sender-no-newline
2107       (if (not (string-equal string ""))
2108           (process-send-eof))
2109     (comint-send-string proc "\n")))
2110
2111 ;; Using DB2 interactively, newlines must be escaped with " \".
2112 ;; The space before the backslash is relevant.
2113 (defun sql-escape-newlines-and-send (proc string)
2114   "Send to PROC input STRING, escaping newlines if necessary.
2115 Every newline in STRING will be preceded with a space and a backslash."
2116   (let ((result "") (start 0) mb me)
2117     (while (string-match "\n" string start)
2118       (setq mb (match-beginning 0)
2119             me (match-end 0))
2120       (if (and (> mb 1)
2121                (string-equal " \\" (substring string (- mb 2) mb)))
2122           (setq result (concat result (substring string start me)))
2123         (setq result (concat result (substring string start mb) " \\\n")))
2124       (setq start me))
2125     (setq result (concat result (substring string start)))
2126     (comint-send-string proc result)
2127     (if comint-input-sender-no-newline
2128         (if (not (string-equal string ""))
2129             (process-send-eof))
2130       (comint-send-string proc "\n"))))
2131
2132 \f
2133
2134 ;;; Sending the region to the SQLi buffer.
2135
2136 (defun sql-send-region (start end)
2137   "Send a region to the SQL process."
2138   (interactive "r")
2139   (if (buffer-live-p sql-buffer)
2140       (save-excursion
2141         (comint-send-region sql-buffer start end)
2142         (if (string-match "\n$" (buffer-substring start end))
2143             ()
2144           (comint-send-string sql-buffer "\n"))
2145         (message "Sent string to buffer %s." (buffer-name sql-buffer))
2146         (if sql-pop-to-buffer-after-send-region
2147             (pop-to-buffer sql-buffer)
2148           (display-buffer sql-buffer)))
2149     (message "No SQL process started.")))
2150
2151 (defun sql-send-paragraph ()
2152   "Send the current paragraph to the SQL process."
2153   (interactive)
2154   (let ((start (save-excursion
2155                  (backward-paragraph)
2156                  (point)))
2157         (end (save-excursion
2158                (forward-paragraph)
2159                (point))))
2160     (sql-send-region start end)))
2161
2162 (defun sql-send-buffer ()
2163   "Send the buffer contents to the SQL process."
2164   (interactive)
2165   (sql-send-region (point-min) (point-max)))
2166
2167 (defun sql-send-string (str)
2168   "Send a string to the SQL process."
2169   (interactive "sSQL Text: ")
2170   (if (buffer-live-p sql-buffer)
2171       (save-excursion
2172         (comint-send-string sql-buffer str)
2173         (comint-send-string sql-buffer "\n")
2174         (message "Sent string to buffer %s." (buffer-name sql-buffer))
2175         (if sql-pop-to-buffer-after-send-region
2176             (pop-to-buffer sql-buffer)
2177           (display-buffer sql-buffer)))
2178     (message "No SQL process started.")))
2179
2180 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
2181   "Toggle `sql-pop-to-buffer-after-send-region'.
2182
2183 If given the optional parameter VALUE, sets
2184 sql-toggle-pop-to-buffer-after-send-region to VALUE."
2185   (interactive "P")
2186   (if value
2187       (setq sql-pop-to-buffer-after-send-region value)
2188     (setq sql-pop-to-buffer-after-send-region
2189           (null sql-pop-to-buffer-after-send-region ))))
2190
2191 \f
2192
2193 ;;; SQL mode -- uses SQL interactive mode
2194
2195 ;;;###autoload
2196 (defun sql-mode ()
2197   "Major mode to edit SQL.
2198
2199 You can send SQL statements to the SQLi buffer using
2200 \\[sql-send-region].  Such a buffer must exist before you can do this.
2201 See `sql-help' on how to create SQLi buffers.
2202
2203 \\{sql-mode-map}
2204 Customization: Entry to this mode runs the `sql-mode-hook'.
2205
2206 When you put a buffer in SQL mode, the buffer stores the last SQLi
2207 buffer created as its destination in the variable `sql-buffer'.  This
2208 will be the buffer \\[sql-send-region] sends the region to.  If this
2209 SQLi buffer is killed, \\[sql-send-region] is no longer able to
2210 determine where the strings should be sent to.  You can set the
2211 value of `sql-buffer' using \\[sql-set-sqli-buffer].
2212
2213 For information on how to create multiple SQLi buffers, see
2214 `sql-interactive-mode'.
2215
2216 Note that SQL doesn't have an escape character unless you specify
2217 one.  If you specify backslash as escape character in SQL,
2218 you must tell Emacs.  Here's how to do that in your `~/.emacs' file:
2219
2220 \(add-hook 'sql-mode-hook
2221           (lambda ()
2222             (modify-syntax-entry ?\\\\ \".\" sql-mode-syntax-table)))"
2223   (interactive)
2224   (kill-all-local-variables)
2225   (setq major-mode 'sql-mode)
2226   (setq mode-name "SQL")
2227   (use-local-map sql-mode-map)
2228   (if sql-mode-menu
2229       (easy-menu-add sql-mode-menu)); XEmacs
2230   (set-syntax-table sql-mode-syntax-table)
2231   (make-local-variable 'font-lock-defaults)
2232   (make-local-variable 'sql-mode-font-lock-keywords)
2233   (make-local-variable 'comment-start)
2234   (setq comment-start "--")
2235   ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
2236   (make-local-variable 'sql-buffer)
2237   ;; Add imenu support for sql-mode.  Note that imenu-generic-expression
2238   ;; is buffer-local, so we don't need a local-variable for it.  SQL is
2239   ;; case-insensitive, that's why we have to set imenu-case-fold-search.
2240   (setq imenu-generic-expression sql-imenu-generic-expression
2241         imenu-case-fold-search t)
2242   ;; Make `sql-send-paragraph' work on paragraphs that contain indented
2243   ;; lines.
2244   (make-local-variable 'paragraph-separate)
2245   (make-local-variable 'paragraph-start)
2246   (setq paragraph-separate "[\f]*$"
2247         paragraph-start "[\n\f]")
2248   ;; Abbrevs
2249   (setq local-abbrev-table sql-mode-abbrev-table)
2250   (setq abbrev-all-caps 1)
2251   ;; Run hook
2252   (run-hooks 'sql-mode-hook)
2253   ;; Catch changes to sql-product and highlight accordingly
2254   (sql-highlight-product)
2255   (add-hook 'hack-local-variables-hook 'sql-highlight-product t t))
2256
2257 \f
2258
2259 ;;; SQL interactive mode
2260
2261 (put 'sql-interactive-mode 'mode-class 'special)
2262
2263 (defun sql-interactive-mode ()
2264   "Major mode to use a SQL interpreter interactively.
2265
2266 Do not call this function by yourself.  The environment must be
2267 initialized by an entry function specific for the SQL interpreter.  See
2268 `sql-help' for a list of available entry functions.
2269
2270 \\[comint-send-input] after the end of the process' output sends the
2271 text from the end of process to the end of the current line.
2272 \\[comint-send-input] before end of process output copies the current
2273 line minus the prompt to the end of the buffer and sends it.
2274 \\[comint-copy-old-input] just copies the current line.
2275 Use \\[sql-accumulate-and-indent] to enter multi-line statements.
2276
2277 If you want to make multiple SQL buffers, rename the `*SQL*' buffer
2278 using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
2279 See `sql-help' for a list of available entry functions.  The last buffer
2280 created by such an entry function is the current SQLi buffer.  SQL
2281 buffers will send strings to the SQLi buffer current at the time of
2282 their creation.  See `sql-mode' for details.
2283
2284 Sample session using two connections:
2285
2286 1. Create first SQLi buffer by calling an entry function.
2287 2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
2288 3. Create a SQL buffer \"test1.sql\".
2289 4. Create second SQLi buffer by calling an entry function.
2290 5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
2291 6. Create a SQL buffer \"test2.sql\".
2292
2293 Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
2294 buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
2295 will send the region to buffer \"*Connection 2*\".
2296
2297 If you accidentally suspend your process, use \\[comint-continue-subjob]
2298 to continue it.  On some operating systems, this will not work because
2299 the signals are not supported.
2300
2301 \\{sql-interactive-mode-map}
2302 Customization: Entry to this mode runs the hooks on `comint-mode-hook'
2303 and `sql-interactive-mode-hook' (in that order).  Before each input, the
2304 hooks on `comint-input-filter-functions' are run.  After each SQL
2305 interpreter output, the hooks on `comint-output-filter-functions' are
2306 run.
2307
2308 Variable `sql-input-ring-file-name' controls the initialisation of the
2309 input ring history.
2310
2311 Variables `comint-output-filter-functions', a hook, and
2312 `comint-scroll-to-bottom-on-input' and
2313 `comint-scroll-to-bottom-on-output' control whether input and output
2314 cause the window to scroll to the end of the buffer.
2315
2316 If you want to make SQL buffers limited in length, add the function
2317 `comint-truncate-buffer' to `comint-output-filter-functions'.
2318
2319 Here is an example for your .emacs file.  It keeps the SQLi buffer a
2320 certain length.
2321
2322 \(add-hook 'sql-interactive-mode-hook
2323     \(function (lambda ()
2324         \(setq comint-output-filter-functions 'comint-truncate-buffer))))
2325
2326 Here is another example.  It will always put point back to the statement
2327 you entered, right above the output it created.
2328
2329 \(setq comint-output-filter-functions
2330        \(function (lambda (STR) (comint-show-output))))"
2331   (comint-mode)
2332   ;; Get the `sql-product' for this interactive session.
2333   (set (make-local-variable 'sql-product)
2334        (or sql-interactive-product
2335            sql-product))
2336   ;; Setup the mode.
2337   (setq major-mode 'sql-interactive-mode)
2338   (setq mode-name (concat "SQLi[" (prin1-to-string sql-product) "]"))
2339   (use-local-map sql-interactive-mode-map)
2340   (if sql-interactive-mode-menu
2341       (easy-menu-add sql-interactive-mode-menu)) ; XEmacs
2342   (set-syntax-table sql-mode-syntax-table)
2343   (make-local-variable 'sql-mode-font-lock-keywords)
2344   (make-local-variable 'font-lock-defaults)
2345   ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
2346   ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
2347   ;; will have just one quote.  Therefore syntactic hilighting is
2348   ;; disabled for interactive buffers.  No imenu support.
2349   (sql-product-font-lock t nil)
2350   ;; Enable commenting and uncommenting of the region.
2351   (make-local-variable 'comment-start)
2352   (setq comment-start "--")
2353   ;; Abbreviation table init and case-insensitive.  It is not activated
2354   ;; by default.
2355   (setq local-abbrev-table sql-mode-abbrev-table)
2356   (setq abbrev-all-caps 1)
2357   ;; Exiting the process will call sql-stop.
2358   (set-process-sentinel (get-buffer-process sql-buffer) 'sql-stop)
2359   ;; Create a usefull name for renaming this buffer later.
2360   (make-local-variable 'sql-alternate-buffer-name)
2361   (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
2362   ;; User stuff.  Initialize before the hook.
2363   (set (make-local-variable 'sql-prompt-regexp)
2364        (sql-product-feature :sqli-prompt-regexp))
2365   (set (make-local-variable 'sql-prompt-length)
2366        (sql-product-feature :sqli-prompt-length))
2367   (make-local-variable 'sql-input-ring-separator)
2368   (make-local-variable 'sql-input-ring-file-name)
2369   ;; Run hook.
2370   (run-hooks 'sql-interactive-mode-hook)
2371   ;; Set comint based on user overrides.
2372   (setq comint-prompt-regexp sql-prompt-regexp)
2373   (setq left-margin sql-prompt-length)
2374   ;; People wanting a different history file for each
2375   ;; buffer/process/client/whatever can change separator and file-name
2376   ;; on the sql-interactive-mode-hook.
2377   (setq comint-input-ring-separator sql-input-ring-separator
2378         comint-input-ring-file-name sql-input-ring-file-name)
2379   ;; Calling the hook before calling comint-read-input-ring allows users
2380   ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
2381   (comint-read-input-ring t))
2382
2383 (defun sql-stop (process event)
2384   "Called when the SQL process is stopped.
2385
2386 Writes the input history to a history file using
2387 `comint-write-input-ring' and inserts a short message in the SQL buffer.
2388
2389 This function is a sentinel watching the SQL interpreter process.
2390 Sentinels will always get the two parameters PROCESS and EVENT."
2391   (comint-write-input-ring)
2392   (if (and (eq (current-buffer) sql-buffer)
2393            (not buffer-read-only))
2394       (insert (format "\nProcess %s %s\n" process event))
2395     (message "Process %s %s" process event)))
2396
2397 \f
2398
2399 ;;; Entry functions for different SQL interpreters.
2400
2401 ;;;###autoload
2402 (defun sql-product-interactive (&optional product)
2403   "Run product interpreter as an inferior process.
2404
2405 If buffer `*SQL*' exists but no process is running, make a new process.
2406 If buffer exists and a process is running, just switch to buffer
2407 `*SQL*'.
2408
2409 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2410   (interactive)
2411   (setq product (or product sql-product))
2412   (when (sql-product-feature :sqli-connect product)
2413     (if (comint-check-proc "*SQL*")
2414         (pop-to-buffer "*SQL*")
2415       ;; Get credentials.
2416       (apply 'sql-get-login (sql-product-feature :sqli-login product))
2417       ;; Connect to database.
2418       (message "Login...")
2419       (funcall (sql-product-feature :sqli-connect product))
2420       ;; Set SQLi mode.
2421       (setq sql-interactive-product product)
2422       (setq sql-buffer (current-buffer))
2423       (sql-interactive-mode)
2424       ;; All done.
2425       (message "Login...done")
2426       (pop-to-buffer sql-buffer))))
2427
2428 ;;;###autoload
2429 (defun sql-oracle ()
2430   "Run sqlplus by Oracle as an inferior process.
2431
2432 If buffer `*SQL*' exists but no process is running, make a new process.
2433 If buffer exists and a process is running, just switch to buffer
2434 `*SQL*'.
2435
2436 Interpreter used comes from variable `sql-oracle-program'.  Login uses
2437 the variables `sql-user', `sql-password', and `sql-database' as
2438 defaults, if set.  Additional command line parameters can be stored in
2439 the list `sql-oracle-options'.
2440
2441 The buffer is put in sql-interactive-mode, giving commands for sending
2442 input.  See `sql-interactive-mode'.
2443
2444 To specify a coding system for converting non-ASCII characters
2445 in the input and output to the process, use \\[universal-coding-system-argument]
2446 before \\[sql-oracle].  You can also specify this with \\[set-buffer-process-coding-system]
2447 in the SQL buffer, after you start the process.
2448 The default comes from `process-coding-system-alist' and
2449 `default-process-coding-system'.
2450
2451 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2452   (interactive)
2453   (sql-product-interactive 'oracle))
2454
2455 (defun sql-connect-oracle ()
2456   "Create comint buffer and connect to Oracle using the login
2457 parameters and command options."
2458   ;; Produce user/password@database construct.  Password without user
2459   ;; is meaningless; database without user/password is meaningless,
2460   ;; because "@param" will ask sqlplus to interpret the script
2461   ;; "param".
2462   (let ((parameter nil))
2463     (if (not (string= "" sql-user))
2464         (if (not (string= "" sql-password))
2465             (setq parameter (concat sql-user "/" sql-password))
2466           (setq parameter sql-user)))
2467     (if (and parameter (not (string= "" sql-database)))
2468         (setq parameter (concat parameter "@" sql-database)))
2469     (if parameter
2470         (setq parameter (nconc (list parameter) sql-oracle-options))
2471       (setq parameter sql-oracle-options))
2472     (if parameter
2473         (set-buffer (apply 'make-comint "SQL" sql-oracle-program nil
2474                            parameter))
2475       (set-buffer (make-comint "SQL" sql-oracle-program nil)))
2476     ;; SQL*Plus is buffered on WindowsNT; this handles &placeholders.
2477     (if (eq window-system 'w32)
2478         (setq comint-input-sender 'sql-query-placeholders-and-send))))
2479
2480 \f
2481
2482 ;;;###autoload
2483 (defun sql-sybase ()
2484   "Run isql by SyBase as an inferior process.
2485
2486 If buffer `*SQL*' exists but no process is running, make a new process.
2487 If buffer exists and a process is running, just switch to buffer
2488 `*SQL*'.
2489
2490 Interpreter used comes from variable `sql-sybase-program'.  Login uses
2491 the variables `sql-server', `sql-user', `sql-password', and
2492 `sql-database' as defaults, if set.  Additional command line parameters
2493 can be stored in the list `sql-sybase-options'.
2494
2495 The buffer is put in sql-interactive-mode, giving commands for sending
2496 input.  See `sql-interactive-mode'.
2497
2498 To specify a coding system for converting non-ASCII characters
2499 in the input and output to the process, use \\[universal-coding-system-argument]
2500 before \\[sql-sybase].  You can also specify this with \\[set-buffer-process-coding-system]
2501 in the SQL buffer, after you start the process.
2502 The default comes from `process-coding-system-alist' and
2503 `default-process-coding-system'.
2504
2505 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2506   (interactive)
2507   (sql-product-interactive 'sybase))
2508
2509 (defun sql-connect-sybase ()
2510   "Create comint buffer and connect to Sybase using the login
2511 parameters and command options."
2512   ;; Put all parameters to the program (if defined) in a list and call
2513   ;; make-comint.
2514   (let ((params sql-sybase-options))
2515     (if (not (string= "" sql-server))
2516         (setq params (append (list "-S" sql-server) params)))
2517     (if (not (string= "" sql-database))
2518         (setq params (append (list "-D" sql-database) params)))
2519     (if (not (string= "" sql-password))
2520         (setq params (append (list "-P" sql-password) params)))
2521     (if (not (string= "" sql-user))
2522         (setq params (append (list "-U" sql-user) params)))
2523     (set-buffer (apply 'make-comint "SQL" sql-sybase-program
2524                        nil params))))
2525
2526 \f
2527
2528 ;;;###autoload
2529 (defun sql-informix ()
2530   "Run dbaccess by Informix as an inferior process.
2531
2532 If buffer `*SQL*' exists but no process is running, make a new process.
2533 If buffer exists and a process is running, just switch to buffer
2534 `*SQL*'.
2535
2536 Interpreter used comes from variable `sql-informix-program'.  Login uses
2537 the variable `sql-database' as default, if set.
2538
2539 The buffer is put in sql-interactive-mode, giving commands for sending
2540 input.  See `sql-interactive-mode'.
2541
2542 To specify a coding system for converting non-ASCII characters
2543 in the input and output to the process, use \\[universal-coding-system-argument]
2544 before \\[sql-informix].  You can also specify this with \\[set-buffer-process-coding-system]
2545 in the SQL buffer, after you start the process.
2546 The default comes from `process-coding-system-alist' and
2547 `default-process-coding-system'.
2548
2549 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2550   (interactive)
2551   (sql-product-interactive 'informix))
2552
2553 (defun sql-connect-informix ()
2554   "Create comint buffer and connect to Informix using the login
2555 parameters and command options."
2556   ;; username and password are ignored.
2557   (if (string= "" sql-database)
2558       (set-buffer (make-comint "SQL" sql-informix-program nil))
2559     (set-buffer (make-comint "SQL" sql-informix-program nil sql-database "-"))))
2560
2561 \f
2562
2563 ;;;###autoload
2564 (defun sql-sqlite ()
2565   "Run sqlite as an inferior process.
2566
2567 SQLite is free software.
2568
2569 If buffer `*SQL*' exists but no process is running, make a new process.
2570 If buffer exists and a process is running, just switch to buffer
2571 `*SQL*'.
2572
2573 Interpreter used comes from variable `sql-sqlite-program'.  Login uses
2574 the variables `sql-user', `sql-password', `sql-database', and
2575 `sql-server' as defaults, if set.  Additional command line parameters
2576 can be stored in the list `sql-sqlite-options'.
2577
2578 The buffer is put in sql-interactive-mode, giving commands for sending
2579 input.  See `sql-interactive-mode'.
2580
2581 To specify a coding system for converting non-ASCII characters
2582 in the input and output to the process, use \\[universal-coding-system-argument]
2583 before \\[sql-sqlite].  You can also specify this with \\[set-buffer-process-coding-system]
2584 in the SQL buffer, after you start the process.
2585 The default comes from `process-coding-system-alist' and
2586 `default-process-coding-system'.
2587
2588 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2589   (interactive)
2590   (sql-product-interactive 'sqlite))
2591
2592 (defun sql-connect-sqlite ()
2593   "Create comint buffer and connect to SQLite using the login
2594 parameters and command options."
2595   ;; Put all parameters to the program (if defined) in a list and call
2596   ;; make-comint.
2597   (let ((params))
2598     (if (not (string= "" sql-database))
2599         (setq params (append (list sql-database) params)))
2600     (if (not (string= "" sql-server))
2601         (setq params (append (list (concat "--host=" sql-server)) params)))
2602     (if (not (string= "" sql-password))
2603         (setq params (append (list (concat "--password=" sql-password)) params)))
2604     (if (not (string= "" sql-user))
2605         (setq params (append (list (concat "--user=" sql-user)) params)))
2606     (if (not (null sql-sqlite-options))
2607         (setq params (append sql-sqlite-options params)))
2608     (set-buffer (apply 'make-comint "SQL" sql-sqlite-program
2609                        nil params))))
2610
2611 \f
2612
2613 ;;;###autoload
2614 (defun sql-mysql ()
2615   "Run mysql by TcX as an inferior process.
2616
2617 Mysql versions 3.23 and up are free software.
2618
2619 If buffer `*SQL*' exists but no process is running, make a new process.
2620 If buffer exists and a process is running, just switch to buffer
2621 `*SQL*'.
2622
2623 Interpreter used comes from variable `sql-mysql-program'.  Login uses
2624 the variables `sql-user', `sql-password', `sql-database', and
2625 `sql-server' as defaults, if set.  Additional command line parameters
2626 can be stored in the list `sql-mysql-options'.
2627
2628 The buffer is put in sql-interactive-mode, giving commands for sending
2629 input.  See `sql-interactive-mode'.
2630
2631 To specify a coding system for converting non-ASCII characters
2632 in the input and output to the process, use \\[universal-coding-system-argument]
2633 before \\[sql-mysql].  You can also specify this with \\[set-buffer-process-coding-system]
2634 in the SQL buffer, after you start the process.
2635 The default comes from `process-coding-system-alist' and
2636 `default-process-coding-system'.
2637
2638 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2639   (interactive)
2640   (sql-product-interactive 'mysql))
2641
2642 (defun sql-connect-mysql ()
2643   "Create comint buffer and connect to MySQL using the login
2644 parameters and command options."
2645   ;; Put all parameters to the program (if defined) in a list and call
2646   ;; make-comint.
2647   (let ((params))
2648     (if (not (string= "" sql-database))
2649         (setq params (append (list sql-database) params)))
2650     (if (not (string= "" sql-server))
2651         (setq params (append (list (concat "--host=" sql-server)) params)))
2652     (if (not (string= "" sql-password))
2653         (setq params (append (list (concat "--password=" sql-password)) params)))
2654     (if (not (string= "" sql-user))
2655         (setq params (append (list (concat "--user=" sql-user)) params)))
2656     (if (not (null sql-mysql-options))
2657         (setq params (append sql-mysql-options params)))
2658     (set-buffer (apply 'make-comint "SQL" sql-mysql-program
2659                        nil params))))
2660
2661 \f
2662
2663 ;;;###autoload
2664 (defun sql-solid ()
2665   "Run solsql by Solid as an inferior process.
2666
2667 If buffer `*SQL*' exists but no process is running, make a new process.
2668 If buffer exists and a process is running, just switch to buffer
2669 `*SQL*'.
2670
2671 Interpreter used comes from variable `sql-solid-program'.  Login uses
2672 the variables `sql-user', `sql-password', and `sql-server' as
2673 defaults, if set.
2674
2675 The buffer is put in sql-interactive-mode, giving commands for sending
2676 input.  See `sql-interactive-mode'.
2677
2678 To specify a coding system for converting non-ASCII characters
2679 in the input and output to the process, use \\[universal-coding-system-argument]
2680 before \\[sql-solid].  You can also specify this with \\[set-buffer-process-coding-system]
2681 in the SQL buffer, after you start the process.
2682 The default comes from `process-coding-system-alist' and
2683 `default-process-coding-system'.
2684
2685 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2686   (interactive)
2687   (sql-product-interactive 'solid))
2688
2689 (defun sql-connect-solid ()
2690   "Create comint buffer and connect to Solid using the login
2691 parameters and command options."
2692   ;; Put all parameters to the program (if defined) in a list and call
2693   ;; make-comint.
2694   (let ((params))
2695     ;; It only makes sense if both username and password are there.
2696     (if (not (or (string= "" sql-user)
2697                  (string= "" sql-password)))
2698         (setq params (append (list sql-user sql-password) params)))
2699     (if (not (string= "" sql-server))
2700         (setq params (append (list sql-server) params)))
2701     (set-buffer (apply 'make-comint "SQL" sql-solid-program
2702                        nil params))))
2703
2704 \f
2705
2706 ;;;###autoload
2707 (defun sql-ingres ()
2708   "Run sql by Ingres as an inferior process.
2709
2710 If buffer `*SQL*' exists but no process is running, make a new process.
2711 If buffer exists and a process is running, just switch to buffer
2712 `*SQL*'.
2713
2714 Interpreter used comes from variable `sql-ingres-program'.  Login uses
2715 the variable `sql-database' as default, if set.
2716
2717 The buffer is put in sql-interactive-mode, giving commands for sending
2718 input.  See `sql-interactive-mode'.
2719
2720 To specify a coding system for converting non-ASCII characters
2721 in the input and output to the process, use \\[universal-coding-system-argument]
2722 before \\[sql-ingres].  You can also specify this with \\[set-buffer-process-coding-system]
2723 in the SQL buffer, after you start the process.
2724 The default comes from `process-coding-system-alist' and
2725 `default-process-coding-system'.
2726
2727 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2728   (interactive)
2729   (sql-product-interactive 'ingres))
2730
2731 (defun sql-connect-ingres ()
2732   "Create comint buffer and connect to Ingres using the login
2733 parameters and command options."
2734   ;; username and password are ignored.
2735   (if (string= "" sql-database)
2736       (set-buffer (make-comint "SQL" sql-ingres-program nil))
2737     (set-buffer (make-comint "SQL" sql-ingres-program nil sql-database))))
2738
2739 \f
2740
2741 ;;;###autoload
2742 (defun sql-ms ()
2743   "Run osql by Microsoft as an inferior process.
2744
2745 If buffer `*SQL*' exists but no process is running, make a new process.
2746 If buffer exists and a process is running, just switch to buffer
2747 `*SQL*'.
2748
2749 Interpreter used comes from variable `sql-ms-program'.  Login uses the
2750 variables `sql-user', `sql-password', `sql-database', and `sql-server'
2751 as defaults, if set.  Additional command line parameters can be stored
2752 in the list `sql-ms-options'.
2753
2754 The buffer is put in sql-interactive-mode, giving commands for sending
2755 input.  See `sql-interactive-mode'.
2756
2757 To specify a coding system for converting non-ASCII characters
2758 in the input and output to the process, use \\[universal-coding-system-argument]
2759 before \\[sql-ms].  You can also specify this with \\[set-buffer-process-coding-system]
2760 in the SQL buffer, after you start the process.
2761 The default comes from `process-coding-system-alist' and
2762 `default-process-coding-system'.
2763
2764 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2765   (interactive)
2766   (sql-product-interactive 'ms))
2767
2768 (defun sql-connect-ms ()
2769   "Create comint buffer and connect to Microsoft using the login
2770 parameters and command options."
2771   ;; Put all parameters to the program (if defined) in a list and call
2772   ;; make-comint.
2773   (let ((params sql-ms-options))
2774     (if (not (string= "" sql-server))
2775         (setq params (append (list "-S" sql-server) params)))
2776     (if (not (string= "" sql-database))
2777         (setq params (append (list "-d" sql-database) params)))
2778     (if (not (string= "" sql-user))
2779         (setq params (append (list "-U" sql-user) params)))
2780     (if (not (string= "" sql-password))
2781         (setq params (append (list "-P" sql-password) params))
2782       (if (string= "" sql-user)
2783           ;; if neither user nor password is provided, use system
2784           ;; credentials.
2785           (setq params (append (list "-E") params))
2786         ;; If -P is passed to ISQL as the last argument without a
2787         ;; password, it's considered null.
2788         (setq params (append params (list "-P")))))
2789     (set-buffer (apply 'make-comint "SQL" sql-ms-program
2790                        nil params))))
2791
2792 \f
2793
2794 ;;;###autoload
2795 (defun sql-postgres ()
2796   "Run psql by Postgres as an inferior process.
2797
2798 If buffer `*SQL*' exists but no process is running, make a new process.
2799 If buffer exists and a process is running, just switch to buffer
2800 `*SQL*'.
2801
2802 Interpreter used comes from variable `sql-postgres-program'.  Login uses
2803 the variables `sql-database' and `sql-server' as default, if set.
2804 Additional command line parameters can be stored in the list
2805 `sql-postgres-options'.
2806
2807 The buffer is put in sql-interactive-mode, giving commands for sending
2808 input.  See `sql-interactive-mode'.
2809
2810 To specify a coding system for converting non-ASCII characters
2811 in the input and output to the process, use \\[universal-coding-system-argument]
2812 before \\[sql-postgres].  You can also specify this with \\[set-buffer-process-coding-system]
2813 in the SQL buffer, after you start the process.
2814 The default comes from `process-coding-system-alist' and
2815 `default-process-coding-system'.  If your output lines end with ^M,
2816 your might try undecided-dos as a coding system.  If this doesn't help,
2817 Try to set `comint-output-filter-functions' like this:
2818
2819 \(setq comint-output-filter-functions (append comint-output-filter-functions
2820                                              '(comint-strip-ctrl-m)))
2821
2822 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2823   (interactive)
2824   (sql-product-interactive 'postgres))
2825
2826 (defun sql-connect-postgres ()
2827   "Create comint buffer and connect to Postgres using the login
2828 parameters and command options."
2829   ;; username and password are ignored.  Mark Stosberg suggest to add
2830   ;; the database at the end.  Jason Beegan suggest using --pset and
2831   ;; pager=off instead of \\o|cat.  The later was the solution by
2832   ;; Gregor Zych.  Jason's suggestion is the default value for
2833   ;; sql-postgres-options.
2834   (let ((params sql-postgres-options))
2835     (if (not (string= "" sql-database))
2836         (setq params (append params (list sql-database))))
2837     (if (not (string= "" sql-server))
2838         (setq params (append (list "-h" sql-server) params)))
2839     (if (not (string= "" sql-user))
2840         (setq params (append (list "-U" sql-user) params)))
2841     (set-buffer (apply 'make-comint "SQL" sql-postgres-program
2842                        nil params))))
2843
2844 \f
2845
2846 ;;;###autoload
2847 (defun sql-interbase ()
2848   "Run isql by Interbase as an inferior process.
2849
2850 If buffer `*SQL*' exists but no process is running, make a new process.
2851 If buffer exists and a process is running, just switch to buffer
2852 `*SQL*'.
2853
2854 Interpreter used comes from variable `sql-interbase-program'.  Login
2855 uses the variables `sql-user', `sql-password', and `sql-database' as
2856 defaults, if set.
2857
2858 The buffer is put in sql-interactive-mode, giving commands for sending
2859 input.  See `sql-interactive-mode'.
2860
2861 To specify a coding system for converting non-ASCII characters
2862 in the input and output to the process, use \\[universal-coding-system-argument]
2863 before \\[sql-interbase].  You can also specify this with \\[set-buffer-process-coding-system]
2864 in the SQL buffer, after you start the process.
2865 The default comes from `process-coding-system-alist' and
2866 `default-process-coding-system'.
2867
2868 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2869   (interactive)
2870   (sql-product-interactive 'interbase))
2871
2872 (defun sql-connect-interbase ()
2873   "Create comint buffer and connect to Interbase using the login
2874 parameters and command options."
2875   ;; Put all parameters to the program (if defined) in a list and call
2876   ;; make-comint.
2877   (let ((params sql-interbase-options))
2878     (if (not (string= "" sql-user))
2879         (setq params (append (list "-u" sql-user) params)))
2880     (if (not (string= "" sql-password))
2881         (setq params (append (list "-p" sql-password) params)))
2882     (if (not (string= "" sql-database))
2883         (setq params (cons sql-database params))) ; add to the front!
2884     (set-buffer (apply 'make-comint "SQL" sql-interbase-program
2885                        nil params))))
2886
2887 \f
2888
2889 ;;;###autoload
2890 (defun sql-db2 ()
2891   "Run db2 by IBM as an inferior process.
2892
2893 If buffer `*SQL*' exists but no process is running, make a new process.
2894 If buffer exists and a process is running, just switch to buffer
2895 `*SQL*'.
2896
2897 Interpreter used comes from variable `sql-db2-program'.  There is not
2898 automatic login.
2899
2900 The buffer is put in sql-interactive-mode, giving commands for sending
2901 input.  See `sql-interactive-mode'.
2902
2903 If you use \\[sql-accumulate-and-indent] to send multiline commands to
2904 db2, newlines will be escaped if necessary.  If you don't want that, set
2905 `comint-input-sender' back to `comint-simple-send' by writing an after
2906 advice.  See the elisp manual for more information.
2907
2908 To specify a coding system for converting non-ASCII characters
2909 in the input and output to the process, use \\[universal-coding-system-argument]
2910 before \\[sql-db2].  You can also specify this with \\[set-buffer-process-coding-system]
2911 in the SQL buffer, after you start the process.
2912 The default comes from `process-coding-system-alist' and
2913 `default-process-coding-system'.
2914
2915 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2916   (interactive)
2917   (sql-product-interactive 'db2))
2918
2919 (defun sql-connect-db2 ()
2920   "Create comint buffer and connect to DB2 using the login
2921 parameters and command options."
2922   ;; Put all parameters to the program (if defined) in a list and call
2923   ;; make-comint.
2924   (set-buffer (apply 'make-comint "SQL" sql-db2-program
2925                      nil sql-db2-options))
2926   ;; Properly escape newlines when DB2 is interactive.
2927   (setq comint-input-sender 'sql-escape-newlines-and-send))
2928
2929 ;;;###autoload
2930 (defun sql-linter ()
2931   "Run inl by RELEX as an inferior process.
2932
2933 If buffer `*SQL*' exists but no process is running, make a new process.
2934 If buffer exists and a process is running, just switch to buffer
2935 `*SQL*'.
2936
2937 Interpreter used comes from variable `sql-linter-program' - usually `inl'.
2938 Login uses the variables `sql-user', `sql-password', `sql-database' and
2939 `sql-server' as defaults, if set.  Additional command line parameters
2940 can be stored in the list `sql-linter-options'. Run inl -h to get help on
2941 parameters.
2942
2943 `sql-database' is used to set the LINTER_MBX environment variable for
2944 local connections, `sql-server' refers to the server name from the
2945 `nodetab' file for the network connection (dbc_tcp or friends must run
2946 for this to work).  If `sql-password' is an empty string, inl will use
2947 an empty password.
2948
2949 The buffer is put in sql-interactive-mode, giving commands for sending
2950 input.  See `sql-interactive-mode'.
2951
2952 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2953   (interactive)
2954   (sql-product-interactive 'linter))
2955
2956 (defun sql-connect-linter ()
2957   "Create comint buffer and connect to Linter using the login
2958 parameters and command options."
2959   ;; Put all parameters to the program (if defined) in a list and call
2960   ;; make-comint.
2961   (let ((params sql-linter-options) (login nil) (old-mbx (getenv "LINTER_MBX")))
2962     (if (not (string= "" sql-user))
2963         (setq login (concat sql-user "/" sql-password)))
2964     (setq params (append (list "-u" login) params))
2965     (if (not (string= "" sql-server))
2966         (setq params (append (list "-n" sql-server) params)))
2967     (if (string= "" sql-database)
2968         (setenv "LINTER_MBX" nil)
2969       (setenv "LINTER_MBX" sql-database))
2970     (set-buffer (apply 'make-comint "SQL" sql-linter-program nil
2971                        params))
2972     (setenv "LINTER_MBX" old-mbx)))
2973
2974 \f
2975
2976 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.sql$" . sql-mode))
2977 (provide 'sql)
2978
2979 ;;; sql.el ends here