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