]> code.delx.au - gnu-emacs/blob - lisp/progmodes/sql.el
Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[gnu-emacs] / lisp / progmodes / sql.el
1 ;;; sql.el --- specialized comint.el for SQL interpreters -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
4
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Michael Mauger <mmaug@yahoo.com>
7 ;; Version: 3.1
8 ;; Keywords: comm languages processes
9 ;; URL: http://savannah.gnu.org/projects/emacs/
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Please send bug reports and bug fixes to the mailing list at
29 ;; help-gnu-emacs@gnu.org. If you want to subscribe to the mailing
30 ;; list, see the web page at
31 ;; http://lists.gnu.org/mailman/listinfo/help-gnu-emacs for
32 ;; instructions. I monitor this list actively. If you send an e-mail
33 ;; to Alex Schroeder it usually makes it to me when Alex has a chance
34 ;; to forward them along (Thanks, Alex).
35
36 ;; This file provides a sql-mode and a sql-interactive-mode. The
37 ;; original goals were two simple modes providing syntactic
38 ;; highlighting. The interactive mode had to provide a command-line
39 ;; history; the other mode had to provide "send region/buffer to SQL
40 ;; interpreter" functions. "simple" in this context means easy to
41 ;; use, easy to maintain and little or no bells and whistles. This
42 ;; has changed somewhat as experience with the mode has accumulated.
43
44 ;; Support for different flavors of SQL and command interpreters was
45 ;; available in early versions of sql.el. This support has been
46 ;; extended and formalized in later versions. Part of the impetus for
47 ;; the improved support of SQL flavors was borne out of the current
48 ;; maintainers consulting experience. In the past twenty years, I
49 ;; have used Oracle, Sybase, Informix, MySQL, Postgres, and SQLServer.
50 ;; On some assignments, I have used two or more of these concurrently.
51
52 ;; If anybody feels like extending this sql mode, take a look at the
53 ;; above mentioned modes and write a sqlx-mode on top of this one. If
54 ;; this proves to be difficult, please suggest changes that will
55 ;; facilitate your plans. Facilities have been provided to add
56 ;; products and product-specific configuration.
57
58 ;; sql-interactive-mode is used to interact with a SQL interpreter
59 ;; process in a SQLi buffer (usually called `*SQL*'). The SQLi buffer
60 ;; is created by calling a SQL interpreter-specific entry function or
61 ;; sql-product-interactive. Do *not* call sql-interactive-mode by
62 ;; itself.
63
64 ;; The list of currently supported interpreters and the corresponding
65 ;; entry function used to create the SQLi buffers is shown with
66 ;; `sql-help' (M-x sql-help).
67
68 ;; Since sql-interactive-mode is built on top of the general
69 ;; command-interpreter-in-a-buffer mode (comint mode), it shares a
70 ;; common base functionality, and a common set of bindings, with all
71 ;; modes derived from comint mode. This makes these modes easier to
72 ;; use.
73
74 ;; sql-mode can be used to keep editing SQL statements. The SQL
75 ;; statements can be sent to the SQL process in the SQLi buffer.
76
77 ;; For documentation on the functionality provided by comint mode, and
78 ;; the hooks available for customizing it, see the file `comint.el'.
79
80 ;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
81 ;; `imenu-add-menubar-index'.
82
83 ;;; Bugs:
84
85 ;; sql-ms now uses osql instead of isql. Osql flushes its error
86 ;; stream more frequently than isql so that error messages are
87 ;; available. There is no prompt and some output still is buffered.
88 ;; This improves the interaction under Emacs but it still is somewhat
89 ;; awkward.
90
91 ;; Quoted identifiers are not supported for highlighting. Most
92 ;; databases support the use of double quoted strings in place of
93 ;; identifiers; ms (Microsoft SQLServer) also supports identifiers
94 ;; enclosed within brackets [].
95
96 ;;; Product Support:
97
98 ;; To add support for additional SQL products the following steps
99 ;; must be followed ("xyz" is the name of the product in the examples
100 ;; below):
101
102 ;; 1) Add the product to the list of known products.
103
104 ;; (sql-add-product 'xyz "XyzDB"
105 ;; '(:free-software t))
106
107 ;; 2) Define font lock settings. All ANSI keywords will be
108 ;; highlighted automatically, so only product specific keywords
109 ;; need to be defined here.
110
111 ;; (defvar my-sql-mode-xyz-font-lock-keywords
112 ;; '(("\\b\\(red\\|orange\\|yellow\\)\\b"
113 ;; . font-lock-keyword-face))
114 ;; "XyzDB SQL keywords used by font-lock.")
115
116 ;; (sql-set-product-feature 'xyz
117 ;; :font-lock
118 ;; 'my-sql-mode-xyz-font-lock-keywords)
119
120 ;; 3) Define any special syntax characters including comments and
121 ;; identifier characters.
122
123 ;; (sql-set-product-feature 'xyz
124 ;; :syntax-alist ((?# . "_")))
125
126 ;; 4) Define the interactive command interpreter for the database
127 ;; product.
128
129 ;; (defcustom my-sql-xyz-program "ixyz"
130 ;; "Command to start ixyz by XyzDB."
131 ;; :type 'file
132 ;; :group 'SQL)
133 ;;
134 ;; (sql-set-product-feature 'xyz
135 ;; :sqli-program 'my-sql-xyz-program)
136 ;; (sql-set-product-feature 'xyz
137 ;; :prompt-regexp "^xyzdb> ")
138 ;; (sql-set-product-feature 'xyz
139 ;; :prompt-length 7)
140
141 ;; 5) Define login parameters and command line formatting.
142
143 ;; (defcustom my-sql-xyz-login-params '(user password server database)
144 ;; "Login parameters to needed to connect to XyzDB."
145 ;; :type 'sql-login-params
146 ;; :group 'SQL)
147 ;;
148 ;; (sql-set-product-feature 'xyz
149 ;; :sqli-login 'my-sql-xyz-login-params)
150
151 ;; (defcustom my-sql-xyz-options '("-X" "-Y" "-Z")
152 ;; "List of additional options for `sql-xyz-program'."
153 ;; :type '(repeat string)
154 ;; :group 'SQL)
155 ;;
156 ;; (sql-set-product-feature 'xyz
157 ;; :sqli-options 'my-sql-xyz-options))
158
159 ;; (defun my-sql-comint-xyz (product options)
160 ;; "Connect ti XyzDB in a comint buffer."
161 ;;
162 ;; ;; Do something with `sql-user', `sql-password',
163 ;; ;; `sql-database', and `sql-server'.
164 ;; (let ((params
165 ;; (append
166 ;; (if (not (string= "" sql-user))
167 ;; (list "-U" sql-user))
168 ;; (if (not (string= "" sql-password))
169 ;; (list "-P" sql-password))
170 ;; (if (not (string= "" sql-database))
171 ;; (list "-D" sql-database))
172 ;; (if (not (string= "" sql-server))
173 ;; (list "-S" sql-server))
174 ;; options)))
175 ;; (sql-comint product params)))
176 ;;
177 ;; (sql-set-product-feature 'xyz
178 ;; :sqli-comint-func 'my-sql-comint-xyz)
179
180 ;; 6) Define a convenience function to invoke the SQL interpreter.
181
182 ;; (defun my-sql-xyz (&optional buffer)
183 ;; "Run ixyz by XyzDB as an inferior process."
184 ;; (interactive "P")
185 ;; (sql-product-interactive 'xyz buffer))
186
187 ;;; To Do:
188
189 ;; Improve keyword highlighting for individual products. I have tried
190 ;; to update those database that I use. Feel free to send me updates,
191 ;; or direct me to the reference manuals for your favorite database.
192
193 ;; When there are no keywords defined, the ANSI keywords are
194 ;; highlighted. ANSI keywords are highlighted even if the keyword is
195 ;; not used for your current product. This should help identify
196 ;; portability concerns.
197
198 ;; Add different highlighting levels.
199
200 ;; Add support for listing available tables or the columns in a table.
201
202 ;;; Thanks to all the people who helped me out:
203
204 ;; Alex Schroeder <alex@gnu.org> -- the original author
205 ;; Kai Blauberg <kai.blauberg@metla.fi>
206 ;; <ibalaban@dalet.com>
207 ;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
208 ;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
209 ;; nino <nino@inform.dk>
210 ;; Berend de Boer <berend@pobox.com>
211 ;; Adam Jenkins <adam@thejenkins.org>
212 ;; Michael Mauger <mmaug@yahoo.com> -- improved product support
213 ;; Drew Adams <drew.adams@oracle.com> -- Emacs 20 support
214 ;; Harald Maier <maierh@myself.com> -- sql-send-string
215 ;; Stefan Monnier <monnier@iro.umontreal.ca> -- font-lock corrections;
216 ;; code polish
217 ;; Paul Sleigh <bat@flurf.net> -- MySQL keyword enhancement
218 ;; Andrew Schein <andrew@andrewschein.com> -- sql-port bug
219 ;; Ian Bjorhovde <idbjorh@dataproxy.com> -- db2 escape newlines
220 ;; incorrectly enabled by default
221
222 \f
223
224 ;;; Code:
225
226 (require 'cl-lib)
227 (require 'comint)
228 ;; Need the following to allow GNU Emacs 19 to compile the file.
229 (eval-when-compile
230 (require 'regexp-opt))
231 (require 'custom)
232 (require 'thingatpt)
233
234 (defvar font-lock-keyword-face)
235 (defvar font-lock-set-defaults)
236 (defvar font-lock-string-face)
237
238 ;;; Allow customization
239
240 (defgroup SQL nil
241 "Running a SQL interpreter from within Emacs buffers."
242 :version "20.4"
243 :group 'languages
244 :group 'processes)
245
246 ;; These four variables will be used as defaults, if set.
247
248 (defcustom sql-user ""
249 "Default username."
250 :type 'string
251 :group 'SQL
252 :safe 'stringp)
253
254 (defcustom sql-password ""
255 "Default password.
256 If you customize this, the value will be stored in your init
257 file. Since that is a plaintext file, this could be dangerous."
258 :type 'string
259 :group 'SQL
260 :risky t)
261
262 (defcustom sql-database ""
263 "Default database."
264 :type 'string
265 :group 'SQL
266 :safe 'stringp)
267
268 (defcustom sql-server ""
269 "Default server or host."
270 :type 'string
271 :group 'SQL
272 :safe 'stringp)
273
274 (defcustom sql-port 0
275 "Default port for connecting to a MySQL or Postgres server."
276 :version "24.1"
277 :type 'number
278 :group 'SQL
279 :safe 'numberp)
280
281 ;; Login parameter type
282
283 (define-widget 'sql-login-params 'lazy
284 "Widget definition of the login parameters list"
285 ;; FIXME: does not implement :default property for the user,
286 ;; database and server options. Anybody have some guidance on how to
287 ;; do this.
288 :tag "Login Parameters"
289 :type '(repeat (choice
290 (const user)
291 (const password)
292 (choice :tag "server"
293 (const server)
294 (list :tag "file"
295 (const :format "" server)
296 (const :format "" :file)
297 regexp)
298 (list :tag "completion"
299 (const :format "" server)
300 (const :format "" :completion)
301 (restricted-sexp
302 :match-alternatives (listp stringp))))
303 (choice :tag "database"
304 (const database)
305 (list :tag "file"
306 (const :format "" database)
307 (const :format "" :file)
308 regexp)
309 (list :tag "completion"
310 (const :format "" database)
311 (const :format "" :completion)
312 (restricted-sexp
313 :match-alternatives (listp stringp))))
314 (const port))))
315
316 ;; SQL Product support
317
318 (defvar sql-interactive-product nil
319 "Product under `sql-interactive-mode'.")
320
321 (defvar sql-connection nil
322 "Connection name if interactive session started by `sql-connect'.")
323
324 (defvar sql-product-alist
325 '((ansi
326 :name "ANSI"
327 :font-lock sql-mode-ansi-font-lock-keywords
328 :statement sql-ansi-statement-starters)
329
330 (db2
331 :name "DB2"
332 :font-lock sql-mode-db2-font-lock-keywords
333 :sqli-program sql-db2-program
334 :sqli-options sql-db2-options
335 :sqli-login sql-db2-login-params
336 :sqli-comint-func sql-comint-db2
337 :prompt-regexp "^db2 => "
338 :prompt-length 7
339 :prompt-cont-regexp "^db2 (cont\.) => "
340 :input-filter sql-escape-newlines-filter)
341
342 (informix
343 :name "Informix"
344 :font-lock sql-mode-informix-font-lock-keywords
345 :sqli-program sql-informix-program
346 :sqli-options sql-informix-options
347 :sqli-login sql-informix-login-params
348 :sqli-comint-func sql-comint-informix
349 :prompt-regexp "^> "
350 :prompt-length 2
351 :syntax-alist ((?{ . "<") (?} . ">")))
352
353 (ingres
354 :name "Ingres"
355 :font-lock sql-mode-ingres-font-lock-keywords
356 :sqli-program sql-ingres-program
357 :sqli-options sql-ingres-options
358 :sqli-login sql-ingres-login-params
359 :sqli-comint-func sql-comint-ingres
360 :prompt-regexp "^\* "
361 :prompt-length 2
362 :prompt-cont-regexp "^\* ")
363
364 (interbase
365 :name "Interbase"
366 :font-lock sql-mode-interbase-font-lock-keywords
367 :sqli-program sql-interbase-program
368 :sqli-options sql-interbase-options
369 :sqli-login sql-interbase-login-params
370 :sqli-comint-func sql-comint-interbase
371 :prompt-regexp "^SQL> "
372 :prompt-length 5)
373
374 (linter
375 :name "Linter"
376 :font-lock sql-mode-linter-font-lock-keywords
377 :sqli-program sql-linter-program
378 :sqli-options sql-linter-options
379 :sqli-login sql-linter-login-params
380 :sqli-comint-func sql-comint-linter
381 :prompt-regexp "^SQL>"
382 :prompt-length 4)
383
384 (ms
385 :name "Microsoft"
386 :font-lock sql-mode-ms-font-lock-keywords
387 :sqli-program sql-ms-program
388 :sqli-options sql-ms-options
389 :sqli-login sql-ms-login-params
390 :sqli-comint-func sql-comint-ms
391 :prompt-regexp "^[0-9]*>"
392 :prompt-length 5
393 :syntax-alist ((?@ . "_"))
394 :terminator ("^go" . "go"))
395
396 (mysql
397 :name "MySQL"
398 :free-software t
399 :font-lock sql-mode-mysql-font-lock-keywords
400 :sqli-program sql-mysql-program
401 :sqli-options sql-mysql-options
402 :sqli-login sql-mysql-login-params
403 :sqli-comint-func sql-comint-mysql
404 :list-all "SHOW TABLES;"
405 :list-table "DESCRIBE %s;"
406 :prompt-regexp "^mysql> "
407 :prompt-length 6
408 :prompt-cont-regexp "^ -> "
409 :syntax-alist ((?# . "< b"))
410 :input-filter sql-remove-tabs-filter)
411
412 (oracle
413 :name "Oracle"
414 :font-lock sql-mode-oracle-font-lock-keywords
415 :sqli-program sql-oracle-program
416 :sqli-options sql-oracle-options
417 :sqli-login sql-oracle-login-params
418 :sqli-comint-func sql-comint-oracle
419 :list-all sql-oracle-list-all
420 :list-table sql-oracle-list-table
421 :completion-object sql-oracle-completion-object
422 :prompt-regexp "^SQL> "
423 :prompt-length 5
424 :prompt-cont-regexp "^\\s-*[[:digit:]]+ "
425 :statement sql-oracle-statement-starters
426 :syntax-alist ((?$ . "_") (?# . "_"))
427 :terminator ("\\(^/\\|;\\)$" . "/")
428 :input-filter sql-placeholders-filter)
429
430 (postgres
431 :name "Postgres"
432 :free-software t
433 :font-lock sql-mode-postgres-font-lock-keywords
434 :sqli-program sql-postgres-program
435 :sqli-options sql-postgres-options
436 :sqli-login sql-postgres-login-params
437 :sqli-comint-func sql-comint-postgres
438 :list-all ("\\d+" . "\\dS+")
439 :list-table ("\\d+ %s" . "\\dS+ %s")
440 :completion-object sql-postgres-completion-object
441 :prompt-regexp "^\\w*=[#>] "
442 :prompt-length 5
443 :prompt-cont-regexp "^\\w*[-(][#>] "
444 :input-filter sql-remove-tabs-filter
445 :terminator ("\\(^\\s-*\\\\g$\\|;\\)" . "\\g"))
446
447 (solid
448 :name "Solid"
449 :font-lock sql-mode-solid-font-lock-keywords
450 :sqli-program sql-solid-program
451 :sqli-options sql-solid-options
452 :sqli-login sql-solid-login-params
453 :sqli-comint-func sql-comint-solid
454 :prompt-regexp "^"
455 :prompt-length 0)
456
457 (sqlite
458 :name "SQLite"
459 :free-software t
460 :font-lock sql-mode-sqlite-font-lock-keywords
461 :sqli-program sql-sqlite-program
462 :sqli-options sql-sqlite-options
463 :sqli-login sql-sqlite-login-params
464 :sqli-comint-func sql-comint-sqlite
465 :list-all ".tables"
466 :list-table ".schema %s"
467 :completion-object sql-sqlite-completion-object
468 :prompt-regexp "^sqlite> "
469 :prompt-length 8
470 :prompt-cont-regexp "^ \.\.\.> "
471 :terminator ";")
472
473 (sybase
474 :name "Sybase"
475 :font-lock sql-mode-sybase-font-lock-keywords
476 :sqli-program sql-sybase-program
477 :sqli-options sql-sybase-options
478 :sqli-login sql-sybase-login-params
479 :sqli-comint-func sql-comint-sybase
480 :prompt-regexp "^SQL> "
481 :prompt-length 5
482 :syntax-alist ((?@ . "_"))
483 :terminator ("^go" . "go"))
484 )
485 "An alist of product specific configuration settings.
486
487 Without an entry in this list a product will not be properly
488 highlighted and will not support `sql-interactive-mode'.
489
490 Each element in the list is in the following format:
491
492 \(PRODUCT FEATURE VALUE ...)
493
494 where PRODUCT is the appropriate value of `sql-product'. The
495 product name is then followed by FEATURE-VALUE pairs. If a
496 FEATURE is not specified, its VALUE is treated as nil. FEATURE
497 may be any one of the following:
498
499 :name string containing the displayable name of
500 the product.
501
502 :free-software is the product Free (as in Freedom) software?
503
504 :font-lock name of the variable containing the product
505 specific font lock highlighting patterns.
506
507 :sqli-program name of the variable containing the product
508 specific interactive program name.
509
510 :sqli-options name of the variable containing the list
511 of product specific options.
512
513 :sqli-login name of the variable containing the list of
514 login parameters (i.e., user, password,
515 database and server) needed to connect to
516 the database.
517
518 :sqli-comint-func name of a function which accepts no
519 parameters that will use the values of
520 `sql-user', `sql-password',
521 `sql-database', `sql-server' and
522 `sql-port' to open a comint buffer and
523 connect to the database. Do product
524 specific configuration of comint in this
525 function.
526
527 :list-all Command string or function which produces
528 a listing of all objects in the database.
529 If it's a cons cell, then the car
530 produces the standard list of objects and
531 the cdr produces an enhanced list of
532 objects. What \"enhanced\" means is
533 dependent on the SQL product and may not
534 exist. In general though, the
535 \"enhanced\" list should include visible
536 objects from other schemas.
537
538 :list-table Command string or function which produces
539 a detailed listing of a specific database
540 table. If its a cons cell, then the car
541 produces the standard list and the cdr
542 produces an enhanced list.
543
544 :completion-object A function that returns a list of
545 objects. Called with a single
546 parameter--if nil then list objects
547 accessible in the current schema, if
548 not-nil it is the name of a schema whose
549 objects should be listed.
550
551 :completion-column A function that returns a list of
552 columns. Called with a single
553 parameter--if nil then list objects
554 accessible in the current schema, if
555 not-nil it is the name of a schema whose
556 objects should be listed.
557
558 :prompt-regexp regular expression string that matches
559 the prompt issued by the product
560 interpreter.
561
562 :prompt-length length of the prompt on the line.
563
564 :prompt-cont-regexp regular expression string that matches
565 the continuation prompt issued by the
566 product interpreter.
567
568 :input-filter function which can filter strings sent to
569 the command interpreter. It is also used
570 by the `sql-send-string',
571 `sql-send-region', `sql-send-paragraph'
572 and `sql-send-buffer' functions. The
573 function is passed the string sent to the
574 command interpreter and must return the
575 filtered string. May also be a list of
576 such functions.
577
578 :statement name of a variable containing a regexp that
579 matches the beginning of SQL statements.
580
581 :terminator the terminator to be sent after a
582 `sql-send-string', `sql-send-region',
583 `sql-send-paragraph' and
584 `sql-send-buffer' command. May be the
585 literal string or a cons of a regexp to
586 match an existing terminator in the
587 string and the terminator to be used if
588 its absent. By default \";\".
589
590 :syntax-alist alist of syntax table entries to enable
591 special character treatment by font-lock
592 and imenu.
593
594 Other features can be stored but they will be ignored. However,
595 you can develop new functionality which is product independent by
596 using `sql-get-product-feature' to lookup the product specific
597 settings.")
598
599 (defvar sql-indirect-features
600 '(:font-lock :sqli-program :sqli-options :sqli-login :statement))
601
602 (defcustom sql-connection-alist nil
603 "An alist of connection parameters for interacting with a SQL product.
604 Each element of the alist is as follows:
605
606 \(CONNECTION \(SQL-VARIABLE VALUE) ...)
607
608 Where CONNECTION is a symbol identifying the connection, SQL-VARIABLE
609 is the symbol name of a SQL mode variable, and VALUE is the value to
610 be assigned to the variable. The most common SQL-VARIABLE settings
611 associated with a connection are: `sql-product', `sql-user',
612 `sql-password', `sql-port', `sql-server', and `sql-database'.
613
614 If a SQL-VARIABLE is part of the connection, it will not be
615 prompted for during login. The command `sql-connect' starts a
616 predefined SQLi session using the parameters from this list.
617 Connections defined here appear in the submenu SQL->Start... for
618 making new SQLi sessions."
619 :type `(alist :key-type (string :tag "Connection")
620 :value-type
621 (set
622 (group (const :tag "Product" sql-product)
623 (choice
624 ,@(mapcar
625 (lambda (prod-info)
626 `(const :tag
627 ,(or (plist-get (cdr prod-info) :name)
628 (capitalize
629 (symbol-name (car prod-info))))
630 (quote ,(car prod-info))))
631 sql-product-alist)))
632 (group (const :tag "Username" sql-user) string)
633 (group (const :tag "Password" sql-password) string)
634 (group (const :tag "Server" sql-server) string)
635 (group (const :tag "Database" sql-database) string)
636 (group (const :tag "Port" sql-port) integer)
637 (repeat :inline t
638 (list :tab "Other"
639 (symbol :tag " Variable Symbol")
640 (sexp :tag "Value Expression")))))
641 :version "24.1"
642 :group 'SQL)
643
644 (defcustom sql-product 'ansi
645 "Select the SQL database product used.
646 This allows highlighting buffers properly when you open them."
647 :type `(choice
648 ,@(mapcar (lambda (prod-info)
649 `(const :tag
650 ,(or (plist-get (cdr prod-info) :name)
651 (capitalize (symbol-name (car prod-info))))
652 ,(car prod-info)))
653 sql-product-alist))
654 :group 'SQL
655 :safe 'symbolp)
656 (defvaralias 'sql-dialect 'sql-product)
657
658 ;; misc customization of sql.el behavior
659
660 (defcustom sql-electric-stuff nil
661 "Treat some input as electric.
662 If set to the symbol `semicolon', then hitting `;' will send current
663 input in the SQLi buffer to the process.
664 If set to the symbol `go', then hitting `go' on a line by itself will
665 send current input in the SQLi buffer to the process.
666 If set to nil, then you must use \\[comint-send-input] in order to send
667 current input in the SQLi buffer to the process."
668 :type '(choice (const :tag "Nothing" nil)
669 (const :tag "The semicolon `;'" semicolon)
670 (const :tag "The string `go' by itself" go))
671 :version "20.8"
672 :group 'SQL)
673
674 (defcustom sql-send-terminator nil
675 "When non-nil, add a terminator to text sent to the SQL interpreter.
676
677 When text is sent to the SQL interpreter (via `sql-send-string',
678 `sql-send-region', `sql-send-paragraph' or `sql-send-buffer'), a
679 command terminator can be automatically sent as well. The
680 terminator is not sent, if the string sent already ends with the
681 terminator.
682
683 If this value is t, then the default command terminator for the
684 SQL interpreter is sent. If this value is a string, then the
685 string is sent.
686
687 If the value is a cons cell of the form (PAT . TERM), then PAT is
688 a regexp used to match the terminator in the string and TERM is
689 the terminator to be sent. This form is useful if the SQL
690 interpreter has more than one way of submitting a SQL command.
691 The PAT regexp can match any of them, and TERM is the way we do
692 it automatically."
693
694 :type '(choice (const :tag "No Terminator" nil)
695 (const :tag "Default Terminator" t)
696 (string :tag "Terminator String")
697 (cons :tag "Terminator Pattern and String"
698 (string :tag "Terminator Pattern")
699 (string :tag "Terminator String")))
700 :version "22.2"
701 :group 'SQL)
702
703 (defvar sql-contains-names nil
704 "When non-nil, the current buffer contains database names.
705
706 Globally should be set to nil; it will be non-nil in `sql-mode',
707 `sql-interactive-mode' and list all buffers.")
708
709
710 (defcustom sql-pop-to-buffer-after-send-region nil
711 "When non-nil, pop to the buffer SQL statements are sent to.
712
713 After a call to `sql-sent-string', `sql-send-region',
714 `sql-send-paragraph' or `sql-send-buffer', the window is split
715 and the SQLi buffer is shown. If this variable is not nil, that
716 buffer's window will be selected by calling `pop-to-buffer'. If
717 this variable is nil, that buffer is shown using
718 `display-buffer'."
719 :type 'boolean
720 :group 'SQL)
721
722 ;; imenu support for sql-mode.
723
724 (defvar sql-imenu-generic-expression
725 ;; Items are in reverse order because they are rendered in reverse.
726 '(("Rules/Defaults" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*\\(?:rule\\|default\\)\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\s-+\\(\\w+\\)" 1)
727 ("Sequences" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*sequence\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\w+\\)" 1)
728 ("Triggers" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*trigger\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\w+\\)" 1)
729 ("Functions" "^\\s-*\\(?:create\\s-+\\(?:\\w+\\s-+\\)*\\)?function\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\w+\\)" 1)
730 ("Procedures" "^\\s-*\\(?:create\\s-+\\(?:\\w+\\s-+\\)*\\)?proc\\(?:edure\\)?\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\w+\\)" 1)
731 ("Packages" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*package\\s-+\\(?:body\\s-+\\)?\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\w+\\)" 1)
732 ("Types" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*type\\s-+\\(?:body\\s-+\\)?\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\w+\\)" 1)
733 ("Indexes" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*index\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\w+\\)" 1)
734 ("Tables/Views" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*\\(?:table\\|view\\)\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\w+\\)" 1))
735 "Define interesting points in the SQL buffer for `imenu'.
736
737 This is used to set `imenu-generic-expression' when SQL mode is
738 entered. Subsequent changes to `sql-imenu-generic-expression' will
739 not affect existing SQL buffers because imenu-generic-expression is
740 a local variable.")
741
742 ;; history file
743
744 (defcustom sql-input-ring-file-name nil
745 "If non-nil, name of the file to read/write input history.
746
747 You have to set this variable if you want the history of your commands
748 saved from one Emacs session to the next. If this variable is set,
749 exiting the SQL interpreter in an SQLi buffer will write the input
750 history to the specified file. Starting a new process in a SQLi buffer
751 will read the input history from the specified file.
752
753 This is used to initialize `comint-input-ring-file-name'.
754
755 Note that the size of the input history is determined by the variable
756 `comint-input-ring-size'."
757 :type '(choice (const :tag "none" nil)
758 (file))
759 :group 'SQL)
760
761 (defcustom sql-input-ring-separator "\n--\n"
762 "Separator between commands in the history file.
763
764 If set to \"\\n\", each line in the history file will be interpreted as
765 one command. Multi-line commands are split into several commands when
766 the input ring is initialized from a history file.
767
768 This variable used to initialize `comint-input-ring-separator'.
769 `comint-input-ring-separator' is part of Emacs 21; if your Emacs
770 does not have it, setting `sql-input-ring-separator' will have no
771 effect. In that case multiline commands will be split into several
772 commands when the input history is read, as if you had set
773 `sql-input-ring-separator' to \"\\n\"."
774 :type 'string
775 :group 'SQL)
776
777 ;; The usual hooks
778
779 (defcustom sql-interactive-mode-hook '()
780 "Hook for customizing `sql-interactive-mode'."
781 :type 'hook
782 :group 'SQL)
783
784 (defcustom sql-mode-hook '()
785 "Hook for customizing `sql-mode'."
786 :type 'hook
787 :group 'SQL)
788
789 (defcustom sql-set-sqli-hook '()
790 "Hook for reacting to changes of `sql-buffer'.
791
792 This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
793 is changed."
794 :type 'hook
795 :group 'SQL)
796
797 (defcustom sql-login-hook '()
798 "Hook for interacting with a buffer in `sql-interactive-mode'.
799
800 This hook is invoked in a buffer once it is ready to accept input
801 for the first time."
802 :version "24.1"
803 :type 'hook
804 :group 'SQL)
805
806 ;; Customization for ANSI
807
808 (defcustom sql-ansi-statement-starters
809 (regexp-opt '("create" "alter" "drop"
810 "select" "insert" "update" "delete" "merge"
811 "grant" "revoke"))
812 "Regexp of keywords that start SQL commands.
813
814 All products share this list; products should define a regexp to
815 identify additional keywords in a variable defined by
816 the :statement feature."
817 :version "24.1"
818 :type 'string
819 :group 'SQL)
820
821 ;; Customization for Oracle
822
823 (defcustom sql-oracle-program "sqlplus"
824 "Command to start sqlplus by Oracle.
825
826 Starts `sql-interactive-mode' after doing some setup.
827
828 On Windows, \"sqlplus\" usually starts the sqlplus \"GUI\". In order
829 to start the sqlplus console, use \"plus33\" or something similar.
830 You will find the file in your Orant\\bin directory."
831 :type 'file
832 :group 'SQL)
833
834 (defcustom sql-oracle-options nil
835 "List of additional options for `sql-oracle-program'."
836 :type '(repeat string)
837 :version "20.8"
838 :group 'SQL)
839
840 (defcustom sql-oracle-login-params '(user password database)
841 "List of login parameters needed to connect to Oracle."
842 :type 'sql-login-params
843 :version "24.1"
844 :group 'SQL)
845
846 (defcustom sql-oracle-statement-starters
847 (regexp-opt '("declare" "begin" "with"))
848 "Additional statement starting keywords in Oracle."
849 :version "24.1"
850 :type 'string
851 :group 'SQL)
852
853 (defcustom sql-oracle-scan-on t
854 "Non-nil if placeholders should be replaced in Oracle SQLi.
855
856 When non-nil, Emacs will scan text sent to sqlplus and prompt
857 for replacement text for & placeholders as sqlplus does. This
858 is needed on Windows where SQL*Plus output is buffered and the
859 prompts are not shown until after the text is entered.
860
861 You need to issue the following command in SQL*Plus to be safe:
862
863 SET DEFINE OFF
864
865 In older versions of SQL*Plus, this was the SET SCAN OFF command."
866 :version "24.1"
867 :type 'boolean
868 :group 'SQL)
869
870 (defcustom sql-db2-escape-newlines nil
871 "Non-nil if newlines should be escaped by a backslash in DB2 SQLi.
872
873 When non-nil, Emacs will automatically insert a space and
874 backslash prior to every newline in multi-line SQL statements as
875 they are submitted to an interactive DB2 session."
876 :version "24.3"
877 :type 'boolean
878 :group 'SQL)
879
880 ;; Customization for SQLite
881
882 (defcustom sql-sqlite-program (or (executable-find "sqlite3")
883 (executable-find "sqlite")
884 "sqlite")
885 "Command to start SQLite.
886
887 Starts `sql-interactive-mode' after doing some setup."
888 :type 'file
889 :group 'SQL)
890
891 (defcustom sql-sqlite-options nil
892 "List of additional options for `sql-sqlite-program'."
893 :type '(repeat string)
894 :version "20.8"
895 :group 'SQL)
896
897 (defcustom sql-sqlite-login-params '((database :file ".*\\.\\(db\\|sqlite[23]?\\)"))
898 "List of login parameters needed to connect to SQLite."
899 :type 'sql-login-params
900 :version "24.1"
901 :group 'SQL)
902
903 ;; Customization for MySQL
904
905 (defcustom sql-mysql-program "mysql"
906 "Command to start mysql by TcX.
907
908 Starts `sql-interactive-mode' after doing some setup."
909 :type 'file
910 :group 'SQL)
911
912 (defcustom sql-mysql-options nil
913 "List of additional options for `sql-mysql-program'.
914 The following list of options is reported to make things work
915 on Windows: \"-C\" \"-t\" \"-f\" \"-n\"."
916 :type '(repeat string)
917 :version "20.8"
918 :group 'SQL)
919
920 (defcustom sql-mysql-login-params '(user password database server)
921 "List of login parameters needed to connect to MySQL."
922 :type 'sql-login-params
923 :version "24.1"
924 :group 'SQL)
925
926 ;; Customization for Solid
927
928 (defcustom sql-solid-program "solsql"
929 "Command to start SOLID SQL Editor.
930
931 Starts `sql-interactive-mode' after doing some setup."
932 :type 'file
933 :group 'SQL)
934
935 (defcustom sql-solid-login-params '(user password server)
936 "List of login parameters needed to connect to Solid."
937 :type 'sql-login-params
938 :version "24.1"
939 :group 'SQL)
940
941 ;; Customization for Sybase
942
943 (defcustom sql-sybase-program "isql"
944 "Command to start isql by Sybase.
945
946 Starts `sql-interactive-mode' after doing some setup."
947 :type 'file
948 :group 'SQL)
949
950 (defcustom sql-sybase-options nil
951 "List of additional options for `sql-sybase-program'.
952 Some versions of isql might require the -n option in order to work."
953 :type '(repeat string)
954 :version "20.8"
955 :group 'SQL)
956
957 (defcustom sql-sybase-login-params '(server user password database)
958 "List of login parameters needed to connect to Sybase."
959 :type 'sql-login-params
960 :version "24.1"
961 :group 'SQL)
962
963 ;; Customization for Informix
964
965 (defcustom sql-informix-program "dbaccess"
966 "Command to start dbaccess by Informix.
967
968 Starts `sql-interactive-mode' after doing some setup."
969 :type 'file
970 :group 'SQL)
971
972 (defcustom sql-informix-login-params '(database)
973 "List of login parameters needed to connect to Informix."
974 :type 'sql-login-params
975 :version "24.1"
976 :group 'SQL)
977
978 ;; Customization for Ingres
979
980 (defcustom sql-ingres-program "sql"
981 "Command to start sql by Ingres.
982
983 Starts `sql-interactive-mode' after doing some setup."
984 :type 'file
985 :group 'SQL)
986
987 (defcustom sql-ingres-login-params '(database)
988 "List of login parameters needed to connect to Ingres."
989 :type 'sql-login-params
990 :version "24.1"
991 :group 'SQL)
992
993 ;; Customization for Microsoft
994
995 (defcustom sql-ms-program "osql"
996 "Command to start osql by Microsoft.
997
998 Starts `sql-interactive-mode' after doing some setup."
999 :type 'file
1000 :group 'SQL)
1001
1002 (defcustom sql-ms-options '("-w" "300" "-n")
1003 ;; -w is the linesize
1004 "List of additional options for `sql-ms-program'."
1005 :type '(repeat string)
1006 :version "22.1"
1007 :group 'SQL)
1008
1009 (defcustom sql-ms-login-params '(user password server database)
1010 "List of login parameters needed to connect to Microsoft."
1011 :type 'sql-login-params
1012 :version "24.1"
1013 :group 'SQL)
1014
1015 ;; Customization for Postgres
1016
1017 (defcustom sql-postgres-program "psql"
1018 "Command to start psql by Postgres.
1019
1020 Starts `sql-interactive-mode' after doing some setup."
1021 :type 'file
1022 :group 'SQL)
1023
1024 (defcustom sql-postgres-options '("-P" "pager=off")
1025 "List of additional options for `sql-postgres-program'.
1026 The default setting includes the -P option which breaks older versions
1027 of the psql client (such as version 6.5.3). The -P option is equivalent
1028 to the --pset option. If you want the psql to prompt you for a user
1029 name, add the string \"-u\" to the list of options. If you want to
1030 provide a user name on the command line (newer versions such as 7.1),
1031 add your name with a \"-U\" prefix (such as \"-Umark\") to the list."
1032 :type '(repeat string)
1033 :version "20.8"
1034 :group 'SQL)
1035
1036 (defcustom sql-postgres-login-params `((user :default ,(user-login-name))
1037 (database :default ,(user-login-name))
1038 server)
1039 "List of login parameters needed to connect to Postgres."
1040 :type 'sql-login-params
1041 :version "24.1"
1042 :group 'SQL)
1043
1044 ;; Customization for Interbase
1045
1046 (defcustom sql-interbase-program "isql"
1047 "Command to start isql by Interbase.
1048
1049 Starts `sql-interactive-mode' after doing some setup."
1050 :type 'file
1051 :group 'SQL)
1052
1053 (defcustom sql-interbase-options nil
1054 "List of additional options for `sql-interbase-program'."
1055 :type '(repeat string)
1056 :version "20.8"
1057 :group 'SQL)
1058
1059 (defcustom sql-interbase-login-params '(user password database)
1060 "List of login parameters needed to connect to Interbase."
1061 :type 'sql-login-params
1062 :version "24.1"
1063 :group 'SQL)
1064
1065 ;; Customization for DB2
1066
1067 (defcustom sql-db2-program "db2"
1068 "Command to start db2 by IBM.
1069
1070 Starts `sql-interactive-mode' after doing some setup."
1071 :type 'file
1072 :group 'SQL)
1073
1074 (defcustom sql-db2-options nil
1075 "List of additional options for `sql-db2-program'."
1076 :type '(repeat string)
1077 :version "20.8"
1078 :group 'SQL)
1079
1080 (defcustom sql-db2-login-params nil
1081 "List of login parameters needed to connect to DB2."
1082 :type 'sql-login-params
1083 :version "24.1"
1084 :group 'SQL)
1085
1086 ;; Customization for Linter
1087
1088 (defcustom sql-linter-program "inl"
1089 "Command to start inl by RELEX.
1090
1091 Starts `sql-interactive-mode' after doing some setup."
1092 :type 'file
1093 :group 'SQL)
1094
1095 (defcustom sql-linter-options nil
1096 "List of additional options for `sql-linter-program'."
1097 :type '(repeat string)
1098 :version "21.3"
1099 :group 'SQL)
1100
1101 (defcustom sql-linter-login-params '(user password database server)
1102 "Login parameters to needed to connect to Linter."
1103 :type 'sql-login-params
1104 :version "24.1"
1105 :group 'SQL)
1106
1107 \f
1108
1109 ;;; Variables which do not need customization
1110
1111 (defvar sql-user-history nil
1112 "History of usernames used.")
1113
1114 (defvar sql-database-history nil
1115 "History of databases used.")
1116
1117 (defvar sql-server-history nil
1118 "History of servers used.")
1119
1120 ;; Passwords are not kept in a history.
1121
1122 (defvar sql-product-history nil
1123 "History of products used.")
1124
1125 (defvar sql-connection-history nil
1126 "History of connections used.")
1127
1128 (defvar sql-buffer nil
1129 "Current SQLi buffer.
1130
1131 The global value of `sql-buffer' is the name of the latest SQLi buffer
1132 created. Any SQL buffer created will make a local copy of this value.
1133 See `sql-interactive-mode' for more on multiple sessions. If you want
1134 to change the SQLi buffer a SQL mode sends its SQL strings to, change
1135 the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
1136
1137 (defvar sql-prompt-regexp nil
1138 "Prompt used to initialize `comint-prompt-regexp'.
1139
1140 You can change `sql-prompt-regexp' on `sql-interactive-mode-hook'.")
1141
1142 (defvar sql-prompt-length 0
1143 "Prompt used to set `left-margin' in `sql-interactive-mode'.
1144
1145 You can change `sql-prompt-length' on `sql-interactive-mode-hook'.")
1146
1147 (defvar sql-prompt-cont-regexp nil
1148 "Prompt pattern of statement continuation prompts.")
1149
1150 (defvar sql-alternate-buffer-name nil
1151 "Buffer-local string used to possibly rename the SQLi buffer.
1152
1153 Used by `sql-rename-buffer'.")
1154
1155 (defun sql-buffer-live-p (buffer &optional product connection)
1156 "Return non-nil if the process associated with buffer is live.
1157
1158 BUFFER can be a buffer object or a buffer name. The buffer must
1159 be a live buffer, have a running process attached to it, be in
1160 `sql-interactive-mode', and, if PRODUCT or CONNECTION are
1161 specified, it's `sql-product' or `sql-connection' must match."
1162
1163 (when buffer
1164 (setq buffer (get-buffer buffer))
1165 (and buffer
1166 (buffer-live-p buffer)
1167 (comint-check-proc buffer)
1168 (with-current-buffer buffer
1169 (and (derived-mode-p 'sql-interactive-mode)
1170 (or (not product)
1171 (eq product sql-product))
1172 (or (not connection)
1173 (eq connection sql-connection)))))))
1174
1175 ;; Keymap for sql-interactive-mode.
1176
1177 (defvar sql-interactive-mode-map
1178 (let ((map (make-sparse-keymap)))
1179 (if (fboundp 'set-keymap-parent)
1180 (set-keymap-parent map comint-mode-map); Emacs
1181 (if (fboundp 'set-keymap-parents)
1182 (set-keymap-parents map (list comint-mode-map)))); XEmacs
1183 (if (fboundp 'set-keymap-name)
1184 (set-keymap-name map 'sql-interactive-mode-map)); XEmacs
1185 (define-key map (kbd "C-j") 'sql-accumulate-and-indent)
1186 (define-key map (kbd "C-c C-w") 'sql-copy-column)
1187 (define-key map (kbd "O") 'sql-magic-go)
1188 (define-key map (kbd "o") 'sql-magic-go)
1189 (define-key map (kbd ";") 'sql-magic-semicolon)
1190 (define-key map (kbd "C-c C-l a") 'sql-list-all)
1191 (define-key map (kbd "C-c C-l t") 'sql-list-table)
1192 map)
1193 "Mode map used for `sql-interactive-mode'.
1194 Based on `comint-mode-map'.")
1195
1196 ;; Keymap for sql-mode.
1197
1198 (defvar sql-mode-map
1199 (let ((map (make-sparse-keymap)))
1200 (define-key map (kbd "C-c C-c") 'sql-send-paragraph)
1201 (define-key map (kbd "C-c C-r") 'sql-send-region)
1202 (define-key map (kbd "C-c C-s") 'sql-send-string)
1203 (define-key map (kbd "C-c C-b") 'sql-send-buffer)
1204 (define-key map (kbd "C-c C-i") 'sql-product-interactive)
1205 (define-key map (kbd "C-c C-l a") 'sql-list-all)
1206 (define-key map (kbd "C-c C-l t") 'sql-list-table)
1207 (define-key map [remap beginning-of-defun] 'sql-beginning-of-statement)
1208 (define-key map [remap end-of-defun] 'sql-end-of-statement)
1209 map)
1210 "Mode map used for `sql-mode'.")
1211
1212 ;; easy menu for sql-mode.
1213
1214 (easy-menu-define
1215 sql-mode-menu sql-mode-map
1216 "Menu for `sql-mode'."
1217 `("SQL"
1218 ["Send Paragraph" sql-send-paragraph (sql-buffer-live-p sql-buffer)]
1219 ["Send Region" sql-send-region (and mark-active
1220 (sql-buffer-live-p sql-buffer))]
1221 ["Send Buffer" sql-send-buffer (sql-buffer-live-p sql-buffer)]
1222 ["Send String" sql-send-string (sql-buffer-live-p sql-buffer)]
1223 "--"
1224 ["List all objects" sql-list-all (and (sql-buffer-live-p sql-buffer)
1225 (sql-get-product-feature sql-product :list-all))]
1226 ["List table details" sql-list-table (and (sql-buffer-live-p sql-buffer)
1227 (sql-get-product-feature sql-product :list-table))]
1228 "--"
1229 ["Start SQLi session" sql-product-interactive
1230 :visible (not sql-connection-alist)
1231 :enable (sql-get-product-feature sql-product :sqli-comint-func)]
1232 ("Start..."
1233 :visible sql-connection-alist
1234 :filter sql-connection-menu-filter
1235 "--"
1236 ["New SQLi Session" sql-product-interactive (sql-get-product-feature sql-product :sqli-comint-func)])
1237 ["--"
1238 :visible sql-connection-alist]
1239 ["Show SQLi buffer" sql-show-sqli-buffer t]
1240 ["Set SQLi buffer" sql-set-sqli-buffer t]
1241 ["Pop to SQLi buffer after send"
1242 sql-toggle-pop-to-buffer-after-send-region
1243 :style toggle
1244 :selected sql-pop-to-buffer-after-send-region]
1245 ["--" nil nil]
1246 ("Product"
1247 ,@(mapcar (lambda (prod-info)
1248 (let* ((prod (pop prod-info))
1249 (name (or (plist-get prod-info :name)
1250 (capitalize (symbol-name prod))))
1251 (cmd (intern (format "sql-highlight-%s-keywords" prod))))
1252 (fset cmd `(lambda () ,(format "Highlight %s SQL keywords." name)
1253 (interactive)
1254 (sql-set-product ',prod)))
1255 (vector name cmd
1256 :style 'radio
1257 :selected `(eq sql-product ',prod))))
1258 sql-product-alist))))
1259
1260 ;; easy menu for sql-interactive-mode.
1261
1262 (easy-menu-define
1263 sql-interactive-mode-menu sql-interactive-mode-map
1264 "Menu for `sql-interactive-mode'."
1265 '("SQL"
1266 ["Rename Buffer" sql-rename-buffer t]
1267 ["Save Connection" sql-save-connection (not sql-connection)]
1268 "--"
1269 ["List all objects" sql-list-all (sql-get-product-feature sql-product :list-all)]
1270 ["List table details" sql-list-table (sql-get-product-feature sql-product :list-table)]))
1271
1272 ;; Abbreviations -- if you want more of them, define them in your init
1273 ;; file. Abbrevs have to be enabled in your init file, too.
1274
1275 (define-abbrev-table 'sql-mode-abbrev-table
1276 '(("ins" "insert" nil nil t)
1277 ("upd" "update" nil nil t)
1278 ("del" "delete" nil nil t)
1279 ("sel" "select" nil nil t)
1280 ("proc" "procedure" nil nil t)
1281 ("func" "function" nil nil t)
1282 ("cr" "create" nil nil t))
1283 "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
1284
1285 ;; Syntax Table
1286
1287 (defvar sql-mode-syntax-table
1288 (let ((table (make-syntax-table)))
1289 ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
1290 (modify-syntax-entry ?/ ". 14" table)
1291 (modify-syntax-entry ?* ". 23" table)
1292 ;; double-dash starts comments
1293 (modify-syntax-entry ?- ". 12b" table)
1294 ;; newline and formfeed end comments
1295 (modify-syntax-entry ?\n "> b" table)
1296 (modify-syntax-entry ?\f "> b" table)
1297 ;; single quotes (') delimit strings
1298 (modify-syntax-entry ?' "\"" table)
1299 ;; double quotes (") don't delimit strings
1300 (modify-syntax-entry ?\" "." table)
1301 ;; Make these all punctuation
1302 (mapc (lambda (c) (modify-syntax-entry c "." table))
1303 (string-to-list "!#$%&+,.:;<=>?@\\|"))
1304 table)
1305 "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
1306
1307 ;; Font lock support
1308
1309 (defvar sql-mode-font-lock-object-name
1310 (eval-when-compile
1311 (list (concat "^\\s-*\\(?:create\\|drop\\|alter\\)\\s-+" ;; lead off with CREATE, DROP or ALTER
1312 "\\(?:\\w+\\s-+\\)*" ;; optional intervening keywords
1313 "\\(?:table\\|view\\|\\(?:package\\|type\\)\\(?:\\s-+body\\)?\\|proc\\(?:edure\\)?"
1314 "\\|function\\|trigger\\|sequence\\|rule\\|default\\)\\s-+"
1315 "\\(?:if\\s-+not\\s-+exists\\s-+\\)?" ;; IF NOT EXISTS
1316 "\\(\\w+\\)")
1317 1 'font-lock-function-name-face))
1318
1319 "Pattern to match the names of top-level objects.
1320
1321 The pattern matches the name in a CREATE, DROP or ALTER
1322 statement. The format of variable should be a valid
1323 `font-lock-keywords' entry.")
1324
1325 ;; While there are international and American standards for SQL, they
1326 ;; are not followed closely, and most vendors offer significant
1327 ;; capabilities beyond those defined in the standard specifications.
1328
1329 ;; SQL mode provides support for highlighting based on the product. In
1330 ;; addition to highlighting the product keywords, any ANSI keywords not
1331 ;; used by the product are also highlighted. This will help identify
1332 ;; keywords that could be restricted in future versions of the product
1333 ;; or might be a problem if ported to another product.
1334
1335 ;; To reduce the complexity and size of the regular expressions
1336 ;; generated to match keywords, ANSI keywords are filtered out of
1337 ;; product keywords if they are equivalent. To do this, we define a
1338 ;; function `sql-font-lock-keywords-builder' that removes any keywords
1339 ;; that are matched by the ANSI patterns and results in the same face
1340 ;; being applied. For this to work properly, we must play some games
1341 ;; with the execution and compile time behavior. This code is a
1342 ;; little tricky but works properly.
1343
1344 ;; When defining the keywords for individual products you should
1345 ;; include all of the keywords that you want matched. The filtering
1346 ;; against the ANSI keywords will be automatic if you use the
1347 ;; `sql-font-lock-keywords-builder' function and follow the
1348 ;; implementation pattern used for the other products in this file.
1349
1350 (eval-when-compile
1351 (defvar sql-mode-ansi-font-lock-keywords)
1352 (setq sql-mode-ansi-font-lock-keywords nil))
1353
1354 (eval-and-compile
1355 (defun sql-font-lock-keywords-builder (face boundaries &rest keywords)
1356 "Generation of regexp matching any one of KEYWORDS."
1357
1358 (let ((bdy (or boundaries '("\\b" . "\\b")))
1359 kwd)
1360
1361 ;; Remove keywords that are defined in ANSI
1362 (setq kwd keywords)
1363 ;; (dolist (k keywords)
1364 ;; (catch 'next
1365 ;; (dolist (a sql-mode-ansi-font-lock-keywords)
1366 ;; (when (and (eq face (cdr a))
1367 ;; (eq (string-match (car a) k 0) 0)
1368 ;; (eq (match-end 0) (length k)))
1369 ;; (setq kwd (delq k kwd))
1370 ;; (throw 'next nil)))))
1371
1372 ;; Create a properly formed font-lock-keywords item
1373 (cons (concat (car bdy)
1374 (regexp-opt kwd t)
1375 (cdr bdy))
1376 face)))
1377
1378 (defun sql-regexp-abbrev (keyword)
1379 (let ((brk (string-match "[~]" keyword))
1380 (len (length keyword))
1381 (sep "\\(?:")
1382 re i)
1383 (if (not brk)
1384 keyword
1385 (setq re (substring keyword 0 brk)
1386 i (+ 2 brk)
1387 brk (1+ brk))
1388 (while (<= i len)
1389 (setq re (concat re sep (substring keyword brk i))
1390 sep "\\|"
1391 i (1+ i)))
1392 (concat re "\\)?"))))
1393
1394 (defun sql-regexp-abbrev-list (&rest keyw-list)
1395 (let ((re nil)
1396 (sep "\\<\\(?:"))
1397 (while keyw-list
1398 (setq re (concat re sep (sql-regexp-abbrev (car keyw-list)))
1399 sep "\\|"
1400 keyw-list (cdr keyw-list)))
1401 (concat re "\\)\\>"))))
1402
1403 (eval-when-compile
1404 (setq sql-mode-ansi-font-lock-keywords
1405 (list
1406 ;; ANSI Non Reserved keywords
1407 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1408 "ada" "asensitive" "assignment" "asymmetric" "atomic" "between"
1409 "bitvar" "called" "catalog_name" "chain" "character_set_catalog"
1410 "character_set_name" "character_set_schema" "checked" "class_origin"
1411 "cobol" "collation_catalog" "collation_name" "collation_schema"
1412 "column_name" "command_function" "command_function_code" "committed"
1413 "condition_number" "connection_name" "constraint_catalog"
1414 "constraint_name" "constraint_schema" "contains" "cursor_name"
1415 "datetime_interval_code" "datetime_interval_precision" "defined"
1416 "definer" "dispatch" "dynamic_function" "dynamic_function_code"
1417 "existing" "exists" "final" "fortran" "generated" "granted"
1418 "hierarchy" "hold" "implementation" "infix" "insensitive" "instance"
1419 "instantiable" "invoker" "key_member" "key_type" "length" "m"
1420 "message_length" "message_octet_length" "message_text" "method" "more"
1421 "mumps" "name" "nullable" "number" "options" "overlaps" "overriding"
1422 "parameter_mode" "parameter_name" "parameter_ordinal_position"
1423 "parameter_specific_catalog" "parameter_specific_name"
1424 "parameter_specific_schema" "pascal" "pli" "position" "repeatable"
1425 "returned_length" "returned_octet_length" "returned_sqlstate"
1426 "routine_catalog" "routine_name" "routine_schema" "row_count" "scale"
1427 "schema_name" "security" "self" "sensitive" "serializable"
1428 "server_name" "similar" "simple" "source" "specific_name" "style"
1429 "subclass_origin" "sublist" "symmetric" "system" "table_name"
1430 "transaction_active" "transactions_committed"
1431 "transactions_rolled_back" "transform" "transforms" "trigger_catalog"
1432 "trigger_name" "trigger_schema" "type" "uncommitted" "unnamed"
1433 "user_defined_type_catalog" "user_defined_type_name"
1434 "user_defined_type_schema"
1435 )
1436
1437 ;; ANSI Reserved keywords
1438 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1439 "absolute" "action" "add" "admin" "after" "aggregate" "alias" "all"
1440 "allocate" "alter" "and" "any" "are" "as" "asc" "assertion" "at"
1441 "authorization" "before" "begin" "both" "breadth" "by" "call"
1442 "cascade" "cascaded" "case" "catalog" "check" "class" "close"
1443 "collate" "collation" "column" "commit" "completion" "connect"
1444 "connection" "constraint" "constraints" "constructor" "continue"
1445 "corresponding" "create" "cross" "cube" "current" "cursor" "cycle"
1446 "data" "day" "deallocate" "declare" "default" "deferrable" "deferred"
1447 "delete" "depth" "deref" "desc" "describe" "descriptor" "destroy"
1448 "destructor" "deterministic" "diagnostics" "dictionary" "disconnect"
1449 "distinct" "domain" "drop" "dynamic" "each" "else" "end" "equals"
1450 "escape" "every" "except" "exception" "exec" "execute" "external"
1451 "false" "fetch" "first" "for" "foreign" "found" "free" "from" "full"
1452 "function" "general" "get" "global" "go" "goto" "grant" "group"
1453 "grouping" "having" "host" "hour" "identity" "ignore" "immediate" "in"
1454 "indicator" "initialize" "initially" "inner" "inout" "input" "insert"
1455 "intersect" "into" "is" "isolation" "iterate" "join" "key" "language"
1456 "last" "lateral" "leading" "left" "less" "level" "like" "limit"
1457 "local" "locator" "map" "match" "minute" "modifies" "modify" "module"
1458 "month" "names" "natural" "new" "next" "no" "none" "not" "null" "of"
1459 "off" "old" "on" "only" "open" "operation" "option" "or" "order"
1460 "ordinality" "out" "outer" "output" "pad" "parameter" "parameters"
1461 "partial" "path" "postfix" "prefix" "preorder" "prepare" "preserve"
1462 "primary" "prior" "privileges" "procedure" "public" "read" "reads"
1463 "recursive" "references" "referencing" "relative" "restrict" "result"
1464 "return" "returns" "revoke" "right" "role" "rollback" "rollup"
1465 "routine" "rows" "savepoint" "schema" "scroll" "search" "second"
1466 "section" "select" "sequence" "session" "set" "sets" "size" "some"
1467 "space" "specific" "specifictype" "sql" "sqlexception" "sqlstate"
1468 "sqlwarning" "start" "state" "statement" "static" "structure" "table"
1469 "temporary" "terminate" "than" "then" "timezone_hour"
1470 "timezone_minute" "to" "trailing" "transaction" "translation"
1471 "trigger" "true" "under" "union" "unique" "unknown" "unnest" "update"
1472 "usage" "using" "value" "values" "variable" "view" "when" "whenever"
1473 "where" "with" "without" "work" "write" "year"
1474 )
1475
1476 ;; ANSI Functions
1477 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1478 "abs" "avg" "bit_length" "cardinality" "cast" "char_length"
1479 "character_length" "coalesce" "convert" "count" "current_date"
1480 "current_path" "current_role" "current_time" "current_timestamp"
1481 "current_user" "extract" "localtime" "localtimestamp" "lower" "max"
1482 "min" "mod" "nullif" "octet_length" "overlay" "placing" "session_user"
1483 "substring" "sum" "system_user" "translate" "treat" "trim" "upper"
1484 "user"
1485 )
1486
1487 ;; ANSI Data Types
1488 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1489 "array" "binary" "bit" "blob" "boolean" "char" "character" "clob"
1490 "date" "dec" "decimal" "double" "float" "int" "integer" "interval"
1491 "large" "national" "nchar" "nclob" "numeric" "object" "precision"
1492 "real" "ref" "row" "scope" "smallint" "time" "timestamp" "varchar"
1493 "varying" "zone"
1494 ))))
1495
1496 (defvar sql-mode-ansi-font-lock-keywords
1497 (eval-when-compile sql-mode-ansi-font-lock-keywords)
1498 "ANSI SQL keywords used by font-lock.
1499
1500 This variable is used by `sql-mode' and `sql-interactive-mode'. The
1501 regular expressions are created during compilation by calling the
1502 function `regexp-opt'. Therefore, take a look at the source before
1503 you define your own `sql-mode-ansi-font-lock-keywords'. You may want
1504 to add functions and PL/SQL keywords.")
1505
1506 (defun sql--oracle-show-reserved-words ()
1507 ;; This function is for use by the maintainer of SQL.EL only.
1508 (if (or (and (not (derived-mode-p 'sql-mode))
1509 (not (derived-mode-p 'sql-interactive-mode)))
1510 (not sql-buffer)
1511 (not (eq sql-product 'oracle)))
1512 (error "Not an Oracle buffer")
1513
1514 (let ((b "*RESERVED WORDS*"))
1515 (sql-execute sql-buffer b
1516 (concat "SELECT "
1517 " keyword "
1518 ", reserved AS \"Res\" "
1519 ", res_type AS \"Type\" "
1520 ", res_attr AS \"Attr\" "
1521 ", res_semi AS \"Semi\" "
1522 ", duplicate AS \"Dup\" "
1523 "FROM V$RESERVED_WORDS "
1524 "WHERE length > 1 "
1525 "AND SUBSTR(keyword, 1, 1) BETWEEN 'A' AND 'Z' "
1526 "ORDER BY 2 DESC, 3 DESC, 4 DESC, 5 DESC, 6 DESC, 1;")
1527 nil nil)
1528 (with-current-buffer b
1529 (set (make-local-variable 'sql-product) 'oracle)
1530 (sql-product-font-lock t nil)
1531 (font-lock-mode +1)))))
1532
1533 (defvar sql-mode-oracle-font-lock-keywords
1534 (eval-when-compile
1535 (list
1536 ;; Oracle SQL*Plus Commands
1537 ;; Only recognized in they start in column 1 and the
1538 ;; abbreviation is followed by a space or the end of line.
1539
1540 "\\|"
1541 (list (concat "^" (sql-regexp-abbrev "rem~ark") "\\(?:\\s-.*\\)?$")
1542 0 'font-lock-comment-face t)
1543
1544 (list
1545 (concat
1546 "^\\(?:"
1547 (sql-regexp-abbrev-list
1548 "[@]\\{1,2\\}" "acc~ept" "a~ppend" "archive" "attribute"
1549 "bre~ak" "bti~tle" "c~hange" "cl~ear" "col~umn" "conn~ect"
1550 "copy" "def~ine" "del" "desc~ribe" "disc~onnect" "ed~it"
1551 "exec~ute" "exit" "get" "help" "ho~st" "[$]" "i~nput" "l~ist"
1552 "passw~ord" "pau~se" "pri~nt" "pro~mpt" "quit" "recover"
1553 "repf~ooter" "reph~eader" "r~un" "sav~e" "sho~w" "shutdown"
1554 "spo~ol" "sta~rt" "startup" "store" "tim~ing" "tti~tle"
1555 "undef~ine" "var~iable" "whenever")
1556 "\\|"
1557 (concat "\\(?:"
1558 (sql-regexp-abbrev "comp~ute")
1559 "\\s-+"
1560 (sql-regexp-abbrev-list
1561 "avg" "cou~nt" "min~imum" "max~imum" "num~ber" "sum"
1562 "std" "var~iance")
1563 "\\)")
1564 "\\|"
1565 (concat "\\(?:set\\s-+"
1566 (sql-regexp-abbrev-list
1567 "appi~nfo" "array~size" "auto~commit" "autop~rint"
1568 "autorecovery" "autot~race" "blo~ckterminator"
1569 "cmds~ep" "colsep" "com~patibility" "con~cat"
1570 "copyc~ommit" "copytypecheck" "def~ine" "describe"
1571 "echo" "editf~ile" "emb~edded" "esc~ape" "feed~back"
1572 "flagger" "flu~sh" "hea~ding" "heads~ep" "instance"
1573 "lin~esize" "lobof~fset" "long" "longc~hunksize"
1574 "mark~up" "newp~age" "null" "numf~ormat" "num~width"
1575 "pages~ize" "pau~se" "recsep" "recsepchar"
1576 "scan" "serverout~put" "shift~inout" "show~mode"
1577 "sqlbl~anklines" "sqlc~ase" "sqlco~ntinue"
1578 "sqln~umber" "sqlpluscompat~ibility" "sqlpre~fix"
1579 "sqlp~rompt" "sqlt~erminator" "suf~fix" "tab"
1580 "term~out" "ti~me" "timi~ng" "trim~out" "trims~pool"
1581 "und~erline" "ver~ify" "wra~p")
1582 "\\)")
1583
1584 "\\)\\(?:\\s-.*\\)?\\(?:[-]\n.*\\)*$")
1585 0 'font-lock-doc-face t)
1586
1587 ;; Oracle Functions
1588 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1589 "abs" "acos" "add_months" "appendchildxml" "ascii" "asciistr" "asin"
1590 "atan" "atan2" "avg" "bfilename" "bin_to_num" "bitand" "cardinality"
1591 "cast" "ceil" "chartorowid" "chr" "cluster_id" "cluster_probability"
1592 "cluster_set" "coalesce" "collect" "compose" "concat" "convert" "corr"
1593 "connect_by_root" "connect_by_iscycle" "connect_by_isleaf"
1594 "corr_k" "corr_s" "cos" "cosh" "count" "covar_pop" "covar_samp"
1595 "cube_table" "cume_dist" "current_date" "current_timestamp" "cv"
1596 "dataobj_to_partition" "dbtimezone" "decode" "decompose" "deletexml"
1597 "dense_rank" "depth" "deref" "dump" "empty_blob" "empty_clob"
1598 "existsnode" "exp" "extract" "extractvalue" "feature_id" "feature_set"
1599 "feature_value" "first" "first_value" "floor" "from_tz" "greatest"
1600 "grouping" "grouping_id" "group_id" "hextoraw" "initcap"
1601 "insertchildxml" "insertchildxmlafter" "insertchildxmlbefore"
1602 "insertxmlafter" "insertxmlbefore" "instr" "instr2" "instr4" "instrb"
1603 "instrc" "iteration_number" "lag" "last" "last_day" "last_value"
1604 "lead" "least" "length" "length2" "length4" "lengthb" "lengthc"
1605 "listagg" "ln" "lnnvl" "localtimestamp" "log" "lower" "lpad" "ltrim"
1606 "make_ref" "max" "median" "min" "mod" "months_between" "nanvl" "nchr"
1607 "new_time" "next_day" "nlssort" "nls_charset_decl_len"
1608 "nls_charset_id" "nls_charset_name" "nls_initcap" "nls_lower"
1609 "nls_upper" "nth_value" "ntile" "nullif" "numtodsinterval"
1610 "numtoyminterval" "nvl" "nvl2" "ora_dst_affected" "ora_dst_convert"
1611 "ora_dst_error" "ora_hash" "path" "percentile_cont" "percentile_disc"
1612 "percent_rank" "power" "powermultiset" "powermultiset_by_cardinality"
1613 "prediction" "prediction_bounds" "prediction_cost"
1614 "prediction_details" "prediction_probability" "prediction_set"
1615 "presentnnv" "presentv" "previous" "rank" "ratio_to_report" "rawtohex"
1616 "rawtonhex" "ref" "reftohex" "regexp_count" "regexp_instr"
1617 "regexp_replace" "regexp_substr" "regr_avgx" "regr_avgy" "regr_count"
1618 "regr_intercept" "regr_r2" "regr_slope" "regr_sxx" "regr_sxy"
1619 "regr_syy" "remainder" "replace" "round" "rowidtochar" "rowidtonchar"
1620 "row_number" "rpad" "rtrim" "scn_to_timestamp" "sessiontimezone" "set"
1621 "sign" "sin" "sinh" "soundex" "sqrt" "stats_binomial_test"
1622 "stats_crosstab" "stats_f_test" "stats_ks_test" "stats_mode"
1623 "stats_mw_test" "stats_one_way_anova" "stats_t_test_indep"
1624 "stats_t_test_indepu" "stats_t_test_one" "stats_t_test_paired"
1625 "stats_wsr_test" "stddev" "stddev_pop" "stddev_samp" "substr"
1626 "substr2" "substr4" "substrb" "substrc" "sum" "sysdate" "systimestamp"
1627 "sys_connect_by_path" "sys_context" "sys_dburigen" "sys_extract_utc"
1628 "sys_guid" "sys_typeid" "sys_xmlagg" "sys_xmlgen" "tan" "tanh"
1629 "timestamp_to_scn" "to_binary_double" "to_binary_float" "to_blob"
1630 "to_char" "to_clob" "to_date" "to_dsinterval" "to_lob" "to_multi_byte"
1631 "to_nchar" "to_nclob" "to_number" "to_single_byte" "to_timestamp"
1632 "to_timestamp_tz" "to_yminterval" "translate" "treat" "trim" "trunc"
1633 "tz_offset" "uid" "unistr" "updatexml" "upper" "user" "userenv"
1634 "value" "variance" "var_pop" "var_samp" "vsize" "width_bucket"
1635 "xmlagg" "xmlcast" "xmlcdata" "xmlcolattval" "xmlcomment" "xmlconcat"
1636 "xmldiff" "xmlelement" "xmlexists" "xmlforest" "xmlisvalid" "xmlparse"
1637 "xmlpatch" "xmlpi" "xmlquery" "xmlroot" "xmlsequence" "xmlserialize"
1638 "xmltable" "xmltransform"
1639 )
1640
1641 ;; See the table V$RESERVED_WORDS
1642 ;; Oracle Keywords
1643 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1644 "abort" "access" "accessed" "account" "activate" "add" "admin"
1645 "advise" "after" "agent" "aggregate" "all" "allocate" "allow" "alter"
1646 "always" "analyze" "ancillary" "and" "any" "apply" "archive"
1647 "archivelog" "array" "as" "asc" "associate" "at" "attribute"
1648 "attributes" "audit" "authenticated" "authid" "authorization" "auto"
1649 "autoallocate" "automatic" "availability" "backup" "before" "begin"
1650 "behalf" "between" "binding" "bitmap" "block" "blocksize" "body"
1651 "both" "buffer_pool" "build" "by" "cache" "call" "cancel"
1652 "cascade" "case" "category" "certificate" "chained" "change" "check"
1653 "checkpoint" "child" "chunk" "class" "clear" "clone" "close" "cluster"
1654 "column" "column_value" "columns" "comment" "commit" "committed"
1655 "compatibility" "compile" "complete" "composite_limit" "compress"
1656 "compute" "connect" "connect_time" "consider" "consistent"
1657 "constraint" "constraints" "constructor" "contents" "context"
1658 "continue" "controlfile" "corruption" "cost" "cpu_per_call"
1659 "cpu_per_session" "create" "cross" "cube" "current" "currval" "cycle"
1660 "dangling" "data" "database" "datafile" "datafiles" "day" "ddl"
1661 "deallocate" "debug" "default" "deferrable" "deferred" "definer"
1662 "delay" "delete" "demand" "desc" "determines" "deterministic"
1663 "dictionary" "dimension" "directory" "disable" "disassociate"
1664 "disconnect" "distinct" "distinguished" "distributed" "dml" "drop"
1665 "each" "element" "else" "enable" "end" "equals_path" "escape"
1666 "estimate" "except" "exceptions" "exchange" "excluding" "exists"
1667 "expire" "explain" "extent" "external" "externally"
1668 "failed_login_attempts" "fast" "file" "final" "finish" "flush" "for"
1669 "force" "foreign" "freelist" "freelists" "freepools" "fresh" "from"
1670 "full" "function" "functions" "generated" "global" "global_name"
1671 "globally" "grant" "group" "grouping" "groups" "guard" "hash"
1672 "hashkeys" "having" "heap" "hierarchy" "id" "identified" "identifier"
1673 "idle_time" "immediate" "in" "including" "increment" "index" "indexed"
1674 "indexes" "indextype" "indextypes" "indicator" "initial" "initialized"
1675 "initially" "initrans" "inner" "insert" "instance" "instantiable"
1676 "instead" "intersect" "into" "invalidate" "is" "isolation" "java"
1677 "join" "keep" "key" "kill" "language" "left" "less" "level"
1678 "levels" "library" "like" "like2" "like4" "likec" "limit" "link"
1679 "list" "lob" "local" "location" "locator" "lock" "log" "logfile"
1680 "logging" "logical" "logical_reads_per_call"
1681 "logical_reads_per_session" "managed" "management" "manual" "map"
1682 "mapping" "master" "matched" "materialized" "maxdatafiles"
1683 "maxextents" "maximize" "maxinstances" "maxlogfiles" "maxloghistory"
1684 "maxlogmembers" "maxsize" "maxtrans" "maxvalue" "member" "memory"
1685 "merge" "migrate" "minextents" "minimize" "minimum" "minus" "minvalue"
1686 "mode" "modify" "monitoring" "month" "mount" "move" "movement" "name"
1687 "named" "natural" "nested" "never" "new" "next" "nextval" "no"
1688 "noarchivelog" "noaudit" "nocache" "nocompress" "nocopy" "nocycle"
1689 "nodelay" "noforce" "nologging" "nomapping" "nomaxvalue" "nominimize"
1690 "nominvalue" "nomonitoring" "none" "noorder" "noparallel" "norely"
1691 "noresetlogs" "noreverse" "normal" "norowdependencies" "nosort"
1692 "noswitch" "not" "nothing" "notimeout" "novalidate" "nowait" "null"
1693 "nulls" "object" "of" "off" "offline" "oidindex" "old" "on" "online"
1694 "only" "open" "operator" "optimal" "option" "or" "order"
1695 "organization" "out" "outer" "outline" "overflow" "overriding"
1696 "package" "packages" "parallel" "parallel_enable" "parameters"
1697 "parent" "partition" "partitions" "password" "password_grace_time"
1698 "password_life_time" "password_lock_time" "password_reuse_max"
1699 "password_reuse_time" "password_verify_function" "pctfree"
1700 "pctincrease" "pctthreshold" "pctused" "pctversion" "percent"
1701 "performance" "permanent" "pfile" "physical" "pipelined" "plan"
1702 "post_transaction" "pragma" "prebuilt" "preserve" "primary" "private"
1703 "private_sga" "privileges" "procedure" "profile" "protection" "public"
1704 "purge" "query" "quiesce" "quota" "range" "read" "reads" "rebuild"
1705 "records_per_block" "recover" "recovery" "recycle" "reduced" "ref"
1706 "references" "referencing" "refresh" "register" "reject" "relational"
1707 "rely" "rename" "reset" "resetlogs" "resize" "resolve" "resolver"
1708 "resource" "restrict" "restrict_references" "restricted" "result"
1709 "resumable" "resume" "retention" "return" "returning" "reuse"
1710 "reverse" "revoke" "rewrite" "right" "rnds" "rnps" "role" "roles"
1711 "rollback" "rollup" "row" "rowdependencies" "rownum" "rows" "sample"
1712 "savepoint" "scan" "schema" "scn" "scope" "segment" "select"
1713 "selectivity" "self" "sequence" "serializable" "session"
1714 "sessions_per_user" "set" "sets" "settings" "shared" "shared_pool"
1715 "shrink" "shutdown" "siblings" "sid" "single" "size" "skip" "some"
1716 "sort" "source" "space" "specification" "spfile" "split" "standby"
1717 "start" "statement_id" "static" "statistics" "stop" "storage" "store"
1718 "structure" "subpartition" "subpartitions" "substitutable"
1719 "successful" "supplemental" "suspend" "switch" "switchover" "synonym"
1720 "sys" "system" "table" "tables" "tablespace" "tempfile" "template"
1721 "temporary" "test" "than" "then" "thread" "through" "time_zone"
1722 "timeout" "to" "trace" "transaction" "trigger" "triggers" "truncate"
1723 "trust" "type" "types" "unarchived" "under" "under_path" "undo"
1724 "uniform" "union" "unique" "unlimited" "unlock" "unquiesce"
1725 "unrecoverable" "until" "unusable" "unused" "update" "upgrade" "usage"
1726 "use" "using" "validate" "validation" "value" "values" "variable"
1727 "varray" "version" "view" "wait" "when" "whenever" "where" "with"
1728 "without" "wnds" "wnps" "work" "write" "xmldata" "xmlschema" "xmltype"
1729 )
1730
1731 ;; Oracle Data Types
1732 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1733 "bfile" "binary_double" "binary_float" "blob" "byte" "char" "charbyte"
1734 "clob" "date" "day" "float" "interval" "local" "long" "longraw"
1735 "minute" "month" "nchar" "nclob" "number" "nvarchar2" "raw" "rowid" "second"
1736 "time" "timestamp" "urowid" "varchar2" "with" "year" "zone"
1737 )
1738
1739 ;; Oracle PL/SQL Attributes
1740 (sql-font-lock-keywords-builder 'font-lock-builtin-face '("%" . "\\b")
1741 "bulk_exceptions" "bulk_rowcount" "found" "isopen" "notfound"
1742 "rowcount" "rowtype" "type"
1743 )
1744
1745 ;; Oracle PL/SQL Functions
1746 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1747 "delete" "trim" "extend" "exists" "first" "last" "count" "limit"
1748 "prior" "next"
1749 )
1750
1751 ;; Oracle PL/SQL Reserved words
1752 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1753 "all" "alter" "and" "any" "as" "asc" "at" "begin" "between" "by"
1754 "case" "check" "clusters" "cluster" "colauth" "columns" "compress"
1755 "connect" "crash" "create" "cursor" "declare" "default" "desc"
1756 "distinct" "drop" "else" "end" "exception" "exclusive" "fetch" "for"
1757 "from" "function" "goto" "grant" "group" "having" "identified" "if"
1758 "in" "index" "indexes" "insert" "intersect" "into" "is" "like" "lock"
1759 "minus" "mode" "nocompress" "not" "nowait" "null" "of" "on" "option"
1760 "or" "order" "overlaps" "procedure" "public" "resource" "revoke"
1761 "select" "share" "size" "sql" "start" "subtype" "tabauth" "table"
1762 "then" "to" "type" "union" "unique" "update" "values" "view" "views"
1763 "when" "where" "with"
1764
1765 "true" "false"
1766 "raise_application_error"
1767 )
1768
1769 ;; Oracle PL/SQL Keywords
1770 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1771 "a" "add" "agent" "aggregate" "array" "attribute" "authid" "avg"
1772 "bfile_base" "binary" "blob_base" "block" "body" "both" "bound" "bulk"
1773 "byte" "c" "call" "calling" "cascade" "char" "char_base" "character"
1774 "charset" "charsetform" "charsetid" "clob_base" "close" "collect"
1775 "comment" "commit" "committed" "compiled" "constant" "constructor"
1776 "context" "continue" "convert" "count" "current" "customdatum"
1777 "dangling" "data" "date" "date_base" "day" "define" "delete"
1778 "deterministic" "double" "duration" "element" "elsif" "empty" "escape"
1779 "except" "exceptions" "execute" "exists" "exit" "external" "final"
1780 "fixed" "float" "forall" "force" "general" "hash" "heap" "hidden"
1781 "hour" "immediate" "including" "indicator" "indices" "infinite"
1782 "instantiable" "int" "interface" "interval" "invalidate" "isolation"
1783 "java" "language" "large" "leading" "length" "level" "library" "like2"
1784 "like4" "likec" "limit" "limited" "local" "long" "loop" "map" "max"
1785 "maxlen" "member" "merge" "min" "minute" "mod" "modify" "month"
1786 "multiset" "name" "nan" "national" "native" "nchar" "new" "nocopy"
1787 "number_base" "object" "ocicoll" "ocidate" "ocidatetime" "ociduration"
1788 "ociinterval" "ociloblocator" "ocinumber" "ociraw" "ociref"
1789 "ocirefcursor" "ocirowid" "ocistring" "ocitype" "old" "only" "opaque"
1790 "open" "operator" "oracle" "oradata" "organization" "orlany" "orlvary"
1791 "others" "out" "overriding" "package" "parallel_enable" "parameter"
1792 "parameters" "parent" "partition" "pascal" "pipe" "pipelined" "pragma"
1793 "precision" "prior" "private" "raise" "range" "raw" "read" "record"
1794 "ref" "reference" "relies_on" "rem" "remainder" "rename" "result"
1795 "result_cache" "return" "returning" "reverse" "rollback" "row"
1796 "sample" "save" "savepoint" "sb1" "sb2" "sb4" "second" "segment"
1797 "self" "separate" "sequence" "serializable" "set" "short" "size_t"
1798 "some" "sparse" "sqlcode" "sqldata" "sqlname" "sqlstate" "standard"
1799 "static" "stddev" "stored" "string" "struct" "style" "submultiset"
1800 "subpartition" "substitutable" "sum" "synonym" "tdo" "the" "time"
1801 "timestamp" "timezone_abbr" "timezone_hour" "timezone_minute"
1802 "timezone_region" "trailing" "transaction" "transactional" "trusted"
1803 "ub1" "ub2" "ub4" "under" "unsigned" "untrusted" "use" "using"
1804 "valist" "value" "variable" "variance" "varray" "varying" "void"
1805 "while" "work" "wrapped" "write" "year" "zone"
1806 ;; Pragma
1807 "autonomous_transaction" "exception_init" "inline"
1808 "restrict_references" "serially_reusable"
1809 )
1810
1811 ;; Oracle PL/SQL Data Types
1812 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1813 "\"BINARY LARGE OBJECT\"" "\"CHAR LARGE OBJECT\"" "\"CHAR VARYING\""
1814 "\"CHARACTER LARGE OBJECT\"" "\"CHARACTER VARYING\""
1815 "\"DOUBLE PRECISION\"" "\"INTERVAL DAY TO SECOND\""
1816 "\"INTERVAL YEAR TO MONTH\"" "\"LONG RAW\"" "\"NATIONAL CHAR\""
1817 "\"NATIONAL CHARACTER LARGE OBJECT\"" "\"NATIONAL CHARACTER\""
1818 "\"NCHAR LARGE OBJECT\"" "\"NCHAR\"" "\"NCLOB\"" "\"NVARCHAR2\""
1819 "\"TIME WITH TIME ZONE\"" "\"TIMESTAMP WITH LOCAL TIME ZONE\""
1820 "\"TIMESTAMP WITH TIME ZONE\""
1821 "bfile" "bfile_base" "binary_double" "binary_float" "binary_integer"
1822 "blob" "blob_base" "boolean" "char" "character" "char_base" "clob"
1823 "clob_base" "cursor" "date" "day" "dec" "decimal"
1824 "dsinterval_unconstrained" "float" "int" "integer" "interval" "local"
1825 "long" "mlslabel" "month" "natural" "naturaln" "nchar_cs" "number"
1826 "number_base" "numeric" "pls_integer" "positive" "positiven" "raw"
1827 "real" "ref" "rowid" "second" "signtype" "simple_double"
1828 "simple_float" "simple_integer" "smallint" "string" "time" "timestamp"
1829 "timestamp_ltz_unconstrained" "timestamp_tz_unconstrained"
1830 "timestamp_unconstrained" "time_tz_unconstrained" "time_unconstrained"
1831 "to" "urowid" "varchar" "varchar2" "with" "year"
1832 "yminterval_unconstrained" "zone"
1833 )
1834
1835 ;; Oracle PL/SQL Exceptions
1836 (sql-font-lock-keywords-builder 'font-lock-warning-face nil
1837 "access_into_null" "case_not_found" "collection_is_null"
1838 "cursor_already_open" "dup_val_on_index" "invalid_cursor"
1839 "invalid_number" "login_denied" "no_data_found" "no_data_needed"
1840 "not_logged_on" "program_error" "rowtype_mismatch" "self_is_null"
1841 "storage_error" "subscript_beyond_count" "subscript_outside_limit"
1842 "sys_invalid_rowid" "timeout_on_resource" "too_many_rows"
1843 "value_error" "zero_divide"
1844 )))
1845
1846 "Oracle SQL keywords used by font-lock.
1847
1848 This variable is used by `sql-mode' and `sql-interactive-mode'. The
1849 regular expressions are created during compilation by calling the
1850 function `regexp-opt'. Therefore, take a look at the source before
1851 you define your own `sql-mode-oracle-font-lock-keywords'. You may want
1852 to add functions and PL/SQL keywords.")
1853
1854 (defvar sql-mode-postgres-font-lock-keywords
1855 (eval-when-compile
1856 (list
1857 ;; Postgres psql commands
1858 '("^\\s-*\\\\.*$" . font-lock-doc-face)
1859
1860 ;; Postgres unreserved words but may have meaning
1861 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil "a"
1862 "abs" "absent" "according" "ada" "alias" "allocate" "are" "array_agg"
1863 "asensitive" "atomic" "attribute" "attributes" "avg" "base64"
1864 "bernoulli" "bit_length" "bitvar" "blob" "blocked" "bom" "breadth" "c"
1865 "call" "cardinality" "catalog_name" "ceil" "ceiling" "char_length"
1866 "character_length" "character_set_catalog" "character_set_name"
1867 "character_set_schema" "characters" "checked" "class_origin" "clob"
1868 "cobol" "collation" "collation_catalog" "collation_name"
1869 "collation_schema" "collect" "column_name" "columns"
1870 "command_function" "command_function_code" "completion" "condition"
1871 "condition_number" "connect" "connection_name" "constraint_catalog"
1872 "constraint_name" "constraint_schema" "constructor" "contains"
1873 "control" "convert" "corr" "corresponding" "count" "covar_pop"
1874 "covar_samp" "cube" "cume_dist" "current_default_transform_group"
1875 "current_path" "current_transform_group_for_type" "cursor_name"
1876 "datalink" "datetime_interval_code" "datetime_interval_precision" "db"
1877 "defined" "degree" "dense_rank" "depth" "deref" "derived" "describe"
1878 "descriptor" "destroy" "destructor" "deterministic" "diagnostics"
1879 "disconnect" "dispatch" "dlnewcopy" "dlpreviouscopy" "dlurlcomplete"
1880 "dlurlcompleteonly" "dlurlcompletewrite" "dlurlpath" "dlurlpathonly"
1881 "dlurlpathwrite" "dlurlscheme" "dlurlserver" "dlvalue" "dynamic"
1882 "dynamic_function" "dynamic_function_code" "element" "empty"
1883 "end-exec" "equals" "every" "exception" "exec" "existing" "exp" "file"
1884 "filter" "final" "first_value" "flag" "floor" "fortran" "found" "free"
1885 "fs" "fusion" "g" "general" "generated" "get" "go" "goto" "grouping"
1886 "hex" "hierarchy" "host" "id" "ignore" "implementation" "import"
1887 "indent" "indicator" "infix" "initialize" "instance" "instantiable"
1888 "integrity" "intersection" "iterate" "k" "key_member" "key_type" "lag"
1889 "last_value" "lateral" "lead" "length" "less" "library" "like_regex"
1890 "link" "ln" "locator" "lower" "m" "map" "matched" "max"
1891 "max_cardinality" "member" "merge" "message_length"
1892 "message_octet_length" "message_text" "method" "min" "mod" "modifies"
1893 "modify" "module" "more" "multiset" "mumps" "namespace" "nclob"
1894 "nesting" "new" "nfc" "nfd" "nfkc" "nfkd" "nil" "normalize"
1895 "normalized" "nth_value" "ntile" "nullable" "number"
1896 "occurrences_regex" "octet_length" "octets" "old" "open" "operation"
1897 "ordering" "ordinality" "others" "output" "overriding" "p" "pad"
1898 "parameter" "parameter_mode" "parameter_name"
1899 "parameter_ordinal_position" "parameter_specific_catalog"
1900 "parameter_specific_name" "parameter_specific_schema" "parameters"
1901 "pascal" "passing" "passthrough" "percent_rank" "percentile_cont"
1902 "percentile_disc" "permission" "pli" "position_regex" "postfix"
1903 "power" "prefix" "preorder" "public" "rank" "reads" "recovery" "ref"
1904 "referencing" "regr_avgx" "regr_avgy" "regr_count" "regr_intercept"
1905 "regr_r2" "regr_slope" "regr_sxx" "regr_sxy" "regr_syy" "requiring"
1906 "respect" "restore" "result" "return" "returned_cardinality"
1907 "returned_length" "returned_octet_length" "returned_sqlstate" "rollup"
1908 "routine" "routine_catalog" "routine_name" "routine_schema"
1909 "row_count" "row_number" "scale" "schema_name" "scope" "scope_catalog"
1910 "scope_name" "scope_schema" "section" "selective" "self" "sensitive"
1911 "server_name" "sets" "size" "source" "space" "specific"
1912 "specific_name" "specifictype" "sql" "sqlcode" "sqlerror"
1913 "sqlexception" "sqlstate" "sqlwarning" "sqrt" "state" "static"
1914 "stddev_pop" "stddev_samp" "structure" "style" "subclass_origin"
1915 "sublist" "submultiset" "substring_regex" "sum" "system_user" "t"
1916 "table_name" "tablesample" "terminate" "than" "ties" "timezone_hour"
1917 "timezone_minute" "token" "top_level_count" "transaction_active"
1918 "transactions_committed" "transactions_rolled_back" "transform"
1919 "transforms" "translate" "translate_regex" "translation"
1920 "trigger_catalog" "trigger_name" "trigger_schema" "trim_array"
1921 "uescape" "under" "unlink" "unnamed" "unnest" "untyped" "upper" "uri"
1922 "usage" "user_defined_type_catalog" "user_defined_type_code"
1923 "user_defined_type_name" "user_defined_type_schema" "var_pop"
1924 "var_samp" "varbinary" "variable" "whenever" "width_bucket" "within"
1925 "xmlagg" "xmlbinary" "xmlcast" "xmlcomment" "xmldeclaration"
1926 "xmldocument" "xmlexists" "xmliterate" "xmlnamespaces" "xmlquery"
1927 "xmlschema" "xmltable" "xmltext" "xmlvalidate"
1928 )
1929
1930 ;; Postgres non-reserved words
1931 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1932 "abort" "absolute" "access" "action" "add" "admin" "after" "aggregate"
1933 "also" "alter" "always" "assertion" "assignment" "at" "attribute" "backward"
1934 "before" "begin" "between" "by" "cache" "called" "cascade" "cascaded"
1935 "catalog" "chain" "characteristics" "checkpoint" "class" "close"
1936 "cluster" "coalesce" "comment" "comments" "commit" "committed"
1937 "configuration" "connection" "constraints" "content" "continue"
1938 "conversion" "copy" "cost" "createdb" "createrole" "createuser" "csv"
1939 "current" "cursor" "cycle" "data" "database" "day" "deallocate" "dec"
1940 "declare" "defaults" "deferred" "definer" "delete" "delimiter"
1941 "delimiters" "dictionary" "disable" "discard" "document" "domain"
1942 "drop" "each" "enable" "encoding" "encrypted" "enum" "escape"
1943 "exclude" "excluding" "exclusive" "execute" "exists" "explain"
1944 "extension" "external" "extract" "family" "first" "float" "following" "force"
1945 "forward" "function" "functions" "global" "granted" "greatest"
1946 "handler" "header" "hold" "hour" "identity" "if" "immediate"
1947 "immutable" "implicit" "including" "increment" "index" "indexes"
1948 "inherit" "inherits" "inline" "inout" "input" "insensitive" "insert"
1949 "instead" "invoker" "isolation" "key" "label" "language" "large" "last"
1950 "lc_collate" "lc_ctype" "leakproof" "least" "level" "listen" "load" "local"
1951 "location" "lock" "login" "mapping" "match" "maxvalue" "minute"
1952 "minvalue" "mode" "month" "move" "names" "national" "nchar"
1953 "next" "no" "nocreatedb" "nocreaterole" "nocreateuser" "noinherit"
1954 "nologin" "none" "noreplication" "nosuperuser" "nothing" "notify" "nowait" "nullif"
1955 "nulls" "object" "of" "off" "oids" "operator" "option" "options" "out"
1956 "overlay" "owned" "owner" "parser" "partial" "partition" "passing" "password"
1957 "plans" "position" "preceding" "precision" "prepare" "prepared" "preserve" "prior"
1958 "privileges" "procedural" "procedure" "quote" "range" "read"
1959 "reassign" "recheck" "recursive" "ref" "reindex" "relative" "release"
1960 "rename" "repeatable" "replace" "replica" "replication" "reset" "restart" "restrict"
1961 "returns" "revoke" "role" "rollback" "row" "rows" "rule" "savepoint"
1962 "schema" "scroll" "search" "second" "security" "sequence"
1963 "serializable" "server" "session" "set" "setof" "share" "show"
1964 "simple" "snapshot" "stable" "standalone" "start" "statement" "statistics"
1965 "stdin" "stdout" "storage" "strict" "strip" "substring" "superuser"
1966 "sysid" "system" "tables" "tablespace" "temp" "template" "temporary"
1967 "transaction" "treat" "trim" "truncate" "trusted" "type" "types"
1968 "unbounded" "uncommitted" "unencrypted" "unlisten" "unlogged" "until"
1969 "update" "vacuum" "valid" "validate" "validator" "value" "values" "varying" "version"
1970 "view" "volatile" "whitespace" "without" "work" "wrapper" "write"
1971 "xmlattributes" "xmlconcat" "xmlelement" "xmlexists" "xmlforest" "xmlparse"
1972 "xmlpi" "xmlroot" "xmlserialize" "year" "yes" "zone"
1973 )
1974
1975 ;; Postgres Reserved
1976 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1977 "all" "analyse" "analyze" "and" "array" "asc" "as" "asymmetric"
1978 "authorization" "binary" "both" "case" "cast" "check" "collate"
1979 "column" "concurrently" "constraint" "create" "cross"
1980 "current_catalog" "current_date" "current_role" "current_schema"
1981 "current_time" "current_timestamp" "current_user" "default"
1982 "deferrable" "desc" "distinct" "do" "else" "end" "except" "false"
1983 "fetch" "foreign" "for" "freeze" "from" "full" "grant" "group"
1984 "having" "ilike" "initially" "inner" "in" "intersect" "into" "isnull"
1985 "is" "join" "leading" "left" "like" "limit" "localtime"
1986 "localtimestamp" "natural" "notnull" "not" "null" "offset"
1987 "only" "on" "order" "or" "outer" "overlaps" "over" "placing" "primary"
1988 "references" "returning" "right" "select" "session_user" "similar"
1989 "some" "symmetric" "table" "then" "to" "trailing" "true" "union"
1990 "unique" "user" "using" "variadic" "verbose" "when" "where" "window"
1991 "with"
1992 )
1993
1994 ;; Postgres PL/pgSQL
1995 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1996 "assign" "if" "case" "loop" "while" "for" "foreach" "exit" "elsif" "return"
1997 "raise" "execsql" "dynexecute" "perform" "getdiag" "open" "fetch" "move" "close"
1998 )
1999
2000 ;; Postgres Data Types
2001 (sql-font-lock-keywords-builder 'font-lock-type-face nil
2002 "bigint" "bigserial" "bit" "bool" "boolean" "box" "bytea" "char"
2003 "character" "cidr" "circle" "date" "decimal" "double" "float4"
2004 "float8" "inet" "int" "int2" "int4" "int8" "integer" "interval" "line"
2005 "lseg" "macaddr" "money" "name" "numeric" "path" "point" "polygon"
2006 "precision" "real" "serial" "serial4" "serial8" "sequences" "smallint" "text"
2007 "time" "timestamp" "timestamptz" "timetz" "tsquery" "tsvector"
2008 "txid_snapshot" "unknown" "uuid" "varbit" "varchar" "varying" "without"
2009 "xml" "zone"
2010 )))
2011
2012 "Postgres SQL keywords used by font-lock.
2013
2014 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2015 regular expressions are created during compilation by calling the
2016 function `regexp-opt'. Therefore, take a look at the source before
2017 you define your own `sql-mode-postgres-font-lock-keywords'.")
2018
2019 (defvar sql-mode-linter-font-lock-keywords
2020 (eval-when-compile
2021 (list
2022 ;; Linter Keywords
2023 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
2024 "autocommit" "autoinc" "autorowid" "cancel" "cascade" "channel"
2025 "committed" "count" "countblob" "cross" "current" "data" "database"
2026 "datafile" "datafiles" "datesplit" "dba" "dbname" "default" "deferred"
2027 "denied" "description" "device" "difference" "directory" "error"
2028 "escape" "euc" "exclusive" "external" "extfile" "false" "file"
2029 "filename" "filesize" "filetime" "filter" "findblob" "first" "foreign"
2030 "full" "fuzzy" "global" "granted" "ignore" "immediate" "increment"
2031 "indexes" "indexfile" "indexfiles" "indextime" "initial" "integrity"
2032 "internal" "key" "last_autoinc" "last_rowid" "limit" "linter"
2033 "linter_file_device" "linter_file_size" "linter_name_length" "ln"
2034 "local" "login" "maxisn" "maxrow" "maxrowid" "maxvalue" "message"
2035 "minvalue" "module" "names" "national" "natural" "new" "new_table"
2036 "no" "node" "noneuc" "nulliferror" "numbers" "off" "old" "old_table"
2037 "only" "operation" "optimistic" "option" "page" "partially" "password"
2038 "phrase" "plan" "precision" "primary" "priority" "privileges"
2039 "proc_info_size" "proc_par_name_len" "protocol" "quant" "range" "raw"
2040 "read" "record" "records" "references" "remote" "rename" "replication"
2041 "restart" "rewrite" "root" "row" "rule" "savepoint" "security"
2042 "sensitive" "sequence" "serializable" "server" "since" "size" "some"
2043 "startup" "statement" "station" "success" "sys_guid" "tables" "test"
2044 "timeout" "trace" "transaction" "translation" "trigger"
2045 "trigger_info_size" "true" "trunc" "uncommitted" "unicode" "unknown"
2046 "unlimited" "unlisted" "user" "utf8" "value" "varying" "volumes"
2047 "wait" "windows_code" "workspace" "write" "xml"
2048 )
2049
2050 ;; Linter Reserved
2051 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
2052 "access" "action" "add" "address" "after" "all" "alter" "always" "and"
2053 "any" "append" "as" "asc" "ascic" "async" "at_begin" "at_end" "audit"
2054 "aud_obj_name_len" "backup" "base" "before" "between" "blobfile"
2055 "blobfiles" "blobpct" "brief" "browse" "by" "case" "cast" "check"
2056 "clear" "close" "column" "comment" "commit" "connect" "contains"
2057 "correct" "create" "delete" "desc" "disable" "disconnect" "distinct"
2058 "drop" "each" "ef" "else" "enable" "end" "event" "except" "exclude"
2059 "execute" "exists" "extract" "fetch" "finish" "for" "from" "get"
2060 "grant" "group" "having" "identified" "in" "index" "inner" "insert"
2061 "instead" "intersect" "into" "is" "isolation" "join" "left" "level"
2062 "like" "lock" "mode" "modify" "not" "nowait" "null" "of" "on" "open"
2063 "or" "order" "outer" "owner" "press" "prior" "procedure" "public"
2064 "purge" "rebuild" "resource" "restrict" "revoke" "right" "role"
2065 "rollback" "rownum" "select" "session" "set" "share" "shutdown"
2066 "start" "stop" "sync" "synchronize" "synonym" "sysdate" "table" "then"
2067 "to" "union" "unique" "unlock" "until" "update" "using" "values"
2068 "view" "when" "where" "with" "without"
2069 )
2070
2071 ;; Linter Functions
2072 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
2073 "abs" "acos" "asin" "atan" "atan2" "avg" "ceil" "cos" "cosh" "divtime"
2074 "exp" "floor" "getbits" "getblob" "getbyte" "getlong" "getraw"
2075 "getstr" "gettext" "getword" "hextoraw" "lenblob" "length" "log"
2076 "lower" "lpad" "ltrim" "max" "min" "mod" "monthname" "nvl"
2077 "octet_length" "power" "rand" "rawtohex" "repeat_string"
2078 "right_substr" "round" "rpad" "rtrim" "sign" "sin" "sinh" "soundex"
2079 "sqrt" "sum" "tan" "tanh" "timeint_to_days" "to_char" "to_date"
2080 "to_gmtime" "to_localtime" "to_number" "trim" "upper" "decode"
2081 "substr" "substring" "chr" "dayname" "days" "greatest" "hex" "initcap"
2082 "instr" "least" "multime" "replace" "width"
2083 )
2084
2085 ;; Linter Data Types
2086 (sql-font-lock-keywords-builder 'font-lock-type-face nil
2087 "bigint" "bitmap" "blob" "boolean" "char" "character" "date"
2088 "datetime" "dec" "decimal" "double" "float" "int" "integer" "nchar"
2089 "number" "numeric" "real" "smallint" "varbyte" "varchar" "byte"
2090 "cursor" "long"
2091 )))
2092
2093 "Linter SQL keywords used by font-lock.
2094
2095 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2096 regular expressions are created during compilation by calling the
2097 function `regexp-opt'.")
2098
2099 (defvar sql-mode-ms-font-lock-keywords
2100 (eval-when-compile
2101 (list
2102 ;; MS isql/osql Commands
2103 (cons
2104 (concat
2105 "^\\(?:\\(?:set\\s-+\\(?:"
2106 (regexp-opt '(
2107 "datefirst" "dateformat" "deadlock_priority" "lock_timeout"
2108 "concat_null_yields_null" "cursor_close_on_commit"
2109 "disable_def_cnst_chk" "fips_flagger" "identity_insert" "language"
2110 "offsets" "quoted_identifier" "arithabort" "arithignore" "fmtonly"
2111 "nocount" "noexec" "numeric_roundabort" "parseonly"
2112 "query_governor_cost_limit" "rowcount" "textsize" "ansi_defaults"
2113 "ansi_null_dflt_off" "ansi_null_dflt_on" "ansi_nulls" "ansi_padding"
2114 "ansi_warnings" "forceplan" "showplan_all" "showplan_text"
2115 "statistics" "implicit_transactions" "remote_proc_transactions"
2116 "transaction" "xact_abort"
2117 ) t)
2118 "\\)\\)\\|go\\s-*\\|use\\s-+\\|setuser\\s-+\\|dbcc\\s-+\\).*$")
2119 'font-lock-doc-face)
2120
2121 ;; MS Reserved
2122 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
2123 "absolute" "add" "all" "alter" "and" "any" "as" "asc" "authorization"
2124 "avg" "backup" "begin" "between" "break" "browse" "bulk" "by"
2125 "cascade" "case" "check" "checkpoint" "close" "clustered" "coalesce"
2126 "column" "commit" "committed" "compute" "confirm" "constraint"
2127 "contains" "containstable" "continue" "controlrow" "convert" "count"
2128 "create" "cross" "current" "current_date" "current_time"
2129 "current_timestamp" "current_user" "database" "deallocate" "declare"
2130 "default" "delete" "deny" "desc" "disk" "distinct" "distributed"
2131 "double" "drop" "dummy" "dump" "else" "end" "errlvl" "errorexit"
2132 "escape" "except" "exec" "execute" "exists" "exit" "fetch" "file"
2133 "fillfactor" "first" "floppy" "for" "foreign" "freetext"
2134 "freetexttable" "from" "full" "goto" "grant" "group" "having"
2135 "holdlock" "identity" "identity_insert" "identitycol" "if" "in"
2136 "index" "inner" "insert" "intersect" "into" "is" "isolation" "join"
2137 "key" "kill" "last" "left" "level" "like" "lineno" "load" "max" "min"
2138 "mirrorexit" "national" "next" "nocheck" "nolock" "nonclustered" "not"
2139 "null" "nullif" "of" "off" "offsets" "on" "once" "only" "open"
2140 "opendatasource" "openquery" "openrowset" "option" "or" "order"
2141 "outer" "output" "over" "paglock" "percent" "perm" "permanent" "pipe"
2142 "plan" "precision" "prepare" "primary" "print" "prior" "privileges"
2143 "proc" "procedure" "processexit" "public" "raiserror" "read"
2144 "readcommitted" "readpast" "readtext" "readuncommitted" "reconfigure"
2145 "references" "relative" "repeatable" "repeatableread" "replication"
2146 "restore" "restrict" "return" "revoke" "right" "rollback" "rowcount"
2147 "rowguidcol" "rowlock" "rule" "save" "schema" "select" "serializable"
2148 "session_user" "set" "shutdown" "some" "statistics" "sum"
2149 "system_user" "table" "tablock" "tablockx" "tape" "temp" "temporary"
2150 "textsize" "then" "to" "top" "tran" "transaction" "trigger" "truncate"
2151 "tsequal" "uncommitted" "union" "unique" "update" "updatetext"
2152 "updlock" "use" "user" "values" "view" "waitfor" "when" "where"
2153 "while" "with" "work" "writetext" "collate" "function" "openxml"
2154 "returns"
2155 )
2156
2157 ;; MS Functions
2158 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
2159 "@@connections" "@@cpu_busy" "@@cursor_rows" "@@datefirst" "@@dbts"
2160 "@@error" "@@fetch_status" "@@identity" "@@idle" "@@io_busy"
2161 "@@langid" "@@language" "@@lock_timeout" "@@max_connections"
2162 "@@max_precision" "@@nestlevel" "@@options" "@@pack_received"
2163 "@@pack_sent" "@@packet_errors" "@@procid" "@@remserver" "@@rowcount"
2164 "@@servername" "@@servicename" "@@spid" "@@textsize" "@@timeticks"
2165 "@@total_errors" "@@total_read" "@@total_write" "@@trancount"
2166 "@@version" "abs" "acos" "and" "app_name" "ascii" "asin" "atan" "atn2"
2167 "avg" "case" "cast" "ceiling" "char" "charindex" "coalesce"
2168 "col_length" "col_name" "columnproperty" "containstable" "convert"
2169 "cos" "cot" "count" "current_timestamp" "current_user" "cursor_status"
2170 "databaseproperty" "datalength" "dateadd" "datediff" "datename"
2171 "datepart" "day" "db_id" "db_name" "degrees" "difference" "exp"
2172 "file_id" "file_name" "filegroup_id" "filegroup_name"
2173 "filegroupproperty" "fileproperty" "floor" "formatmessage"
2174 "freetexttable" "fulltextcatalogproperty" "fulltextserviceproperty"
2175 "getansinull" "getdate" "grouping" "host_id" "host_name" "ident_incr"
2176 "ident_seed" "identity" "index_col" "indexproperty" "is_member"
2177 "is_srvrolemember" "isdate" "isnull" "isnumeric" "left" "len" "log"
2178 "log10" "lower" "ltrim" "max" "min" "month" "nchar" "newid" "nullif"
2179 "object_id" "object_name" "objectproperty" "openquery" "openrowset"
2180 "parsename" "patindex" "patindex" "permissions" "pi" "power"
2181 "quotename" "radians" "rand" "replace" "replicate" "reverse" "right"
2182 "round" "rtrim" "session_user" "sign" "sin" "soundex" "space" "sqrt"
2183 "square" "stats_date" "stdev" "stdevp" "str" "stuff" "substring" "sum"
2184 "suser_id" "suser_name" "suser_sid" "suser_sname" "system_user" "tan"
2185 "textptr" "textvalid" "typeproperty" "unicode" "upper" "user"
2186 "user_id" "user_name" "var" "varp" "year"
2187 )
2188
2189 ;; MS Variables
2190 '("\\b@[a-zA-Z0-9_]*\\b" . font-lock-variable-name-face)
2191
2192 ;; MS Types
2193 (sql-font-lock-keywords-builder 'font-lock-type-face nil
2194 "binary" "bit" "char" "character" "cursor" "datetime" "dec" "decimal"
2195 "double" "float" "image" "int" "integer" "money" "national" "nchar"
2196 "ntext" "numeric" "numeric" "nvarchar" "precision" "real"
2197 "smalldatetime" "smallint" "smallmoney" "text" "timestamp" "tinyint"
2198 "uniqueidentifier" "varbinary" "varchar" "varying"
2199 )))
2200
2201 "Microsoft SQLServer SQL keywords used by font-lock.
2202
2203 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2204 regular expressions are created during compilation by calling the
2205 function `regexp-opt'. Therefore, take a look at the source before
2206 you define your own `sql-mode-ms-font-lock-keywords'.")
2207
2208 (defvar sql-mode-sybase-font-lock-keywords nil
2209 "Sybase SQL keywords used by font-lock.
2210
2211 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2212 regular expressions are created during compilation by calling the
2213 function `regexp-opt'. Therefore, take a look at the source before
2214 you define your own `sql-mode-sybase-font-lock-keywords'.")
2215
2216 (defvar sql-mode-informix-font-lock-keywords nil
2217 "Informix SQL keywords used by font-lock.
2218
2219 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2220 regular expressions are created during compilation by calling the
2221 function `regexp-opt'. Therefore, take a look at the source before
2222 you define your own `sql-mode-informix-font-lock-keywords'.")
2223
2224 (defvar sql-mode-interbase-font-lock-keywords nil
2225 "Interbase SQL keywords used by font-lock.
2226
2227 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2228 regular expressions are created during compilation by calling the
2229 function `regexp-opt'. Therefore, take a look at the source before
2230 you define your own `sql-mode-interbase-font-lock-keywords'.")
2231
2232 (defvar sql-mode-ingres-font-lock-keywords nil
2233 "Ingres SQL keywords used by font-lock.
2234
2235 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2236 regular expressions are created during compilation by calling the
2237 function `regexp-opt'. Therefore, take a look at the source before
2238 you define your own `sql-mode-interbase-font-lock-keywords'.")
2239
2240 (defvar sql-mode-solid-font-lock-keywords nil
2241 "Solid SQL keywords used by font-lock.
2242
2243 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2244 regular expressions are created during compilation by calling the
2245 function `regexp-opt'. Therefore, take a look at the source before
2246 you define your own `sql-mode-solid-font-lock-keywords'.")
2247
2248 (defvar sql-mode-mysql-font-lock-keywords
2249 (eval-when-compile
2250 (list
2251 ;; MySQL Functions
2252 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
2253 "ascii" "avg" "bdmpolyfromtext" "bdmpolyfromwkb" "bdpolyfromtext"
2254 "bdpolyfromwkb" "benchmark" "bin" "bit_and" "bit_length" "bit_or"
2255 "bit_xor" "both" "cast" "char_length" "character_length" "coalesce"
2256 "concat" "concat_ws" "connection_id" "conv" "convert" "count"
2257 "curdate" "current_date" "current_time" "current_timestamp" "curtime"
2258 "elt" "encrypt" "export_set" "field" "find_in_set" "found_rows" "from"
2259 "geomcollfromtext" "geomcollfromwkb" "geometrycollectionfromtext"
2260 "geometrycollectionfromwkb" "geometryfromtext" "geometryfromwkb"
2261 "geomfromtext" "geomfromwkb" "get_lock" "group_concat" "hex" "ifnull"
2262 "instr" "interval" "isnull" "last_insert_id" "lcase" "leading"
2263 "length" "linefromtext" "linefromwkb" "linestringfromtext"
2264 "linestringfromwkb" "load_file" "locate" "lower" "lpad" "ltrim"
2265 "make_set" "master_pos_wait" "max" "mid" "min" "mlinefromtext"
2266 "mlinefromwkb" "mpointfromtext" "mpointfromwkb" "mpolyfromtext"
2267 "mpolyfromwkb" "multilinestringfromtext" "multilinestringfromwkb"
2268 "multipointfromtext" "multipointfromwkb" "multipolygonfromtext"
2269 "multipolygonfromwkb" "now" "nullif" "oct" "octet_length" "ord"
2270 "pointfromtext" "pointfromwkb" "polyfromtext" "polyfromwkb"
2271 "polygonfromtext" "polygonfromwkb" "position" "quote" "rand"
2272 "release_lock" "repeat" "replace" "reverse" "rpad" "rtrim" "soundex"
2273 "space" "std" "stddev" "substring" "substring_index" "sum" "sysdate"
2274 "trailing" "trim" "ucase" "unix_timestamp" "upper" "user" "variance"
2275 )
2276
2277 ;; MySQL Keywords
2278 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
2279 "action" "add" "after" "against" "all" "alter" "and" "as" "asc"
2280 "auto_increment" "avg_row_length" "bdb" "between" "by" "cascade"
2281 "case" "change" "character" "check" "checksum" "close" "collate"
2282 "collation" "column" "columns" "comment" "committed" "concurrent"
2283 "constraint" "create" "cross" "data" "database" "default"
2284 "delay_key_write" "delayed" "delete" "desc" "directory" "disable"
2285 "distinct" "distinctrow" "do" "drop" "dumpfile" "duplicate" "else" "elseif"
2286 "enable" "enclosed" "end" "escaped" "exists" "fields" "first" "for"
2287 "force" "foreign" "from" "full" "fulltext" "global" "group" "handler"
2288 "having" "heap" "high_priority" "if" "ignore" "in" "index" "infile"
2289 "inner" "insert" "insert_method" "into" "is" "isam" "isolation" "join"
2290 "key" "keys" "last" "left" "level" "like" "limit" "lines" "load"
2291 "local" "lock" "low_priority" "match" "max_rows" "merge" "min_rows"
2292 "mode" "modify" "mrg_myisam" "myisam" "natural" "next" "no" "not"
2293 "null" "offset" "oj" "on" "open" "optionally" "or" "order" "outer"
2294 "outfile" "pack_keys" "partial" "password" "prev" "primary"
2295 "procedure" "quick" "raid0" "raid_type" "read" "references" "rename"
2296 "repeatable" "restrict" "right" "rollback" "rollup" "row_format"
2297 "savepoint" "select" "separator" "serializable" "session" "set"
2298 "share" "show" "sql_big_result" "sql_buffer_result" "sql_cache"
2299 "sql_calc_found_rows" "sql_no_cache" "sql_small_result" "starting"
2300 "straight_join" "striped" "table" "tables" "temporary" "terminated"
2301 "then" "to" "transaction" "truncate" "type" "uncommitted" "union"
2302 "unique" "unlock" "update" "use" "using" "values" "when" "where"
2303 "with" "write" "xor"
2304 )
2305
2306 ;; MySQL Data Types
2307 (sql-font-lock-keywords-builder 'font-lock-type-face nil
2308 "bigint" "binary" "bit" "blob" "bool" "boolean" "char" "curve" "date"
2309 "datetime" "dec" "decimal" "double" "enum" "fixed" "float" "geometry"
2310 "geometrycollection" "int" "integer" "line" "linearring" "linestring"
2311 "longblob" "longtext" "mediumblob" "mediumint" "mediumtext"
2312 "multicurve" "multilinestring" "multipoint" "multipolygon"
2313 "multisurface" "national" "numeric" "point" "polygon" "precision"
2314 "real" "smallint" "surface" "text" "time" "timestamp" "tinyblob"
2315 "tinyint" "tinytext" "unsigned" "varchar" "year" "year2" "year4"
2316 "zerofill"
2317 )))
2318
2319 "MySQL SQL keywords used by font-lock.
2320
2321 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2322 regular expressions are created during compilation by calling the
2323 function `regexp-opt'. Therefore, take a look at the source before
2324 you define your own `sql-mode-mysql-font-lock-keywords'.")
2325
2326 (defvar sql-mode-sqlite-font-lock-keywords
2327 (eval-when-compile
2328 (list
2329 ;; SQLite commands
2330 '("^[.].*$" . font-lock-doc-face)
2331
2332 ;; SQLite Keyword
2333 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
2334 "abort" "action" "add" "after" "all" "alter" "analyze" "and" "as"
2335 "asc" "attach" "autoincrement" "before" "begin" "between" "by"
2336 "cascade" "case" "cast" "check" "collate" "column" "commit" "conflict"
2337 "constraint" "create" "cross" "database" "default" "deferrable"
2338 "deferred" "delete" "desc" "detach" "distinct" "drop" "each" "else"
2339 "end" "escape" "except" "exclusive" "exists" "explain" "fail" "for"
2340 "foreign" "from" "full" "glob" "group" "having" "if" "ignore"
2341 "immediate" "in" "index" "indexed" "initially" "inner" "insert"
2342 "instead" "intersect" "into" "is" "isnull" "join" "key" "left" "like"
2343 "limit" "match" "natural" "no" "not" "notnull" "null" "of" "offset"
2344 "on" "or" "order" "outer" "plan" "pragma" "primary" "query" "raise"
2345 "references" "regexp" "reindex" "release" "rename" "replace"
2346 "restrict" "right" "rollback" "row" "savepoint" "select" "set" "table"
2347 "temp" "temporary" "then" "to" "transaction" "trigger" "union"
2348 "unique" "update" "using" "vacuum" "values" "view" "virtual" "when"
2349 "where"
2350 )
2351 ;; SQLite Data types
2352 (sql-font-lock-keywords-builder 'font-lock-type-face nil
2353 "int" "integer" "tinyint" "smallint" "mediumint" "bigint" "unsigned"
2354 "big" "int2" "int8" "character" "varchar" "varying" "nchar" "native"
2355 "nvarchar" "text" "clob" "blob" "real" "double" "precision" "float"
2356 "numeric" "number" "decimal" "boolean" "date" "datetime"
2357 )
2358 ;; SQLite Functions
2359 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
2360 ;; Core functions
2361 "abs" "changes" "coalesce" "glob" "ifnull" "hex" "last_insert_rowid"
2362 "length" "like" "load_extension" "lower" "ltrim" "max" "min" "nullif"
2363 "quote" "random" "randomblob" "replace" "round" "rtrim" "soundex"
2364 "sqlite_compileoption_get" "sqlite_compileoption_used"
2365 "sqlite_source_id" "sqlite_version" "substr" "total_changes" "trim"
2366 "typeof" "upper" "zeroblob"
2367 ;; Date/time functions
2368 "time" "julianday" "strftime"
2369 "current_date" "current_time" "current_timestamp"
2370 ;; Aggregate functions
2371 "avg" "count" "group_concat" "max" "min" "sum" "total"
2372 )))
2373
2374 "SQLite SQL keywords used by font-lock.
2375
2376 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2377 regular expressions are created during compilation by calling the
2378 function `regexp-opt'. Therefore, take a look at the source before
2379 you define your own `sql-mode-sqlite-font-lock-keywords'.")
2380
2381 (defvar sql-mode-db2-font-lock-keywords nil
2382 "DB2 SQL keywords used by font-lock.
2383
2384 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2385 regular expressions are created during compilation by calling the
2386 function `regexp-opt'. Therefore, take a look at the source before
2387 you define your own `sql-mode-db2-font-lock-keywords'.")
2388
2389 (defvar sql-mode-font-lock-keywords nil
2390 "SQL keywords used by font-lock.
2391
2392 Setting this variable directly no longer has any affect. Use
2393 `sql-product' and `sql-add-product-keywords' to control the
2394 highlighting rules in SQL mode.")
2395
2396 \f
2397
2398 ;;; SQL Product support functions
2399
2400 (defun sql-read-product (prompt &optional initial)
2401 "Read a valid SQL product."
2402 (let ((init (or (and initial (symbol-name initial)) "ansi")))
2403 (intern (completing-read
2404 prompt
2405 (mapcar (lambda (info) (symbol-name (car info)))
2406 sql-product-alist)
2407 nil 'require-match
2408 init 'sql-product-history init))))
2409
2410 (defun sql-add-product (product display &rest plist)
2411 "Add support for a database product in `sql-mode'.
2412
2413 Add PRODUCT to `sql-product-alist' which enables `sql-mode' to
2414 properly support syntax highlighting and interactive interaction.
2415 DISPLAY is the name of the SQL product that will appear in the
2416 menu bar and in messages. PLIST initializes the product
2417 configuration."
2418
2419 ;; Don't do anything if the product is already supported
2420 (if (assoc product sql-product-alist)
2421 (message "Product `%s' is already defined" product)
2422
2423 ;; Add product to the alist
2424 (add-to-list 'sql-product-alist `((,product :name ,display . ,plist)))
2425 ;; Add a menu item to the SQL->Product menu
2426 (easy-menu-add-item sql-mode-menu '("Product")
2427 ;; Each product is represented by a radio
2428 ;; button with it's display name.
2429 `[,display
2430 (sql-set-product ',product)
2431 :style radio
2432 :selected (eq sql-product ',product)]
2433 ;; Maintain the product list in
2434 ;; (case-insensitive) alphabetic order of the
2435 ;; display names. Loop thru each keymap item
2436 ;; looking for an item whose display name is
2437 ;; after this product's name.
2438 (let ((next-item)
2439 (down-display (downcase display)))
2440 (map-keymap (lambda (k b)
2441 (when (and (not next-item)
2442 (string-lessp down-display
2443 (downcase (cadr b))))
2444 (setq next-item k)))
2445 (easy-menu-get-map sql-mode-menu '("Product")))
2446 next-item))
2447 product))
2448
2449 (defun sql-del-product (product)
2450 "Remove support for PRODUCT in `sql-mode'."
2451
2452 ;; Remove the menu item based on the display name
2453 (easy-menu-remove-item sql-mode-menu '("Product") (sql-get-product-feature product :name))
2454 ;; Remove the product alist item
2455 (setq sql-product-alist (assq-delete-all product sql-product-alist))
2456 nil)
2457
2458 (defun sql-set-product-feature (product feature newvalue)
2459 "Set FEATURE of database PRODUCT to NEWVALUE.
2460
2461 The PRODUCT must be a symbol which identifies the database
2462 product. The product must have already exist on the product
2463 list. See `sql-add-product' to add new products. The FEATURE
2464 argument must be a plist keyword accepted by
2465 `sql-product-alist'."
2466
2467 (let* ((p (assoc product sql-product-alist))
2468 (v (plist-get (cdr p) feature)))
2469 (if p
2470 (if (and
2471 (member feature sql-indirect-features)
2472 (symbolp v))
2473 (set v newvalue)
2474 (setcdr p (plist-put (cdr p) feature newvalue)))
2475 (message "`%s' is not a known product; use `sql-add-product' to add it first." product))))
2476
2477 (defun sql-get-product-feature (product feature &optional fallback not-indirect)
2478 "Lookup FEATURE associated with a SQL PRODUCT.
2479
2480 If the FEATURE is nil for PRODUCT, and FALLBACK is specified,
2481 then the FEATURE associated with the FALLBACK product is
2482 returned.
2483
2484 If the FEATURE is in the list `sql-indirect-features', and the
2485 NOT-INDIRECT parameter is not set, then the value of the symbol
2486 stored in the connect alist is returned.
2487
2488 See `sql-product-alist' for a list of products and supported features."
2489 (let* ((p (assoc product sql-product-alist))
2490 (v (plist-get (cdr p) feature)))
2491
2492 (if p
2493 ;; If no value and fallback, lookup feature for fallback
2494 (if (and (not v)
2495 fallback
2496 (not (eq product fallback)))
2497 (sql-get-product-feature fallback feature)
2498
2499 (if (and
2500 (member feature sql-indirect-features)
2501 (not not-indirect)
2502 (symbolp v))
2503 (symbol-value v)
2504 v))
2505 (message "`%s' is not a known product; use `sql-add-product' to add it first." product)
2506 nil)))
2507
2508 (defun sql-product-font-lock (keywords-only imenu)
2509 "Configure font-lock and imenu with product-specific settings.
2510
2511 The KEYWORDS-ONLY flag is passed to font-lock to specify whether
2512 only keywords should be highlighted and syntactic highlighting
2513 skipped. The IMENU flag indicates whether `imenu-mode' should
2514 also be configured."
2515
2516 (let
2517 ;; Get the product-specific syntax-alist.
2518 ((syntax-alist (sql-product-font-lock-syntax-alist)))
2519
2520 ;; Get the product-specific keywords.
2521 (set (make-local-variable 'sql-mode-font-lock-keywords)
2522 (append
2523 (unless (eq sql-product 'ansi)
2524 (sql-get-product-feature sql-product :font-lock))
2525 ;; Always highlight ANSI keywords
2526 (sql-get-product-feature 'ansi :font-lock)
2527 ;; Fontify object names in CREATE, DROP and ALTER DDL
2528 ;; statements
2529 (list sql-mode-font-lock-object-name)))
2530
2531 ;; Setup font-lock. Force re-parsing of `font-lock-defaults'.
2532 (kill-local-variable 'font-lock-set-defaults)
2533 (set (make-local-variable 'font-lock-defaults)
2534 (list 'sql-mode-font-lock-keywords
2535 keywords-only t syntax-alist))
2536
2537 ;; Force font lock to reinitialize if it is already on
2538 ;; Otherwise, we can wait until it can be started.
2539 (when (and (fboundp 'font-lock-mode)
2540 (boundp 'font-lock-mode)
2541 font-lock-mode)
2542 (font-lock-mode-internal nil)
2543 (font-lock-mode-internal t))
2544
2545 (add-hook 'font-lock-mode-hook
2546 (lambda ()
2547 ;; Provide defaults for new font-lock faces.
2548 (defvar font-lock-builtin-face
2549 (if (boundp 'font-lock-preprocessor-face)
2550 font-lock-preprocessor-face
2551 font-lock-keyword-face))
2552 (defvar font-lock-doc-face font-lock-string-face))
2553 nil t)
2554
2555 ;; Setup imenu; it needs the same syntax-alist.
2556 (when imenu
2557 (setq imenu-syntax-alist syntax-alist))))
2558
2559 ;;;###autoload
2560 (defun sql-add-product-keywords (product keywords &optional append)
2561 "Add highlighting KEYWORDS for SQL PRODUCT.
2562
2563 PRODUCT should be a symbol, the name of a SQL product, such as
2564 `oracle'. KEYWORDS should be a list; see the variable
2565 `font-lock-keywords'. By default they are added at the beginning
2566 of the current highlighting list. If optional argument APPEND is
2567 `set', they are used to replace the current highlighting list.
2568 If APPEND is any other non-nil value, they are added at the end
2569 of the current highlighting list.
2570
2571 For example:
2572
2573 (sql-add-product-keywords 'ms
2574 '((\"\\\\b\\\\w+_t\\\\b\" . font-lock-type-face)))
2575
2576 adds a fontification pattern to fontify identifiers ending in
2577 `_t' as data types."
2578
2579 (let* ((sql-indirect-features nil)
2580 (font-lock-var (sql-get-product-feature product :font-lock))
2581 (old-val))
2582
2583 (setq old-val (symbol-value font-lock-var))
2584 (set font-lock-var
2585 (if (eq append 'set)
2586 keywords
2587 (if append
2588 (append old-val keywords)
2589 (append keywords old-val))))))
2590
2591 (defun sql-for-each-login (login-params body)
2592 "Iterate through login parameters and return a list of results."
2593 (delq nil
2594 (mapcar
2595 (lambda (param)
2596 (let ((token (or (car-safe param) param))
2597 (plist (cdr-safe param)))
2598 (funcall body token plist)))
2599 login-params)))
2600
2601 \f
2602
2603 ;;; Functions to switch highlighting
2604
2605 (defun sql-product-syntax-table ()
2606 (let ((table (copy-syntax-table sql-mode-syntax-table)))
2607 (mapc (lambda (entry)
2608 (modify-syntax-entry (car entry) (cdr entry) table))
2609 (sql-get-product-feature sql-product :syntax-alist))
2610 table))
2611
2612 (defun sql-product-font-lock-syntax-alist ()
2613 (append
2614 ;; Change all symbol character to word characters
2615 (mapcar
2616 (lambda (entry) (if (string= (substring (cdr entry) 0 1) "_")
2617 (cons (car entry)
2618 (concat "w" (substring (cdr entry) 1)))
2619 entry))
2620 (sql-get-product-feature sql-product :syntax-alist))
2621 '((?_ . "w"))))
2622
2623 (defun sql-highlight-product ()
2624 "Turn on the font highlighting for the SQL product selected."
2625 (when (derived-mode-p 'sql-mode)
2626 ;; Enhance the syntax table for the product
2627 (set-syntax-table (sql-product-syntax-table))
2628
2629 ;; Setup font-lock
2630 (sql-product-font-lock nil t)
2631
2632 ;; Set the mode name to include the product.
2633 (setq mode-name (concat "SQL[" (or (sql-get-product-feature sql-product :name)
2634 (symbol-name sql-product)) "]"))))
2635
2636 (defun sql-set-product (product)
2637 "Set `sql-product' to PRODUCT and enable appropriate highlighting."
2638 (interactive
2639 (list (sql-read-product "SQL product: ")))
2640 (if (stringp product) (setq product (intern product)))
2641 (when (not (assoc product sql-product-alist))
2642 (error "SQL product %s is not supported; treated as ANSI" product)
2643 (setq product 'ansi))
2644
2645 ;; Save product setting and fontify.
2646 (setq sql-product product)
2647 (sql-highlight-product))
2648 \f
2649
2650 ;;; Compatibility functions
2651
2652 (if (not (fboundp 'comint-line-beginning-position))
2653 ;; comint-line-beginning-position is defined in Emacs 21
2654 (defun comint-line-beginning-position ()
2655 "Return the buffer position of the beginning of the line, after any prompt.
2656 The prompt is assumed to be any text at the beginning of the line
2657 matching the regular expression `comint-prompt-regexp', a buffer
2658 local variable."
2659 (save-excursion (comint-bol nil) (point))))
2660
2661 ;;; SMIE support
2662
2663 ;; Needs a lot more love than I can provide. --Stef
2664
2665 ;; (require 'smie)
2666
2667 ;; (defconst sql-smie-grammar
2668 ;; (smie-prec2->grammar
2669 ;; (smie-bnf->prec2
2670 ;; ;; Partly based on http://www.h2database.com/html/grammar.html
2671 ;; '((cmd ("SELECT" select-exp "FROM" select-table-exp)
2672 ;; )
2673 ;; (select-exp ("*") (exp) (exp "AS" column-alias))
2674 ;; (column-alias)
2675 ;; (select-table-exp (table-exp "WHERE" exp) (table-exp))
2676 ;; (table-exp)
2677 ;; (exp ("CASE" exp "WHEN" exp "THEN" exp "ELSE" exp "END")
2678 ;; ("CASE" exp "WHEN" exp "THEN" exp "END"))
2679 ;; ;; Random ad-hoc additions.
2680 ;; (foo (foo "," foo))
2681 ;; )
2682 ;; '((assoc ",")))))
2683
2684 ;; (defun sql-smie-rules (kind token)
2685 ;; (pcase (cons kind token)
2686 ;; (`(:list-intro . ,_) t)
2687 ;; (`(:before . "(") (smie-rule-parent))))
2688
2689 ;;; Motion Functions
2690
2691 (defun sql-statement-regexp (prod)
2692 (let* ((ansi-stmt (sql-get-product-feature 'ansi :statement))
2693 (prod-stmt (sql-get-product-feature prod :statement)))
2694 (concat "^\\<"
2695 (if prod-stmt
2696 ansi-stmt
2697 (concat "\\(" ansi-stmt "\\|" prod-stmt "\\)"))
2698 "\\>")))
2699
2700 (defun sql-beginning-of-statement (arg)
2701 "Move to the beginning of the current SQL statement."
2702 (interactive "p")
2703
2704 (let ((here (point))
2705 (regexp (sql-statement-regexp sql-product))
2706 last next)
2707
2708 ;; Go to the end of the statement before the start we desire
2709 (setq last (or (sql-end-of-statement (- arg))
2710 (point-min)))
2711 ;; And find the end after that
2712 (setq next (or (sql-end-of-statement 1)
2713 (point-max)))
2714
2715 ;; Our start must be between them
2716 (goto-char last)
2717 ;; Find an beginning-of-stmt that's not in a comment
2718 (while (and (re-search-forward regexp next t 1)
2719 (nth 7 (syntax-ppss)))
2720 (goto-char (match-end 0)))
2721 (goto-char
2722 (if (match-data)
2723 (match-beginning 0)
2724 last))
2725 (beginning-of-line)
2726 ;; If we didn't move, try again
2727 (when (= here (point))
2728 (sql-beginning-of-statement (* 2 (cl-signum arg))))))
2729
2730 (defun sql-end-of-statement (arg)
2731 "Move to the end of the current SQL statement."
2732 (interactive "p")
2733 (let ((term (sql-get-product-feature sql-product :terminator))
2734 (re-search (if (> 0 arg) 're-search-backward 're-search-forward))
2735 (here (point))
2736 (n 0))
2737 (when (consp term)
2738 (setq term (car term)))
2739 ;; Iterate until we've moved the desired number of stmt ends
2740 (while (not (= (cl-signum arg) 0))
2741 ;; if we're looking at the terminator, jump by 2
2742 (if (or (and (> 0 arg) (looking-back term))
2743 (and (< 0 arg) (looking-at term)))
2744 (setq n 2)
2745 (setq n 1))
2746 ;; If we found another end-of-stmt
2747 (if (not (apply re-search term nil t n nil))
2748 (setq arg 0)
2749 ;; count it if we're not in a comment
2750 (unless (nth 7 (syntax-ppss))
2751 (setq arg (- arg (cl-signum arg))))))
2752 (goto-char (if (match-data)
2753 (match-end 0)
2754 here))))
2755
2756 ;;; Small functions
2757
2758 (defun sql-magic-go (arg)
2759 "Insert \"o\" and call `comint-send-input'.
2760 `sql-electric-stuff' must be the symbol `go'."
2761 (interactive "P")
2762 (self-insert-command (prefix-numeric-value arg))
2763 (if (and (equal sql-electric-stuff 'go)
2764 (save-excursion
2765 (comint-bol nil)
2766 (looking-at "go\\b")))
2767 (comint-send-input)))
2768
2769 (defun sql-magic-semicolon (arg)
2770 "Insert semicolon and call `comint-send-input'.
2771 `sql-electric-stuff' must be the symbol `semicolon'."
2772 (interactive "P")
2773 (self-insert-command (prefix-numeric-value arg))
2774 (if (equal sql-electric-stuff 'semicolon)
2775 (comint-send-input)))
2776
2777 (defun sql-accumulate-and-indent ()
2778 "Continue SQL statement on the next line."
2779 (interactive)
2780 (if (fboundp 'comint-accumulate)
2781 (comint-accumulate)
2782 (newline))
2783 (indent-according-to-mode))
2784
2785 (defun sql-help-list-products (indent freep)
2786 "Generate listing of products available for use under SQLi.
2787
2788 List products with :free-software attribute set to FREEP. Indent
2789 each line with INDENT."
2790
2791 (let (sqli-func doc)
2792 (setq doc "")
2793 (dolist (p sql-product-alist)
2794 (setq sqli-func (intern (concat "sql-" (symbol-name (car p)))))
2795
2796 (if (and (fboundp sqli-func)
2797 (eq (sql-get-product-feature (car p) :free-software) freep))
2798 (setq doc
2799 (concat doc
2800 indent
2801 (or (sql-get-product-feature (car p) :name)
2802 (symbol-name (car p)))
2803 ":\t"
2804 "\\["
2805 (symbol-name sqli-func)
2806 "]\n"))))
2807 doc))
2808
2809 ;;;###autoload
2810 (eval
2811 ;; FIXME: This dynamic-docstring-function trick doesn't work for byte-compiled
2812 ;; functions, because of the lazy-loading of docstrings, which strips away
2813 ;; text properties.
2814 '(defun sql-help ()
2815 #("Show short help for the SQL modes.
2816
2817 Use an entry function to open an interactive SQL buffer. This buffer is
2818 usually named `*SQL*'. The name of the major mode is SQLi.
2819
2820 Use the following commands to start a specific SQL interpreter:
2821
2822 \\\\FREE
2823
2824 Other non-free SQL implementations are also supported:
2825
2826 \\\\NONFREE
2827
2828 But we urge you to choose a free implementation instead of these.
2829
2830 You can also use \\[sql-product-interactive] to invoke the
2831 interpreter for the current `sql-product'.
2832
2833 Once you have the SQLi buffer, you can enter SQL statements in the
2834 buffer. The output generated is appended to the buffer and a new prompt
2835 is generated. See the In/Out menu in the SQLi buffer for some functions
2836 that help you navigate through the buffer, the input history, etc.
2837
2838 If you have a really complex SQL statement or if you are writing a
2839 procedure, you can do this in a separate buffer. Put the new buffer in
2840 `sql-mode' by calling \\[sql-mode]. The name of this buffer can be
2841 anything. The name of the major mode is SQL.
2842
2843 In this SQL buffer (SQL mode), you can send the region or the entire
2844 buffer to the interactive SQL buffer (SQLi mode). The results are
2845 appended to the SQLi buffer without disturbing your SQL buffer."
2846 0 1 (dynamic-docstring-function sql--make-help-docstring))
2847 (interactive)
2848 (describe-function 'sql-help)))
2849
2850 (defun sql--make-help-docstring (doc _fun)
2851 "Insert references to loaded products into the help buffer string."
2852
2853 ;; Insert FREE software list
2854 (when (string-match "^\\(\\s-*\\)[\\\\][\\\\]FREE\\s-*\n" doc 0)
2855 (setq doc (replace-match (sql-help-list-products (match-string 1 doc) t)
2856 t t doc 0)))
2857
2858 ;; Insert non-FREE software list
2859 (when (string-match "^\\(\\s-*\\)[\\\\][\\\\]NONFREE\\s-*\n" doc 0)
2860 (setq doc (replace-match (sql-help-list-products (match-string 1 doc) nil)
2861 t t doc 0)))
2862 doc)
2863
2864 (defun sql-get-login-ext (symbol prompt history-var plist)
2865 "Prompt user with extended login parameters.
2866
2867 The global value of SYMBOL is the last value and the global value
2868 of the SYMBOL is set based on the user's input.
2869
2870 If PLIST is nil, then the user is simply prompted for a string
2871 value.
2872
2873 The property `:default' specifies the default value. If the
2874 `:number' property is non-nil then ask for a number.
2875
2876 The `:file' property prompts for a file name that must match the
2877 regexp pattern specified in its value.
2878
2879 The `:completion' property prompts for a string specified by its
2880 value. (The property value is used as the PREDICATE argument to
2881 `completing-read'.)"
2882 (set-default
2883 symbol
2884 (let* ((default (plist-get plist :default))
2885 (last-value (default-value symbol))
2886 (prompt-def
2887 (if default
2888 (if (string-match "\\(\\):[ \t]*\\'" prompt)
2889 (replace-match (format " (default \"%s\")" default) t t prompt 1)
2890 (replace-regexp-in-string "[ \t]*\\'"
2891 (format " (default \"%s\") " default)
2892 prompt t t))
2893 prompt))
2894 (use-dialog-box nil))
2895 (cond
2896 ((plist-member plist :file)
2897 (expand-file-name
2898 (read-file-name prompt
2899 (file-name-directory last-value) default t
2900 (file-name-nondirectory last-value)
2901 (when (plist-get plist :file)
2902 `(lambda (f)
2903 (string-match
2904 (concat "\\<" ,(plist-get plist :file) "\\>")
2905 (file-name-nondirectory f)))))))
2906
2907 ((plist-member plist :completion)
2908 (completing-read prompt-def (plist-get plist :completion) nil t
2909 last-value history-var default))
2910
2911 ((plist-get plist :number)
2912 (read-number prompt (or default last-value 0)))
2913
2914 (t
2915 (read-string prompt-def last-value history-var default))))))
2916
2917 (defun sql-get-login (&rest what)
2918 "Get username, password and database from the user.
2919
2920 The variables `sql-user', `sql-password', `sql-server', and
2921 `sql-database' can be customized. They are used as the default values.
2922 Usernames, servers and databases are stored in `sql-user-history',
2923 `sql-server-history' and `database-history'. Passwords are not stored
2924 in a history.
2925
2926 Parameter WHAT is a list of tokens passed as arguments in the
2927 function call. The function asks for the username if WHAT
2928 contains the symbol `user', for the password if it contains the
2929 symbol `password', for the server if it contains the symbol
2930 `server', and for the database if it contains the symbol
2931 `database'. The members of WHAT are processed in the order in
2932 which they are provided.
2933
2934 Each token may also be a list with the token in the car and a
2935 plist of options as the cdr. The following properties are
2936 supported:
2937
2938 :file <filename-regexp>
2939 :completion <list-of-strings-or-function>
2940 :default <default-value>
2941 :number t
2942
2943 In order to ask the user for username, password and database, call the
2944 function like this: (sql-get-login 'user 'password 'database)."
2945 (dolist (w what)
2946 (let ((plist (cdr-safe w)))
2947 (pcase (or (car-safe w) w)
2948 (`user
2949 (sql-get-login-ext 'sql-user "User: " 'sql-user-history plist))
2950
2951 (`password
2952 (setq-default sql-password
2953 (read-passwd "Password: " nil sql-password)))
2954
2955 (`server
2956 (sql-get-login-ext 'sql-server "Server: " 'sql-server-history plist))
2957
2958 (`database
2959 (sql-get-login-ext 'sql-database "Database: "
2960 'sql-database-history plist))
2961
2962 (`port
2963 (sql-get-login-ext 'sql-port "Port: "
2964 nil (append '(:number t) plist)))))))
2965
2966 (defun sql-find-sqli-buffer (&optional product connection)
2967 "Return the name of the current default SQLi buffer or nil.
2968 In order to qualify, the SQLi buffer must be alive, be in
2969 `sql-interactive-mode' and have a process."
2970 (let ((buf sql-buffer)
2971 (prod (or product sql-product)))
2972 (or
2973 ;; Current sql-buffer, if there is one.
2974 (and (sql-buffer-live-p buf prod connection)
2975 buf)
2976 ;; Global sql-buffer
2977 (and (setq buf (default-value 'sql-buffer))
2978 (sql-buffer-live-p buf prod connection)
2979 buf)
2980 ;; Look thru each buffer
2981 (car (apply 'append
2982 (mapcar (lambda (b)
2983 (and (sql-buffer-live-p b prod connection)
2984 (list (buffer-name b))))
2985 (buffer-list)))))))
2986
2987 (defun sql-set-sqli-buffer-generally ()
2988 "Set SQLi buffer for all SQL buffers that have none.
2989 This function checks all SQL buffers for their SQLi buffer. If their
2990 SQLi buffer is nonexistent or has no process, it is set to the current
2991 default SQLi buffer. The current default SQLi buffer is determined
2992 using `sql-find-sqli-buffer'. If `sql-buffer' is set,
2993 `sql-set-sqli-hook' is run."
2994 (interactive)
2995 (save-excursion
2996 (let ((buflist (buffer-list))
2997 (default-buffer (sql-find-sqli-buffer)))
2998 (setq-default sql-buffer default-buffer)
2999 (while (not (null buflist))
3000 (let ((candidate (car buflist)))
3001 (set-buffer candidate)
3002 (if (and (derived-mode-p 'sql-mode)
3003 (not (sql-buffer-live-p sql-buffer)))
3004 (progn
3005 (setq sql-buffer default-buffer)
3006 (when default-buffer
3007 (run-hooks 'sql-set-sqli-hook)))))
3008 (setq buflist (cdr buflist))))))
3009
3010 (defun sql-set-sqli-buffer ()
3011 "Set the SQLi buffer SQL strings are sent to.
3012
3013 Call this function in a SQL buffer in order to set the SQLi buffer SQL
3014 strings are sent to. Calling this function sets `sql-buffer' and runs
3015 `sql-set-sqli-hook'.
3016
3017 If you call it from a SQL buffer, this sets the local copy of
3018 `sql-buffer'.
3019
3020 If you call it from anywhere else, it sets the global copy of
3021 `sql-buffer'."
3022 (interactive)
3023 (let ((default-buffer (sql-find-sqli-buffer)))
3024 (if (null default-buffer)
3025 (error "There is no suitable SQLi buffer")
3026 (let ((new-buffer (read-buffer "New SQLi buffer: " default-buffer t)))
3027 (if (null (sql-buffer-live-p new-buffer))
3028 (error "Buffer %s is not a working SQLi buffer" new-buffer)
3029 (when new-buffer
3030 (setq sql-buffer new-buffer)
3031 (run-hooks 'sql-set-sqli-hook)))))))
3032
3033 (defun sql-show-sqli-buffer ()
3034 "Show the name of current SQLi buffer.
3035
3036 This is the buffer SQL strings are sent to. It is stored in the
3037 variable `sql-buffer'. See `sql-help' on how to create such a buffer."
3038 (interactive)
3039 (if (or (null sql-buffer)
3040 (null (buffer-live-p (get-buffer sql-buffer))))
3041 (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
3042 (if (null (get-buffer-process sql-buffer))
3043 (message "Buffer %s has no process." sql-buffer)
3044 (message "Current SQLi buffer is %s." sql-buffer))))
3045
3046 (defun sql-make-alternate-buffer-name ()
3047 "Return a string that can be used to rename a SQLi buffer.
3048
3049 This is used to set `sql-alternate-buffer-name' within
3050 `sql-interactive-mode'.
3051
3052 If the session was started with `sql-connect' then the alternate
3053 name would be the name of the connection.
3054
3055 Otherwise, it uses the parameters identified by the :sqlilogin
3056 parameter.
3057
3058 If all else fails, the alternate name would be the user and
3059 server/database name."
3060
3061 (let ((name ""))
3062
3063 ;; Build a name using the :sqli-login setting
3064 (setq name
3065 (apply 'concat
3066 (cdr
3067 (apply 'append nil
3068 (sql-for-each-login
3069 (sql-get-product-feature sql-product :sqli-login)
3070 (lambda (token plist)
3071 (pcase token
3072 (`user
3073 (unless (string= "" sql-user)
3074 (list "/" sql-user)))
3075 (`port
3076 (unless (or (not (numberp sql-port))
3077 (= 0 sql-port))
3078 (list ":" (number-to-string sql-port))))
3079 (`server
3080 (unless (string= "" sql-server)
3081 (list "."
3082 (if (plist-member plist :file)
3083 (file-name-nondirectory sql-server)
3084 sql-server))))
3085 (`database
3086 (unless (string= "" sql-database)
3087 (list "@"
3088 (if (plist-member plist :file)
3089 (file-name-nondirectory sql-database)
3090 sql-database))))
3091
3092 ;; (`password nil)
3093 (_ nil))))))))
3094
3095 ;; If there's a connection, use it and the name thus far
3096 (if sql-connection
3097 (format "<%s>%s" sql-connection (or name ""))
3098
3099 ;; If there is no name, try to create something meaningful
3100 (if (string= "" (or name ""))
3101 (concat
3102 (if (string= "" sql-user)
3103 (if (string= "" (user-login-name))
3104 ()
3105 (concat (user-login-name) "/"))
3106 (concat sql-user "/"))
3107 (if (string= "" sql-database)
3108 (if (string= "" sql-server)
3109 (system-name)
3110 sql-server)
3111 sql-database))
3112
3113 ;; Use the name we've got
3114 name))))
3115
3116 (defun sql-rename-buffer (&optional new-name)
3117 "Rename a SQL interactive buffer.
3118
3119 Prompts for the new name if command is preceded by
3120 \\[universal-argument]. If no buffer name is provided, then the
3121 `sql-alternate-buffer-name' is used.
3122
3123 The actual buffer name set will be \"*SQL: NEW-NAME*\". If
3124 NEW-NAME is empty, then the buffer name will be \"*SQL*\"."
3125 (interactive "P")
3126
3127 (if (not (derived-mode-p 'sql-interactive-mode))
3128 (message "Current buffer is not a SQL interactive buffer")
3129
3130 (setq sql-alternate-buffer-name
3131 (cond
3132 ((stringp new-name) new-name)
3133 ((consp new-name)
3134 (read-string "Buffer name (\"*SQL: XXX*\"; enter `XXX'): "
3135 sql-alternate-buffer-name))
3136 (t sql-alternate-buffer-name)))
3137
3138 (rename-buffer (if (string= "" sql-alternate-buffer-name)
3139 "*SQL*"
3140 (format "*SQL: %s*" sql-alternate-buffer-name))
3141 t)))
3142
3143 (defun sql-copy-column ()
3144 "Copy current column to the end of buffer.
3145 Inserts SELECT or commas if appropriate."
3146 (interactive)
3147 (let ((column))
3148 (save-excursion
3149 (setq column (buffer-substring-no-properties
3150 (progn (forward-char 1) (backward-sexp 1) (point))
3151 (progn (forward-sexp 1) (point))))
3152 (goto-char (point-max))
3153 (let ((bol (comint-line-beginning-position)))
3154 (cond
3155 ;; if empty command line, insert SELECT
3156 ((= bol (point))
3157 (insert "SELECT "))
3158 ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
3159 ((save-excursion
3160 (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
3161 bol t))
3162 (insert ", "))
3163 ;; else insert a space
3164 (t
3165 (if (eq (preceding-char) ?\s)
3166 nil
3167 (insert " ")))))
3168 ;; in any case, insert the column
3169 (insert column)
3170 (message "%s" column))))
3171
3172 ;; On Windows, SQL*Plus for Oracle turns on full buffering for stdout
3173 ;; if it is not attached to a character device; therefore placeholder
3174 ;; replacement by SQL*Plus is fully buffered. The workaround lets
3175 ;; Emacs query for the placeholders.
3176
3177 (defvar sql-placeholder-history nil
3178 "History of placeholder values used.")
3179
3180 (defun sql-placeholders-filter (string)
3181 "Replace placeholders in STRING.
3182 Placeholders are words starting with an ampersand like &this."
3183
3184 (when sql-oracle-scan-on
3185 (while (string-match "&\\(\\sw+\\)" string)
3186 (setq string (replace-match
3187 (read-from-minibuffer
3188 (format "Enter value for %s: " (match-string 1 string))
3189 nil nil nil 'sql-placeholder-history)
3190 t t string))))
3191 string)
3192
3193 ;; Using DB2 interactively, newlines must be escaped with " \".
3194 ;; The space before the backslash is relevant.
3195
3196 (defun sql-escape-newlines-filter (string)
3197 "Escape newlines in STRING.
3198 Every newline in STRING will be preceded with a space and a backslash."
3199 (if (not sql-db2-escape-newlines)
3200 string
3201 (let ((result "") (start 0) mb me)
3202 (while (string-match "\n" string start)
3203 (setq mb (match-beginning 0)
3204 me (match-end 0)
3205 result (concat result
3206 (substring string start mb)
3207 (if (and (> mb 1)
3208 (string-equal " \\" (substring string (- mb 2) mb)))
3209 "" " \\\n"))
3210 start me))
3211 (concat result (substring string start)))))
3212
3213 \f
3214
3215 ;;; Input sender for SQLi buffers
3216
3217 (defvar sql-output-newline-count 0
3218 "Number of newlines in the input string.
3219
3220 Allows the suppression of continuation prompts.")
3221
3222 (defvar sql-output-by-send nil
3223 "Non-nil if the command in the input was generated by `sql-send-string'.")
3224
3225 (defun sql-input-sender (proc string)
3226 "Send STRING to PROC after applying filters."
3227
3228 (let* ((product (with-current-buffer (process-buffer proc) sql-product))
3229 (filter (sql-get-product-feature product :input-filter)))
3230
3231 ;; Apply filter(s)
3232 (cond
3233 ((not filter)
3234 nil)
3235 ((functionp filter)
3236 (setq string (funcall filter string)))
3237 ((listp filter)
3238 (mapc (lambda (f) (setq string (funcall f string))) filter))
3239 (t nil))
3240
3241 ;; Count how many newlines in the string
3242 (setq sql-output-newline-count 0)
3243 (mapc (lambda (ch)
3244 (when (eq ch ?\n)
3245 (setq sql-output-newline-count (1+ sql-output-newline-count))))
3246 string)
3247
3248 ;; Send the string
3249 (comint-simple-send proc string)))
3250
3251 ;;; Strip out continuation prompts
3252
3253 (defvar sql-preoutput-hold nil)
3254
3255 (defun sql-interactive-remove-continuation-prompt (oline)
3256 "Strip out continuation prompts out of the OLINE.
3257
3258 Added to the `comint-preoutput-filter-functions' hook in a SQL
3259 interactive buffer. If `sql-output-newline-count' is greater than
3260 zero, then an output line matching the continuation prompt is filtered
3261 out. If the count is zero, then a newline is inserted into the output
3262 to force the output from the query to appear on a new line.
3263
3264 The complication to this filter is that the continuation prompts
3265 may arrive in multiple chunks. If they do, then the function
3266 saves any unfiltered output in a buffer and prepends that buffer
3267 to the next chunk to properly match the broken-up prompt.
3268
3269 If the filter gets confused, it should reset and stop filtering
3270 to avoid deleting non-prompt output."
3271
3272 (let (did-filter)
3273 (setq oline (concat (or sql-preoutput-hold "") oline)
3274 sql-preoutput-hold nil)
3275
3276 (if (and comint-prompt-regexp
3277 (integerp sql-output-newline-count)
3278 (>= sql-output-newline-count 1))
3279 (progn
3280 (while (and (not (string= oline ""))
3281 (> sql-output-newline-count 0)
3282 (string-match comint-prompt-regexp oline)
3283 (= (match-beginning 0) 0))
3284
3285 (setq oline (replace-match "" nil nil oline)
3286 sql-output-newline-count (1- sql-output-newline-count)
3287 did-filter t))
3288
3289 (if (= sql-output-newline-count 0)
3290 (setq sql-output-newline-count nil
3291 oline (concat "\n" oline)
3292 sql-output-by-send nil)
3293
3294 (setq sql-preoutput-hold oline
3295 oline ""))
3296
3297 (unless did-filter
3298 (setq oline (or sql-preoutput-hold "")
3299 sql-preoutput-hold nil
3300 sql-output-newline-count nil)))
3301
3302 (setq sql-output-newline-count nil))
3303
3304 oline))
3305
3306 ;;; Sending the region to the SQLi buffer.
3307
3308 (defun sql-send-string (str)
3309 "Send the string STR to the SQL process."
3310 (interactive "sSQL Text: ")
3311
3312 (let ((comint-input-sender-no-newline nil)
3313 (s (replace-regexp-in-string "[[:space:]\n\r]+\\'" "" str)))
3314 (if (sql-buffer-live-p sql-buffer)
3315 (progn
3316 ;; Ignore the hoping around...
3317 (save-excursion
3318 ;; Set product context
3319 (with-current-buffer sql-buffer
3320 ;; Send the string (trim the trailing whitespace)
3321 (sql-input-sender (get-buffer-process sql-buffer) s)
3322
3323 ;; Send a command terminator if we must
3324 (if sql-send-terminator
3325 (sql-send-magic-terminator sql-buffer s sql-send-terminator))
3326
3327 (message "Sent string to buffer %s." sql-buffer)))
3328
3329 ;; Display the sql buffer
3330 (if sql-pop-to-buffer-after-send-region
3331 (pop-to-buffer sql-buffer)
3332 (display-buffer sql-buffer)))
3333
3334 ;; We don't have no stinkin' sql
3335 (message "No SQL process started."))))
3336
3337 (defun sql-send-region (start end)
3338 "Send a region to the SQL process."
3339 (interactive "r")
3340 (sql-send-string (buffer-substring-no-properties start end)))
3341
3342 (defun sql-send-paragraph ()
3343 "Send the current paragraph to the SQL process."
3344 (interactive)
3345 (let ((start (save-excursion
3346 (backward-paragraph)
3347 (point)))
3348 (end (save-excursion
3349 (forward-paragraph)
3350 (point))))
3351 (sql-send-region start end)))
3352
3353 (defun sql-send-buffer ()
3354 "Send the buffer contents to the SQL process."
3355 (interactive)
3356 (sql-send-region (point-min) (point-max)))
3357
3358 (defun sql-send-magic-terminator (buf str terminator)
3359 "Send TERMINATOR to buffer BUF if its not present in STR."
3360 (let (comint-input-sender-no-newline pat term)
3361 ;; If flag is merely on(t), get product-specific terminator
3362 (if (eq terminator t)
3363 (setq terminator (sql-get-product-feature sql-product :terminator)))
3364
3365 ;; If there is no terminator specified, use default ";"
3366 (unless terminator
3367 (setq terminator ";"))
3368
3369 ;; Parse the setting into the pattern and the terminator string
3370 (cond ((stringp terminator)
3371 (setq pat (regexp-quote terminator)
3372 term terminator))
3373 ((consp terminator)
3374 (setq pat (car terminator)
3375 term (cdr terminator)))
3376 (t
3377 nil))
3378
3379 ;; Check to see if the pattern is present in the str already sent
3380 (unless (and pat term
3381 (string-match (concat pat "\\'") str))
3382 (comint-simple-send (get-buffer-process buf) term)
3383 (setq sql-output-newline-count
3384 (if sql-output-newline-count
3385 (1+ sql-output-newline-count)
3386 1)))
3387 (setq sql-output-by-send t)))
3388
3389 (defun sql-remove-tabs-filter (str)
3390 "Replace tab characters with spaces."
3391 (replace-regexp-in-string "\t" " " str nil t))
3392
3393 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
3394 "Toggle `sql-pop-to-buffer-after-send-region'.
3395
3396 If given the optional parameter VALUE, sets
3397 `sql-toggle-pop-to-buffer-after-send-region' to VALUE."
3398 (interactive "P")
3399 (if value
3400 (setq sql-pop-to-buffer-after-send-region value)
3401 (setq sql-pop-to-buffer-after-send-region
3402 (null sql-pop-to-buffer-after-send-region))))
3403
3404 \f
3405
3406 ;;; Redirect output functions
3407
3408 (defvar sql-debug-redirect nil
3409 "If non-nil, display messages related to the use of redirection.")
3410
3411 (defun sql-str-literal (s)
3412 (concat "'" (replace-regexp-in-string "[']" "''" s) "'"))
3413
3414 (defun sql-redirect (sqlbuf command &optional outbuf save-prior)
3415 "Execute the SQL command and send output to OUTBUF.
3416
3417 SQLBUF must be an active SQL interactive buffer. OUTBUF may be
3418 an existing buffer, or the name of a non-existing buffer. If
3419 omitted the output is sent to a temporary buffer which will be
3420 killed after the command completes. COMMAND should be a string
3421 of commands accepted by the SQLi program. COMMAND may also be a
3422 list of SQLi command strings."
3423
3424 (let* ((visible (and outbuf
3425 (not (string= " " (substring outbuf 0 1))))))
3426 (when visible
3427 (message "Executing SQL command..."))
3428 (if (consp command)
3429 (mapc (lambda (c) (sql-redirect-one sqlbuf c outbuf save-prior))
3430 command)
3431 (sql-redirect-one sqlbuf command outbuf save-prior))
3432 (when visible
3433 (message "Executing SQL command...done"))))
3434
3435 (defun sql-redirect-one (sqlbuf command outbuf save-prior)
3436 (with-current-buffer sqlbuf
3437 (let ((buf (get-buffer-create (or outbuf " *SQL-Redirect*")))
3438 (proc (get-buffer-process (current-buffer)))
3439 (comint-prompt-regexp (sql-get-product-feature sql-product
3440 :prompt-regexp))
3441 (start nil))
3442 (with-current-buffer buf
3443 (setq view-read-only nil)
3444 (unless save-prior
3445 (erase-buffer))
3446 (goto-char (point-max))
3447 (unless (zerop (buffer-size))
3448 (insert "\n"))
3449 (setq start (point)))
3450
3451 (when sql-debug-redirect
3452 (message ">>SQL> %S" command))
3453
3454 ;; Run the command
3455 (comint-redirect-send-command-to-process command buf proc nil t)
3456 (while (null comint-redirect-completed)
3457 (accept-process-output nil 1))
3458
3459 ;; Clean up the output results
3460 (with-current-buffer buf
3461 ;; Remove trailing whitespace
3462 (goto-char (point-max))
3463 (when (looking-back "[ \t\f\n\r]*" start)
3464 (delete-region (match-beginning 0) (match-end 0)))
3465 ;; Remove echo if there was one
3466 (goto-char start)
3467 (when (looking-at (concat "^" (regexp-quote command) "[\\n]"))
3468 (delete-region (match-beginning 0) (match-end 0)))
3469 ;; Remove Ctrl-Ms
3470 (goto-char start)
3471 (while (re-search-forward "\r+$" nil t)
3472 (replace-match "" t t))
3473 (goto-char start)))))
3474
3475 (defun sql-redirect-value (sqlbuf command regexp &optional regexp-groups)
3476 "Execute the SQL command and return part of result.
3477
3478 SQLBUF must be an active SQL interactive buffer. COMMAND should
3479 be a string of commands accepted by the SQLi program. From the
3480 output, the REGEXP is repeatedly matched and the list of
3481 REGEXP-GROUPS submatches is returned. This behaves much like
3482 \\[comint-redirect-results-list-from-process] but instead of
3483 returning a single submatch it returns a list of each submatch
3484 for each match."
3485
3486 (let ((outbuf " *SQL-Redirect-values*")
3487 (results nil))
3488 (sql-redirect sqlbuf command outbuf nil)
3489 (with-current-buffer outbuf
3490 (while (re-search-forward regexp nil t)
3491 (push
3492 (cond
3493 ;; no groups-return all of them
3494 ((null regexp-groups)
3495 (let ((i (/ (length (match-data)) 2))
3496 (r nil))
3497 (while (> i 0)
3498 (setq i (1- i))
3499 (push (match-string i) r))
3500 r))
3501 ;; one group specified
3502 ((numberp regexp-groups)
3503 (match-string regexp-groups))
3504 ;; list of numbers; return the specified matches only
3505 ((consp regexp-groups)
3506 (mapcar (lambda (c)
3507 (cond
3508 ((numberp c) (match-string c))
3509 ((stringp c) (match-substitute-replacement c))
3510 (t (error "sql-redirect-value: unknown REGEXP-GROUPS value - %s" c))))
3511 regexp-groups))
3512 ;; String is specified; return replacement string
3513 ((stringp regexp-groups)
3514 (match-substitute-replacement regexp-groups))
3515 (t
3516 (error "sql-redirect-value: unknown REGEXP-GROUPS value - %s"
3517 regexp-groups)))
3518 results)))
3519
3520 (when sql-debug-redirect
3521 (message ">>SQL> = %S" (reverse results)))
3522
3523 (nreverse results)))
3524
3525 (defun sql-execute (sqlbuf outbuf command enhanced arg)
3526 "Execute a command in a SQL interactive buffer and capture the output.
3527
3528 The commands are run in SQLBUF and the output saved in OUTBUF.
3529 COMMAND must be a string, a function or a list of such elements.
3530 Functions are called with SQLBUF, OUTBUF and ARG as parameters;
3531 strings are formatted with ARG and executed.
3532
3533 If the results are empty the OUTBUF is deleted, otherwise the
3534 buffer is popped into a view window."
3535 (mapc
3536 (lambda (c)
3537 (cond
3538 ((stringp c)
3539 (sql-redirect sqlbuf (if arg (format c arg) c) outbuf) t)
3540 ((functionp c)
3541 (apply c sqlbuf outbuf enhanced arg nil))
3542 (t (error "Unknown sql-execute item %s" c))))
3543 (if (consp command) command (cons command nil)))
3544
3545 (setq outbuf (get-buffer outbuf))
3546 (if (zerop (buffer-size outbuf))
3547 (kill-buffer outbuf)
3548 (let ((one-win (eq (selected-window)
3549 (get-lru-window))))
3550 (with-current-buffer outbuf
3551 (set-buffer-modified-p nil)
3552 (setq view-read-only t))
3553 (view-buffer-other-window outbuf)
3554 (when one-win
3555 (shrink-window-if-larger-than-buffer)))))
3556
3557 (defun sql-execute-feature (sqlbuf outbuf feature enhanced arg)
3558 "List objects or details in a separate display buffer."
3559 (let (command)
3560 (with-current-buffer sqlbuf
3561 (setq command (sql-get-product-feature sql-product feature)))
3562 (unless command
3563 (error "%s does not support %s" sql-product feature))
3564 (when (consp command)
3565 (setq command (if enhanced
3566 (cdr command)
3567 (car command))))
3568 (sql-execute sqlbuf outbuf command enhanced arg)))
3569
3570 (defvar sql-completion-object nil
3571 "A list of database objects used for completion.
3572
3573 The list is maintained in SQL interactive buffers.")
3574
3575 (defvar sql-completion-column nil
3576 "A list of column names used for completion.
3577
3578 The list is maintained in SQL interactive buffers.")
3579
3580 (defun sql-build-completions-1 (schema completion-list feature)
3581 "Generate a list of objects in the database for use as completions."
3582 (let ((f (sql-get-product-feature sql-product feature)))
3583 (when f
3584 (set completion-list
3585 (let (cl)
3586 (dolist (e (append (symbol-value completion-list)
3587 (apply f (current-buffer) (cons schema nil)))
3588 cl)
3589 (unless (member e cl) (setq cl (cons e cl))))
3590 (sort cl (function string<)))))))
3591
3592 (defun sql-build-completions (schema)
3593 "Generate a list of names in the database for use as completions."
3594 (sql-build-completions-1 schema 'sql-completion-object :completion-object)
3595 (sql-build-completions-1 schema 'sql-completion-column :completion-column))
3596
3597 (defvar sql-completion-sqlbuf nil)
3598
3599 (defun sql--completion-table (string pred action)
3600 (when sql-completion-sqlbuf
3601 (with-current-buffer sql-completion-sqlbuf
3602 (let ((schema (and (string-match "\\`\\(\\sw\\(:?\\sw\\|\\s_\\)*\\)[.]" string)
3603 (downcase (match-string 1 string)))))
3604
3605 ;; If we haven't loaded any object name yet, load local schema
3606 (unless sql-completion-object
3607 (sql-build-completions nil))
3608
3609 ;; If they want another schema, load it if we haven't yet
3610 (when schema
3611 (let ((schema-dot (concat schema "."))
3612 (schema-len (1+ (length schema)))
3613 (names sql-completion-object)
3614 has-schema)
3615
3616 (while (and (not has-schema) names)
3617 (setq has-schema (and
3618 (>= (length (car names)) schema-len)
3619 (string= schema-dot
3620 (downcase (substring (car names)
3621 0 schema-len))))
3622 names (cdr names)))
3623 (unless has-schema
3624 (sql-build-completions schema)))))
3625
3626 ;; Try to find the completion
3627 (complete-with-action action sql-completion-object string pred))))
3628
3629 (defun sql-read-table-name (prompt)
3630 "Read the name of a database table."
3631 (let* ((tname
3632 (and (buffer-local-value 'sql-contains-names (current-buffer))
3633 (thing-at-point-looking-at
3634 (concat "\\_<\\sw\\(:?\\sw\\|\\s_\\)*"
3635 "\\(?:[.]+\\sw\\(?:\\sw\\|\\s_\\)*\\)*\\_>"))
3636 (buffer-substring-no-properties (match-beginning 0)
3637 (match-end 0))))
3638 (sql-completion-sqlbuf (sql-find-sqli-buffer))
3639 (product (with-current-buffer sql-completion-sqlbuf sql-product))
3640 (completion-ignore-case t))
3641
3642 (if (sql-get-product-feature product :completion-object)
3643 (completing-read prompt #'sql--completion-table
3644 nil nil tname)
3645 (read-from-minibuffer prompt tname))))
3646
3647 (defun sql-list-all (&optional enhanced)
3648 "List all database objects.
3649 With optional prefix argument ENHANCED, displays additional
3650 details or extends the listing to include other schemas objects."
3651 (interactive "P")
3652 (let ((sqlbuf (sql-find-sqli-buffer)))
3653 (unless sqlbuf
3654 (error "No SQL interactive buffer found"))
3655 (sql-execute-feature sqlbuf "*List All*" :list-all enhanced nil)
3656 (with-current-buffer sqlbuf
3657 ;; Contains the name of database objects
3658 (set (make-local-variable 'sql-contains-names) t)
3659 (set (make-local-variable 'sql-buffer) sqlbuf))))
3660
3661 (defun sql-list-table (name &optional enhanced)
3662 "List the details of a database table named NAME.
3663 Displays the columns in the relation. With optional prefix argument
3664 ENHANCED, displays additional details about each column."
3665 (interactive
3666 (list (sql-read-table-name "Table name: ")
3667 current-prefix-arg))
3668 (let ((sqlbuf (sql-find-sqli-buffer)))
3669 (unless sqlbuf
3670 (error "No SQL interactive buffer found"))
3671 (unless name
3672 (error "No table name specified"))
3673 (sql-execute-feature sqlbuf (format "*List %s*" name)
3674 :list-table enhanced name)))
3675 \f
3676
3677 ;;; SQL mode -- uses SQL interactive mode
3678
3679 ;;;###autoload
3680 (define-derived-mode sql-mode prog-mode "SQL"
3681 "Major mode to edit SQL.
3682
3683 You can send SQL statements to the SQLi buffer using
3684 \\[sql-send-region]. Such a buffer must exist before you can do this.
3685 See `sql-help' on how to create SQLi buffers.
3686
3687 \\{sql-mode-map}
3688 Customization: Entry to this mode runs the `sql-mode-hook'.
3689
3690 When you put a buffer in SQL mode, the buffer stores the last SQLi
3691 buffer created as its destination in the variable `sql-buffer'. This
3692 will be the buffer \\[sql-send-region] sends the region to. If this
3693 SQLi buffer is killed, \\[sql-send-region] is no longer able to
3694 determine where the strings should be sent to. You can set the
3695 value of `sql-buffer' using \\[sql-set-sqli-buffer].
3696
3697 For information on how to create multiple SQLi buffers, see
3698 `sql-interactive-mode'.
3699
3700 Note that SQL doesn't have an escape character unless you specify
3701 one. If you specify backslash as escape character in SQL, you
3702 must tell Emacs. Here's how to do that in your init file:
3703
3704 \(add-hook 'sql-mode-hook
3705 (lambda ()
3706 (modify-syntax-entry ?\\\\ \".\" sql-mode-syntax-table)))"
3707 :abbrev-table sql-mode-abbrev-table
3708 (if sql-mode-menu
3709 (easy-menu-add sql-mode-menu)); XEmacs
3710
3711 ;; (smie-setup sql-smie-grammar #'sql-smie-rules)
3712 (set (make-local-variable 'comment-start) "--")
3713 ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
3714 (make-local-variable 'sql-buffer)
3715 ;; Add imenu support for sql-mode. Note that imenu-generic-expression
3716 ;; is buffer-local, so we don't need a local-variable for it. SQL is
3717 ;; case-insensitive, that's why we have to set imenu-case-fold-search.
3718 (setq imenu-generic-expression sql-imenu-generic-expression
3719 imenu-case-fold-search t)
3720 ;; Make `sql-send-paragraph' work on paragraphs that contain indented
3721 ;; lines.
3722 (set (make-local-variable 'paragraph-separate) "[\f]*$")
3723 (set (make-local-variable 'paragraph-start) "[\n\f]")
3724 ;; Abbrevs
3725 (setq-local abbrev-all-caps 1)
3726 ;; Contains the name of database objects
3727 (set (make-local-variable 'sql-contains-names) t)
3728 ;; Catch changes to sql-product and highlight accordingly
3729 (add-hook 'hack-local-variables-hook 'sql-highlight-product t t))
3730
3731 \f
3732
3733 ;;; SQL interactive mode
3734
3735 (put 'sql-interactive-mode 'mode-class 'special)
3736
3737 (defun sql-interactive-mode ()
3738 "Major mode to use a SQL interpreter interactively.
3739
3740 Do not call this function by yourself. The environment must be
3741 initialized by an entry function specific for the SQL interpreter.
3742 See `sql-help' for a list of available entry functions.
3743
3744 \\[comint-send-input] after the end of the process' output sends the
3745 text from the end of process to the end of the current line.
3746 \\[comint-send-input] before end of process output copies the current
3747 line minus the prompt to the end of the buffer and sends it.
3748 \\[comint-copy-old-input] just copies the current line.
3749 Use \\[sql-accumulate-and-indent] to enter multi-line statements.
3750
3751 If you want to make multiple SQL buffers, rename the `*SQL*' buffer
3752 using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
3753 See `sql-help' for a list of available entry functions. The last buffer
3754 created by such an entry function is the current SQLi buffer. SQL
3755 buffers will send strings to the SQLi buffer current at the time of
3756 their creation. See `sql-mode' for details.
3757
3758 Sample session using two connections:
3759
3760 1. Create first SQLi buffer by calling an entry function.
3761 2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
3762 3. Create a SQL buffer \"test1.sql\".
3763 4. Create second SQLi buffer by calling an entry function.
3764 5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
3765 6. Create a SQL buffer \"test2.sql\".
3766
3767 Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
3768 buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
3769 will send the region to buffer \"*Connection 2*\".
3770
3771 If you accidentally suspend your process, use \\[comint-continue-subjob]
3772 to continue it. On some operating systems, this will not work because
3773 the signals are not supported.
3774
3775 \\{sql-interactive-mode-map}
3776 Customization: Entry to this mode runs the hooks on `comint-mode-hook'
3777 and `sql-interactive-mode-hook' (in that order). Before each input, the
3778 hooks on `comint-input-filter-functions' are run. After each SQL
3779 interpreter output, the hooks on `comint-output-filter-functions' are
3780 run.
3781
3782 Variable `sql-input-ring-file-name' controls the initialization of the
3783 input ring history.
3784
3785 Variables `comint-output-filter-functions', a hook, and
3786 `comint-scroll-to-bottom-on-input' and
3787 `comint-scroll-to-bottom-on-output' control whether input and output
3788 cause the window to scroll to the end of the buffer.
3789
3790 If you want to make SQL buffers limited in length, add the function
3791 `comint-truncate-buffer' to `comint-output-filter-functions'.
3792
3793 Here is an example for your init file. It keeps the SQLi buffer a
3794 certain length.
3795
3796 \(add-hook 'sql-interactive-mode-hook
3797 \(function (lambda ()
3798 \(setq comint-output-filter-functions 'comint-truncate-buffer))))
3799
3800 Here is another example. It will always put point back to the statement
3801 you entered, right above the output it created.
3802
3803 \(setq comint-output-filter-functions
3804 \(function (lambda (STR) (comint-show-output))))"
3805 (delay-mode-hooks (comint-mode))
3806
3807 ;; Get the `sql-product' for this interactive session.
3808 (set (make-local-variable 'sql-product)
3809 (or sql-interactive-product
3810 sql-product))
3811
3812 ;; Setup the mode.
3813 (setq major-mode 'sql-interactive-mode)
3814 (setq mode-name
3815 (concat "SQLi[" (or (sql-get-product-feature sql-product :name)
3816 (symbol-name sql-product)) "]"))
3817 (use-local-map sql-interactive-mode-map)
3818 (if sql-interactive-mode-menu
3819 (easy-menu-add sql-interactive-mode-menu)) ; XEmacs
3820 (set-syntax-table sql-mode-syntax-table)
3821
3822 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
3823 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
3824 ;; will have just one quote. Therefore syntactic highlighting is
3825 ;; disabled for interactive buffers. No imenu support.
3826 (sql-product-font-lock t nil)
3827
3828 ;; Enable commenting and uncommenting of the region.
3829 (set (make-local-variable 'comment-start) "--")
3830 ;; Abbreviation table init and case-insensitive. It is not activated
3831 ;; by default.
3832 (setq local-abbrev-table sql-mode-abbrev-table)
3833 (setq abbrev-all-caps 1)
3834 ;; Exiting the process will call sql-stop.
3835 (set-process-sentinel (get-buffer-process (current-buffer)) 'sql-stop)
3836 ;; Save the connection and login params
3837 (set (make-local-variable 'sql-user) sql-user)
3838 (set (make-local-variable 'sql-database) sql-database)
3839 (set (make-local-variable 'sql-server) sql-server)
3840 (set (make-local-variable 'sql-port) sql-port)
3841 (set (make-local-variable 'sql-connection) sql-connection)
3842 (setq-default sql-connection nil)
3843 ;; Contains the name of database objects
3844 (set (make-local-variable 'sql-contains-names) t)
3845 ;; Keep track of existing object names
3846 (set (make-local-variable 'sql-completion-object) nil)
3847 (set (make-local-variable 'sql-completion-column) nil)
3848 ;; Create a useful name for renaming this buffer later.
3849 (set (make-local-variable 'sql-alternate-buffer-name)
3850 (sql-make-alternate-buffer-name))
3851 ;; User stuff. Initialize before the hook.
3852 (set (make-local-variable 'sql-prompt-regexp)
3853 (sql-get-product-feature sql-product :prompt-regexp))
3854 (set (make-local-variable 'sql-prompt-length)
3855 (sql-get-product-feature sql-product :prompt-length))
3856 (set (make-local-variable 'sql-prompt-cont-regexp)
3857 (sql-get-product-feature sql-product :prompt-cont-regexp))
3858 (make-local-variable 'sql-output-newline-count)
3859 (make-local-variable 'sql-preoutput-hold)
3860 (make-local-variable 'sql-output-by-send)
3861 (add-hook 'comint-preoutput-filter-functions
3862 'sql-interactive-remove-continuation-prompt nil t)
3863 (make-local-variable 'sql-input-ring-separator)
3864 (make-local-variable 'sql-input-ring-file-name)
3865 ;; Run the mode hook (along with comint's hooks).
3866 (run-mode-hooks 'sql-interactive-mode-hook)
3867 ;; Set comint based on user overrides.
3868 (setq comint-prompt-regexp
3869 (if sql-prompt-cont-regexp
3870 (concat "\\(" sql-prompt-regexp
3871 "\\|" sql-prompt-cont-regexp "\\)")
3872 sql-prompt-regexp))
3873 (setq left-margin sql-prompt-length)
3874 ;; Install input sender
3875 (set (make-local-variable 'comint-input-sender) 'sql-input-sender)
3876 ;; People wanting a different history file for each
3877 ;; buffer/process/client/whatever can change separator and file-name
3878 ;; on the sql-interactive-mode-hook.
3879 (setq comint-input-ring-separator sql-input-ring-separator
3880 comint-input-ring-file-name sql-input-ring-file-name)
3881 ;; Calling the hook before calling comint-read-input-ring allows users
3882 ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
3883 (comint-read-input-ring t))
3884
3885 (defun sql-stop (process event)
3886 "Called when the SQL process is stopped.
3887
3888 Writes the input history to a history file using
3889 `comint-write-input-ring' and inserts a short message in the SQL buffer.
3890
3891 This function is a sentinel watching the SQL interpreter process.
3892 Sentinels will always get the two parameters PROCESS and EVENT."
3893 (comint-write-input-ring)
3894 (if (and (eq (current-buffer) sql-buffer)
3895 (not buffer-read-only))
3896 (insert (format "\nProcess %s %s\n" process event))
3897 (message "Process %s %s" process event)))
3898
3899 \f
3900
3901 ;;; Connection handling
3902
3903 (defun sql-read-connection (prompt &optional initial default)
3904 "Read a connection name."
3905 (let ((completion-ignore-case t))
3906 (completing-read prompt
3907 (mapcar (lambda (c) (car c))
3908 sql-connection-alist)
3909 nil t initial 'sql-connection-history default)))
3910
3911 ;;;###autoload
3912 (defun sql-connect (connection &optional new-name)
3913 "Connect to an interactive session using CONNECTION settings.
3914
3915 See `sql-connection-alist' to see how to define connections and
3916 their settings.
3917
3918 The user will not be prompted for any login parameters if a value
3919 is specified in the connection settings."
3920
3921 ;; Prompt for the connection from those defined in the alist
3922 (interactive
3923 (if sql-connection-alist
3924 (list (sql-read-connection "Connection: " nil '(nil))
3925 current-prefix-arg)
3926 nil))
3927
3928 ;; Are there connections defined
3929 (if sql-connection-alist
3930 ;; Was one selected
3931 (when connection
3932 ;; Get connection settings
3933 (let ((connect-set (assoc connection sql-connection-alist)))
3934 ;; Settings are defined
3935 (if connect-set
3936 ;; Set the desired parameters
3937 (let (param-var login-params set-params rem-params)
3938
3939 ;; :sqli-login params variable
3940 (setq param-var
3941 (sql-get-product-feature sql-product :sqli-login nil t))
3942
3943 ;; :sqli-login params value
3944 (setq login-params
3945 (sql-get-product-feature sql-product :sqli-login))
3946
3947 ;; Params in the connection
3948 (setq set-params
3949 (mapcar
3950 (lambda (v)
3951 (pcase (car v)
3952 (`sql-user 'user)
3953 (`sql-password 'password)
3954 (`sql-server 'server)
3955 (`sql-database 'database)
3956 (`sql-port 'port)
3957 (s s)))
3958 (cdr connect-set)))
3959
3960 ;; the remaining params (w/o the connection params)
3961 (setq rem-params
3962 (sql-for-each-login login-params
3963 (lambda (token plist)
3964 (unless (member token set-params)
3965 (if plist (cons token plist) token)))))
3966
3967 ;; Set the parameters and start the interactive session
3968 (mapc
3969 (lambda (vv)
3970 (set-default (car vv) (eval (cadr vv))))
3971 (cdr connect-set))
3972 (setq-default sql-connection connection)
3973
3974 ;; Start the SQLi session with revised list of login parameters
3975 (eval `(let ((,param-var ',rem-params))
3976 (sql-product-interactive ',sql-product ',new-name))))
3977
3978 (message "SQL Connection <%s> does not exist" connection)
3979 nil)))
3980
3981 (message "No SQL Connections defined")
3982 nil))
3983
3984 (defun sql-save-connection (name)
3985 "Captures the connection information of the current SQLi session.
3986
3987 The information is appended to `sql-connection-alist' and
3988 optionally is saved to the user's init file."
3989
3990 (interactive "sNew connection name: ")
3991
3992 (unless (derived-mode-p 'sql-interactive-mode)
3993 (error "Not in a SQL interactive mode!"))
3994
3995 ;; Capture the buffer local settings
3996 (let* ((buf (current-buffer))
3997 (connection (buffer-local-value 'sql-connection buf))
3998 (product (buffer-local-value 'sql-product buf))
3999 (user (buffer-local-value 'sql-user buf))
4000 (database (buffer-local-value 'sql-database buf))
4001 (server (buffer-local-value 'sql-server buf))
4002 (port (buffer-local-value 'sql-port buf)))
4003
4004 (if connection
4005 (message "This session was started by a connection; it's already been saved.")
4006
4007 (let ((login (sql-get-product-feature product :sqli-login))
4008 (alist sql-connection-alist)
4009 connect)
4010
4011 ;; Remove the existing connection if the user says so
4012 (when (and (assoc name alist)
4013 (yes-or-no-p (format "Replace connection definition <%s>? " name)))
4014 (setq alist (assq-delete-all name alist)))
4015
4016 ;; Add the new connection if it doesn't exist
4017 (if (assoc name alist)
4018 (message "Connection <%s> already exists" name)
4019 (setq connect
4020 (cons name
4021 (sql-for-each-login
4022 `(product ,@login)
4023 (lambda (token _plist)
4024 (pcase token
4025 (`product `(sql-product ',product))
4026 (`user `(sql-user ,user))
4027 (`database `(sql-database ,database))
4028 (`server `(sql-server ,server))
4029 (`port `(sql-port ,port)))))))
4030
4031 (setq alist (append alist (list connect)))
4032
4033 ;; confirm whether we want to save the connections
4034 (if (yes-or-no-p "Save the connections for future sessions? ")
4035 (customize-save-variable 'sql-connection-alist alist)
4036 (customize-set-variable 'sql-connection-alist alist)))))))
4037
4038 (defun sql-connection-menu-filter (tail)
4039 "Generate menu entries for using each connection."
4040 (append
4041 (mapcar
4042 (lambda (conn)
4043 (vector
4044 (format "Connection <%s>\t%s" (car conn)
4045 (let ((sql-user "") (sql-database "")
4046 (sql-server "") (sql-port 0))
4047 (eval `(let ,(cdr conn) (sql-make-alternate-buffer-name)))))
4048 (list 'sql-connect (car conn))
4049 t))
4050 sql-connection-alist)
4051 tail))
4052
4053 \f
4054
4055 ;;; Entry functions for different SQL interpreters.
4056
4057 ;;;###autoload
4058 (defun sql-product-interactive (&optional product new-name)
4059 "Run PRODUCT interpreter as an inferior process.
4060
4061 If buffer `*SQL*' exists but no process is running, make a new process.
4062 If buffer exists and a process is running, just switch to buffer `*SQL*'.
4063
4064 To specify the SQL product, prefix the call with
4065 \\[universal-argument]. To set the buffer name as well, prefix
4066 the call to \\[sql-product-interactive] with
4067 \\[universal-argument] \\[universal-argument].
4068
4069 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4070 (interactive "P")
4071
4072 ;; Handle universal arguments if specified
4073 (when (not (or executing-kbd-macro noninteractive))
4074 (when (and (consp product)
4075 (not (cdr product))
4076 (numberp (car product)))
4077 (when (>= (prefix-numeric-value product) 16)
4078 (when (not new-name)
4079 (setq new-name '(4)))
4080 (setq product '(4)))))
4081
4082 ;; Get the value of product that we need
4083 (setq product
4084 (cond
4085 ((= (prefix-numeric-value product) 4) ; C-u, prompt for product
4086 (sql-read-product "SQL product: " sql-product))
4087 ((and product ; Product specified
4088 (symbolp product)) product)
4089 (t sql-product))) ; Default to sql-product
4090
4091 ;; If we have a product and it has a interactive mode
4092 (if product
4093 (when (sql-get-product-feature product :sqli-comint-func)
4094 ;; If no new name specified, try to pop to an active SQL
4095 ;; interactive for the same product
4096 (let ((buf (sql-find-sqli-buffer product sql-connection)))
4097 (if (and (not new-name) buf)
4098 (pop-to-buffer buf)
4099
4100 ;; We have a new name or sql-buffer doesn't exist or match
4101 ;; Start by remembering where we start
4102 (let ((start-buffer (current-buffer))
4103 new-sqli-buffer)
4104
4105 ;; Get credentials.
4106 (apply #'sql-get-login
4107 (sql-get-product-feature product :sqli-login))
4108
4109 ;; Connect to database.
4110 (message "Login...")
4111 (let ((sql-user (default-value 'sql-user))
4112 (sql-password (default-value 'sql-password))
4113 (sql-server (default-value 'sql-server))
4114 (sql-database (default-value 'sql-database))
4115 (sql-port (default-value 'sql-port)))
4116 (funcall (sql-get-product-feature product :sqli-comint-func)
4117 product
4118 (sql-get-product-feature product :sqli-options)))
4119
4120 ;; Set SQLi mode.
4121 (let ((sql-interactive-product product))
4122 (sql-interactive-mode))
4123
4124 ;; Set the new buffer name
4125 (setq new-sqli-buffer (current-buffer))
4126 (when new-name
4127 (sql-rename-buffer new-name))
4128 (set (make-local-variable 'sql-buffer)
4129 (buffer-name new-sqli-buffer))
4130
4131 ;; Set `sql-buffer' in the start buffer
4132 (with-current-buffer start-buffer
4133 (when (derived-mode-p 'sql-mode)
4134 (setq sql-buffer (buffer-name new-sqli-buffer))
4135 (run-hooks 'sql-set-sqli-hook)))
4136
4137 ;; All done.
4138 (message "Login...done")
4139 (run-hooks 'sql-login-hook)
4140 (pop-to-buffer new-sqli-buffer)))))
4141 (message "No default SQL product defined. Set `sql-product'.")))
4142
4143 (defun sql-comint (product params)
4144 "Set up a comint buffer to run the SQL processor.
4145
4146 PRODUCT is the SQL product. PARAMS is a list of strings which are
4147 passed as command line arguments."
4148 (let ((program (sql-get-product-feature product :sqli-program))
4149 (buf-name "SQL"))
4150 ;; Make sure we can find the program. `executable-find' does not
4151 ;; work for remote hosts; we suppress the check there.
4152 (unless (or (file-remote-p default-directory)
4153 (executable-find program))
4154 (error "Unable to locate SQL program \'%s\'" program))
4155 ;; Make sure buffer name is unique.
4156 (when (sql-buffer-live-p (format "*%s*" buf-name))
4157 (setq buf-name (format "SQL-%s" product))
4158 (when (sql-buffer-live-p (format "*%s*" buf-name))
4159 (let ((i 1))
4160 (while (sql-buffer-live-p
4161 (format "*%s*"
4162 (setq buf-name (format "SQL-%s%d" product i))))
4163 (setq i (1+ i))))))
4164 (set-buffer
4165 (apply 'make-comint buf-name program nil params))))
4166
4167 ;;;###autoload
4168 (defun sql-oracle (&optional buffer)
4169 "Run sqlplus by Oracle as an inferior process.
4170
4171 If buffer `*SQL*' exists but no process is running, make a new process.
4172 If buffer exists and a process is running, just switch to buffer
4173 `*SQL*'.
4174
4175 Interpreter used comes from variable `sql-oracle-program'. Login uses
4176 the variables `sql-user', `sql-password', and `sql-database' as
4177 defaults, if set. Additional command line parameters can be stored in
4178 the list `sql-oracle-options'.
4179
4180 The buffer is put in SQL interactive mode, giving commands for sending
4181 input. See `sql-interactive-mode'.
4182
4183 To set the buffer name directly, use \\[universal-argument]
4184 before \\[sql-oracle]. Once session has started,
4185 \\[sql-rename-buffer] can be called separately to rename the
4186 buffer.
4187
4188 To specify a coding system for converting non-ASCII characters
4189 in the input and output to the process, use \\[universal-coding-system-argument]
4190 before \\[sql-oracle]. You can also specify this with \\[set-buffer-process-coding-system]
4191 in the SQL buffer, after you start the process.
4192 The default comes from `process-coding-system-alist' and
4193 `default-process-coding-system'.
4194
4195 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4196 (interactive "P")
4197 (sql-product-interactive 'oracle buffer))
4198
4199 (defun sql-comint-oracle (product options)
4200 "Create comint buffer and connect to Oracle."
4201 ;; Produce user/password@database construct. Password without user
4202 ;; is meaningless; database without user/password is meaningless,
4203 ;; because "@param" will ask sqlplus to interpret the script
4204 ;; "param".
4205 (let ((parameter nil))
4206 (if (not (string= "" sql-user))
4207 (if (not (string= "" sql-password))
4208 (setq parameter (concat sql-user "/" sql-password))
4209 (setq parameter sql-user)))
4210 (if (and parameter (not (string= "" sql-database)))
4211 (setq parameter (concat parameter "@" sql-database)))
4212 (if parameter
4213 (setq parameter (nconc (list parameter) options))
4214 (setq parameter options))
4215 (sql-comint product parameter)))
4216
4217 (defun sql-oracle-save-settings (sqlbuf)
4218 "Save most SQL*Plus settings so they may be reset by \\[sql-redirect]."
4219 ;; Note: does not capture the following settings:
4220 ;;
4221 ;; APPINFO
4222 ;; BTITLE
4223 ;; COMPATIBILITY
4224 ;; COPYTYPECHECK
4225 ;; MARKUP
4226 ;; RELEASE
4227 ;; REPFOOTER
4228 ;; REPHEADER
4229 ;; SQLPLUSCOMPATIBILITY
4230 ;; TTITLE
4231 ;; USER
4232 ;;
4233
4234 (append
4235 ;; (apply 'concat (append
4236 ;; '("SET")
4237
4238 ;; option value...
4239 (sql-redirect-value
4240 sqlbuf
4241 (concat "SHOW ARRAYSIZE AUTOCOMMIT AUTOPRINT AUTORECOVERY AUTOTRACE"
4242 " CMDSEP COLSEP COPYCOMMIT DESCRIBE ECHO EDITFILE EMBEDDED"
4243 " ESCAPE FLAGGER FLUSH HEADING INSTANCE LINESIZE LNO LOBOFFSET"
4244 " LOGSOURCE LONG LONGCHUNKSIZE NEWPAGE NULL NUMFORMAT NUMWIDTH"
4245 " PAGESIZE PAUSE PNO RECSEP SERVEROUTPUT SHIFTINOUT SHOWMODE"
4246 " SPOOL SQLBLANKLINES SQLCASE SQLCODE SQLCONTINUE SQLNUMBER"
4247 " SQLPROMPT SUFFIX TAB TERMOUT TIMING TRIMOUT TRIMSPOOL VERIFY")
4248 "^.+$"
4249 "SET \\&")
4250
4251 ;; option "c" (hex xx)
4252 (sql-redirect-value
4253 sqlbuf
4254 (concat "SHOW BLOCKTERMINATOR CONCAT DEFINE SQLPREFIX SQLTERMINATOR"
4255 " UNDERLINE HEADSEP RECSEPCHAR")
4256 "^\\(.+\\) (hex ..)$"
4257 "SET \\1")
4258
4259 ;; FEEDBACK ON for 99 or more rows
4260 ;; feedback OFF
4261 (sql-redirect-value
4262 sqlbuf
4263 "SHOW FEEDBACK"
4264 "^\\(?:FEEDBACK ON for \\([[:digit:]]+\\) or more rows\\|feedback \\(OFF\\)\\)"
4265 "SET FEEDBACK \\1\\2")
4266
4267 ;; wrap : lines will be wrapped
4268 ;; wrap : lines will be truncated
4269 (list (concat "SET WRAP "
4270 (if (string=
4271 (car (sql-redirect-value
4272 sqlbuf
4273 "SHOW WRAP"
4274 "^wrap : lines will be \\(wrapped\\|truncated\\)" 1))
4275 "wrapped")
4276 "ON" "OFF")))))
4277
4278 (defun sql-oracle-restore-settings (sqlbuf saved-settings)
4279 "Restore the SQL*Plus settings in SAVED-SETTINGS."
4280
4281 ;; Remove any settings that haven't changed
4282 (mapc
4283 (lambda (one-cur-setting)
4284 (setq saved-settings (delete one-cur-setting saved-settings)))
4285 (sql-oracle-save-settings sqlbuf))
4286
4287 ;; Restore the changed settings
4288 (sql-redirect sqlbuf saved-settings))
4289
4290 (defun sql-oracle-list-all (sqlbuf outbuf enhanced _table-name)
4291 ;; Query from USER_OBJECTS or ALL_OBJECTS
4292 (let ((settings (sql-oracle-save-settings sqlbuf))
4293 (simple-sql
4294 (concat
4295 "SELECT INITCAP(x.object_type) AS SQL_EL_TYPE "
4296 ", x.object_name AS SQL_EL_NAME "
4297 "FROM user_objects x "
4298 "WHERE x.object_type NOT LIKE '%% BODY' "
4299 "ORDER BY 2, 1;"))
4300 (enhanced-sql
4301 (concat
4302 "SELECT INITCAP(x.object_type) AS SQL_EL_TYPE "
4303 ", x.owner ||'.'|| x.object_name AS SQL_EL_NAME "
4304 "FROM all_objects x "
4305 "WHERE x.object_type NOT LIKE '%% BODY' "
4306 "AND x.owner <> 'SYS' "
4307 "ORDER BY 2, 1;")))
4308
4309 (sql-redirect sqlbuf
4310 (concat "SET LINESIZE 80 PAGESIZE 50000 TRIMOUT ON"
4311 " TAB OFF TIMING OFF FEEDBACK OFF"))
4312
4313 (sql-redirect sqlbuf
4314 (list "COLUMN SQL_EL_TYPE HEADING \"Type\" FORMAT A19"
4315 "COLUMN SQL_EL_NAME HEADING \"Name\""
4316 (format "COLUMN SQL_EL_NAME FORMAT A%d"
4317 (if enhanced 60 35))))
4318
4319 (sql-redirect sqlbuf
4320 (if enhanced enhanced-sql simple-sql)
4321 outbuf)
4322
4323 (sql-redirect sqlbuf
4324 '("COLUMN SQL_EL_NAME CLEAR"
4325 "COLUMN SQL_EL_TYPE CLEAR"))
4326
4327 (sql-oracle-restore-settings sqlbuf settings)))
4328
4329 (defun sql-oracle-list-table (sqlbuf outbuf _enhanced table-name)
4330 "Implements :list-table under Oracle."
4331 (let ((settings (sql-oracle-save-settings sqlbuf)))
4332
4333 (sql-redirect sqlbuf
4334 (format
4335 (concat "SET LINESIZE %d PAGESIZE 50000"
4336 " DESCRIBE DEPTH 1 LINENUM OFF INDENT ON")
4337 (max 65 (min 120 (window-width)))))
4338
4339 (sql-redirect sqlbuf (format "DESCRIBE %s" table-name)
4340 outbuf)
4341
4342 (sql-oracle-restore-settings sqlbuf settings)))
4343
4344 (defcustom sql-oracle-completion-types '("FUNCTION" "PACKAGE" "PROCEDURE"
4345 "SEQUENCE" "SYNONYM" "TABLE" "TRIGGER"
4346 "TYPE" "VIEW")
4347 "List of object types to include for completion under Oracle.
4348
4349 See the distinct values in ALL_OBJECTS.OBJECT_TYPE for possible values."
4350 :version "24.1"
4351 :type '(repeat string)
4352 :group 'SQL)
4353
4354 (defun sql-oracle-completion-object (sqlbuf schema)
4355 (sql-redirect-value
4356 sqlbuf
4357 (concat
4358 "SELECT CHR(1)||"
4359 (if schema
4360 (format "owner||'.'||object_name AS o FROM all_objects WHERE owner = %s AND "
4361 (sql-str-literal (upcase schema)))
4362 "object_name AS o FROM user_objects WHERE ")
4363 "temporary = 'N' AND generated = 'N' AND secondary = 'N' AND "
4364 "object_type IN ("
4365 (mapconcat (function sql-str-literal) sql-oracle-completion-types ",")
4366 ");")
4367 "^[\001]\\(.+\\)$" 1))
4368 \f
4369
4370 ;;;###autoload
4371 (defun sql-sybase (&optional buffer)
4372 "Run isql by Sybase as an inferior process.
4373
4374 If buffer `*SQL*' exists but no process is running, make a new process.
4375 If buffer exists and a process is running, just switch to buffer
4376 `*SQL*'.
4377
4378 Interpreter used comes from variable `sql-sybase-program'. Login uses
4379 the variables `sql-server', `sql-user', `sql-password', and
4380 `sql-database' as defaults, if set. Additional command line parameters
4381 can be stored in the list `sql-sybase-options'.
4382
4383 The buffer is put in SQL interactive mode, giving commands for sending
4384 input. See `sql-interactive-mode'.
4385
4386 To set the buffer name directly, use \\[universal-argument]
4387 before \\[sql-sybase]. Once session has started,
4388 \\[sql-rename-buffer] can be called separately to rename the
4389 buffer.
4390
4391 To specify a coding system for converting non-ASCII characters
4392 in the input and output to the process, use \\[universal-coding-system-argument]
4393 before \\[sql-sybase]. You can also specify this with \\[set-buffer-process-coding-system]
4394 in the SQL buffer, after you start the process.
4395 The default comes from `process-coding-system-alist' and
4396 `default-process-coding-system'.
4397
4398 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4399 (interactive "P")
4400 (sql-product-interactive 'sybase buffer))
4401
4402 (defun sql-comint-sybase (product options)
4403 "Create comint buffer and connect to Sybase."
4404 ;; Put all parameters to the program (if defined) in a list and call
4405 ;; make-comint.
4406 (let ((params
4407 (append
4408 (if (not (string= "" sql-user))
4409 (list "-U" sql-user))
4410 (if (not (string= "" sql-password))
4411 (list "-P" sql-password))
4412 (if (not (string= "" sql-database))
4413 (list "-D" sql-database))
4414 (if (not (string= "" sql-server))
4415 (list "-S" sql-server))
4416 options)))
4417 (sql-comint product params)))
4418
4419 \f
4420
4421 ;;;###autoload
4422 (defun sql-informix (&optional buffer)
4423 "Run dbaccess by Informix as an inferior process.
4424
4425 If buffer `*SQL*' exists but no process is running, make a new process.
4426 If buffer exists and a process is running, just switch to buffer
4427 `*SQL*'.
4428
4429 Interpreter used comes from variable `sql-informix-program'. Login uses
4430 the variable `sql-database' as default, if set.
4431
4432 The buffer is put in SQL interactive mode, giving commands for sending
4433 input. See `sql-interactive-mode'.
4434
4435 To set the buffer name directly, use \\[universal-argument]
4436 before \\[sql-informix]. Once session has started,
4437 \\[sql-rename-buffer] can be called separately to rename the
4438 buffer.
4439
4440 To specify a coding system for converting non-ASCII characters
4441 in the input and output to the process, use \\[universal-coding-system-argument]
4442 before \\[sql-informix]. You can also specify this with \\[set-buffer-process-coding-system]
4443 in the SQL buffer, after you start the process.
4444 The default comes from `process-coding-system-alist' and
4445 `default-process-coding-system'.
4446
4447 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4448 (interactive "P")
4449 (sql-product-interactive 'informix buffer))
4450
4451 (defun sql-comint-informix (product options)
4452 "Create comint buffer and connect to Informix."
4453 ;; username and password are ignored.
4454 (let ((db (if (string= "" sql-database)
4455 "-"
4456 (if (string= "" sql-server)
4457 sql-database
4458 (concat sql-database "@" sql-server)))))
4459 (sql-comint product (append `(,db "-") options))))
4460
4461 \f
4462
4463 ;;;###autoload
4464 (defun sql-sqlite (&optional buffer)
4465 "Run sqlite as an inferior process.
4466
4467 SQLite is free software.
4468
4469 If buffer `*SQL*' exists but no process is running, make a new process.
4470 If buffer exists and a process is running, just switch to buffer
4471 `*SQL*'.
4472
4473 Interpreter used comes from variable `sql-sqlite-program'. Login uses
4474 the variables `sql-user', `sql-password', `sql-database', and
4475 `sql-server' as defaults, if set. Additional command line parameters
4476 can be stored in the list `sql-sqlite-options'.
4477
4478 The buffer is put in SQL interactive mode, giving commands for sending
4479 input. See `sql-interactive-mode'.
4480
4481 To set the buffer name directly, use \\[universal-argument]
4482 before \\[sql-sqlite]. Once session has started,
4483 \\[sql-rename-buffer] can be called separately to rename the
4484 buffer.
4485
4486 To specify a coding system for converting non-ASCII characters
4487 in the input and output to the process, use \\[universal-coding-system-argument]
4488 before \\[sql-sqlite]. You can also specify this with \\[set-buffer-process-coding-system]
4489 in the SQL buffer, after you start the process.
4490 The default comes from `process-coding-system-alist' and
4491 `default-process-coding-system'.
4492
4493 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4494 (interactive "P")
4495 (sql-product-interactive 'sqlite buffer))
4496
4497 (defun sql-comint-sqlite (product options)
4498 "Create comint buffer and connect to SQLite."
4499 ;; Put all parameters to the program (if defined) in a list and call
4500 ;; make-comint.
4501 (let ((params
4502 (append options
4503 (if (not (string= "" sql-database))
4504 `(,(expand-file-name sql-database))))))
4505 (sql-comint product params)))
4506
4507 (defun sql-sqlite-completion-object (sqlbuf _schema)
4508 (sql-redirect-value sqlbuf ".tables" "\\sw\\(?:\\sw\\|\\s_\\)*" 0))
4509
4510 \f
4511
4512 ;;;###autoload
4513 (defun sql-mysql (&optional buffer)
4514 "Run mysql by TcX as an inferior process.
4515
4516 Mysql versions 3.23 and up are free software.
4517
4518 If buffer `*SQL*' exists but no process is running, make a new process.
4519 If buffer exists and a process is running, just switch to buffer
4520 `*SQL*'.
4521
4522 Interpreter used comes from variable `sql-mysql-program'. Login uses
4523 the variables `sql-user', `sql-password', `sql-database', and
4524 `sql-server' as defaults, if set. Additional command line parameters
4525 can be stored in the list `sql-mysql-options'.
4526
4527 The buffer is put in SQL interactive mode, giving commands for sending
4528 input. See `sql-interactive-mode'.
4529
4530 To set the buffer name directly, use \\[universal-argument]
4531 before \\[sql-mysql]. Once session has started,
4532 \\[sql-rename-buffer] can be called separately to rename the
4533 buffer.
4534
4535 To specify a coding system for converting non-ASCII characters
4536 in the input and output to the process, use \\[universal-coding-system-argument]
4537 before \\[sql-mysql]. You can also specify this with \\[set-buffer-process-coding-system]
4538 in the SQL buffer, after you start the process.
4539 The default comes from `process-coding-system-alist' and
4540 `default-process-coding-system'.
4541
4542 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4543 (interactive "P")
4544 (sql-product-interactive 'mysql buffer))
4545
4546 (defun sql-comint-mysql (product options)
4547 "Create comint buffer and connect to MySQL."
4548 ;; Put all parameters to the program (if defined) in a list and call
4549 ;; make-comint.
4550 (let ((params
4551 (append
4552 options
4553 (if (not (string= "" sql-user))
4554 (list (concat "--user=" sql-user)))
4555 (if (not (string= "" sql-password))
4556 (list (concat "--password=" sql-password)))
4557 (if (not (= 0 sql-port))
4558 (list (concat "--port=" (number-to-string sql-port))))
4559 (if (not (string= "" sql-server))
4560 (list (concat "--host=" sql-server)))
4561 (if (not (string= "" sql-database))
4562 (list sql-database)))))
4563 (sql-comint product params)))
4564
4565 \f
4566
4567 ;;;###autoload
4568 (defun sql-solid (&optional buffer)
4569 "Run solsql by Solid as an inferior process.
4570
4571 If buffer `*SQL*' exists but no process is running, make a new process.
4572 If buffer exists and a process is running, just switch to buffer
4573 `*SQL*'.
4574
4575 Interpreter used comes from variable `sql-solid-program'. Login uses
4576 the variables `sql-user', `sql-password', and `sql-server' as
4577 defaults, if set.
4578
4579 The buffer is put in SQL interactive mode, giving commands for sending
4580 input. See `sql-interactive-mode'.
4581
4582 To set the buffer name directly, use \\[universal-argument]
4583 before \\[sql-solid]. Once session has started,
4584 \\[sql-rename-buffer] can be called separately to rename the
4585 buffer.
4586
4587 To specify a coding system for converting non-ASCII characters
4588 in the input and output to the process, use \\[universal-coding-system-argument]
4589 before \\[sql-solid]. You can also specify this with \\[set-buffer-process-coding-system]
4590 in the SQL buffer, after you start the process.
4591 The default comes from `process-coding-system-alist' and
4592 `default-process-coding-system'.
4593
4594 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4595 (interactive "P")
4596 (sql-product-interactive 'solid buffer))
4597
4598 (defun sql-comint-solid (product options)
4599 "Create comint buffer and connect to Solid."
4600 ;; Put all parameters to the program (if defined) in a list and call
4601 ;; make-comint.
4602 (let ((params
4603 (append
4604 (if (not (string= "" sql-server))
4605 (list sql-server))
4606 ;; It only makes sense if both username and password are there.
4607 (if (not (or (string= "" sql-user)
4608 (string= "" sql-password)))
4609 (list sql-user sql-password))
4610 options)))
4611 (sql-comint product params)))
4612
4613 \f
4614
4615 ;;;###autoload
4616 (defun sql-ingres (&optional buffer)
4617 "Run sql by Ingres as an inferior process.
4618
4619 If buffer `*SQL*' exists but no process is running, make a new process.
4620 If buffer exists and a process is running, just switch to buffer
4621 `*SQL*'.
4622
4623 Interpreter used comes from variable `sql-ingres-program'. Login uses
4624 the variable `sql-database' as default, if set.
4625
4626 The buffer is put in SQL interactive mode, giving commands for sending
4627 input. See `sql-interactive-mode'.
4628
4629 To set the buffer name directly, use \\[universal-argument]
4630 before \\[sql-ingres]. Once session has started,
4631 \\[sql-rename-buffer] can be called separately to rename the
4632 buffer.
4633
4634 To specify a coding system for converting non-ASCII characters
4635 in the input and output to the process, use \\[universal-coding-system-argument]
4636 before \\[sql-ingres]. You can also specify this with \\[set-buffer-process-coding-system]
4637 in the SQL buffer, after you start the process.
4638 The default comes from `process-coding-system-alist' and
4639 `default-process-coding-system'.
4640
4641 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4642 (interactive "P")
4643 (sql-product-interactive 'ingres buffer))
4644
4645 (defun sql-comint-ingres (product options)
4646 "Create comint buffer and connect to Ingres."
4647 ;; username and password are ignored.
4648 (sql-comint product
4649 (append (if (string= "" sql-database)
4650 nil
4651 (list sql-database))
4652 options)))
4653
4654 \f
4655
4656 ;;;###autoload
4657 (defun sql-ms (&optional buffer)
4658 "Run osql by Microsoft as an inferior process.
4659
4660 If buffer `*SQL*' exists but no process is running, make a new process.
4661 If buffer exists and a process is running, just switch to buffer
4662 `*SQL*'.
4663
4664 Interpreter used comes from variable `sql-ms-program'. Login uses the
4665 variables `sql-user', `sql-password', `sql-database', and `sql-server'
4666 as defaults, if set. Additional command line parameters can be stored
4667 in the list `sql-ms-options'.
4668
4669 The buffer is put in SQL interactive mode, giving commands for sending
4670 input. See `sql-interactive-mode'.
4671
4672 To set the buffer name directly, use \\[universal-argument]
4673 before \\[sql-ms]. Once session has started,
4674 \\[sql-rename-buffer] can be called separately to rename the
4675 buffer.
4676
4677 To specify a coding system for converting non-ASCII characters
4678 in the input and output to the process, use \\[universal-coding-system-argument]
4679 before \\[sql-ms]. You can also specify this with \\[set-buffer-process-coding-system]
4680 in the SQL buffer, after you start the process.
4681 The default comes from `process-coding-system-alist' and
4682 `default-process-coding-system'.
4683
4684 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4685 (interactive "P")
4686 (sql-product-interactive 'ms buffer))
4687
4688 (defun sql-comint-ms (product options)
4689 "Create comint buffer and connect to Microsoft SQL Server."
4690 ;; Put all parameters to the program (if defined) in a list and call
4691 ;; make-comint.
4692 (let ((params
4693 (append
4694 (if (not (string= "" sql-user))
4695 (list "-U" sql-user))
4696 (if (not (string= "" sql-database))
4697 (list "-d" sql-database))
4698 (if (not (string= "" sql-server))
4699 (list "-S" sql-server))
4700 options)))
4701 (setq params
4702 (if (not (string= "" sql-password))
4703 `("-P" ,sql-password ,@params)
4704 (if (string= "" sql-user)
4705 ;; If neither user nor password is provided, use system
4706 ;; credentials.
4707 `("-E" ,@params)
4708 ;; If -P is passed to ISQL as the last argument without a
4709 ;; password, it's considered null.
4710 `(,@params "-P"))))
4711 (sql-comint product params)))
4712
4713 \f
4714
4715 ;;;###autoload
4716 (defun sql-postgres (&optional buffer)
4717 "Run psql by Postgres as an inferior process.
4718
4719 If buffer `*SQL*' exists but no process is running, make a new process.
4720 If buffer exists and a process is running, just switch to buffer
4721 `*SQL*'.
4722
4723 Interpreter used comes from variable `sql-postgres-program'. Login uses
4724 the variables `sql-database' and `sql-server' as default, if set.
4725 Additional command line parameters can be stored in the list
4726 `sql-postgres-options'.
4727
4728 The buffer is put in SQL interactive mode, giving commands for sending
4729 input. See `sql-interactive-mode'.
4730
4731 To set the buffer name directly, use \\[universal-argument]
4732 before \\[sql-postgres]. Once session has started,
4733 \\[sql-rename-buffer] can be called separately to rename the
4734 buffer.
4735
4736 To specify a coding system for converting non-ASCII characters
4737 in the input and output to the process, use \\[universal-coding-system-argument]
4738 before \\[sql-postgres]. You can also specify this with \\[set-buffer-process-coding-system]
4739 in the SQL buffer, after you start the process.
4740 The default comes from `process-coding-system-alist' and
4741 `default-process-coding-system'. If your output lines end with ^M,
4742 your might try undecided-dos as a coding system. If this doesn't help,
4743 Try to set `comint-output-filter-functions' like this:
4744
4745 \(setq comint-output-filter-functions (append comint-output-filter-functions
4746 '(comint-strip-ctrl-m)))
4747
4748 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4749 (interactive "P")
4750 (sql-product-interactive 'postgres buffer))
4751
4752 (defun sql-comint-postgres (product options)
4753 "Create comint buffer and connect to Postgres."
4754 ;; username and password are ignored. Mark Stosberg suggests to add
4755 ;; the database at the end. Jason Beegan suggests using --pset and
4756 ;; pager=off instead of \\o|cat. The later was the solution by
4757 ;; Gregor Zych. Jason's suggestion is the default value for
4758 ;; sql-postgres-options.
4759 (let ((params
4760 (append
4761 (if (not (= 0 sql-port))
4762 (list "-p" (number-to-string sql-port)))
4763 (if (not (string= "" sql-user))
4764 (list "-U" sql-user))
4765 (if (not (string= "" sql-server))
4766 (list "-h" sql-server))
4767 options
4768 (if (not (string= "" sql-database))
4769 (list sql-database)))))
4770 (sql-comint product params)))
4771
4772 (defun sql-postgres-completion-object (sqlbuf schema)
4773 (sql-redirect sqlbuf "\\t on")
4774 (let ((aligned
4775 (string= "aligned"
4776 (car (sql-redirect-value
4777 sqlbuf "\\a"
4778 "Output format is \\(.*\\)[.]$" 1)))))
4779 (when aligned
4780 (sql-redirect sqlbuf "\\a"))
4781 (let* ((fs (or (car (sql-redirect-value
4782 sqlbuf "\\f" "Field separator is \"\\(.\\)[.]$" 1))
4783 "|"))
4784 (re (concat "^\\([^" fs "]*\\)" fs "\\([^" fs "]*\\)"
4785 fs "[^" fs "]*" fs "[^" fs "]*$"))
4786 (cl (if (not schema)
4787 (sql-redirect-value sqlbuf "\\d" re '(1 2))
4788 (append (sql-redirect-value
4789 sqlbuf (format "\\dt %s.*" schema) re '(1 2))
4790 (sql-redirect-value
4791 sqlbuf (format "\\dv %s.*" schema) re '(1 2))
4792 (sql-redirect-value
4793 sqlbuf (format "\\ds %s.*" schema) re '(1 2))))))
4794
4795 ;; Restore tuples and alignment to what they were.
4796 (sql-redirect sqlbuf "\\t off")
4797 (when (not aligned)
4798 (sql-redirect sqlbuf "\\a"))
4799
4800 ;; Return the list of table names (public schema name can be omitted)
4801 (mapcar (lambda (tbl)
4802 (if (string= (car tbl) "public")
4803 (cadr tbl)
4804 (format "%s.%s" (car tbl) (cadr tbl))))
4805 cl))))
4806
4807 \f
4808
4809 ;;;###autoload
4810 (defun sql-interbase (&optional buffer)
4811 "Run isql by Interbase as an inferior process.
4812
4813 If buffer `*SQL*' exists but no process is running, make a new process.
4814 If buffer exists and a process is running, just switch to buffer
4815 `*SQL*'.
4816
4817 Interpreter used comes from variable `sql-interbase-program'. Login
4818 uses the variables `sql-user', `sql-password', and `sql-database' as
4819 defaults, if set.
4820
4821 The buffer is put in SQL interactive mode, giving commands for sending
4822 input. See `sql-interactive-mode'.
4823
4824 To set the buffer name directly, use \\[universal-argument]
4825 before \\[sql-interbase]. Once session has started,
4826 \\[sql-rename-buffer] can be called separately to rename the
4827 buffer.
4828
4829 To specify a coding system for converting non-ASCII characters
4830 in the input and output to the process, use \\[universal-coding-system-argument]
4831 before \\[sql-interbase]. You can also specify this with \\[set-buffer-process-coding-system]
4832 in the SQL buffer, after you start the process.
4833 The default comes from `process-coding-system-alist' and
4834 `default-process-coding-system'.
4835
4836 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4837 (interactive "P")
4838 (sql-product-interactive 'interbase buffer))
4839
4840 (defun sql-comint-interbase (product options)
4841 "Create comint buffer and connect to Interbase."
4842 ;; Put all parameters to the program (if defined) in a list and call
4843 ;; make-comint.
4844 (let ((params
4845 (append
4846 (if (not (string= "" sql-database))
4847 (list sql-database)) ; Add to the front!
4848 (if (not (string= "" sql-password))
4849 (list "-p" sql-password))
4850 (if (not (string= "" sql-user))
4851 (list "-u" sql-user))
4852 options)))
4853 (sql-comint product params)))
4854
4855 \f
4856
4857 ;;;###autoload
4858 (defun sql-db2 (&optional buffer)
4859 "Run db2 by IBM as an inferior process.
4860
4861 If buffer `*SQL*' exists but no process is running, make a new process.
4862 If buffer exists and a process is running, just switch to buffer
4863 `*SQL*'.
4864
4865 Interpreter used comes from variable `sql-db2-program'. There is not
4866 automatic login.
4867
4868 The buffer is put in SQL interactive mode, giving commands for sending
4869 input. See `sql-interactive-mode'.
4870
4871 If you use \\[sql-accumulate-and-indent] to send multiline commands to
4872 db2, newlines will be escaped if necessary. If you don't want that, set
4873 `comint-input-sender' back to `comint-simple-send' by writing an after
4874 advice. See the elisp manual for more information.
4875
4876 To set the buffer name directly, use \\[universal-argument]
4877 before \\[sql-db2]. Once session has started,
4878 \\[sql-rename-buffer] can be called separately to rename the
4879 buffer.
4880
4881 To specify a coding system for converting non-ASCII characters
4882 in the input and output to the process, use \\[universal-coding-system-argument]
4883 before \\[sql-db2]. You can also specify this with \\[set-buffer-process-coding-system]
4884 in the SQL buffer, after you start the process.
4885 The default comes from `process-coding-system-alist' and
4886 `default-process-coding-system'.
4887
4888 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4889 (interactive "P")
4890 (sql-product-interactive 'db2 buffer))
4891
4892 (defun sql-comint-db2 (product options)
4893 "Create comint buffer and connect to DB2."
4894 ;; Put all parameters to the program (if defined) in a list and call
4895 ;; make-comint.
4896 (sql-comint product options))
4897
4898 ;;;###autoload
4899 (defun sql-linter (&optional buffer)
4900 "Run inl by RELEX as an inferior process.
4901
4902 If buffer `*SQL*' exists but no process is running, make a new process.
4903 If buffer exists and a process is running, just switch to buffer
4904 `*SQL*'.
4905
4906 Interpreter used comes from variable `sql-linter-program' - usually `inl'.
4907 Login uses the variables `sql-user', `sql-password', `sql-database' and
4908 `sql-server' as defaults, if set. Additional command line parameters
4909 can be stored in the list `sql-linter-options'. Run inl -h to get help on
4910 parameters.
4911
4912 `sql-database' is used to set the LINTER_MBX environment variable for
4913 local connections, `sql-server' refers to the server name from the
4914 `nodetab' file for the network connection (dbc_tcp or friends must run
4915 for this to work). If `sql-password' is an empty string, inl will use
4916 an empty password.
4917
4918 The buffer is put in SQL interactive mode, giving commands for sending
4919 input. See `sql-interactive-mode'.
4920
4921 To set the buffer name directly, use \\[universal-argument]
4922 before \\[sql-linter]. Once session has started,
4923 \\[sql-rename-buffer] can be called separately to rename the
4924 buffer.
4925
4926 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4927 (interactive "P")
4928 (sql-product-interactive 'linter buffer))
4929
4930 (defun sql-comint-linter (product options)
4931 "Create comint buffer and connect to Linter."
4932 ;; Put all parameters to the program (if defined) in a list and call
4933 ;; make-comint.
4934 (let* ((login
4935 (if (not (string= "" sql-user))
4936 (concat sql-user "/" sql-password)))
4937 (params
4938 (append
4939 (if (not (string= "" sql-server))
4940 (list "-n" sql-server))
4941 (list "-u" login)
4942 options)))
4943 (cl-letf (((getenv "LINTER_MBX")
4944 (unless (string= "" sql-database) sql-database)))
4945 (sql-comint product params))))
4946
4947 \f
4948
4949 (provide 'sql)
4950
4951 ;;; sql.el ends here
4952
4953 ; LocalWords: sql SQL SQLite sqlite Sybase Informix MySQL
4954 ; LocalWords: Postgres SQLServer SQLi