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