* lisp/replace.el: Add "collect" feature to occur.
[emacs.git] / lisp / progmodes / sql.el
blobacb34eacc2b0e2e0fa25b2cb20bdc2050e3133d4
1 ;;; sql.el --- specialized comint.el for SQL interpreters
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Alex Schroeder <alex@gnu.org>
7 ;; Maintainer: Michael Mauger <mmaug@yahoo.com>
8 ;; Version: 2.8
9 ;; Keywords: comm languages processes
10 ;; URL: http://savannah.gnu.org/projects/emacs/
11 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlMode
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Commentary:
30 ;; Please send bug reports and bug fixes to the mailing list at
31 ;; help-gnu-emacs@gnu.org. If you want to subscribe to the mailing
32 ;; list, see the web page at
33 ;; http://lists.gnu.org/mailman/listinfo/help-gnu-emacs for
34 ;; instructions. I monitor this list actively. If you send an e-mail
35 ;; to Alex Schroeder it usually makes it to me when Alex has a chance
36 ;; to forward them along (Thanks, Alex).
38 ;; This file provides a sql-mode and a sql-interactive-mode. The
39 ;; original goals were two simple modes providing syntactic
40 ;; highlighting. The interactive mode had to provide a command-line
41 ;; history; the other mode had to provide "send region/buffer to SQL
42 ;; interpreter" functions. "simple" in this context means easy to
43 ;; use, easy to maintain and little or no bells and whistles. This
44 ;; has changed somewhat as experience with the mode has accumulated.
46 ;; Support for different flavors of SQL and command interpreters was
47 ;; available in early versions of sql.el. This support has been
48 ;; extended and formalized in later versions. Part of the impetus for
49 ;; the improved support of SQL flavors was borne out of the current
50 ;; maintainer's consulting experience. In the past fifteen years, I
51 ;; have used Oracle, Sybase, Informix, MySQL, Postgres, and SQLServer.
52 ;; On some assignments, I have used two or more of these concurrently.
54 ;; If anybody feels like extending this sql mode, take a look at the
55 ;; above mentioned modes and write a sqlx-mode on top of this one. If
56 ;; this proves to be difficult, please suggest changes that will
57 ;; facilitate your plans. Facilities have been provided to add
58 ;; products and product-specific configuration.
60 ;; sql-interactive-mode is used to interact with a SQL interpreter
61 ;; process in a SQLi buffer (usually called `*SQL*'). The SQLi buffer
62 ;; is created by calling a SQL interpreter-specific entry function or
63 ;; sql-product-interactive. Do *not* call sql-interactive-mode by
64 ;; itself.
66 ;; The list of currently supported interpreters and the corresponding
67 ;; entry function used to create the SQLi buffers is shown with
68 ;; `sql-help' (M-x sql-help).
70 ;; Since sql-interactive-mode is built on top of the general
71 ;; command-interpreter-in-a-buffer mode (comint mode), it shares a
72 ;; common base functionality, and a common set of bindings, with all
73 ;; modes derived from comint mode. This makes these modes easier to
74 ;; use.
76 ;; sql-mode can be used to keep editing SQL statements. The SQL
77 ;; statements can be sent to the SQL process in the SQLi buffer.
79 ;; For documentation on the functionality provided by comint mode, and
80 ;; the hooks available for customizing it, see the file `comint.el'.
82 ;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
83 ;; `imenu-add-menubar-index'.
85 ;;; Requirements for Emacs 19.34:
87 ;; If you are using Emacs 19.34, you will have to get and install
88 ;; the file regexp-opt.el
89 ;; <URL:ftp://ftp.ifi.uio.no/pub/emacs/emacs-20.3/lisp/emacs-lisp/regexp-opt.el>
90 ;; and the custom package
91 ;; <URL:http://www.dina.kvl.dk/~abraham/custom/>.
93 ;;; Bugs:
95 ;; sql-ms now uses osql instead of isql. Osql flushes its error
96 ;; stream more frequently than isql so that error messages are
97 ;; available. There is no prompt and some output still is buffered.
98 ;; This improves the interaction under Emacs but it still is somewhat
99 ;; awkward.
101 ;; Quoted identifiers are not supported for hilighting. Most
102 ;; databases support the use of double quoted strings in place of
103 ;; identifiers; ms (Microsoft SQLServer) also supports identifiers
104 ;; enclosed within brackets [].
106 ;;; Product Support:
108 ;; To add support for additional SQL products the following steps
109 ;; must be followed ("xyz" is the name of the product in the examples
110 ;; below):
112 ;; 1) Add the product to the list of known products.
114 ;; (sql-add-product 'xyz "XyzDB"
115 ;; '(:free-software t))
117 ;; 2) Define font lock settings. All ANSI keywords will be
118 ;; highlighted automatically, so only product specific keywords
119 ;; need to be defined here.
121 ;; (defvar my-sql-mode-xyz-font-lock-keywords
122 ;; '(("\\b\\(red\\|orange\\|yellow\\)\\b"
123 ;; . font-lock-keyword-face))
124 ;; "XyzDB SQL keywords used by font-lock.")
126 ;; (sql-set-product-feature 'xyz
127 ;; :font-lock
128 ;; 'my-sql-mode-xyz-font-lock-keywords)
130 ;; 3) Define any special syntax characters including comments and
131 ;; identifier characters.
133 ;; (sql-set-product-feature 'xyz
134 ;; :syntax-alist ((?# . "w")))
136 ;; 4) Define the interactive command interpreter for the database
137 ;; product.
139 ;; (defcustom my-sql-xyz-program "ixyz"
140 ;; "Command to start ixyz by XyzDB."
141 ;; :type 'file
142 ;; :group 'SQL)
144 ;; (sql-set-product-feature 'xyz
145 ;; :sqli-program 'my-sql-xyz-program)
146 ;; (sql-set-product-feature 'xyz
147 ;; :prompt-regexp "^xyzdb> ")
148 ;; (sql-set-product-feature 'xyz
149 ;; :prompt-length 7)
151 ;; 5) Define login parameters and command line formatting.
153 ;; (defcustom my-sql-xyz-login-params '(user password server database)
154 ;; "Login parameters to needed to connect to XyzDB."
155 ;; :type 'sql-login-params
156 ;; :group 'SQL)
158 ;; (sql-set-product-feature 'xyz
159 ;; :sqli-login 'my-sql-xyz-login-params)
161 ;; (defcustom my-sql-xyz-options '("-X" "-Y" "-Z")
162 ;; "List of additional options for `sql-xyz-program'."
163 ;; :type '(repeat string)
164 ;; :group 'SQL)
166 ;; (sql-set-product-feature 'xyz
167 ;; :sqli-options 'my-sql-xyz-options))
169 ;; (defun my-sql-comint-xyz (product options)
170 ;; "Connect ti XyzDB in a comint buffer."
172 ;; ;; Do something with `sql-user', `sql-password',
173 ;; ;; `sql-database', and `sql-server'.
174 ;; (let ((params options))
175 ;; (if (not (string= "" sql-server))
176 ;; (setq params (append (list "-S" sql-server) params)))
177 ;; (if (not (string= "" sql-database))
178 ;; (setq params (append (list "-D" sql-database) params)))
179 ;; (if (not (string= "" sql-password))
180 ;; (setq params (append (list "-P" sql-password) params)))
181 ;; (if (not (string= "" sql-user))
182 ;; (setq params (append (list "-U" sql-user) params)))
183 ;; (sql-comint product params)))
185 ;; (sql-set-product-feature 'xyz
186 ;; :sqli-comint-func 'my-sql-comint-xyz)
188 ;; 6) Define a convienence function to invoke the SQL interpreter.
190 ;; (defun my-sql-xyz (&optional buffer)
191 ;; "Run ixyz by XyzDB as an inferior process."
192 ;; (interactive "P")
193 ;; (sql-product-interactive 'xyz buffer))
195 ;;; To Do:
197 ;; Improve keyword highlighting for individual products. I have tried
198 ;; to update those database that I use. Feel free to send me updates,
199 ;; or direct me to the reference manuals for your favorite database.
201 ;; When there are no keywords defined, the ANSI keywords are
202 ;; highlighted. ANSI keywords are highlighted even if the keyword is
203 ;; not used for your current product. This should help identify
204 ;; portability concerns.
206 ;; Add different highlighting levels.
208 ;; Add support for listing available tables or the columns in a table.
210 ;;; Thanks to all the people who helped me out:
212 ;; Alex Schroeder <alex@gnu.org> -- the original author
213 ;; Kai Blauberg <kai.blauberg@metla.fi>
214 ;; <ibalaban@dalet.com>
215 ;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
216 ;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
217 ;; nino <nino@inform.dk>
218 ;; Berend de Boer <berend@pobox.com>
219 ;; Adam Jenkins <adam@thejenkins.org>
220 ;; Michael Mauger <mmaug@yahoo.com> -- improved product support
221 ;; Drew Adams <drew.adams@oracle.com> -- Emacs 20 support
222 ;; Harald Maier <maierh@myself.com> -- sql-send-string
223 ;; Stefan Monnier <monnier@iro.umontreal.ca> -- font-lock corrections; code polish
227 ;;; Code:
229 (require 'comint)
230 ;; Need the following to allow GNU Emacs 19 to compile the file.
231 (eval-when-compile
232 (require 'regexp-opt))
233 (require 'custom)
234 (eval-when-compile ;; needed in Emacs 19, 20
235 (setq max-specpdl-size (max max-specpdl-size 2000)))
237 (defvar font-lock-keyword-face)
238 (defvar font-lock-set-defaults)
239 (defvar font-lock-string-face)
241 ;;; Allow customization
243 (defgroup SQL nil
244 "Running a SQL interpreter from within Emacs buffers."
245 :version "20.4"
246 :group 'languages
247 :group 'processes)
249 ;; These four variables will be used as defaults, if set.
251 (defcustom sql-user ""
252 "Default username."
253 :type 'string
254 :group 'SQL
255 :safe 'stringp)
257 (defcustom sql-password ""
258 "Default password.
260 Storing your password in a textfile such as ~/.emacs could be dangerous.
261 Customizing your password will store it in your ~/.emacs file."
262 :type 'string
263 :group 'SQL
264 :risky t)
266 (defcustom sql-database ""
267 "Default database."
268 :type 'string
269 :group 'SQL
270 :safe 'stringp)
272 (defcustom sql-server ""
273 "Default server or host."
274 :type 'string
275 :group 'SQL
276 :safe 'stringp)
278 (defcustom sql-port 0
279 "Default port."
280 :version "24.1"
281 :type 'number
282 :group 'SQL
283 :safe 'numberp)
285 ;; Login parameter type
287 (define-widget 'sql-login-params 'lazy
288 "Widget definition of the login parameters list"
289 ;; FIXME: does not implement :default property for the user,
290 ;; database and server options. Anybody have some guidance on how to
291 ;; do this.
292 :tag "Login Parameters"
293 :type '(repeat (choice
294 (const user)
295 (const password)
296 (choice :tag "server"
297 (const server)
298 (list :tag "file"
299 (const :format "" server)
300 (const :format "" :file)
301 regexp)
302 (list :tag "completion"
303 (const :format "" server)
304 (const :format "" :completion)
305 (restricted-sexp
306 :match-alternatives (listp stringp))))
307 (choice :tag "database"
308 (const database)
309 (list :tag "file"
310 (const :format "" database)
311 (const :format "" :file)
312 regexp)
313 (list :tag "completion"
314 (const :format "" database)
315 (const :format "" :completion)
316 (restricted-sexp
317 :match-alternatives (listp stringp))))
318 (const port))))
320 ;; SQL Product support
322 (defvar sql-interactive-product nil
323 "Product under `sql-interactive-mode'.")
325 (defvar sql-connection nil
326 "Connection name if interactive session started by `sql-connect'.")
328 (defvar sql-product-alist
329 '((ansi
330 :name "ANSI"
331 :font-lock sql-mode-ansi-font-lock-keywords)
333 (db2
334 :name "DB2"
335 :font-lock sql-mode-db2-font-lock-keywords
336 :sqli-program sql-db2-program
337 :sqli-options sql-db2-options
338 :sqli-login sql-db2-login-params
339 :sqli-comint-func sql-comint-db2
340 :prompt-regexp "^db2 => "
341 :prompt-length 7
342 :prompt-cont-regexp "^db2 (cont\.) => "
343 :input-filter sql-escape-newlines-filter)
345 (informix
346 :name "Informix"
347 :font-lock sql-mode-informix-font-lock-keywords
348 :sqli-program sql-informix-program
349 :sqli-options sql-informix-options
350 :sqli-login sql-informix-login-params
351 :sqli-comint-func sql-comint-informix
352 :prompt-regexp "^> "
353 :prompt-length 2
354 :syntax-alist ((?{ . "<") (?} . ">")))
356 (ingres
357 :name "Ingres"
358 :font-lock sql-mode-ingres-font-lock-keywords
359 :sqli-program sql-ingres-program
360 :sqli-options sql-ingres-options
361 :sqli-login sql-ingres-login-params
362 :sqli-comint-func sql-comint-ingres
363 :prompt-regexp "^\* "
364 :prompt-length 2
365 :prompt-cont-regexp "^\* ")
367 (interbase
368 :name "Interbase"
369 :font-lock sql-mode-interbase-font-lock-keywords
370 :sqli-program sql-interbase-program
371 :sqli-options sql-interbase-options
372 :sqli-login sql-interbase-login-params
373 :sqli-comint-func sql-comint-interbase
374 :prompt-regexp "^SQL> "
375 :prompt-length 5)
377 (linter
378 :name "Linter"
379 :font-lock sql-mode-linter-font-lock-keywords
380 :sqli-program sql-linter-program
381 :sqli-options sql-linter-options
382 :sqli-login sql-linter-login-params
383 :sqli-comint-func sql-comint-linter
384 :prompt-regexp "^SQL>"
385 :prompt-length 4)
388 :name "Microsoft"
389 :font-lock sql-mode-ms-font-lock-keywords
390 :sqli-program sql-ms-program
391 :sqli-options sql-ms-options
392 :sqli-login sql-ms-login-params
393 :sqli-comint-func sql-comint-ms
394 :prompt-regexp "^[0-9]*>"
395 :prompt-length 5
396 :syntax-alist ((?@ . "w"))
397 :terminator ("^go" . "go"))
399 (mysql
400 :name "MySQL"
401 :free-software t
402 :font-lock sql-mode-mysql-font-lock-keywords
403 :sqli-program sql-mysql-program
404 :sqli-options sql-mysql-options
405 :sqli-login sql-mysql-login-params
406 :sqli-comint-func sql-comint-mysql
407 :list-all "SHOW TABLES;"
408 :list-table "DESCRIBE %s;"
409 :prompt-regexp "^mysql> "
410 :prompt-length 6
411 :prompt-cont-regexp "^ -> "
412 :input-filter sql-remove-tabs-filter)
414 (oracle
415 :name "Oracle"
416 :font-lock sql-mode-oracle-font-lock-keywords
417 :sqli-program sql-oracle-program
418 :sqli-options sql-oracle-options
419 :sqli-login sql-oracle-login-params
420 :sqli-comint-func sql-comint-oracle
421 :prompt-regexp "^SQL> "
422 :prompt-length 5
423 :prompt-cont-regexp "^\\s-*\\d+> "
424 :syntax-alist ((?$ . "w") (?# . "w"))
425 :terminator ("\\(^/\\|;\\)" . "/")
426 :input-filter sql-placeholders-filter)
428 (postgres
429 :name "Postgres"
430 :free-software t
431 :font-lock sql-mode-postgres-font-lock-keywords
432 :sqli-program sql-postgres-program
433 :sqli-options sql-postgres-options
434 :sqli-login sql-postgres-login-params
435 :sqli-comint-func sql-comint-postgres
436 :list-all ("\\d+" . "\\dS+")
437 :list-table ("\\d+ %s" . "\\dS+ %s")
438 :prompt-regexp "^.*=[#>] "
439 :prompt-length 5
440 :prompt-cont-regexp "^.*[-(][#>] "
441 :input-filter sql-remove-tabs-filter
442 :terminator ("\\(^\\s-*\\\\g\\|;\\)" . ";"))
444 (solid
445 :name "Solid"
446 :font-lock sql-mode-solid-font-lock-keywords
447 :sqli-program sql-solid-program
448 :sqli-options sql-solid-options
449 :sqli-login sql-solid-login-params
450 :sqli-comint-func sql-comint-solid
451 :prompt-regexp "^"
452 :prompt-length 0)
454 (sqlite
455 :name "SQLite"
456 :free-software t
457 :font-lock sql-mode-sqlite-font-lock-keywords
458 :sqli-program sql-sqlite-program
459 :sqli-options sql-sqlite-options
460 :sqli-login sql-sqlite-login-params
461 :sqli-comint-func sql-comint-sqlite
462 :list-all ".tables"
463 :list-table ".schema %s"
464 :prompt-regexp "^sqlite> "
465 :prompt-length 8
466 :prompt-cont-regexp "^ ...> "
467 :terminator ";")
469 (sybase
470 :name "Sybase"
471 :font-lock sql-mode-sybase-font-lock-keywords
472 :sqli-program sql-sybase-program
473 :sqli-options sql-sybase-options
474 :sqli-login sql-sybase-login-params
475 :sqli-comint-func sql-comint-sybase
476 :prompt-regexp "^SQL> "
477 :prompt-length 5
478 :syntax-alist ((?@ . "w"))
479 :terminator ("^go" . "go"))
481 "An alist of product specific configuration settings.
483 Without an entry in this list a product will not be properly
484 highlighted and will not support `sql-interactive-mode'.
486 Each element in the list is in the following format:
488 \(PRODUCT FEATURE VALUE ...)
490 where PRODUCT is the appropriate value of `sql-product'. The
491 product name is then followed by FEATURE-VALUE pairs. If a
492 FEATURE is not specified, its VALUE is treated as nil. FEATURE
493 may be any one of the following:
495 :name string containing the displayable name of
496 the product.
498 :free-software is the product Free (as in Freedom) software?
500 :font-lock name of the variable containing the product
501 specific font lock highlighting patterns.
503 :sqli-program name of the variable containing the product
504 specific interactive program name.
506 :sqli-options name of the variable containing the list
507 of product specific options.
509 :sqli-login name of the variable containing the list of
510 login parameters (i.e., user, password,
511 database and server) needed to connect to
512 the database.
514 :sqli-comint-func name of a function which accepts no
515 parameters that will use the values of
516 `sql-user', `sql-password',
517 `sql-database' and `sql-server' to open a
518 comint buffer and connect to the
519 database. Do product specific
520 configuration of comint in this function.
522 :list-all Command string or function which produces
523 a listing of all objects in the database.
524 If it's a cons cell, then the car
525 produces the standard list of objects and
526 the cdr produces an enhanced list of
527 objects. What \"enhanced\" means is
528 dependent on the SQL product and may not
529 exist. In general though, the
530 \"enhanced\" list should include visible
531 objects from other schemas.
533 :list-table Command string or function which produces
534 a detailed listing of a specific database
535 table. If its a cons cell, then the car
536 produces the standard list and the cdr
537 produces an enhanced list.
539 :prompt-regexp regular expression string that matches
540 the prompt issued by the product
541 interpreter.
543 :prompt-length length of the prompt on the line.
545 :prompt-cont-regexp regular expression string that matches
546 the continuation prompt issued by the
547 product interpreter.
549 :input-filter function which can filter strings sent to
550 the command interpreter. It is also used
551 by the `sql-send-string',
552 `sql-send-region', `sql-send-paragraph'
553 and `sql-send-buffer' functions. The
554 function is passed the string sent to the
555 command interpreter and must return the
556 filtered string. May also be a list of
557 such functions.
559 :terminator the terminator to be sent after a
560 `sql-send-string', `sql-send-region',
561 `sql-send-paragraph' and
562 `sql-send-buffer' command. May be the
563 literal string or a cons of a regexp to
564 match an existing terminator in the
565 string and the terminator to be used if
566 its absent. By default \";\".
568 :syntax-alist alist of syntax table entries to enable
569 special character treatment by font-lock
570 and imenu.
572 Other features can be stored but they will be ignored. However,
573 you can develop new functionality which is product independent by
574 using `sql-get-product-feature' to lookup the product specific
575 settings.")
577 (defvar sql-indirect-features
578 '(:font-lock :sqli-program :sqli-options :sqli-login))
580 (defcustom sql-connection-alist nil
581 "An alist of connection parameters for interacting with a SQL
582 product.
584 Each element of the alist is as follows:
586 \(CONNECTION \(SQL-VARIABLE VALUE) ...)
588 Where CONNECTION is a symbol identifying the connection, SQL-VARIABLE
589 is the symbol name of a SQL mode variable, and VALUE is the value to
590 be assigned to the variable.
592 The most common SQL-VARIABLE settings associated with a connection
593 are:
595 `sql-product'
596 `sql-user'
597 `sql-password'
598 `sql-port'
599 `sql-server'
600 `sql-database'
602 If a SQL-VARIABLE is part of the connection, it will not be
603 prompted for during login."
605 :type `(alist :key-type (string :tag "Connection")
606 :value-type
607 (set
608 (group (const :tag "Product" sql-product)
609 (choice
610 ,@(mapcar (lambda (prod-info)
611 `(const :tag
612 ,(or (plist-get (cdr prod-info) :name)
613 (capitalize (symbol-name (car prod-info))))
614 (quote ,(car prod-info))))
615 sql-product-alist)))
616 (group (const :tag "Username" sql-user) string)
617 (group (const :tag "Password" sql-password) string)
618 (group (const :tag "Server" sql-server) string)
619 (group (const :tag "Database" sql-database) string)
620 (group (const :tag "Port" sql-port) integer)
621 (repeat :inline t
622 (list :tab "Other"
623 (symbol :tag " Variable Symbol")
624 (sexp :tag "Value Expression")))))
625 :version "24.1"
626 :group 'SQL)
628 (defcustom sql-product 'ansi
629 "Select the SQL database product used so that buffers can be
630 highlighted properly when you open them."
631 :type `(choice
632 ,@(mapcar (lambda (prod-info)
633 `(const :tag
634 ,(or (plist-get (cdr prod-info) :name)
635 (capitalize (symbol-name (car prod-info))))
636 ,(car prod-info)))
637 sql-product-alist))
638 :group 'SQL
639 :safe 'symbolp)
640 (defvaralias 'sql-dialect 'sql-product)
642 ;; misc customization of sql.el behaviour
644 (defcustom sql-electric-stuff nil
645 "Treat some input as electric.
646 If set to the symbol `semicolon', then hitting `;' will send current
647 input in the SQLi buffer to the process.
648 If set to the symbol `go', then hitting `go' on a line by itself will
649 send current input in the SQLi buffer to the process.
650 If set to nil, then you must use \\[comint-send-input] in order to send
651 current input in the SQLi buffer to the process."
652 :type '(choice (const :tag "Nothing" nil)
653 (const :tag "The semicolon `;'" semicolon)
654 (const :tag "The string `go' by itself" go))
655 :version "20.8"
656 :group 'SQL)
658 (defcustom sql-send-terminator nil
659 "When non-nil, add a terminator to text sent to the SQL interpreter.
661 When text is sent to the SQL interpreter (via `sql-send-string',
662 `sql-send-region', `sql-send-paragraph' or `sql-send-buffer'), a
663 command terminator can be automatically sent as well. The
664 terminator is not sent, if the string sent already ends with the
665 terminator.
667 If this value is t, then the default command terminator for the
668 SQL interpreter is sent. If this value is a string, then the
669 string is sent.
671 If the value is a cons cell of the form (PAT . TERM), then PAT is
672 a regexp used to match the terminator in the string and TERM is
673 the terminator to be sent. This form is useful if the SQL
674 interpreter has more than one way of submitting a SQL command.
675 The PAT regexp can match any of them, and TERM is the way we do
676 it automatically."
678 :type '(choice (const :tag "No Terminator" nil)
679 (const :tag "Default Terminator" t)
680 (string :tag "Terminator String")
681 (cons :tag "Terminator Pattern and String"
682 (string :tag "Terminator Pattern")
683 (string :tag "Terminator String")))
684 :version "22.2"
685 :group 'SQL)
687 (defcustom sql-pop-to-buffer-after-send-region nil
688 "When non-nil, pop to the buffer SQL statements are sent to.
690 After a call to `sql-sent-string', `sql-send-region',
691 `sql-send-paragraph' or `sql-send-buffer', the window is split
692 and the SQLi buffer is shown. If this variable is not nil, that
693 buffer's window will be selected by calling `pop-to-buffer'. If
694 this variable is nil, that buffer is shown using
695 `display-buffer'."
696 :type 'boolean
697 :group 'SQL)
699 ;; imenu support for sql-mode.
701 (defvar sql-imenu-generic-expression
702 ;; Items are in reverse order because they are rendered in reverse.
703 '(("Rules/Defaults" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*\\(rule\\|default\\)\\s-+\\(\\w+\\)" 3)
704 ("Sequences" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*sequence\\s-+\\(\\w+\\)" 2)
705 ("Triggers" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*trigger\\s-+\\(\\w+\\)" 2)
706 ("Functions" "^\\s-*\\(create\\s-+\\(\\w+\\s-+\\)*\\)?function\\s-+\\(\\w+\\)" 3)
707 ("Procedures" "^\\s-*\\(create\\s-+\\(\\w+\\s-+\\)*\\)?proc\\(edure\\)?\\s-+\\(\\w+\\)" 4)
708 ("Packages" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*package\\s-+\\(body\\s-+\\)?\\(\\w+\\)" 3)
709 ("Types" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*type\\s-+\\(body\\s-+\\)?\\(\\w+\\)" 3)
710 ("Indexes" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*index\\s-+\\(\\w+\\)" 2)
711 ("Tables/Views" "^\\s-*create\\s-+\\(\\w+\\s-+\\)*\\(table\\|view\\)\\s-+\\(\\w+\\)" 3))
712 "Define interesting points in the SQL buffer for `imenu'.
714 This is used to set `imenu-generic-expression' when SQL mode is
715 entered. Subsequent changes to `sql-imenu-generic-expression' will
716 not affect existing SQL buffers because imenu-generic-expression is
717 a local variable.")
719 ;; history file
721 (defcustom sql-input-ring-file-name nil
722 "If non-nil, name of the file to read/write input history.
724 You have to set this variable if you want the history of your commands
725 saved from one Emacs session to the next. If this variable is set,
726 exiting the SQL interpreter in an SQLi buffer will write the input
727 history to the specified file. Starting a new process in a SQLi buffer
728 will read the input history from the specified file.
730 This is used to initialize `comint-input-ring-file-name'.
732 Note that the size of the input history is determined by the variable
733 `comint-input-ring-size'."
734 :type '(choice (const :tag "none" nil)
735 (file))
736 :group 'SQL)
738 (defcustom sql-input-ring-separator "\n--\n"
739 "Separator between commands in the history file.
741 If set to \"\\n\", each line in the history file will be interpreted as
742 one command. Multi-line commands are split into several commands when
743 the input ring is initialized from a history file.
745 This variable used to initialize `comint-input-ring-separator'.
746 `comint-input-ring-separator' is part of Emacs 21; if your Emacs
747 does not have it, setting `sql-input-ring-separator' will have no
748 effect. In that case multiline commands will be split into several
749 commands when the input history is read, as if you had set
750 `sql-input-ring-separator' to \"\\n\"."
751 :type 'string
752 :group 'SQL)
754 ;; The usual hooks
756 (defcustom sql-interactive-mode-hook '()
757 "Hook for customizing `sql-interactive-mode'."
758 :type 'hook
759 :group 'SQL)
761 (defcustom sql-mode-hook '()
762 "Hook for customizing `sql-mode'."
763 :type 'hook
764 :group 'SQL)
766 (defcustom sql-set-sqli-hook '()
767 "Hook for reacting to changes of `sql-buffer'.
769 This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
770 is changed."
771 :type 'hook
772 :group 'SQL)
774 ;; Customization for Oracle
776 (defcustom sql-oracle-program "sqlplus"
777 "Command to start sqlplus by Oracle.
779 Starts `sql-interactive-mode' after doing some setup.
781 On Windows, \"sqlplus\" usually starts the sqlplus \"GUI\". In order
782 to start the sqlplus console, use \"plus33\" or something similar.
783 You will find the file in your Orant\\bin directory."
784 :type 'file
785 :group 'SQL)
787 (defcustom sql-oracle-options nil
788 "List of additional options for `sql-oracle-program'."
789 :type '(repeat string)
790 :version "20.8"
791 :group 'SQL)
793 (defcustom sql-oracle-login-params '(user password database)
794 "List of login parameters needed to connect to Oracle."
795 :type 'sql-login-params
796 :version "24.1"
797 :group 'SQL)
799 (defcustom sql-oracle-scan-on t
800 "Non-nil if placeholders should be replaced in Oracle SQLi.
802 When non-nil, Emacs will scan text sent to sqlplus and prompt
803 for replacement text for & placeholders as sqlplus does. This
804 is needed on Windows where sqlplus output is buffered and the
805 prompts are not shown until after the text is entered.
807 You will probably want to issue the following command in sqlplus
808 to be safe:
810 SET SCAN OFF"
811 :type 'boolean
812 :group 'SQL)
814 ;; Customization for SQLite
816 (defcustom sql-sqlite-program (or (executable-find "sqlite3")
817 (executable-find "sqlite")
818 "sqlite")
819 "Command to start SQLite.
821 Starts `sql-interactive-mode' after doing some setup."
822 :type 'file
823 :group 'SQL)
825 (defcustom sql-sqlite-options nil
826 "List of additional options for `sql-sqlite-program'."
827 :type '(repeat string)
828 :version "20.8"
829 :group 'SQL)
831 (defcustom sql-sqlite-login-params '((database :file ".*\\.\\(db\\|sqlite[23]?\\)"))
832 "List of login parameters needed to connect to SQLite."
833 :type 'sql-login-params
834 :version "24.1"
835 :group 'SQL)
837 ;; Customization for MySql
839 (defcustom sql-mysql-program "mysql"
840 "Command to start mysql by TcX.
842 Starts `sql-interactive-mode' after doing some setup."
843 :type 'file
844 :group 'SQL)
846 (defcustom sql-mysql-options nil
847 "List of additional options for `sql-mysql-program'.
848 The following list of options is reported to make things work
849 on Windows: \"-C\" \"-t\" \"-f\" \"-n\"."
850 :type '(repeat string)
851 :version "20.8"
852 :group 'SQL)
854 (defcustom sql-mysql-login-params '(user password database server)
855 "List of login parameters needed to connect to MySql."
856 :type 'sql-login-params
857 :version "24.1"
858 :group 'SQL)
860 ;; Customization for Solid
862 (defcustom sql-solid-program "solsql"
863 "Command to start SOLID SQL Editor.
865 Starts `sql-interactive-mode' after doing some setup."
866 :type 'file
867 :group 'SQL)
869 (defcustom sql-solid-login-params '(user password server)
870 "List of login parameters needed to connect to Solid."
871 :type 'sql-login-params
872 :version "24.1"
873 :group 'SQL)
875 ;; Customization for Sybase
877 (defcustom sql-sybase-program "isql"
878 "Command to start isql by Sybase.
880 Starts `sql-interactive-mode' after doing some setup."
881 :type 'file
882 :group 'SQL)
884 (defcustom sql-sybase-options nil
885 "List of additional options for `sql-sybase-program'.
886 Some versions of isql might require the -n option in order to work."
887 :type '(repeat string)
888 :version "20.8"
889 :group 'SQL)
891 (defcustom sql-sybase-login-params '(server user password database)
892 "List of login parameters needed to connect to Sybase."
893 :type 'sql-login-params
894 :version "24.1"
895 :group 'SQL)
897 ;; Customization for Informix
899 (defcustom sql-informix-program "dbaccess"
900 "Command to start dbaccess by Informix.
902 Starts `sql-interactive-mode' after doing some setup."
903 :type 'file
904 :group 'SQL)
906 (defcustom sql-informix-login-params '(database)
907 "List of login parameters needed to connect to Informix."
908 :type 'sql-login-params
909 :version "24.1"
910 :group 'SQL)
912 ;; Customization for Ingres
914 (defcustom sql-ingres-program "sql"
915 "Command to start sql by Ingres.
917 Starts `sql-interactive-mode' after doing some setup."
918 :type 'file
919 :group 'SQL)
921 (defcustom sql-ingres-login-params '(database)
922 "List of login parameters needed to connect to Ingres."
923 :type 'sql-login-params
924 :version "24.1"
925 :group 'SQL)
927 ;; Customization for Microsoft
929 (defcustom sql-ms-program "osql"
930 "Command to start osql by Microsoft.
932 Starts `sql-interactive-mode' after doing some setup."
933 :type 'file
934 :group 'SQL)
936 (defcustom sql-ms-options '("-w" "300" "-n")
937 ;; -w is the linesize
938 "List of additional options for `sql-ms-program'."
939 :type '(repeat string)
940 :version "22.1"
941 :group 'SQL)
943 (defcustom sql-ms-login-params '(user password server database)
944 "List of login parameters needed to connect to Microsoft."
945 :type 'sql-login-params
946 :version "24.1"
947 :group 'SQL)
949 ;; Customization for Postgres
951 (defcustom sql-postgres-program "psql"
952 "Command to start psql by Postgres.
954 Starts `sql-interactive-mode' after doing some setup."
955 :type 'file
956 :group 'SQL)
958 (defcustom sql-postgres-options '("-P" "pager=off")
959 "List of additional options for `sql-postgres-program'.
960 The default setting includes the -P option which breaks older versions
961 of the psql client (such as version 6.5.3). The -P option is equivalent
962 to the --pset option. If you want the psql to prompt you for a user
963 name, add the string \"-u\" to the list of options. If you want to
964 provide a user name on the command line (newer versions such as 7.1),
965 add your name with a \"-U\" prefix (such as \"-Umark\") to the list."
966 :type '(repeat string)
967 :version "20.8"
968 :group 'SQL)
970 (defcustom sql-postgres-login-params `((user :default ,(user-login-name))
971 (database :default ,(user-login-name))
972 server)
973 "List of login parameters needed to connect to Postgres."
974 :type 'sql-login-params
975 :version "24.1"
976 :group 'SQL)
978 ;; Customization for Interbase
980 (defcustom sql-interbase-program "isql"
981 "Command to start isql by Interbase.
983 Starts `sql-interactive-mode' after doing some setup."
984 :type 'file
985 :group 'SQL)
987 (defcustom sql-interbase-options nil
988 "List of additional options for `sql-interbase-program'."
989 :type '(repeat string)
990 :version "20.8"
991 :group 'SQL)
993 (defcustom sql-interbase-login-params '(user password database)
994 "List of login parameters needed to connect to Interbase."
995 :type 'sql-login-params
996 :version "24.1"
997 :group 'SQL)
999 ;; Customization for DB2
1001 (defcustom sql-db2-program "db2"
1002 "Command to start db2 by IBM.
1004 Starts `sql-interactive-mode' after doing some setup."
1005 :type 'file
1006 :group 'SQL)
1008 (defcustom sql-db2-options nil
1009 "List of additional options for `sql-db2-program'."
1010 :type '(repeat string)
1011 :version "20.8"
1012 :group 'SQL)
1014 (defcustom sql-db2-login-params nil
1015 "List of login parameters needed to connect to DB2."
1016 :type 'sql-login-params
1017 :version "24.1"
1018 :group 'SQL)
1020 ;; Customization for Linter
1022 (defcustom sql-linter-program "inl"
1023 "Command to start inl by RELEX.
1025 Starts `sql-interactive-mode' after doing some setup."
1026 :type 'file
1027 :group 'SQL)
1029 (defcustom sql-linter-options nil
1030 "List of additional options for `sql-linter-program'."
1031 :type '(repeat string)
1032 :version "21.3"
1033 :group 'SQL)
1035 (defcustom sql-linter-login-params '(user password database server)
1036 "Login parameters to needed to connect to Linter."
1037 :type 'sql-login-params
1038 :version "24.1"
1039 :group 'SQL)
1043 ;;; Variables which do not need customization
1045 (defvar sql-user-history nil
1046 "History of usernames used.")
1048 (defvar sql-database-history nil
1049 "History of databases used.")
1051 (defvar sql-server-history nil
1052 "History of servers used.")
1054 ;; Passwords are not kept in a history.
1056 (defvar sql-product-history nil
1057 "History of products used.")
1059 (defvar sql-connection-history nil
1060 "History of connections used.")
1062 (defvar sql-buffer nil
1063 "Current SQLi buffer.
1065 The global value of `sql-buffer' is the name of the latest SQLi buffer
1066 created. Any SQL buffer created will make a local copy of this value.
1067 See `sql-interactive-mode' for more on multiple sessions. If you want
1068 to change the SQLi buffer a SQL mode sends its SQL strings to, change
1069 the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
1071 (defvar sql-prompt-regexp nil
1072 "Prompt used to initialize `comint-prompt-regexp'.
1074 You can change `sql-prompt-regexp' on `sql-interactive-mode-hook'.")
1076 (defvar sql-prompt-length 0
1077 "Prompt used to set `left-margin' in `sql-interactive-mode'.
1079 You can change `sql-prompt-length' on `sql-interactive-mode-hook'.")
1081 (defvar sql-prompt-cont-regexp nil
1082 "Prompt pattern of statement continuation prompts.")
1084 (defvar sql-alternate-buffer-name nil
1085 "Buffer-local string used to possibly rename the SQLi buffer.
1087 Used by `sql-rename-buffer'.")
1089 (defun sql-buffer-live-p (buffer &optional product)
1090 "Returns non-nil if the process associated with buffer is live.
1092 BUFFER can be a buffer object or a buffer name. The buffer must
1093 be a live buffer, have an running process attached to it, be in
1094 `sql-interactive-mode', and, if PRODUCT is specified, it's
1095 `sql-product' must match."
1097 (when buffer
1098 (setq buffer (get-buffer buffer))
1099 (and buffer
1100 (buffer-live-p buffer)
1101 (get-buffer-process buffer)
1102 (comint-check-proc buffer)
1103 (with-current-buffer buffer
1104 (and (derived-mode-p 'sql-interactive-mode)
1105 (or (not product)
1106 (eq product sql-product)))))))
1108 ;; Keymap for sql-interactive-mode.
1110 (defvar sql-interactive-mode-map
1111 (let ((map (make-sparse-keymap)))
1112 (if (fboundp 'set-keymap-parent)
1113 (set-keymap-parent map comint-mode-map); Emacs
1114 (if (fboundp 'set-keymap-parents)
1115 (set-keymap-parents map (list comint-mode-map)))); XEmacs
1116 (if (fboundp 'set-keymap-name)
1117 (set-keymap-name map 'sql-interactive-mode-map)); XEmacs
1118 (define-key map (kbd "C-j") 'sql-accumulate-and-indent)
1119 (define-key map (kbd "C-c C-w") 'sql-copy-column)
1120 (define-key map (kbd "O") 'sql-magic-go)
1121 (define-key map (kbd "o") 'sql-magic-go)
1122 (define-key map (kbd ";") 'sql-magic-semicolon)
1123 (define-key map (kbd "C-c C-l a") 'sql-list-all)
1124 (define-key map (kbd "C-c C-l t") 'sql-list-table)
1125 map)
1126 "Mode map used for `sql-interactive-mode'.
1127 Based on `comint-mode-map'.")
1129 ;; Keymap for sql-mode.
1131 (defvar sql-mode-map
1132 (let ((map (make-sparse-keymap)))
1133 (define-key map (kbd "C-c C-c") 'sql-send-paragraph)
1134 (define-key map (kbd "C-c C-r") 'sql-send-region)
1135 (define-key map (kbd "C-c C-s") 'sql-send-string)
1136 (define-key map (kbd "C-c C-b") 'sql-send-buffer)
1137 (define-key map (kbd "C-c C-i") 'sql-product-interactive)
1138 (define-key map (kbd "C-c C-l a") 'sql-list-all)
1139 (define-key map (kbd "C-c C-l t") 'sql-list-table)
1140 map)
1141 "Mode map used for `sql-mode'.")
1143 ;; easy menu for sql-mode.
1145 (easy-menu-define
1146 sql-mode-menu sql-mode-map
1147 "Menu for `sql-mode'."
1148 `("SQL"
1149 ["Send Paragraph" sql-send-paragraph (sql-buffer-live-p sql-buffer)]
1150 ["Send Region" sql-send-region (and mark-active
1151 (sql-buffer-live-p sql-buffer))]
1152 ["Send Buffer" sql-send-buffer (sql-buffer-live-p sql-buffer)]
1153 ["Send String" sql-send-string (sql-buffer-live-p sql-buffer)]
1154 "--"
1155 ["List all objects" sql-list-all (sql-buffer-live-p sql-buffer)]
1156 ["List table details" sql-list-table (sql-buffer-live-p sql-buffer)]
1157 "--"
1158 ["Start SQLi session" sql-product-interactive
1159 :visible (not sql-connection-alist)
1160 :enable (sql-get-product-feature sql-product :sqli-comint-func)]
1161 ("Start..."
1162 :visible sql-connection-alist
1163 :filter sql-connection-menu-filter
1164 "--"
1165 ["New SQLi Session" sql-product-interactive (sql-get-product-feature sql-product :sqli-comint-func)])
1166 ["--"
1167 :visible sql-connection-alist]
1168 ["Show SQLi buffer" sql-show-sqli-buffer t]
1169 ["Set SQLi buffer" sql-set-sqli-buffer t]
1170 ["Pop to SQLi buffer after send"
1171 sql-toggle-pop-to-buffer-after-send-region
1172 :style toggle
1173 :selected sql-pop-to-buffer-after-send-region]
1174 ["--" nil nil]
1175 ("Product"
1176 ,@(mapcar (lambda (prod-info)
1177 (let* ((prod (pop prod-info))
1178 (name (or (plist-get prod-info :name)
1179 (capitalize (symbol-name prod))))
1180 (cmd (intern (format "sql-highlight-%s-keywords" prod))))
1181 (fset cmd `(lambda () ,(format "Highlight %s SQL keywords." name)
1182 (interactive)
1183 (sql-set-product ',prod)))
1184 (vector name cmd
1185 :style 'radio
1186 :selected `(eq sql-product ',prod))))
1187 sql-product-alist))))
1189 ;; easy menu for sql-interactive-mode.
1191 (easy-menu-define
1192 sql-interactive-mode-menu sql-interactive-mode-map
1193 "Menu for `sql-interactive-mode'."
1194 '("SQL"
1195 ["Rename Buffer" sql-rename-buffer t]
1196 ["Save Connection" sql-save-connection (not sql-connection)]
1197 "--"
1198 ["List all objects" sql-list-all t]
1199 ["List table details" sql-list-table t]))
1201 ;; Abbreviations -- if you want more of them, define them in your
1202 ;; ~/.emacs file. Abbrevs have to be enabled in your ~/.emacs, too.
1204 (defvar sql-mode-abbrev-table nil
1205 "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
1206 (unless sql-mode-abbrev-table
1207 (define-abbrev-table 'sql-mode-abbrev-table nil))
1209 (mapc
1210 ;; In Emacs 22+, provide SYSTEM-FLAG to define-abbrev.
1211 '(lambda (abbrev)
1212 (let ((name (car abbrev))
1213 (expansion (cdr abbrev)))
1214 (condition-case nil
1215 (define-abbrev sql-mode-abbrev-table name expansion nil 0 t)
1216 (error
1217 (define-abbrev sql-mode-abbrev-table name expansion)))))
1218 '(("ins" . "insert")
1219 ("upd" . "update")
1220 ("del" . "delete")
1221 ("sel" . "select")
1222 ("proc" . "procedure")
1223 ("func" . "function")
1224 ("cr" . "create")))
1226 ;; Syntax Table
1228 (defvar sql-mode-syntax-table
1229 (let ((table (make-syntax-table)))
1230 ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
1231 (modify-syntax-entry ?/ ". 14" table)
1232 (modify-syntax-entry ?* ". 23" table)
1233 ;; double-dash starts comments
1234 (modify-syntax-entry ?- ". 12b" table)
1235 ;; newline and formfeed end comments
1236 (modify-syntax-entry ?\n "> b" table)
1237 (modify-syntax-entry ?\f "> b" table)
1238 ;; single quotes (') delimit strings
1239 (modify-syntax-entry ?' "\"" table)
1240 ;; double quotes (") don't delimit strings
1241 (modify-syntax-entry ?\" "." table)
1242 ;; backslash is no escape character
1243 (modify-syntax-entry ?\\ "." table)
1244 table)
1245 "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
1247 ;; Font lock support
1249 (defvar sql-mode-font-lock-object-name
1250 (eval-when-compile
1251 (list (concat "^\\s-*\\(?:create\\|drop\\|alter\\)\\s-+" ;; lead off with CREATE, DROP or ALTER
1252 "\\(?:\\w+\\s-+\\)*" ;; optional intervening keywords
1253 "\\(?:table\\|view\\|\\(?:package\\|type\\)\\(?:\\s-+body\\)?\\|proc\\(?:edure\\)?"
1254 "\\|function\\|trigger\\|sequence\\|rule\\|default\\)\\s-+"
1255 "\\(\\w+\\)")
1256 1 'font-lock-function-name-face))
1258 "Pattern to match the names of top-level objects.
1260 The pattern matches the name in a CREATE, DROP or ALTER
1261 statement. The format of variable should be a valid
1262 `font-lock-keywords' entry.")
1264 ;; While there are international and American standards for SQL, they
1265 ;; are not followed closely, and most vendors offer significant
1266 ;; capabilities beyond those defined in the standard specifications.
1268 ;; SQL mode provides support for hilighting based on the product. In
1269 ;; addition to hilighting the product keywords, any ANSI keywords not
1270 ;; used by the product are also hilighted. This will help identify
1271 ;; keywords that could be restricted in future versions of the product
1272 ;; or might be a problem if ported to another product.
1274 ;; To reduce the complexity and size of the regular expressions
1275 ;; generated to match keywords, ANSI keywords are filtered out of
1276 ;; product keywords if they are equivalent. To do this, we define a
1277 ;; function `sql-font-lock-keywords-builder' that removes any keywords
1278 ;; that are matched by the ANSI patterns and results in the same face
1279 ;; being applied. For this to work properly, we must play some games
1280 ;; with the execution and compile time behavior. This code is a
1281 ;; little tricky but works properly.
1283 ;; When defining the keywords for individual products you should
1284 ;; include all of the keywords that you want matched. The filtering
1285 ;; against the ANSI keywords will be automatic if you use the
1286 ;; `sql-font-lock-keywords-builder' function and follow the
1287 ;; implementation pattern used for the other products in this file.
1289 (eval-when-compile
1290 (defvar sql-mode-ansi-font-lock-keywords)
1291 (setq sql-mode-ansi-font-lock-keywords nil))
1293 (eval-and-compile
1294 (defun sql-font-lock-keywords-builder (face boundaries &rest keywords)
1295 "Generation of regexp matching any one of KEYWORDS."
1297 (let ((bdy (or boundaries '("\\b" . "\\b")))
1298 kwd)
1300 ;; Remove keywords that are defined in ANSI
1301 (setq kwd keywords)
1302 (dolist (k keywords)
1303 (catch 'next
1304 (dolist (a sql-mode-ansi-font-lock-keywords)
1305 (when (and (eq face (cdr a))
1306 (eq (string-match (car a) k 0) 0)
1307 (eq (match-end 0) (length k)))
1308 (setq kwd (delq k kwd))
1309 (throw 'next nil)))))
1311 ;; Create a properly formed font-lock-keywords item
1312 (cons (concat (car bdy)
1313 (regexp-opt kwd t)
1314 (cdr bdy))
1315 face))))
1317 (eval-when-compile
1318 (setq sql-mode-ansi-font-lock-keywords
1319 (list
1320 ;; ANSI Non Reserved keywords
1321 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1322 "ada" "asensitive" "assignment" "asymmetric" "atomic" "between"
1323 "bitvar" "called" "catalog_name" "chain" "character_set_catalog"
1324 "character_set_name" "character_set_schema" "checked" "class_origin"
1325 "cobol" "collation_catalog" "collation_name" "collation_schema"
1326 "column_name" "command_function" "command_function_code" "committed"
1327 "condition_number" "connection_name" "constraint_catalog"
1328 "constraint_name" "constraint_schema" "contains" "cursor_name"
1329 "datetime_interval_code" "datetime_interval_precision" "defined"
1330 "definer" "dispatch" "dynamic_function" "dynamic_function_code"
1331 "existing" "exists" "final" "fortran" "generated" "granted"
1332 "hierarchy" "hold" "implementation" "infix" "insensitive" "instance"
1333 "instantiable" "invoker" "key_member" "key_type" "length" "m"
1334 "message_length" "message_octet_length" "message_text" "method" "more"
1335 "mumps" "name" "nullable" "number" "options" "overlaps" "overriding"
1336 "parameter_mode" "parameter_name" "parameter_ordinal_position"
1337 "parameter_specific_catalog" "parameter_specific_name"
1338 "parameter_specific_schema" "pascal" "pli" "position" "repeatable"
1339 "returned_length" "returned_octet_length" "returned_sqlstate"
1340 "routine_catalog" "routine_name" "routine_schema" "row_count" "scale"
1341 "schema_name" "security" "self" "sensitive" "serializable"
1342 "server_name" "similar" "simple" "source" "specific_name" "style"
1343 "subclass_origin" "sublist" "symmetric" "system" "table_name"
1344 "transaction_active" "transactions_committed"
1345 "transactions_rolled_back" "transform" "transforms" "trigger_catalog"
1346 "trigger_name" "trigger_schema" "type" "uncommitted" "unnamed"
1347 "user_defined_type_catalog" "user_defined_type_name"
1348 "user_defined_type_schema"
1350 ;; ANSI Reserved keywords
1351 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1352 "absolute" "action" "add" "admin" "after" "aggregate" "alias" "all"
1353 "allocate" "alter" "and" "any" "are" "as" "asc" "assertion" "at"
1354 "authorization" "before" "begin" "both" "breadth" "by" "call"
1355 "cascade" "cascaded" "case" "catalog" "check" "class" "close"
1356 "collate" "collation" "column" "commit" "completion" "connect"
1357 "connection" "constraint" "constraints" "constructor" "continue"
1358 "corresponding" "create" "cross" "cube" "current" "cursor" "cycle"
1359 "data" "day" "deallocate" "declare" "default" "deferrable" "deferred"
1360 "delete" "depth" "deref" "desc" "describe" "descriptor" "destroy"
1361 "destructor" "deterministic" "diagnostics" "dictionary" "disconnect"
1362 "distinct" "domain" "drop" "dynamic" "each" "else" "end" "equals"
1363 "escape" "every" "except" "exception" "exec" "execute" "external"
1364 "false" "fetch" "first" "for" "foreign" "found" "free" "from" "full"
1365 "function" "general" "get" "global" "go" "goto" "grant" "group"
1366 "grouping" "having" "host" "hour" "identity" "ignore" "immediate" "in"
1367 "indicator" "initialize" "initially" "inner" "inout" "input" "insert"
1368 "intersect" "into" "is" "isolation" "iterate" "join" "key" "language"
1369 "last" "lateral" "leading" "left" "less" "level" "like" "limit"
1370 "local" "locator" "map" "match" "minute" "modifies" "modify" "module"
1371 "month" "names" "natural" "new" "next" "no" "none" "not" "null" "of"
1372 "off" "old" "on" "only" "open" "operation" "option" "or" "order"
1373 "ordinality" "out" "outer" "output" "pad" "parameter" "parameters"
1374 "partial" "path" "postfix" "prefix" "preorder" "prepare" "preserve"
1375 "primary" "prior" "privileges" "procedure" "public" "read" "reads"
1376 "recursive" "references" "referencing" "relative" "restrict" "result"
1377 "return" "returns" "revoke" "right" "role" "rollback" "rollup"
1378 "routine" "rows" "savepoint" "schema" "scroll" "search" "second"
1379 "section" "select" "sequence" "session" "set" "sets" "size" "some"
1380 "space" "specific" "specifictype" "sql" "sqlexception" "sqlstate"
1381 "sqlwarning" "start" "state" "statement" "static" "structure" "table"
1382 "temporary" "terminate" "than" "then" "timezone_hour"
1383 "timezone_minute" "to" "trailing" "transaction" "translation"
1384 "trigger" "true" "under" "union" "unique" "unknown" "unnest" "update"
1385 "usage" "using" "value" "values" "variable" "view" "when" "whenever"
1386 "where" "with" "without" "work" "write" "year"
1389 ;; ANSI Functions
1390 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1391 "abs" "avg" "bit_length" "cardinality" "cast" "char_length"
1392 "character_length" "coalesce" "convert" "count" "current_date"
1393 "current_path" "current_role" "current_time" "current_timestamp"
1394 "current_user" "extract" "localtime" "localtimestamp" "lower" "max"
1395 "min" "mod" "nullif" "octet_length" "overlay" "placing" "session_user"
1396 "substring" "sum" "system_user" "translate" "treat" "trim" "upper"
1397 "user"
1399 ;; ANSI Data Types
1400 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1401 "array" "binary" "bit" "blob" "boolean" "char" "character" "clob"
1402 "date" "dec" "decimal" "double" "float" "int" "integer" "interval"
1403 "large" "national" "nchar" "nclob" "numeric" "object" "precision"
1404 "real" "ref" "row" "scope" "smallint" "time" "timestamp" "varchar"
1405 "varying" "zone"
1406 ))))
1408 (defvar sql-mode-ansi-font-lock-keywords
1409 (eval-when-compile sql-mode-ansi-font-lock-keywords)
1410 "ANSI SQL keywords used by font-lock.
1412 This variable is used by `sql-mode' and `sql-interactive-mode'. The
1413 regular expressions are created during compilation by calling the
1414 function `regexp-opt'. Therefore, take a look at the source before
1415 you define your own `sql-mode-ansi-font-lock-keywords'. You may want
1416 to add functions and PL/SQL keywords.")
1418 (defvar sql-mode-oracle-font-lock-keywords
1419 (eval-when-compile
1420 (list
1421 ;; Oracle SQL*Plus Commands
1422 (cons
1423 (concat
1424 "^\\s-*\\(?:\\(?:" (regexp-opt '(
1425 "@" "@@" "accept" "append" "archive" "attribute" "break"
1426 "btitle" "change" "clear" "column" "connect" "copy" "define"
1427 "del" "describe" "disconnect" "edit" "execute" "exit" "get" "help"
1428 "host" "input" "list" "password" "pause" "print" "prompt" "recover"
1429 "remark" "repfooter" "repheader" "run" "save" "show" "shutdown"
1430 "spool" "start" "startup" "store" "timing" "ttitle" "undefine"
1431 "variable" "whenever"
1432 ) t)
1434 "\\)\\|"
1435 "\\(?:compute\\s-+\\(?:avg\\|cou\\|min\\|max\\|num\\|sum\\|std\\|var\\)\\)\\|"
1436 "\\(?:set\\s-+\\("
1438 (regexp-opt
1439 '("appi" "appinfo" "array" "arraysize" "auto" "autocommit"
1440 "autop" "autoprint" "autorecovery" "autot" "autotrace" "blo"
1441 "blockterminator" "buffer" "closecursor" "cmds" "cmdsep"
1442 "colsep" "com" "compatibility" "con" "concat" "constraint"
1443 "constraints" "copyc" "copycommit" "copytypecheck" "database"
1444 "def" "define" "document" "echo" "editf" "editfile" "emb"
1445 "embedded" "esc" "escape" "feed" "feedback" "flagger" "flu"
1446 "flush" "hea" "heading" "heads" "headsep" "instance" "lin"
1447 "linesize" "lobof" "loboffset" "logsource" "long" "longc"
1448 "longchunksize" "maxdata" "newp" "newpage" "null" "num"
1449 "numf" "numformat" "numwidth" "pages" "pagesize" "pau"
1450 "pause" "recsep" "recsepchar" "role" "scan" "serveroutput"
1451 "shift" "shiftinout" "show" "showmode" "space" "sqlbl"
1452 "sqlblanklines" "sqlc" "sqlcase" "sqlco" "sqlcontinue" "sqln"
1453 "sqlnumber" "sqlp" "sqlpluscompat" "sqlpluscompatibility"
1454 "sqlpre" "sqlprefix" "sqlprompt" "sqlt" "sqlterminator"
1455 "statement_id" "suf" "suffix" "tab" "term" "termout" "ti"
1456 "time" "timi" "timing" "transaction" "trim" "trimout" "trims"
1457 "trimspool" "truncate" "und" "underline" "ver" "verify" "wra"
1458 "wrap")) "\\)\\)"
1460 "\\)\\b.*"
1462 'font-lock-doc-face)
1463 '("^\\s-*rem\\(?:ark\\)?\\>.*" . font-lock-comment-face)
1465 ;; Oracle Functions
1466 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1467 "abs" "acos" "add_months" "ascii" "asciistr" "asin" "atan" "atan2"
1468 "avg" "bfilename" "bin_to_num" "bitand" "cast" "ceil" "chartorowid"
1469 "chr" "coalesce" "compose" "concat" "convert" "corr" "cos" "cosh"
1470 "count" "covar_pop" "covar_samp" "cume_dist" "current_date"
1471 "current_timestamp" "current_user" "dbtimezone" "decode" "decompose"
1472 "dense_rank" "depth" "deref" "dump" "empty_clob" "existsnode" "exp"
1473 "extract" "extractvalue" "first" "first_value" "floor" "following"
1474 "from_tz" "greatest" "group_id" "grouping_id" "hextoraw" "initcap"
1475 "instr" "lag" "last" "last_day" "last_value" "lead" "least" "length"
1476 "ln" "localtimestamp" "lower" "lpad" "ltrim" "make_ref" "max" "min"
1477 "mod" "months_between" "new_time" "next_day" "nls_charset_decl_len"
1478 "nls_charset_id" "nls_charset_name" "nls_initcap" "nls_lower"
1479 "nls_upper" "nlssort" "ntile" "nullif" "numtodsinterval"
1480 "numtoyminterval" "nvl" "nvl2" "over" "path" "percent_rank"
1481 "percentile_cont" "percentile_disc" "power" "preceding" "rank"
1482 "ratio_to_report" "rawtohex" "rawtonhex" "reftohex" "regr_"
1483 "regr_avgx" "regr_avgy" "regr_count" "regr_intercept" "regr_r2"
1484 "regr_slope" "regr_sxx" "regr_sxy" "regr_syy" "replace" "round"
1485 "row_number" "rowidtochar" "rowidtonchar" "rpad" "rtrim"
1486 "sessiontimezone" "sign" "sin" "sinh" "soundex" "sqrt" "stddev"
1487 "stddev_pop" "stddev_samp" "substr" "sum" "sys_connect_by_path"
1488 "sys_context" "sys_dburigen" "sys_extract_utc" "sys_guid" "sys_typeid"
1489 "sys_xmlagg" "sys_xmlgen" "sysdate" "systimestamp" "tan" "tanh"
1490 "to_char" "to_clob" "to_date" "to_dsinterval" "to_lob" "to_multi_byte"
1491 "to_nchar" "to_nclob" "to_number" "to_single_byte" "to_timestamp"
1492 "to_timestamp_tz" "to_yminterval" "translate" "treat" "trim" "trunc"
1493 "tz_offset" "uid" "unbounded" "unistr" "updatexml" "upper" "user"
1494 "userenv" "var_pop" "var_samp" "variance" "vsize" "width_bucket" "xml"
1495 "xmlagg" "xmlattribute" "xmlcolattval" "xmlconcat" "xmlelement"
1496 "xmlforest" "xmlsequence" "xmltransform"
1498 ;; Oracle Keywords
1499 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1500 "abort" "access" "accessed" "account" "activate" "add" "admin"
1501 "advise" "after" "agent" "aggregate" "all" "allocate" "allow" "alter"
1502 "always" "analyze" "ancillary" "and" "any" "apply" "archive"
1503 "archivelog" "array" "as" "asc" "associate" "at" "attribute"
1504 "attributes" "audit" "authenticated" "authid" "authorization" "auto"
1505 "autoallocate" "automatic" "availability" "backup" "before" "begin"
1506 "behalf" "between" "binding" "bitmap" "block" "blocksize" "body"
1507 "both" "buffer_pool" "build" "by" "cache" "call" "cancel"
1508 "cascade" "case" "category" "certificate" "chained" "change" "check"
1509 "checkpoint" "child" "chunk" "class" "clear" "clone" "close" "cluster"
1510 "column" "column_value" "columns" "comment" "commit" "committed"
1511 "compatibility" "compile" "complete" "composite_limit" "compress"
1512 "compute" "connect" "connect_time" "consider" "consistent"
1513 "constraint" "constraints" "constructor" "contents" "context"
1514 "continue" "controlfile" "corruption" "cost" "cpu_per_call"
1515 "cpu_per_session" "create" "cross" "cube" "current" "currval" "cycle"
1516 "dangling" "data" "database" "datafile" "datafiles" "day" "ddl"
1517 "deallocate" "debug" "default" "deferrable" "deferred" "definer"
1518 "delay" "delete" "demand" "desc" "determines" "deterministic"
1519 "dictionary" "dimension" "directory" "disable" "disassociate"
1520 "disconnect" "distinct" "distinguished" "distributed" "dml" "drop"
1521 "each" "element" "else" "enable" "end" "equals_path" "escape"
1522 "estimate" "except" "exceptions" "exchange" "excluding" "exists"
1523 "expire" "explain" "extent" "external" "externally"
1524 "failed_login_attempts" "fast" "file" "final" "finish" "flush" "for"
1525 "force" "foreign" "freelist" "freelists" "freepools" "fresh" "from"
1526 "full" "function" "functions" "generated" "global" "global_name"
1527 "globally" "grant" "group" "grouping" "groups" "guard" "hash"
1528 "hashkeys" "having" "heap" "hierarchy" "id" "identified" "identifier"
1529 "idle_time" "immediate" "in" "including" "increment" "index" "indexed"
1530 "indexes" "indextype" "indextypes" "indicator" "initial" "initialized"
1531 "initially" "initrans" "inner" "insert" "instance" "instantiable"
1532 "instead" "intersect" "into" "invalidate" "is" "isolation" "java"
1533 "join" "keep" "key" "kill" "language" "left" "less" "level"
1534 "levels" "library" "like" "like2" "like4" "likec" "limit" "link"
1535 "list" "lob" "local" "location" "locator" "lock" "log" "logfile"
1536 "logging" "logical" "logical_reads_per_call"
1537 "logical_reads_per_session" "managed" "management" "manual" "map"
1538 "mapping" "master" "matched" "materialized" "maxdatafiles"
1539 "maxextents" "maximize" "maxinstances" "maxlogfiles" "maxloghistory"
1540 "maxlogmembers" "maxsize" "maxtrans" "maxvalue" "member" "memory"
1541 "merge" "migrate" "minextents" "minimize" "minimum" "minus" "minvalue"
1542 "mode" "modify" "monitoring" "month" "mount" "move" "movement" "name"
1543 "named" "natural" "nested" "never" "new" "next" "nextval" "no"
1544 "noarchivelog" "noaudit" "nocache" "nocompress" "nocopy" "nocycle"
1545 "nodelay" "noforce" "nologging" "nomapping" "nomaxvalue" "nominimize"
1546 "nominvalue" "nomonitoring" "none" "noorder" "noparallel" "norely"
1547 "noresetlogs" "noreverse" "normal" "norowdependencies" "nosort"
1548 "noswitch" "not" "nothing" "notimeout" "novalidate" "nowait" "null"
1549 "nulls" "object" "of" "off" "offline" "oidindex" "old" "on" "online"
1550 "only" "open" "operator" "optimal" "option" "or" "order"
1551 "organization" "out" "outer" "outline" "overflow" "overriding"
1552 "package" "packages" "parallel" "parallel_enable" "parameters"
1553 "parent" "partition" "partitions" "password" "password_grace_time"
1554 "password_life_time" "password_lock_time" "password_reuse_max"
1555 "password_reuse_time" "password_verify_function" "pctfree"
1556 "pctincrease" "pctthreshold" "pctused" "pctversion" "percent"
1557 "performance" "permanent" "pfile" "physical" "pipelined" "plan"
1558 "post_transaction" "pragma" "prebuilt" "preserve" "primary" "private"
1559 "private_sga" "privileges" "procedure" "profile" "protection" "public"
1560 "purge" "query" "quiesce" "quota" "range" "read" "reads" "rebuild"
1561 "records_per_block" "recover" "recovery" "recycle" "reduced" "ref"
1562 "references" "referencing" "refresh" "register" "reject" "relational"
1563 "rely" "rename" "reset" "resetlogs" "resize" "resolve" "resolver"
1564 "resource" "restrict" "restrict_references" "restricted" "result"
1565 "resumable" "resume" "retention" "return" "returning" "reuse"
1566 "reverse" "revoke" "rewrite" "right" "rnds" "rnps" "role" "roles"
1567 "rollback" "rollup" "row" "rowdependencies" "rownum" "rows" "sample"
1568 "savepoint" "scan" "schema" "scn" "scope" "segment" "select"
1569 "selectivity" "self" "sequence" "serializable" "session"
1570 "sessions_per_user" "set" "sets" "settings" "shared" "shared_pool"
1571 "shrink" "shutdown" "siblings" "sid" "single" "size" "skip" "some"
1572 "sort" "source" "space" "specification" "spfile" "split" "standby"
1573 "start" "statement_id" "static" "statistics" "stop" "storage" "store"
1574 "structure" "subpartition" "subpartitions" "substitutable"
1575 "successful" "supplemental" "suspend" "switch" "switchover" "synonym"
1576 "sys" "system" "table" "tables" "tablespace" "tempfile" "template"
1577 "temporary" "test" "than" "then" "thread" "through" "time_zone"
1578 "timeout" "to" "trace" "transaction" "trigger" "triggers" "truncate"
1579 "trust" "type" "types" "unarchived" "under" "under_path" "undo"
1580 "uniform" "union" "unique" "unlimited" "unlock" "unquiesce"
1581 "unrecoverable" "until" "unusable" "unused" "update" "upgrade" "usage"
1582 "use" "using" "validate" "validation" "value" "values" "variable"
1583 "varray" "version" "view" "wait" "when" "whenever" "where" "with"
1584 "without" "wnds" "wnps" "work" "write" "xmldata" "xmlschema" "xmltype"
1586 ;; Oracle Data Types
1587 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1588 "bfile" "blob" "byte" "char" "character" "clob" "date" "dec" "decimal"
1589 "double" "float" "int" "integer" "interval" "long" "national" "nchar"
1590 "nclob" "number" "numeric" "nvarchar2" "precision" "raw" "real"
1591 "rowid" "second" "smallint" "time" "timestamp" "urowid" "varchar"
1592 "varchar2" "varying" "year" "zone"
1595 ;; Oracle PL/SQL Attributes
1596 (sql-font-lock-keywords-builder 'font-lock-builtin-face '("" . "\\b")
1597 "%bulk_rowcount" "%found" "%isopen" "%notfound" "%rowcount" "%rowtype"
1598 "%type"
1601 ;; Oracle PL/SQL Functions
1602 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1603 "extend" "prior"
1606 ;; Oracle PL/SQL Keywords
1607 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1608 "autonomous_transaction" "bulk" "char_base" "collect" "constant"
1609 "cursor" "declare" "do" "elsif" "exception_init" "execute" "exit"
1610 "extends" "false" "fetch" "forall" "goto" "hour" "if" "interface"
1611 "loop" "minute" "number_base" "ocirowid" "opaque" "others" "rowtype"
1612 "separate" "serially_reusable" "sql" "sqlcode" "sqlerrm" "subtype"
1613 "the" "timezone_abbr" "timezone_hour" "timezone_minute"
1614 "timezone_region" "true" "varrying" "while"
1617 ;; Oracle PL/SQL Data Types
1618 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1619 "binary_integer" "boolean" "naturaln" "pls_integer" "positive"
1620 "positiven" "record" "signtype" "string"
1623 ;; Oracle PL/SQL Exceptions
1624 (sql-font-lock-keywords-builder 'font-lock-warning-face nil
1625 "access_into_null" "case_not_found" "collection_is_null"
1626 "cursor_already_open" "dup_val_on_index" "invalid_cursor"
1627 "invalid_number" "login_denied" "no_data_found" "not_logged_on"
1628 "program_error" "rowtype_mismatch" "self_is_null" "storage_error"
1629 "subscript_beyond_count" "subscript_outside_limit" "sys_invalid_rowid"
1630 "timeout_on_resource" "too_many_rows" "value_error" "zero_divide"
1631 "exception" "notfound"
1634 "Oracle SQL keywords used by font-lock.
1636 This variable is used by `sql-mode' and `sql-interactive-mode'. The
1637 regular expressions are created during compilation by calling the
1638 function `regexp-opt'. Therefore, take a look at the source before
1639 you define your own `sql-mode-oracle-font-lock-keywords'. You may want
1640 to add functions and PL/SQL keywords.")
1642 (defvar sql-mode-postgres-font-lock-keywords
1643 (eval-when-compile
1644 (list
1645 ;; Postgres psql commands
1646 '("^\\s-*\\\\.*$" . font-lock-doc-face)
1648 ;; Postgres unreserved words but may have meaning
1649 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil "a"
1650 "abs" "absent" "according" "ada" "alias" "allocate" "are" "array_agg"
1651 "asensitive" "atomic" "attribute" "attributes" "avg" "base64"
1652 "bernoulli" "bit_length" "bitvar" "blob" "blocked" "bom" "breadth" "c"
1653 "call" "cardinality" "catalog_name" "ceil" "ceiling" "char_length"
1654 "character_length" "character_set_catalog" "character_set_name"
1655 "character_set_schema" "characters" "checked" "class_origin" "clob"
1656 "cobol" "collation" "collation_catalog" "collation_name"
1657 "collation_schema" "collect" "column_name" "columns"
1658 "command_function" "command_function_code" "completion" "condition"
1659 "condition_number" "connect" "connection_name" "constraint_catalog"
1660 "constraint_name" "constraint_schema" "constructor" "contains"
1661 "control" "convert" "corr" "corresponding" "count" "covar_pop"
1662 "covar_samp" "cube" "cume_dist" "current_default_transform_group"
1663 "current_path" "current_transform_group_for_type" "cursor_name"
1664 "datalink" "datetime_interval_code" "datetime_interval_precision" "db"
1665 "defined" "degree" "dense_rank" "depth" "deref" "derived" "describe"
1666 "descriptor" "destroy" "destructor" "deterministic" "diagnostics"
1667 "disconnect" "dispatch" "dlnewcopy" "dlpreviouscopy" "dlurlcomplete"
1668 "dlurlcompleteonly" "dlurlcompletewrite" "dlurlpath" "dlurlpathonly"
1669 "dlurlpathwrite" "dlurlscheme" "dlurlserver" "dlvalue" "dynamic"
1670 "dynamic_function" "dynamic_function_code" "element" "empty"
1671 "end-exec" "equals" "every" "exception" "exec" "existing" "exp" "file"
1672 "filter" "final" "first_value" "flag" "floor" "fortran" "found" "free"
1673 "fs" "fusion" "g" "general" "generated" "get" "go" "goto" "grouping"
1674 "hex" "hierarchy" "host" "id" "ignore" "implementation" "import"
1675 "indent" "indicator" "infix" "initialize" "instance" "instantiable"
1676 "integrity" "intersection" "iterate" "k" "key_member" "key_type" "lag"
1677 "last_value" "lateral" "lead" "length" "less" "library" "like_regex"
1678 "link" "ln" "locator" "lower" "m" "map" "matched" "max"
1679 "max_cardinality" "member" "merge" "message_length"
1680 "message_octet_length" "message_text" "method" "min" "mod" "modifies"
1681 "modify" "module" "more" "multiset" "mumps" "namespace" "nclob"
1682 "nesting" "new" "nfc" "nfd" "nfkc" "nfkd" "nil" "normalize"
1683 "normalized" "nth_value" "ntile" "nullable" "number"
1684 "occurrences_regex" "octet_length" "octets" "old" "open" "operation"
1685 "ordering" "ordinality" "others" "output" "overriding" "p" "pad"
1686 "parameter" "parameter_mode" "parameter_name"
1687 "parameter_ordinal_position" "parameter_specific_catalog"
1688 "parameter_specific_name" "parameter_specific_schema" "parameters"
1689 "pascal" "passing" "passthrough" "percent_rank" "percentile_cont"
1690 "percentile_disc" "permission" "pli" "position_regex" "postfix"
1691 "power" "prefix" "preorder" "public" "rank" "reads" "recovery" "ref"
1692 "referencing" "regr_avgx" "regr_avgy" "regr_count" "regr_intercept"
1693 "regr_r2" "regr_slope" "regr_sxx" "regr_sxy" "regr_syy" "requiring"
1694 "respect" "restore" "result" "return" "returned_cardinality"
1695 "returned_length" "returned_octet_length" "returned_sqlstate" "rollup"
1696 "routine" "routine_catalog" "routine_name" "routine_schema"
1697 "row_count" "row_number" "scale" "schema_name" "scope" "scope_catalog"
1698 "scope_name" "scope_schema" "section" "selective" "self" "sensitive"
1699 "server_name" "sets" "size" "source" "space" "specific"
1700 "specific_name" "specifictype" "sql" "sqlcode" "sqlerror"
1701 "sqlexception" "sqlstate" "sqlwarning" "sqrt" "state" "static"
1702 "stddev_pop" "stddev_samp" "structure" "style" "subclass_origin"
1703 "sublist" "submultiset" "substring_regex" "sum" "system_user" "t"
1704 "table_name" "tablesample" "terminate" "than" "ties" "timezone_hour"
1705 "timezone_minute" "token" "top_level_count" "transaction_active"
1706 "transactions_committed" "transactions_rolled_back" "transform"
1707 "transforms" "translate" "translate_regex" "translation"
1708 "trigger_catalog" "trigger_name" "trigger_schema" "trim_array"
1709 "uescape" "under" "unlink" "unnamed" "unnest" "untyped" "upper" "uri"
1710 "usage" "user_defined_type_catalog" "user_defined_type_code"
1711 "user_defined_type_name" "user_defined_type_schema" "var_pop"
1712 "var_samp" "varbinary" "variable" "whenever" "width_bucket" "within"
1713 "xmlagg" "xmlbinary" "xmlcast" "xmlcomment" "xmldeclaration"
1714 "xmldocument" "xmlexists" "xmliterate" "xmlnamespaces" "xmlquery"
1715 "xmlschema" "xmltable" "xmltext" "xmlvalidate"
1718 ;; Postgres non-reserved words
1719 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1720 "abort" "absolute" "access" "action" "add" "admin" "after" "aggregate"
1721 "also" "alter" "always" "assertion" "assignment" "at" "backward"
1722 "before" "begin" "between" "by" "cache" "called" "cascade" "cascaded"
1723 "catalog" "chain" "characteristics" "checkpoint" "class" "close"
1724 "cluster" "coalesce" "comment" "comments" "commit" "committed"
1725 "configuration" "connection" "constraints" "content" "continue"
1726 "conversion" "copy" "cost" "createdb" "createrole" "createuser" "csv"
1727 "current" "cursor" "cycle" "data" "database" "day" "deallocate" "dec"
1728 "declare" "defaults" "deferred" "definer" "delete" "delimiter"
1729 "delimiters" "dictionary" "disable" "discard" "document" "domain"
1730 "drop" "each" "enable" "encoding" "encrypted" "enum" "escape"
1731 "exclude" "excluding" "exclusive" "execute" "exists" "explain"
1732 "external" "extract" "family" "first" "float" "following" "force"
1733 "forward" "function" "functions" "global" "granted" "greatest"
1734 "handler" "header" "hold" "hour" "identity" "if" "immediate"
1735 "immutable" "implicit" "including" "increment" "index" "indexes"
1736 "inherit" "inherits" "inline" "inout" "input" "insensitive" "insert"
1737 "instead" "invoker" "isolation" "key" "language" "large" "last"
1738 "lc_collate" "lc_ctype" "least" "level" "listen" "load" "local"
1739 "location" "lock" "login" "mapping" "match" "maxvalue" "minute"
1740 "minvalue" "mode" "month" "move" "name" "names" "national" "nchar"
1741 "next" "no" "nocreatedb" "nocreaterole" "nocreateuser" "noinherit"
1742 "nologin" "none" "nosuperuser" "nothing" "notify" "nowait" "nullif"
1743 "nulls" "object" "of" "oids" "operator" "option" "options" "out"
1744 "overlay" "owned" "owner" "parser" "partial" "partition" "password"
1745 "plans" "position" "preceding" "prepare" "prepared" "preserve" "prior"
1746 "privileges" "procedural" "procedure" "quote" "range" "read"
1747 "reassign" "recheck" "recursive" "reindex" "relative" "release"
1748 "rename" "repeatable" "replace" "replica" "reset" "restart" "restrict"
1749 "returns" "revoke" "role" "rollback" "row" "rows" "rule" "savepoint"
1750 "schema" "scroll" "search" "second" "security" "sequence" "sequences"
1751 "serializable" "server" "session" "set" "setof" "share" "show"
1752 "simple" "stable" "standalone" "start" "statement" "statistics"
1753 "stdin" "stdout" "storage" "strict" "strip" "substring" "superuser"
1754 "sysid" "system" "tables" "tablespace" "temp" "template" "temporary"
1755 "transaction" "treat" "trigger" "trim" "truncate" "trusted" "type"
1756 "unbounded" "uncommitted" "unencrypted" "unknown" "unlisten" "until"
1757 "update" "vacuum" "valid" "validator" "value" "values" "version"
1758 "view" "volatile" "whitespace" "work" "wrapper" "write"
1759 "xmlattributes" "xmlconcat" "xmlelement" "xmlforest" "xmlparse"
1760 "xmlpi" "xmlroot" "xmlserialize" "year" "yes"
1763 ;; Postgres Reserved
1764 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1765 "all" "analyse" "analyze" "and" "any" "array" "asc" "as" "asymmetric"
1766 "authorization" "binary" "both" "case" "cast" "check" "collate"
1767 "column" "concurrently" "constraint" "create" "cross"
1768 "current_catalog" "current_date" "current_role" "current_schema"
1769 "current_time" "current_timestamp" "current_user" "default"
1770 "deferrable" "desc" "distinct" "do" "else" "end" "except" "false"
1771 "fetch" "foreign" "for" "freeze" "from" "full" "grant" "group"
1772 "having" "ilike" "initially" "inner" "in" "intersect" "into" "isnull"
1773 "is" "join" "leading" "left" "like" "limit" "localtime"
1774 "localtimestamp" "natural" "notnull" "not" "null" "off" "offset"
1775 "only" "on" "order" "or" "outer" "overlaps" "over" "placing" "primary"
1776 "references" "returning" "right" "select" "session_user" "similar"
1777 "some" "symmetric" "table" "then" "to" "trailing" "true" "union"
1778 "unique" "user" "using" "variadic" "verbose" "when" "where" "window"
1779 "with"
1782 ;; Postgres Data Types
1783 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1784 "bigint" "bigserial" "bit" "bool" "boolean" "box" "bytea" "char"
1785 "character" "cidr" "circle" "date" "decimal" "double" "float4"
1786 "float8" "inet" "int" "int2" "int4" "int8" "integer" "interval" "line"
1787 "lseg" "macaddr" "money" "numeric" "path" "point" "polygon"
1788 "precision" "real" "serial" "serial4" "serial8" "smallint" "text"
1789 "time" "timestamp" "timestamptz" "timetz" "tsquery" "tsvector"
1790 "txid_snapshot" "uuid" "varbit" "varchar" "varying" "without"
1791 "xml" "zone"
1794 "Postgres SQL keywords used by font-lock.
1796 This variable is used by `sql-mode' and `sql-interactive-mode'. The
1797 regular expressions are created during compilation by calling the
1798 function `regexp-opt'. Therefore, take a look at the source before
1799 you define your own `sql-mode-postgres-font-lock-keywords'.")
1801 (defvar sql-mode-linter-font-lock-keywords
1802 (eval-when-compile
1803 (list
1804 ;; Linter Keywords
1805 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1806 "autocommit" "autoinc" "autorowid" "cancel" "cascade" "channel"
1807 "committed" "count" "countblob" "cross" "current" "data" "database"
1808 "datafile" "datafiles" "datesplit" "dba" "dbname" "default" "deferred"
1809 "denied" "description" "device" "difference" "directory" "error"
1810 "escape" "euc" "exclusive" "external" "extfile" "false" "file"
1811 "filename" "filesize" "filetime" "filter" "findblob" "first" "foreign"
1812 "full" "fuzzy" "global" "granted" "ignore" "immediate" "increment"
1813 "indexes" "indexfile" "indexfiles" "indextime" "initial" "integrity"
1814 "internal" "key" "last_autoinc" "last_rowid" "limit" "linter"
1815 "linter_file_device" "linter_file_size" "linter_name_length" "ln"
1816 "local" "login" "maxisn" "maxrow" "maxrowid" "maxvalue" "message"
1817 "minvalue" "module" "names" "national" "natural" "new" "new_table"
1818 "no" "node" "noneuc" "nulliferror" "numbers" "off" "old" "old_table"
1819 "only" "operation" "optimistic" "option" "page" "partially" "password"
1820 "phrase" "plan" "precision" "primary" "priority" "privileges"
1821 "proc_info_size" "proc_par_name_len" "protocol" "quant" "range" "raw"
1822 "read" "record" "records" "references" "remote" "rename" "replication"
1823 "restart" "rewrite" "root" "row" "rule" "savepoint" "security"
1824 "sensitive" "sequence" "serializable" "server" "since" "size" "some"
1825 "startup" "statement" "station" "success" "sys_guid" "tables" "test"
1826 "timeout" "trace" "transaction" "translation" "trigger"
1827 "trigger_info_size" "true" "trunc" "uncommitted" "unicode" "unknown"
1828 "unlimited" "unlisted" "user" "utf8" "value" "varying" "volumes"
1829 "wait" "windows_code" "workspace" "write" "xml"
1832 ;; Linter Reserved
1833 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1834 "access" "action" "add" "address" "after" "all" "alter" "always" "and"
1835 "any" "append" "as" "asc" "ascic" "async" "at_begin" "at_end" "audit"
1836 "aud_obj_name_len" "backup" "base" "before" "between" "blobfile"
1837 "blobfiles" "blobpct" "brief" "browse" "by" "case" "cast" "check"
1838 "clear" "close" "column" "comment" "commit" "connect" "contains"
1839 "correct" "create" "delete" "desc" "disable" "disconnect" "distinct"
1840 "drop" "each" "ef" "else" "enable" "end" "event" "except" "exclude"
1841 "execute" "exists" "extract" "fetch" "finish" "for" "from" "get"
1842 "grant" "group" "having" "identified" "in" "index" "inner" "insert"
1843 "instead" "intersect" "into" "is" "isolation" "join" "left" "level"
1844 "like" "lock" "mode" "modify" "not" "nowait" "null" "of" "on" "open"
1845 "or" "order" "outer" "owner" "press" "prior" "procedure" "public"
1846 "purge" "rebuild" "resource" "restrict" "revoke" "right" "role"
1847 "rollback" "rownum" "select" "session" "set" "share" "shutdown"
1848 "start" "stop" "sync" "synchronize" "synonym" "sysdate" "table" "then"
1849 "to" "union" "unique" "unlock" "until" "update" "using" "values"
1850 "view" "when" "where" "with" "without"
1853 ;; Linter Functions
1854 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1855 "abs" "acos" "asin" "atan" "atan2" "avg" "ceil" "cos" "cosh" "divtime"
1856 "exp" "floor" "getbits" "getblob" "getbyte" "getlong" "getraw"
1857 "getstr" "gettext" "getword" "hextoraw" "lenblob" "length" "log"
1858 "lower" "lpad" "ltrim" "max" "min" "mod" "monthname" "nvl"
1859 "octet_length" "power" "rand" "rawtohex" "repeat_string"
1860 "right_substr" "round" "rpad" "rtrim" "sign" "sin" "sinh" "soundex"
1861 "sqrt" "sum" "tan" "tanh" "timeint_to_days" "to_char" "to_date"
1862 "to_gmtime" "to_localtime" "to_number" "trim" "upper" "decode"
1863 "substr" "substring" "chr" "dayname" "days" "greatest" "hex" "initcap"
1864 "instr" "least" "multime" "replace" "width"
1867 ;; Linter Data Types
1868 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1869 "bigint" "bitmap" "blob" "boolean" "char" "character" "date"
1870 "datetime" "dec" "decimal" "double" "float" "int" "integer" "nchar"
1871 "number" "numeric" "real" "smallint" "varbyte" "varchar" "byte"
1872 "cursor" "long"
1875 "Linter SQL keywords used by font-lock.
1877 This variable is used by `sql-mode' and `sql-interactive-mode'. The
1878 regular expressions are created during compilation by calling the
1879 function `regexp-opt'.")
1881 (defvar sql-mode-ms-font-lock-keywords
1882 (eval-when-compile
1883 (list
1884 ;; MS isql/osql Commands
1885 (cons
1886 (concat
1887 "^\\(?:\\(?:set\\s-+\\(?:"
1888 (regexp-opt '(
1889 "datefirst" "dateformat" "deadlock_priority" "lock_timeout"
1890 "concat_null_yields_null" "cursor_close_on_commit"
1891 "disable_def_cnst_chk" "fips_flagger" "identity_insert" "language"
1892 "offsets" "quoted_identifier" "arithabort" "arithignore" "fmtonly"
1893 "nocount" "noexec" "numeric_roundabort" "parseonly"
1894 "query_governor_cost_limit" "rowcount" "textsize" "ansi_defaults"
1895 "ansi_null_dflt_off" "ansi_null_dflt_on" "ansi_nulls" "ansi_padding"
1896 "ansi_warnings" "forceplan" "showplan_all" "showplan_text"
1897 "statistics" "implicit_transactions" "remote_proc_transactions"
1898 "transaction" "xact_abort"
1899 ) t)
1900 "\\)\\)\\|go\\s-*\\|use\\s-+\\|setuser\\s-+\\|dbcc\\s-+\\).*$")
1901 'font-lock-doc-face)
1903 ;; MS Reserved
1904 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
1905 "absolute" "add" "all" "alter" "and" "any" "as" "asc" "authorization"
1906 "avg" "backup" "begin" "between" "break" "browse" "bulk" "by"
1907 "cascade" "case" "check" "checkpoint" "close" "clustered" "coalesce"
1908 "column" "commit" "committed" "compute" "confirm" "constraint"
1909 "contains" "containstable" "continue" "controlrow" "convert" "count"
1910 "create" "cross" "current" "current_date" "current_time"
1911 "current_timestamp" "current_user" "database" "deallocate" "declare"
1912 "default" "delete" "deny" "desc" "disk" "distinct" "distributed"
1913 "double" "drop" "dummy" "dump" "else" "end" "errlvl" "errorexit"
1914 "escape" "except" "exec" "execute" "exists" "exit" "fetch" "file"
1915 "fillfactor" "first" "floppy" "for" "foreign" "freetext"
1916 "freetexttable" "from" "full" "goto" "grant" "group" "having"
1917 "holdlock" "identity" "identity_insert" "identitycol" "if" "in"
1918 "index" "inner" "insert" "intersect" "into" "is" "isolation" "join"
1919 "key" "kill" "last" "left" "level" "like" "lineno" "load" "max" "min"
1920 "mirrorexit" "national" "next" "nocheck" "nolock" "nonclustered" "not"
1921 "null" "nullif" "of" "off" "offsets" "on" "once" "only" "open"
1922 "opendatasource" "openquery" "openrowset" "option" "or" "order"
1923 "outer" "output" "over" "paglock" "percent" "perm" "permanent" "pipe"
1924 "plan" "precision" "prepare" "primary" "print" "prior" "privileges"
1925 "proc" "procedure" "processexit" "public" "raiserror" "read"
1926 "readcommitted" "readpast" "readtext" "readuncommitted" "reconfigure"
1927 "references" "relative" "repeatable" "repeatableread" "replication"
1928 "restore" "restrict" "return" "revoke" "right" "rollback" "rowcount"
1929 "rowguidcol" "rowlock" "rule" "save" "schema" "select" "serializable"
1930 "session_user" "set" "shutdown" "some" "statistics" "sum"
1931 "system_user" "table" "tablock" "tablockx" "tape" "temp" "temporary"
1932 "textsize" "then" "to" "top" "tran" "transaction" "trigger" "truncate"
1933 "tsequal" "uncommitted" "union" "unique" "update" "updatetext"
1934 "updlock" "use" "user" "values" "view" "waitfor" "when" "where"
1935 "while" "with" "work" "writetext" "collate" "function" "openxml"
1936 "returns"
1939 ;; MS Functions
1940 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
1941 "@@connections" "@@cpu_busy" "@@cursor_rows" "@@datefirst" "@@dbts"
1942 "@@error" "@@fetch_status" "@@identity" "@@idle" "@@io_busy"
1943 "@@langid" "@@language" "@@lock_timeout" "@@max_connections"
1944 "@@max_precision" "@@nestlevel" "@@options" "@@pack_received"
1945 "@@pack_sent" "@@packet_errors" "@@procid" "@@remserver" "@@rowcount"
1946 "@@servername" "@@servicename" "@@spid" "@@textsize" "@@timeticks"
1947 "@@total_errors" "@@total_read" "@@total_write" "@@trancount"
1948 "@@version" "abs" "acos" "and" "app_name" "ascii" "asin" "atan" "atn2"
1949 "avg" "case" "cast" "ceiling" "char" "charindex" "coalesce"
1950 "col_length" "col_name" "columnproperty" "containstable" "convert"
1951 "cos" "cot" "count" "current_timestamp" "current_user" "cursor_status"
1952 "databaseproperty" "datalength" "dateadd" "datediff" "datename"
1953 "datepart" "day" "db_id" "db_name" "degrees" "difference" "exp"
1954 "file_id" "file_name" "filegroup_id" "filegroup_name"
1955 "filegroupproperty" "fileproperty" "floor" "formatmessage"
1956 "freetexttable" "fulltextcatalogproperty" "fulltextserviceproperty"
1957 "getansinull" "getdate" "grouping" "host_id" "host_name" "ident_incr"
1958 "ident_seed" "identity" "index_col" "indexproperty" "is_member"
1959 "is_srvrolemember" "isdate" "isnull" "isnumeric" "left" "len" "log"
1960 "log10" "lower" "ltrim" "max" "min" "month" "nchar" "newid" "nullif"
1961 "object_id" "object_name" "objectproperty" "openquery" "openrowset"
1962 "parsename" "patindex" "patindex" "permissions" "pi" "power"
1963 "quotename" "radians" "rand" "replace" "replicate" "reverse" "right"
1964 "round" "rtrim" "session_user" "sign" "sin" "soundex" "space" "sqrt"
1965 "square" "stats_date" "stdev" "stdevp" "str" "stuff" "substring" "sum"
1966 "suser_id" "suser_name" "suser_sid" "suser_sname" "system_user" "tan"
1967 "textptr" "textvalid" "typeproperty" "unicode" "upper" "user"
1968 "user_id" "user_name" "var" "varp" "year"
1971 ;; MS Variables
1972 '("\\b@[a-zA-Z0-9_]*\\b" . font-lock-variable-name-face)
1974 ;; MS Types
1975 (sql-font-lock-keywords-builder 'font-lock-type-face nil
1976 "binary" "bit" "char" "character" "cursor" "datetime" "dec" "decimal"
1977 "double" "float" "image" "int" "integer" "money" "national" "nchar"
1978 "ntext" "numeric" "numeric" "nvarchar" "precision" "real"
1979 "smalldatetime" "smallint" "smallmoney" "text" "timestamp" "tinyint"
1980 "uniqueidentifier" "varbinary" "varchar" "varying"
1983 "Microsoft SQLServer SQL keywords used by font-lock.
1985 This variable is used by `sql-mode' and `sql-interactive-mode'. The
1986 regular expressions are created during compilation by calling the
1987 function `regexp-opt'. Therefore, take a look at the source before
1988 you define your own `sql-mode-ms-font-lock-keywords'.")
1990 (defvar sql-mode-sybase-font-lock-keywords nil
1991 "Sybase SQL keywords used by font-lock.
1993 This variable is used by `sql-mode' and `sql-interactive-mode'. The
1994 regular expressions are created during compilation by calling the
1995 function `regexp-opt'. Therefore, take a look at the source before
1996 you define your own `sql-mode-sybase-font-lock-keywords'.")
1998 (defvar sql-mode-informix-font-lock-keywords nil
1999 "Informix SQL keywords used by font-lock.
2001 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2002 regular expressions are created during compilation by calling the
2003 function `regexp-opt'. Therefore, take a look at the source before
2004 you define your own `sql-mode-informix-font-lock-keywords'.")
2006 (defvar sql-mode-interbase-font-lock-keywords nil
2007 "Interbase SQL keywords used by font-lock.
2009 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2010 regular expressions are created during compilation by calling the
2011 function `regexp-opt'. Therefore, take a look at the source before
2012 you define your own `sql-mode-interbase-font-lock-keywords'.")
2014 (defvar sql-mode-ingres-font-lock-keywords nil
2015 "Ingres SQL keywords used by font-lock.
2017 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2018 regular expressions are created during compilation by calling the
2019 function `regexp-opt'. Therefore, take a look at the source before
2020 you define your own `sql-mode-interbase-font-lock-keywords'.")
2022 (defvar sql-mode-solid-font-lock-keywords nil
2023 "Solid SQL keywords used by font-lock.
2025 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2026 regular expressions are created during compilation by calling the
2027 function `regexp-opt'. Therefore, take a look at the source before
2028 you define your own `sql-mode-solid-font-lock-keywords'.")
2030 (defvar sql-mode-mysql-font-lock-keywords
2031 (eval-when-compile
2032 (list
2033 ;; MySQL Functions
2034 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
2035 "ascii" "avg" "bdmpolyfromtext" "bdmpolyfromwkb" "bdpolyfromtext"
2036 "bdpolyfromwkb" "benchmark" "bin" "bit_and" "bit_length" "bit_or"
2037 "bit_xor" "both" "cast" "char_length" "character_length" "coalesce"
2038 "concat" "concat_ws" "connection_id" "conv" "convert" "count"
2039 "curdate" "current_date" "current_time" "current_timestamp" "curtime"
2040 "elt" "encrypt" "export_set" "field" "find_in_set" "found_rows" "from"
2041 "geomcollfromtext" "geomcollfromwkb" "geometrycollectionfromtext"
2042 "geometrycollectionfromwkb" "geometryfromtext" "geometryfromwkb"
2043 "geomfromtext" "geomfromwkb" "get_lock" "group_concat" "hex" "ifnull"
2044 "instr" "interval" "isnull" "last_insert_id" "lcase" "leading"
2045 "length" "linefromtext" "linefromwkb" "linestringfromtext"
2046 "linestringfromwkb" "load_file" "locate" "lower" "lpad" "ltrim"
2047 "make_set" "master_pos_wait" "max" "mid" "min" "mlinefromtext"
2048 "mlinefromwkb" "mpointfromtext" "mpointfromwkb" "mpolyfromtext"
2049 "mpolyfromwkb" "multilinestringfromtext" "multilinestringfromwkb"
2050 "multipointfromtext" "multipointfromwkb" "multipolygonfromtext"
2051 "multipolygonfromwkb" "now" "nullif" "oct" "octet_length" "ord"
2052 "pointfromtext" "pointfromwkb" "polyfromtext" "polyfromwkb"
2053 "polygonfromtext" "polygonfromwkb" "position" "quote" "rand"
2054 "release_lock" "repeat" "replace" "reverse" "rpad" "rtrim" "soundex"
2055 "space" "std" "stddev" "substring" "substring_index" "sum" "sysdate"
2056 "trailing" "trim" "ucase" "unix_timestamp" "upper" "user" "variance"
2059 ;; MySQL Keywords
2060 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
2061 "action" "add" "after" "against" "all" "alter" "and" "as" "asc"
2062 "auto_increment" "avg_row_length" "bdb" "between" "by" "cascade"
2063 "case" "change" "character" "check" "checksum" "close" "collate"
2064 "collation" "column" "columns" "comment" "committed" "concurrent"
2065 "constraint" "create" "cross" "data" "database" "default"
2066 "delay_key_write" "delayed" "delete" "desc" "directory" "disable"
2067 "distinct" "distinctrow" "do" "drop" "dumpfile" "duplicate" "else"
2068 "enable" "enclosed" "end" "escaped" "exists" "fields" "first" "for"
2069 "force" "foreign" "from" "full" "fulltext" "global" "group" "handler"
2070 "having" "heap" "high_priority" "if" "ignore" "in" "index" "infile"
2071 "inner" "insert" "insert_method" "into" "is" "isam" "isolation" "join"
2072 "key" "keys" "last" "left" "level" "like" "limit" "lines" "load"
2073 "local" "lock" "low_priority" "match" "max_rows" "merge" "min_rows"
2074 "mode" "modify" "mrg_myisam" "myisam" "natural" "next" "no" "not"
2075 "null" "offset" "oj" "on" "open" "optionally" "or" "order" "outer"
2076 "outfile" "pack_keys" "partial" "password" "prev" "primary"
2077 "procedure" "quick" "raid0" "raid_type" "read" "references" "rename"
2078 "repeatable" "restrict" "right" "rollback" "rollup" "row_format"
2079 "savepoint" "select" "separator" "serializable" "session" "set"
2080 "share" "show" "sql_big_result" "sql_buffer_result" "sql_cache"
2081 "sql_calc_found_rows" "sql_no_cache" "sql_small_result" "starting"
2082 "straight_join" "striped" "table" "tables" "temporary" "terminated"
2083 "then" "to" "transaction" "truncate" "type" "uncommitted" "union"
2084 "unique" "unlock" "update" "use" "using" "values" "when" "where"
2085 "with" "write" "xor"
2088 ;; MySQL Data Types
2089 (sql-font-lock-keywords-builder 'font-lock-type-face nil
2090 "bigint" "binary" "bit" "blob" "bool" "boolean" "char" "curve" "date"
2091 "datetime" "dec" "decimal" "double" "enum" "fixed" "float" "geometry"
2092 "geometrycollection" "int" "integer" "line" "linearring" "linestring"
2093 "longblob" "longtext" "mediumblob" "mediumint" "mediumtext"
2094 "multicurve" "multilinestring" "multipoint" "multipolygon"
2095 "multisurface" "national" "numeric" "point" "polygon" "precision"
2096 "real" "smallint" "surface" "text" "time" "timestamp" "tinyblob"
2097 "tinyint" "tinytext" "unsigned" "varchar" "year" "year2" "year4"
2098 "zerofill"
2101 "MySQL SQL keywords used by font-lock.
2103 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2104 regular expressions are created during compilation by calling the
2105 function `regexp-opt'. Therefore, take a look at the source before
2106 you define your own `sql-mode-mysql-font-lock-keywords'.")
2108 (defvar sql-mode-sqlite-font-lock-keywords
2109 (eval-when-compile
2110 (list
2111 ;; SQLite commands
2112 '("^[.].*$" . font-lock-doc-face)
2114 ;; SQLite Keyword
2115 (sql-font-lock-keywords-builder 'font-lock-keyword-face nil
2116 "abort" "action" "add" "after" "all" "alter" "analyze" "and" "as"
2117 "asc" "attach" "autoincrement" "before" "begin" "between" "by"
2118 "cascade" "case" "cast" "check" "collate" "column" "commit" "conflict"
2119 "constraint" "create" "cross" "database" "default" "deferrable"
2120 "deferred" "delete" "desc" "detach" "distinct" "drop" "each" "else"
2121 "end" "escape" "except" "exclusive" "exists" "explain" "fail" "for"
2122 "foreign" "from" "full" "glob" "group" "having" "if" "ignore"
2123 "immediate" "in" "index" "indexed" "initially" "inner" "insert"
2124 "instead" "intersect" "into" "is" "isnull" "join" "key" "left" "like"
2125 "limit" "match" "natural" "no" "not" "notnull" "null" "of" "offset"
2126 "on" "or" "order" "outer" "plan" "pragma" "primary" "query" "raise"
2127 "references" "regexp" "reindex" "release" "rename" "replace"
2128 "restrict" "right" "rollback" "row" "savepoint" "select" "set" "table"
2129 "temp" "temporary" "then" "to" "transaction" "trigger" "union"
2130 "unique" "update" "using" "vacuum" "values" "view" "virtual" "when"
2131 "where"
2133 ;; SQLite Data types
2134 (sql-font-lock-keywords-builder 'font-lock-type-face nil
2135 "int" "integer" "tinyint" "smallint" "mediumint" "bigint" "unsigned"
2136 "big" "int2" "int8" "character" "varchar" "varying" "nchar" "native"
2137 "nvarchar" "text" "clob" "blob" "real" "double" "precision" "float"
2138 "numeric" "number" "decimal" "boolean" "date" "datetime"
2140 ;; SQLite Functions
2141 (sql-font-lock-keywords-builder 'font-lock-builtin-face nil
2142 ;; Core functions
2143 "abs" "changes" "coalesce" "glob" "ifnull" "hex" "last_insert_rowid"
2144 "length" "like" "load_extension" "lower" "ltrim" "max" "min" "nullif"
2145 "quote" "random" "randomblob" "replace" "round" "rtrim" "soundex"
2146 "sqlite_compileoption_get" "sqlite_compileoption_used"
2147 "sqlite_source_id" "sqlite_version" "substr" "total_changes" "trim"
2148 "typeof" "upper" "zeroblob"
2149 ;; Date/time functions
2150 "time" "julianday" "strftime"
2151 "current_date" "current_time" "current_timestamp"
2152 ;; Aggregate functions
2153 "avg" "count" "group_concat" "max" "min" "sum" "total"
2156 "SQLite SQL keywords used by font-lock.
2158 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2159 regular expressions are created during compilation by calling the
2160 function `regexp-opt'. Therefore, take a look at the source before
2161 you define your own `sql-mode-sqlite-font-lock-keywords'.")
2163 (defvar sql-mode-db2-font-lock-keywords nil
2164 "DB2 SQL keywords used by font-lock.
2166 This variable is used by `sql-mode' and `sql-interactive-mode'. The
2167 regular expressions are created during compilation by calling the
2168 function `regexp-opt'. Therefore, take a look at the source before
2169 you define your own `sql-mode-db2-font-lock-keywords'.")
2171 (defvar sql-mode-font-lock-keywords nil
2172 "SQL keywords used by font-lock.
2174 Setting this variable directly no longer has any affect. Use
2175 `sql-product' and `sql-add-product-keywords' to control the
2176 highlighting rules in SQL mode.")
2180 ;;; SQL Product support functions
2182 (defun sql-read-product (prompt &optional initial)
2183 "Read a valid SQL product."
2184 (let ((init (or (and initial (symbol-name initial)) "ansi")))
2185 (intern (completing-read
2186 prompt
2187 (mapcar (lambda (info) (symbol-name (car info)))
2188 sql-product-alist)
2189 nil 'require-match
2190 init 'sql-product-history init))))
2192 (defun sql-add-product (product display &rest plist)
2193 "Add support for a database product in `sql-mode'.
2195 Add PRODUCT to `sql-product-alist' which enables `sql-mode' to
2196 properly support syntax highlighting and interactive interaction.
2197 DISPLAY is the name of the SQL product that will appear in the
2198 menu bar and in messages. PLIST initializes the product
2199 configuration."
2201 ;; Don't do anything if the product is already supported
2202 (if (assoc product sql-product-alist)
2203 (message "Product `%s' is already defined" product)
2205 ;; Add product to the alist
2206 (add-to-list 'sql-product-alist `((,product :name ,display . ,plist)))
2207 ;; Add a menu item to the SQL->Product menu
2208 (easy-menu-add-item sql-mode-menu '("Product")
2209 ;; Each product is represented by a radio
2210 ;; button with it's display name.
2211 `[,display
2212 (sql-set-product ',product)
2213 :style radio
2214 :selected (eq sql-product ',product)]
2215 ;; Maintain the product list in
2216 ;; (case-insensitive) alphabetic order of the
2217 ;; display names. Loop thru each keymap item
2218 ;; looking for an item whose display name is
2219 ;; after this product's name.
2220 (let ((next-item)
2221 (down-display (downcase display)))
2222 (map-keymap (lambda (k b)
2223 (when (and (not next-item)
2224 (string-lessp down-display
2225 (downcase (cadr b))))
2226 (setq next-item k)))
2227 (easy-menu-get-map sql-mode-menu '("Product")))
2228 next-item))
2229 product))
2231 (defun sql-del-product (product)
2232 "Remove support for PRODUCT in `sql-mode'."
2234 ;; Remove the menu item based on the display name
2235 (easy-menu-remove-item sql-mode-menu '("Product") (sql-get-product-feature product :name))
2236 ;; Remove the product alist item
2237 (setq sql-product-alist (assq-delete-all product sql-product-alist))
2238 nil)
2240 (defun sql-set-product-feature (product feature newvalue)
2241 "Set FEATURE of database PRODUCT to NEWVALUE.
2243 The PRODUCT must be a symbol which identifies the database
2244 product. The product must have already exist on the product
2245 list. See `sql-add-product' to add new products. The FEATURE
2246 argument must be a plist keyword accepted by
2247 `sql-product-alist'."
2249 (let* ((p (assoc product sql-product-alist))
2250 (v (plist-get (cdr p) feature)))
2251 (if p
2252 (if (and
2253 (member feature sql-indirect-features)
2254 (symbolp v))
2255 (set v newvalue)
2256 (setcdr p (plist-put (cdr p) feature newvalue)))
2257 (message "`%s' is not a known product; use `sql-add-product' to add it first." product))))
2259 (defun sql-get-product-feature (product feature &optional fallback not-indirect)
2260 "Lookup FEATURE associated with a SQL PRODUCT.
2262 If the FEATURE is nil for PRODUCT, and FALLBACK is specified,
2263 then the FEATURE associated with the FALLBACK product is
2264 returned.
2266 If the FEATURE is in the list `sql-indirect-features', and the
2267 NOT-INDIRECT parameter is not set, then the value of the symbol
2268 stored in the connect alist is returned.
2270 See `sql-product-alist' for a list of products and supported features."
2271 (let* ((p (assoc product sql-product-alist))
2272 (v (plist-get (cdr p) feature)))
2274 (if p
2275 ;; If no value and fallback, lookup feature for fallback
2276 (if (and (not v)
2277 fallback
2278 (not (eq product fallback)))
2279 (sql-get-product-feature fallback feature)
2281 (if (and
2282 (member feature sql-indirect-features)
2283 (not not-indirect)
2284 (symbolp v))
2285 (symbol-value v)
2287 (message "`%s' is not a known product; use `sql-add-product' to add it first." product)
2288 nil)))
2290 (defun sql-product-font-lock (keywords-only imenu)
2291 "Configure font-lock and imenu with product-specific settings.
2293 The KEYWORDS-ONLY flag is passed to font-lock to specify whether
2294 only keywords should be hilighted and syntactic hilighting
2295 skipped. The IMENU flag indicates whether `imenu-mode' should
2296 also be configured."
2298 (let
2299 ;; Get the product-specific syntax-alist.
2300 ((syntax-alist
2301 (append
2302 (sql-get-product-feature sql-product :syntax-alist)
2303 '((?_ . "w") (?. . "w")))))
2305 ;; Get the product-specific keywords.
2306 (setq sql-mode-font-lock-keywords
2307 (append
2308 (unless (eq sql-product 'ansi)
2309 (sql-get-product-feature sql-product :font-lock))
2310 ;; Always highlight ANSI keywords
2311 (sql-get-product-feature 'ansi :font-lock)
2312 ;; Fontify object names in CREATE, DROP and ALTER DDL
2313 ;; statements
2314 (list sql-mode-font-lock-object-name)))
2316 ;; Setup font-lock. Force re-parsing of `font-lock-defaults'.
2317 (kill-local-variable 'font-lock-set-defaults)
2318 (setq font-lock-defaults (list 'sql-mode-font-lock-keywords
2319 keywords-only t syntax-alist))
2321 ;; Force font lock to reinitialize if it is already on
2322 ;; Otherwise, we can wait until it can be started.
2323 (when (and (fboundp 'font-lock-mode)
2324 (boundp 'font-lock-mode)
2325 font-lock-mode)
2326 (font-lock-mode-internal nil)
2327 (font-lock-mode-internal t))
2329 (add-hook 'font-lock-mode-hook
2330 (lambda ()
2331 ;; Provide defaults for new font-lock faces.
2332 (defvar font-lock-builtin-face
2333 (if (boundp 'font-lock-preprocessor-face)
2334 font-lock-preprocessor-face
2335 font-lock-keyword-face))
2336 (defvar font-lock-doc-face font-lock-string-face))
2337 nil t)
2339 ;; Setup imenu; it needs the same syntax-alist.
2340 (when imenu
2341 (setq imenu-syntax-alist syntax-alist))))
2343 ;;;###autoload
2344 (defun sql-add-product-keywords (product keywords &optional append)
2345 "Add highlighting KEYWORDS for SQL PRODUCT.
2347 PRODUCT should be a symbol, the name of a SQL product, such as
2348 `oracle'. KEYWORDS should be a list; see the variable
2349 `font-lock-keywords'. By default they are added at the beginning
2350 of the current highlighting list. If optional argument APPEND is
2351 `set', they are used to replace the current highlighting list.
2352 If APPEND is any other non-nil value, they are added at the end
2353 of the current highlighting list.
2355 For example:
2357 (sql-add-product-keywords 'ms
2358 '((\"\\\\b\\\\w+_t\\\\b\" . font-lock-type-face)))
2360 adds a fontification pattern to fontify identifiers ending in
2361 `_t' as data types."
2363 (let* ((sql-indirect-features nil)
2364 (font-lock-var (sql-get-product-feature product :font-lock))
2365 (old-val))
2367 (setq old-val (symbol-value font-lock-var))
2368 (set font-lock-var
2369 (if (eq append 'set)
2370 keywords
2371 (if append
2372 (append old-val keywords)
2373 (append keywords old-val))))))
2375 (defun sql-for-each-login (login-params body)
2376 "Iterates through login parameters and returns a list of results."
2378 (delq nil
2379 (mapcar
2380 (lambda (param)
2381 (let ((token (or (and (listp param) (car param)) param))
2382 (plist (or (and (listp param) (cdr param)) nil)))
2384 (funcall body token plist)))
2385 login-params)))
2389 ;;; Functions to switch highlighting
2391 (defun sql-highlight-product ()
2392 "Turn on the font highlighting for the SQL product selected."
2393 (when (derived-mode-p 'sql-mode)
2394 ;; Setup font-lock
2395 (sql-product-font-lock nil t)
2397 ;; Set the mode name to include the product.
2398 (setq mode-name (concat "SQL[" (or (sql-get-product-feature sql-product :name)
2399 (symbol-name sql-product)) "]"))))
2401 (defun sql-set-product (product)
2402 "Set `sql-product' to PRODUCT and enable appropriate highlighting."
2403 (interactive
2404 (list (sql-read-product "SQL product: ")))
2405 (if (stringp product) (setq product (intern product)))
2406 (when (not (assoc product sql-product-alist))
2407 (error "SQL product %s is not supported; treated as ANSI" product)
2408 (setq product 'ansi))
2410 ;; Save product setting and fontify.
2411 (setq sql-product product)
2412 (sql-highlight-product))
2415 ;;; Compatibility functions
2417 (if (not (fboundp 'comint-line-beginning-position))
2418 ;; comint-line-beginning-position is defined in Emacs 21
2419 (defun comint-line-beginning-position ()
2420 "Return the buffer position of the beginning of the line, after any prompt.
2421 The prompt is assumed to be any text at the beginning of the line matching
2422 the regular expression `comint-prompt-regexp', a buffer local variable."
2423 (save-excursion (comint-bol nil) (point))))
2427 ;;; Small functions
2429 (defun sql-magic-go (arg)
2430 "Insert \"o\" and call `comint-send-input'.
2431 `sql-electric-stuff' must be the symbol `go'."
2432 (interactive "P")
2433 (self-insert-command (prefix-numeric-value arg))
2434 (if (and (equal sql-electric-stuff 'go)
2435 (save-excursion
2436 (comint-bol nil)
2437 (looking-at "go\\b")))
2438 (comint-send-input)))
2440 (defun sql-magic-semicolon (arg)
2441 "Insert semicolon and call `comint-send-input'.
2442 `sql-electric-stuff' must be the symbol `semicolon'."
2443 (interactive "P")
2444 (self-insert-command (prefix-numeric-value arg))
2445 (if (equal sql-electric-stuff 'semicolon)
2446 (comint-send-input)))
2448 (defun sql-accumulate-and-indent ()
2449 "Continue SQL statement on the next line."
2450 (interactive)
2451 (if (fboundp 'comint-accumulate)
2452 (comint-accumulate)
2453 (newline))
2454 (indent-according-to-mode))
2456 (defun sql-help-list-products (indent freep)
2457 "Generate listing of products available for use under SQLi.
2459 List products with :free-softare attribute set to FREEP. Indent
2460 each line with INDENT."
2462 (let (sqli-func doc)
2463 (setq doc "")
2464 (dolist (p sql-product-alist)
2465 (setq sqli-func (intern (concat "sql-" (symbol-name (car p)))))
2467 (if (and (fboundp sqli-func)
2468 (eq (sql-get-product-feature (car p) :free-software) freep))
2469 (setq doc
2470 (concat doc
2471 indent
2472 (or (sql-get-product-feature (car p) :name)
2473 (symbol-name (car p)))
2474 ":\t"
2475 "\\["
2476 (symbol-name sqli-func)
2477 "]\n"))))
2478 doc))
2480 ;;;###autoload
2481 (defun sql-help ()
2482 "Show short help for the SQL modes.
2484 Use an entry function to open an interactive SQL buffer. This buffer is
2485 usually named `*SQL*'. The name of the major mode is SQLi.
2487 Use the following commands to start a specific SQL interpreter:
2489 \\\\FREE
2491 Other non-free SQL implementations are also supported:
2493 \\\\NONFREE
2495 But we urge you to choose a free implementation instead of these.
2497 You can also use \\[sql-product-interactive] to invoke the
2498 interpreter for the current `sql-product'.
2500 Once you have the SQLi buffer, you can enter SQL statements in the
2501 buffer. The output generated is appended to the buffer and a new prompt
2502 is generated. See the In/Out menu in the SQLi buffer for some functions
2503 that help you navigate through the buffer, the input history, etc.
2505 If you have a really complex SQL statement or if you are writing a
2506 procedure, you can do this in a separate buffer. Put the new buffer in
2507 `sql-mode' by calling \\[sql-mode]. The name of this buffer can be
2508 anything. The name of the major mode is SQL.
2510 In this SQL buffer (SQL mode), you can send the region or the entire
2511 buffer to the interactive SQL buffer (SQLi mode). The results are
2512 appended to the SQLi buffer without disturbing your SQL buffer."
2513 (interactive)
2515 ;; Insert references to loaded products into the help buffer string
2516 (let ((doc (documentation 'sql-help t))
2517 changedp)
2518 (setq changedp nil)
2520 ;; Insert FREE software list
2521 (when (string-match "^\\(\\s-*\\)[\\\\][\\\\]FREE\\s-*\n" doc 0)
2522 (setq doc (replace-match (sql-help-list-products (match-string 1 doc) t)
2523 t t doc 0)
2524 changedp t))
2526 ;; Insert non-FREE software list
2527 (when (string-match "^\\(\\s-*\\)[\\\\][\\\\]NONFREE\\s-*\n" doc 0)
2528 (setq doc (replace-match (sql-help-list-products (match-string 1 doc) nil)
2529 t t doc 0)
2530 changedp t))
2532 ;; If we changed the help text, save the change so that the help
2533 ;; sub-system will see it
2534 (when changedp
2535 (put 'sql-help 'function-documentation doc)))
2537 ;; Call help on this function
2538 (describe-function 'sql-help))
2540 (defun sql-read-passwd (prompt &optional default)
2541 "Read a password using PROMPT. Optional DEFAULT is password to start with."
2542 (read-passwd prompt nil default))
2544 (defun sql-get-login-ext (prompt last-value history-var plist)
2545 "Prompt user with extended login parameters.
2547 If PLIST is nil, then the user is simply prompted for a string
2548 value.
2550 The property `:default' specifies the default value. If the
2551 `:number' property is non-nil then ask for a number.
2553 The `:file' property prompts for a file name that must match the
2554 regexp pattern specified in its value.
2556 The `:completion' property prompts for a string specified by its
2557 value. (The property value is used as the PREDICATE argument to
2558 `completing-read'.)"
2559 (let* ((default (plist-get plist :default))
2560 (prompt-def
2561 (if default
2562 (if (string-match "\\(\\):[ \t]*\\'" prompt)
2563 (replace-match (format " (default \"%s\")" default) t t prompt 1)
2564 (replace-regexp-in-string "[ \t]*\\'"
2565 (format " (default \"%s\") " default)
2566 prompt t t))
2567 prompt))
2568 (use-dialog-box nil))
2569 (cond
2570 ((plist-member plist :file)
2571 (expand-file-name
2572 (read-file-name prompt
2573 (file-name-directory last-value) default t
2574 (file-name-nondirectory last-value)
2575 (when (plist-get plist :file)
2576 `(lambda (f)
2577 (string-match
2578 (concat "\\<" ,(plist-get plist :file) "\\>")
2579 (file-name-nondirectory f)))))))
2581 ((plist-member plist :completion)
2582 (completing-read prompt-def (plist-get plist :completion) nil t
2583 last-value history-var default))
2585 ((plist-get plist :number)
2586 (read-number prompt (or default last-value 0)))
2589 (let ((r (read-from-minibuffer prompt-def last-value nil nil history-var nil)))
2590 (if (string= "" r) (or default "") r))))))
2592 (defun sql-get-login (&rest what)
2593 "Get username, password and database from the user.
2595 The variables `sql-user', `sql-password', `sql-server', and
2596 `sql-database' can be customized. They are used as the default values.
2597 Usernames, servers and databases are stored in `sql-user-history',
2598 `sql-server-history' and `database-history'. Passwords are not stored
2599 in a history.
2601 Parameter WHAT is a list of tokens passed as arguments in the
2602 function call. The function asks for the username if WHAT
2603 contains the symbol `user', for the password if it contains the
2604 symbol `password', for the server if it contains the symbol
2605 `server', and for the database if it contains the symbol
2606 `database'. The members of WHAT are processed in the order in
2607 which they are provided.
2609 Each token may also be a list with the token in the car and a
2610 plist of options as the cdr. The following properties are
2611 supported:
2613 :file <filename-regexp>
2614 :completion <list-of-strings-or-function>
2615 :default <default-value>
2616 :number t
2618 In order to ask the user for username, password and database, call the
2619 function like this: (sql-get-login 'user 'password 'database)."
2620 (interactive)
2621 (mapcar
2622 (lambda (w)
2623 (let ((token (or (and (consp w) (car w)) w))
2624 (plist (or (and (consp w) (cdr w)) nil)))
2626 (cond
2627 ((eq token 'user) ; user
2628 (setq sql-user
2629 (sql-get-login-ext "User: " sql-user
2630 'sql-user-history plist)))
2632 ((eq token 'password) ; password
2633 (setq sql-password
2634 (sql-read-passwd "Password: " sql-password)))
2636 ((eq token 'server) ; server
2637 (setq sql-server
2638 (sql-get-login-ext "Server: " sql-server
2639 'sql-server-history plist)))
2641 ((eq token 'database) ; database
2642 (setq sql-database
2643 (sql-get-login-ext "Database: " sql-database
2644 'sql-database-history plist)))
2646 ((eq token 'port) ; port
2647 (setq sql-port
2648 (sql-get-login-ext "Port: " sql-port
2649 nil (append '(:number t) plist)))))))
2650 what))
2652 (defun sql-find-sqli-buffer (&optional product)
2653 "Returns the name of the current default SQLi buffer or nil.
2654 In order to qualify, the SQLi buffer must be alive, be in
2655 `sql-interactive-mode' and have a process."
2656 (let ((buf sql-buffer)
2657 (prod (or product sql-product)))
2659 ;; Current sql-buffer, if there is one.
2660 (and (sql-buffer-live-p buf prod)
2661 buf)
2662 ;; Global sql-buffer
2663 (and (setq buf (default-value 'sql-buffer))
2664 (sql-buffer-live-p buf prod)
2665 buf)
2666 ;; Look thru each buffer
2667 (car (apply 'append
2668 (mapcar (lambda (b)
2669 (and (sql-buffer-live-p b prod)
2670 (list (buffer-name b))))
2671 (buffer-list)))))))
2673 (defun sql-set-sqli-buffer-generally ()
2674 "Set SQLi buffer for all SQL buffers that have none.
2675 This function checks all SQL buffers for their SQLi buffer. If their
2676 SQLi buffer is nonexistent or has no process, it is set to the current
2677 default SQLi buffer. The current default SQLi buffer is determined
2678 using `sql-find-sqli-buffer'. If `sql-buffer' is set,
2679 `sql-set-sqli-hook' is run."
2680 (interactive)
2681 (save-excursion
2682 (let ((buflist (buffer-list))
2683 (default-buffer (sql-find-sqli-buffer)))
2684 (setq-default sql-buffer default-buffer)
2685 (while (not (null buflist))
2686 (let ((candidate (car buflist)))
2687 (set-buffer candidate)
2688 (if (and (derived-mode-p 'sql-mode)
2689 (not (sql-buffer-live-p sql-buffer)))
2690 (progn
2691 (setq sql-buffer default-buffer)
2692 (when default-buffer
2693 (run-hooks 'sql-set-sqli-hook)))))
2694 (setq buflist (cdr buflist))))))
2696 (defun sql-set-sqli-buffer ()
2697 "Set the SQLi buffer SQL strings are sent to.
2699 Call this function in a SQL buffer in order to set the SQLi buffer SQL
2700 strings are sent to. Calling this function sets `sql-buffer' and runs
2701 `sql-set-sqli-hook'.
2703 If you call it from a SQL buffer, this sets the local copy of
2704 `sql-buffer'.
2706 If you call it from anywhere else, it sets the global copy of
2707 `sql-buffer'."
2708 (interactive)
2709 (let ((default-buffer (sql-find-sqli-buffer)))
2710 (if (null default-buffer)
2711 (error "There is no suitable SQLi buffer")
2712 (let ((new-buffer (read-buffer "New SQLi buffer: " default-buffer t)))
2713 (if (null (sql-buffer-live-p new-buffer))
2714 (error "Buffer %s is not a working SQLi buffer" new-buffer)
2715 (when new-buffer
2716 (setq sql-buffer new-buffer)
2717 (run-hooks 'sql-set-sqli-hook)))))))
2719 (defun sql-show-sqli-buffer ()
2720 "Show the name of current SQLi buffer.
2722 This is the buffer SQL strings are sent to. It is stored in the
2723 variable `sql-buffer'. See `sql-help' on how to create such a buffer."
2724 (interactive)
2725 (if (null (buffer-live-p (get-buffer sql-buffer)))
2726 (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
2727 (if (null (get-buffer-process sql-buffer))
2728 (message "Buffer %s has no process." sql-buffer)
2729 (message "Current SQLi buffer is %s." sql-buffer))))
2731 (defun sql-make-alternate-buffer-name ()
2732 "Return a string that can be used to rename a SQLi buffer.
2734 This is used to set `sql-alternate-buffer-name' within
2735 `sql-interactive-mode'.
2737 If the session was started with `sql-connect' then the alternate
2738 name would be the name of the connection.
2740 Otherwise, it uses the parameters identified by the :sqlilogin
2741 parameter.
2743 If all else fails, the alternate name would be the user and
2744 server/database name."
2746 (let ((name ""))
2748 ;; Build a name using the :sqli-login setting
2749 (setq name
2750 (apply 'concat
2751 (cdr
2752 (apply 'append nil
2753 (sql-for-each-login
2754 (sql-get-product-feature sql-product :sqli-login)
2755 (lambda (token plist)
2756 (cond
2757 ((eq token 'user)
2758 (unless (string= "" sql-user)
2759 (list "/" sql-user)))
2760 ((eq token 'port)
2761 (unless (or (not (numberp sql-port))
2762 (= 0 sql-port))
2763 (list ":" (number-to-string sql-port))))
2764 ((eq token 'server)
2765 (unless (string= "" sql-server)
2766 (list "."
2767 (if (plist-member plist :file)
2768 (file-name-nondirectory sql-server)
2769 sql-server))))
2770 ((eq token 'database)
2771 (unless (string= "" sql-database)
2772 (list "@"
2773 (if (plist-member plist :file)
2774 (file-name-nondirectory sql-database)
2775 sql-database))))
2777 ((eq token 'password) nil)
2778 (t nil))))))))
2780 ;; If there's a connection, use it and the name thus far
2781 (if sql-connection
2782 (format "<%s>%s" sql-connection (or name ""))
2784 ;; If there is no name, try to create something meaningful
2785 (if (string= "" (or name ""))
2786 (concat
2787 (if (string= "" sql-user)
2788 (if (string= "" (user-login-name))
2790 (concat (user-login-name) "/"))
2791 (concat sql-user "/"))
2792 (if (string= "" sql-database)
2793 (if (string= "" sql-server)
2794 (system-name)
2795 sql-server)
2796 sql-database))
2798 ;; Use the name we've got
2799 name))))
2801 (defun sql-rename-buffer (&optional new-name)
2802 "Rename a SQL interactive buffer.
2804 Prompts for the new name if command is preceeded by
2805 \\[universal-argument]. If no buffer name is provided, then the
2806 `sql-alternate-buffer-name' is used.
2808 The actual buffer name set will be \"*SQL: NEW-NAME*\". If
2809 NEW-NAME is empty, then the buffer name will be \"*SQL*\"."
2810 (interactive "P")
2812 (if (not (derived-mode-p 'sql-interactive-mode))
2813 (message "Current buffer is not a SQL interactive buffer")
2815 (setq sql-alternate-buffer-name
2816 (cond
2817 ((stringp new-name) new-name)
2818 ((consp new-name)
2819 (read-string "Buffer name (\"*SQL: XXX*\"; enter `XXX'): "
2820 sql-alternate-buffer-name))
2821 (t sql-alternate-buffer-name)))
2823 (rename-buffer (if (string= "" sql-alternate-buffer-name)
2824 "*SQL*"
2825 (format "*SQL: %s*" sql-alternate-buffer-name))
2826 t)))
2828 (defun sql-copy-column ()
2829 "Copy current column to the end of buffer.
2830 Inserts SELECT or commas if appropriate."
2831 (interactive)
2832 (let ((column))
2833 (save-excursion
2834 (setq column (buffer-substring-no-properties
2835 (progn (forward-char 1) (backward-sexp 1) (point))
2836 (progn (forward-sexp 1) (point))))
2837 (goto-char (point-max))
2838 (let ((bol (comint-line-beginning-position)))
2839 (cond
2840 ;; if empty command line, insert SELECT
2841 ((= bol (point))
2842 (insert "SELECT "))
2843 ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
2844 ((save-excursion
2845 (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
2846 bol t))
2847 (insert ", "))
2848 ;; else insert a space
2850 (if (eq (preceding-char) ?\s)
2852 (insert " ")))))
2853 ;; in any case, insert the column
2854 (insert column)
2855 (message "%s" column))))
2857 ;; On Windows, SQL*Plus for Oracle turns on full buffering for stdout
2858 ;; if it is not attached to a character device; therefore placeholder
2859 ;; replacement by SQL*Plus is fully buffered. The workaround lets
2860 ;; Emacs query for the placeholders.
2862 (defvar sql-placeholder-history nil
2863 "History of placeholder values used.")
2865 (defun sql-placeholders-filter (string)
2866 "Replace placeholders in STRING.
2867 Placeholders are words starting with an ampersand like &this."
2869 (when sql-oracle-scan-on
2870 (while (string-match "&\\(\\sw+\\)" string)
2871 (setq string (replace-match
2872 (read-from-minibuffer
2873 (format "Enter value for %s: " (match-string 1 string))
2874 nil nil nil 'sql-placeholder-history)
2875 t t string))))
2876 string)
2878 ;; Using DB2 interactively, newlines must be escaped with " \".
2879 ;; The space before the backslash is relevant.
2880 (defun sql-escape-newlines-filter (string)
2881 "Escape newlines in STRING.
2882 Every newline in STRING will be preceded with a space and a backslash."
2883 (let ((result "") (start 0) mb me)
2884 (while (string-match "\n" string start)
2885 (setq mb (match-beginning 0)
2886 me (match-end 0)
2887 result (concat result
2888 (substring string start mb)
2889 (if (and (> mb 1)
2890 (string-equal " \\" (substring string (- mb 2) mb)))
2891 "" " \\\n"))
2892 start me))
2893 (concat result (substring string start))))
2897 ;;; Input sender for SQLi buffers
2899 (defvar sql-output-newline-count 0
2900 "Number of newlines in the input string.
2902 Allows the suppression of continuation prompts.")
2904 (defvar sql-output-by-send nil
2905 "Non-nil if the command in the input was generated by `sql-send-string'.")
2907 (defun sql-input-sender (proc string)
2908 "Send STRING to PROC after applying filters."
2910 (let* ((product (with-current-buffer (process-buffer proc) sql-product))
2911 (filter (sql-get-product-feature product :input-filter)))
2913 ;; Apply filter(s)
2914 (cond
2915 ((not filter)
2916 nil)
2917 ((functionp filter)
2918 (setq string (funcall filter string)))
2919 ((listp filter)
2920 (mapc (lambda (f) (setq string (funcall f string))) filter))
2921 (t nil))
2923 ;; Count how many newlines in the string
2924 (setq sql-output-newline-count 0)
2925 (mapc (lambda (ch)
2926 (when (eq ch ?\n)
2927 (setq sql-output-newline-count (1+ sql-output-newline-count))))
2928 string)
2930 ;; Send the string
2931 (comint-simple-send proc string)))
2933 ;;; Strip out continuation prompts
2935 (defun sql-interactive-remove-continuation-prompt (oline)
2936 "Strip out continuation prompts out of the OLINE.
2938 Added to the `comint-preoutput-filter-functions' hook in a SQL
2939 interactive buffer. If `sql-outut-newline-count' is greater than
2940 zero, then an output line matching the continuation prompt is filtered
2941 out. If the count is one, then the prompt is replaced with a newline
2942 to force the output from the query to appear on a new line."
2943 (if (and sql-prompt-cont-regexp
2944 sql-output-newline-count
2945 (numberp sql-output-newline-count)
2946 (>= sql-output-newline-count 1))
2947 (progn
2948 (while (and oline
2949 sql-output-newline-count
2950 (> sql-output-newline-count 0)
2951 (string-match sql-prompt-cont-regexp oline))
2953 (setq oline
2954 (replace-match (if (and
2955 (= 1 sql-output-newline-count)
2956 sql-output-by-send)
2957 "\n" "")
2958 nil nil oline)
2959 sql-output-newline-count
2960 (1- sql-output-newline-count)))
2961 (if (= sql-output-newline-count 0)
2962 (setq sql-output-newline-count nil))
2963 (setq sql-output-by-send nil))
2964 (setq sql-output-newline-count nil))
2965 oline)
2967 ;;; Sending the region to the SQLi buffer.
2969 (defun sql-send-string (str)
2970 "Send the string STR to the SQL process."
2971 (interactive "sSQL Text: ")
2973 (let ((comint-input-sender-no-newline nil)
2974 (s (replace-regexp-in-string "[[:space:]\n\r]+\\'" "" str)))
2975 (if (sql-buffer-live-p sql-buffer)
2976 (progn
2977 ;; Ignore the hoping around...
2978 (save-excursion
2979 ;; Set product context
2980 (with-current-buffer sql-buffer
2981 ;; Send the string (trim the trailing whitespace)
2982 (sql-input-sender (get-buffer-process sql-buffer) s)
2984 ;; Send a command terminator if we must
2985 (if sql-send-terminator
2986 (sql-send-magic-terminator sql-buffer s sql-send-terminator))
2988 (message "Sent string to buffer %s." sql-buffer)))
2990 ;; Display the sql buffer
2991 (if sql-pop-to-buffer-after-send-region
2992 (pop-to-buffer sql-buffer)
2993 (display-buffer sql-buffer)))
2995 ;; We don't have no stinkin' sql
2996 (message "No SQL process started."))))
2998 (defun sql-send-region (start end)
2999 "Send a region to the SQL process."
3000 (interactive "r")
3001 (sql-send-string (buffer-substring-no-properties start end)))
3003 (defun sql-send-paragraph ()
3004 "Send the current paragraph to the SQL process."
3005 (interactive)
3006 (let ((start (save-excursion
3007 (backward-paragraph)
3008 (point)))
3009 (end (save-excursion
3010 (forward-paragraph)
3011 (point))))
3012 (sql-send-region start end)))
3014 (defun sql-send-buffer ()
3015 "Send the buffer contents to the SQL process."
3016 (interactive)
3017 (sql-send-region (point-min) (point-max)))
3019 (defun sql-send-magic-terminator (buf str terminator)
3020 "Send TERMINATOR to buffer BUF if its not present in STR."
3021 (let (comint-input-sender-no-newline pat term)
3022 ;; If flag is merely on(t), get product-specific terminator
3023 (if (eq terminator t)
3024 (setq terminator (sql-get-product-feature sql-product :terminator)))
3026 ;; If there is no terminator specified, use default ";"
3027 (unless terminator
3028 (setq terminator ";"))
3030 ;; Parse the setting into the pattern and the terminator string
3031 (cond ((stringp terminator)
3032 (setq pat (regexp-quote terminator)
3033 term terminator))
3034 ((consp terminator)
3035 (setq pat (car terminator)
3036 term (cdr terminator)))
3038 nil))
3040 ;; Check to see if the pattern is present in the str already sent
3041 (unless (and pat term
3042 (string-match (concat pat "\\'") str))
3043 (comint-simple-send (get-buffer-process buf) term)
3044 (setq sql-output-newline-count
3045 (if sql-output-newline-count
3046 (1+ sql-output-newline-count)
3047 1)))
3048 (setq sql-output-by-send t)))
3050 (defun sql-remove-tabs-filter (str)
3051 "Replace tab characters with spaces."
3052 (replace-regexp-in-string "\t" " " str nil t))
3054 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
3055 "Toggle `sql-pop-to-buffer-after-send-region'.
3057 If given the optional parameter VALUE, sets
3058 `sql-toggle-pop-to-buffer-after-send-region' to VALUE."
3059 (interactive "P")
3060 (if value
3061 (setq sql-pop-to-buffer-after-send-region value)
3062 (setq sql-pop-to-buffer-after-send-region
3063 (null sql-pop-to-buffer-after-send-region))))
3067 ;;; Redirect output functions
3069 (defun sql-redirect (command combuf &optional outbuf save-prior)
3070 "Execute the SQL command and send output to OUTBUF.
3072 COMBUF must be an active SQL interactive buffer. OUTBUF may be
3073 an existing buffer, or the name of a non-existing buffer. If
3074 omitted the output is sent to a temporary buffer which will be
3075 killed after the command completes. COMMAND should be a string
3076 of commands accepted by the SQLi program."
3078 (with-current-buffer combuf
3079 (let ((buf (get-buffer-create (or outbuf " *SQL-Redirect*")))
3080 (proc (get-buffer-process (current-buffer)))
3081 (comint-prompt-regexp (sql-get-product-feature sql-product
3082 :prompt-regexp))
3083 (start nil))
3084 (with-current-buffer buf
3085 (toggle-read-only -1)
3086 (unless save-prior
3087 (erase-buffer))
3088 (goto-char (point-max))
3089 (unless (zerop (buffer-size))
3090 (insert "\n"))
3091 (setq start (point)))
3093 ;; Run the command
3094 (message "Executing SQL command...")
3095 (comint-redirect-send-command-to-process command buf proc nil t)
3096 (while (null comint-redirect-completed)
3097 (accept-process-output nil 1))
3098 (message "Executing SQL command...done")
3100 ;; Clean up the output results
3101 (with-current-buffer buf
3102 ;; Remove trailing whitespace
3103 (goto-char (point-max))
3104 (when (looking-back "[ \t\f\n\r]*" start)
3105 (delete-region (match-beginning 0) (match-end 0)))
3106 ;; Remove echo if there was one
3107 (goto-char start)
3108 (when (looking-at (concat "^" (regexp-quote command) "[\\n]"))
3109 (delete-region (match-beginning 0) (match-end 0)))
3110 (goto-char start)))))
3112 (defun sql-redirect-value (command combuf regexp &optional regexp-groups)
3113 "Execute the SQL command and return part of result.
3115 COMBUF must be an active SQL interactive buffer. COMMAND should
3116 be a string of commands accepted by the SQLi program. From the
3117 output, the REGEXP is repeatedly matched and the list of
3118 REGEXP-GROUPS submatches is returned. This behaves much like
3119 \\[comint-redirect-results-list-from-process] but instead of
3120 returning a single submatch it returns a list of each submatch
3121 for each match."
3123 (let ((outbuf " *SQL-Redirect-values*")
3124 (results nil))
3125 (sql-redirect command combuf outbuf nil)
3126 (with-current-buffer outbuf
3127 (while (re-search-forward regexp nil t)
3128 (push
3129 (cond
3130 ;; no groups-return all of them
3131 ((null regexp-groups)
3132 (let ((i 1)
3133 (r nil))
3134 (while (match-beginning i)
3135 (push (match-string i) r))
3136 (nreverse r)))
3137 ;; one group specified
3138 ((numberp regexp-groups)
3139 (match-string regexp-groups))
3140 ;; list of numbers; return the specified matches only
3141 ((consp regexp-groups)
3142 (mapcar (lambda (c)
3143 (cond
3144 ((numberp c) (match-string c))
3145 ((stringp c) (match-substitute-replacement c))
3146 (t (error "sql-redirect-value: unknown REGEXP-GROUPS value - %s" c))))
3147 regexp-groups))
3148 ;; String is specified; return replacement string
3149 ((stringp regexp-groups)
3150 (match-substitute-replacement regexp-groups))
3152 (error "sql-redirect-value: unknown REGEXP-GROUPS value - %s"
3153 regexp-groups)))
3154 results)))
3155 (nreverse results)))
3157 (defun sql-execute (sqlbuf outbuf command arg)
3158 "Executes a command in a SQL interacive buffer and captures the output.
3160 The commands are run in SQLBUF and the output saved in OUTBUF.
3161 COMMAND must be a string, a function or a list of such elements.
3162 Functions are called with SQLBUF, OUTBUF and ARG as parameters;
3163 strings are formatted with ARG and executed.
3165 If the results are empty the OUTBUF is deleted, otherwise the
3166 buffer is popped into a view window. "
3167 (mapc
3168 (lambda (c)
3169 (cond
3170 ((stringp c)
3171 (sql-redirect (if arg (format c arg) c) sqlbuf outbuf) t)
3172 ((functionp c)
3173 (apply c sqlbuf outbuf arg))
3174 (t (error "Unknown sql-execute item %s" c))))
3175 (if (consp command) command (cons command nil)))
3177 (setq outbuf (get-buffer outbuf))
3178 (if (zerop (buffer-size outbuf))
3179 (kill-buffer outbuf)
3180 (let ((one-win (eq (selected-window)
3181 (get-lru-window))))
3182 (with-current-buffer outbuf
3183 (set-buffer-modified-p nil)
3184 (toggle-read-only 1))
3185 (view-buffer-other-window outbuf)
3186 (when one-win
3187 (shrink-window-if-larger-than-buffer)))))
3189 (defun sql-execute-feature (sqlbuf outbuf feature enhanced arg)
3190 "List objects or details in a separate display buffer."
3191 (let (command)
3192 (with-current-buffer sqlbuf
3193 (setq command (sql-get-product-feature sql-product feature)))
3194 (unless command
3195 (error "%s does not support %s" sql-product feature))
3196 (when (consp command)
3197 (setq command (if enhanced
3198 (cdr command)
3199 (car command))))
3200 (sql-execute sqlbuf outbuf command arg)))
3202 (defun sql-read-table-name (prompt)
3203 "Read the name of a database table."
3204 ;; TODO: Fetch table/view names from database and provide completion.
3205 ;; Also implement thing-at-point if the buffer has valid names in it
3206 ;; (i.e. sql-mode, sql-interactive-mode, or sql-list-all buffers)
3207 (read-from-minibuffer prompt))
3209 (defun sql-list-all (&optional enhanced)
3210 "List all database objects."
3211 (interactive "P")
3212 (let ((sqlbuf (sql-find-sqli-buffer)))
3213 (unless sqlbuf
3214 (error "No SQL interactive buffer found"))
3215 (sql-execute-feature sqlbuf "*List All*" :list-all enhanced nil)))
3217 (defun sql-list-table (name &optional enhanced)
3218 "List the details of a database table. "
3219 (interactive
3220 (list (sql-read-table-name "Table name: ")
3221 current-prefix-arg))
3222 (let ((sqlbuf (sql-find-sqli-buffer)))
3223 (unless sqlbuf
3224 (error "No SQL interactive buffer found"))
3225 (unless name
3226 (error "No table name specified"))
3227 (sql-execute-feature sqlbuf (format "*List %s*" name)
3228 :list-table enhanced name)))
3232 ;;; SQL mode -- uses SQL interactive mode
3234 ;;;###autoload
3235 (defun sql-mode ()
3236 "Major mode to edit SQL.
3238 You can send SQL statements to the SQLi buffer using
3239 \\[sql-send-region]. Such a buffer must exist before you can do this.
3240 See `sql-help' on how to create SQLi buffers.
3242 \\{sql-mode-map}
3243 Customization: Entry to this mode runs the `sql-mode-hook'.
3245 When you put a buffer in SQL mode, the buffer stores the last SQLi
3246 buffer created as its destination in the variable `sql-buffer'. This
3247 will be the buffer \\[sql-send-region] sends the region to. If this
3248 SQLi buffer is killed, \\[sql-send-region] is no longer able to
3249 determine where the strings should be sent to. You can set the
3250 value of `sql-buffer' using \\[sql-set-sqli-buffer].
3252 For information on how to create multiple SQLi buffers, see
3253 `sql-interactive-mode'.
3255 Note that SQL doesn't have an escape character unless you specify
3256 one. If you specify backslash as escape character in SQL,
3257 you must tell Emacs. Here's how to do that in your `~/.emacs' file:
3259 \(add-hook 'sql-mode-hook
3260 (lambda ()
3261 (modify-syntax-entry ?\\\\ \".\" sql-mode-syntax-table)))"
3262 (interactive)
3263 (kill-all-local-variables)
3264 (setq major-mode 'sql-mode)
3265 (setq mode-name "SQL")
3266 (use-local-map sql-mode-map)
3267 (if sql-mode-menu
3268 (easy-menu-add sql-mode-menu)); XEmacs
3269 (set-syntax-table sql-mode-syntax-table)
3270 (make-local-variable 'font-lock-defaults)
3271 (make-local-variable 'sql-mode-font-lock-keywords)
3272 (make-local-variable 'comment-start)
3273 (setq comment-start "--")
3274 ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
3275 (make-local-variable 'sql-buffer)
3276 ;; Add imenu support for sql-mode. Note that imenu-generic-expression
3277 ;; is buffer-local, so we don't need a local-variable for it. SQL is
3278 ;; case-insensitive, that's why we have to set imenu-case-fold-search.
3279 (setq imenu-generic-expression sql-imenu-generic-expression
3280 imenu-case-fold-search t)
3281 ;; Make `sql-send-paragraph' work on paragraphs that contain indented
3282 ;; lines.
3283 (make-local-variable 'paragraph-separate)
3284 (make-local-variable 'paragraph-start)
3285 (setq paragraph-separate "[\f]*$"
3286 paragraph-start "[\n\f]")
3287 ;; Abbrevs
3288 (setq local-abbrev-table sql-mode-abbrev-table)
3289 (setq abbrev-all-caps 1)
3290 ;; Run hook
3291 (run-mode-hooks 'sql-mode-hook)
3292 ;; Catch changes to sql-product and highlight accordingly
3293 (sql-highlight-product)
3294 (add-hook 'hack-local-variables-hook 'sql-highlight-product t t))
3298 ;;; SQL interactive mode
3300 (put 'sql-interactive-mode 'mode-class 'special)
3302 (defun sql-interactive-mode ()
3303 "Major mode to use a SQL interpreter interactively.
3305 Do not call this function by yourself. The environment must be
3306 initialized by an entry function specific for the SQL interpreter.
3307 See `sql-help' for a list of available entry functions.
3309 \\[comint-send-input] after the end of the process' output sends the
3310 text from the end of process to the end of the current line.
3311 \\[comint-send-input] before end of process output copies the current
3312 line minus the prompt to the end of the buffer and sends it.
3313 \\[comint-copy-old-input] just copies the current line.
3314 Use \\[sql-accumulate-and-indent] to enter multi-line statements.
3316 If you want to make multiple SQL buffers, rename the `*SQL*' buffer
3317 using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
3318 See `sql-help' for a list of available entry functions. The last buffer
3319 created by such an entry function is the current SQLi buffer. SQL
3320 buffers will send strings to the SQLi buffer current at the time of
3321 their creation. See `sql-mode' for details.
3323 Sample session using two connections:
3325 1. Create first SQLi buffer by calling an entry function.
3326 2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
3327 3. Create a SQL buffer \"test1.sql\".
3328 4. Create second SQLi buffer by calling an entry function.
3329 5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
3330 6. Create a SQL buffer \"test2.sql\".
3332 Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
3333 buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
3334 will send the region to buffer \"*Connection 2*\".
3336 If you accidentally suspend your process, use \\[comint-continue-subjob]
3337 to continue it. On some operating systems, this will not work because
3338 the signals are not supported.
3340 \\{sql-interactive-mode-map}
3341 Customization: Entry to this mode runs the hooks on `comint-mode-hook'
3342 and `sql-interactive-mode-hook' (in that order). Before each input, the
3343 hooks on `comint-input-filter-functions' are run. After each SQL
3344 interpreter output, the hooks on `comint-output-filter-functions' are
3345 run.
3347 Variable `sql-input-ring-file-name' controls the initialization of the
3348 input ring history.
3350 Variables `comint-output-filter-functions', a hook, and
3351 `comint-scroll-to-bottom-on-input' and
3352 `comint-scroll-to-bottom-on-output' control whether input and output
3353 cause the window to scroll to the end of the buffer.
3355 If you want to make SQL buffers limited in length, add the function
3356 `comint-truncate-buffer' to `comint-output-filter-functions'.
3358 Here is an example for your .emacs file. It keeps the SQLi buffer a
3359 certain length.
3361 \(add-hook 'sql-interactive-mode-hook
3362 \(function (lambda ()
3363 \(setq comint-output-filter-functions 'comint-truncate-buffer))))
3365 Here is another example. It will always put point back to the statement
3366 you entered, right above the output it created.
3368 \(setq comint-output-filter-functions
3369 \(function (lambda (STR) (comint-show-output))))"
3370 (delay-mode-hooks (comint-mode))
3372 ;; Get the `sql-product' for this interactive session.
3373 (set (make-local-variable 'sql-product)
3374 (or sql-interactive-product
3375 sql-product))
3377 ;; Setup the mode.
3378 (setq major-mode 'sql-interactive-mode)
3379 (setq mode-name (concat "SQLi[" (or (sql-get-product-feature sql-product :name)
3380 (symbol-name sql-product)) "]"))
3381 (use-local-map sql-interactive-mode-map)
3382 (if sql-interactive-mode-menu
3383 (easy-menu-add sql-interactive-mode-menu)) ; XEmacs
3384 (set-syntax-table sql-mode-syntax-table)
3385 (make-local-variable 'sql-mode-font-lock-keywords)
3386 (make-local-variable 'font-lock-defaults)
3388 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
3389 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
3390 ;; will have just one quote. Therefore syntactic hilighting is
3391 ;; disabled for interactive buffers. No imenu support.
3392 (sql-product-font-lock t nil)
3394 ;; Enable commenting and uncommenting of the region.
3395 (make-local-variable 'comment-start)
3396 (setq comment-start "--")
3397 ;; Abbreviation table init and case-insensitive. It is not activated
3398 ;; by default.
3399 (setq local-abbrev-table sql-mode-abbrev-table)
3400 (setq abbrev-all-caps 1)
3401 ;; Exiting the process will call sql-stop.
3402 (set-process-sentinel (get-buffer-process (current-buffer)) 'sql-stop)
3403 ;; Save the connection name
3404 (make-local-variable 'sql-connection)
3405 ;; Create a usefull name for renaming this buffer later.
3406 (make-local-variable 'sql-alternate-buffer-name)
3407 (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
3408 ;; User stuff. Initialize before the hook.
3409 (set (make-local-variable 'sql-prompt-regexp)
3410 (sql-get-product-feature sql-product :prompt-regexp))
3411 (set (make-local-variable 'sql-prompt-length)
3412 (sql-get-product-feature sql-product :prompt-length))
3413 (set (make-local-variable 'sql-prompt-cont-regexp)
3414 (sql-get-product-feature sql-product :prompt-cont-regexp))
3415 (make-local-variable 'sql-output-newline-count)
3416 (make-local-variable 'sql-output-by-send)
3417 (add-hook 'comint-preoutput-filter-functions
3418 'sql-interactive-remove-continuation-prompt nil t)
3419 (make-local-variable 'sql-input-ring-separator)
3420 (make-local-variable 'sql-input-ring-file-name)
3421 ;; Run the mode hook (along with comint's hooks).
3422 (run-mode-hooks 'sql-interactive-mode-hook)
3423 ;; Set comint based on user overrides.
3424 (setq comint-prompt-regexp
3425 (if sql-prompt-cont-regexp
3426 (concat "\\(" sql-prompt-regexp
3427 "\\|" sql-prompt-cont-regexp "\\)")
3428 sql-prompt-regexp))
3429 (setq left-margin sql-prompt-length)
3430 ;; Install input sender
3431 (set (make-local-variable 'comint-input-sender) 'sql-input-sender)
3432 ;; People wanting a different history file for each
3433 ;; buffer/process/client/whatever can change separator and file-name
3434 ;; on the sql-interactive-mode-hook.
3435 (setq comint-input-ring-separator sql-input-ring-separator
3436 comint-input-ring-file-name sql-input-ring-file-name)
3437 ;; Calling the hook before calling comint-read-input-ring allows users
3438 ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
3439 (comint-read-input-ring t))
3441 (defun sql-stop (process event)
3442 "Called when the SQL process is stopped.
3444 Writes the input history to a history file using
3445 `comint-write-input-ring' and inserts a short message in the SQL buffer.
3447 This function is a sentinel watching the SQL interpreter process.
3448 Sentinels will always get the two parameters PROCESS and EVENT."
3449 (comint-write-input-ring)
3450 (if (and (eq (current-buffer) sql-buffer)
3451 (not buffer-read-only))
3452 (insert (format "\nProcess %s %s\n" process event))
3453 (message "Process %s %s" process event)))
3457 ;;; Connection handling
3459 (defun sql-read-connection (prompt &optional initial default)
3460 "Read a connection name."
3461 (let ((completion-ignore-case t))
3462 (completing-read prompt
3463 (mapcar (lambda (c) (car c))
3464 sql-connection-alist)
3465 nil t initial 'sql-connection-history default)))
3467 ;;;###autoload
3468 (defun sql-connect (connection)
3469 "Connect to an interactive session using CONNECTION settings.
3471 See `sql-connection-alist' to see how to define connections and
3472 their settings.
3474 The user will not be prompted for any login parameters if a value
3475 is specified in the connection settings."
3477 ;; Prompt for the connection from those defined in the alist
3478 (interactive
3479 (if sql-connection-alist
3480 (list (sql-read-connection "Connection: " nil '(nil)))
3481 nil))
3483 ;; Are there connections defined
3484 (if sql-connection-alist
3485 ;; Was one selected
3486 (when connection
3487 ;; Get connection settings
3488 (let ((connect-set (assoc connection sql-connection-alist)))
3489 ;; Settings are defined
3490 (if connect-set
3491 ;; Set the desired parameters
3492 (eval `(let*
3493 (,@(cdr connect-set)
3494 ;; :sqli-login params variable
3495 (param-var (sql-get-product-feature sql-product
3496 :sqli-login nil t))
3497 ;; :sqli-login params value
3498 (login-params (sql-get-product-feature sql-product
3499 :sqli-login))
3500 ;; which params are in the connection
3501 (set-params (mapcar
3502 (lambda (v)
3503 (cond
3504 ((eq (car v) 'sql-user) 'user)
3505 ((eq (car v) 'sql-password) 'password)
3506 ((eq (car v) 'sql-server) 'server)
3507 ((eq (car v) 'sql-database) 'database)
3508 ((eq (car v) 'sql-port) 'port)
3509 (t (car v))))
3510 (cdr connect-set)))
3511 ;; the remaining params (w/o the connection params)
3512 (rem-params (sql-for-each-login
3513 login-params
3514 (lambda (token plist)
3515 (unless (member token set-params)
3516 (if plist
3517 (cons token plist)
3518 token)))))
3519 ;; Remember the connection
3520 (sql-connection connection))
3522 ;; Set the remaining parameters and start the
3523 ;; interactive session
3524 (eval `(let ((,param-var ',rem-params))
3525 (sql-product-interactive sql-product)))))
3526 (message "SQL Connection <%s> does not exist" connection)
3527 nil)))
3528 (message "No SQL Connections defined")
3529 nil))
3531 (defun sql-save-connection (name)
3532 "Captures the connection information of the current SQLi session.
3534 The information is appended to `sql-connection-alist' and
3535 optionally is saved to the user's init file."
3537 (interactive "sNew connection name: ")
3539 (if sql-connection
3540 (message "This session was started by a connection; it's already been saved.")
3542 (let ((login (sql-get-product-feature sql-product :sqli-login))
3543 (alist sql-connection-alist)
3544 connect)
3546 ;; Remove the existing connection if the user says so
3547 (when (and (assoc name alist)
3548 (yes-or-no-p (format "Replace connection definition <%s>? " name)))
3549 (setq alist (assq-delete-all name alist)))
3551 ;; Add the new connection if it doesn't exist
3552 (if (assoc name alist)
3553 (message "Connection <%s> already exists" name)
3554 (setq connect
3555 (append (list name)
3556 (sql-for-each-login
3557 `(product ,@login)
3558 (lambda (token plist)
3559 (cond
3560 ((eq token 'product) `(sql-product ',sql-product))
3561 ((eq token 'user) `(sql-user ,sql-user))
3562 ((eq token 'database) `(sql-database ,sql-database))
3563 ((eq token 'server) `(sql-server ,sql-server))
3564 ((eq token 'port) `(sql-port ,sql-port)))))))
3566 (setq alist (append alist (list connect)))
3568 ;; confirm whether we want to save the connections
3569 (if (yes-or-no-p "Save the connections for future sessions? ")
3570 (customize-save-variable 'sql-connection-alist alist)
3571 (customize-set-variable 'sql-connection-alist alist))))))
3573 (defun sql-connection-menu-filter (tail)
3574 "Generates menu entries for using each connection."
3575 (append
3576 (mapcar
3577 (lambda (conn)
3578 (vector
3579 (format "Connection <%s>" (car conn))
3580 (list 'sql-connect (car conn))
3582 sql-connection-alist)
3583 tail))
3587 ;;; Entry functions for different SQL interpreters.
3589 ;;;###autoload
3590 (defun sql-product-interactive (&optional product new-name)
3591 "Run PRODUCT interpreter as an inferior process.
3593 If buffer `*SQL*' exists but no process is running, make a new process.
3594 If buffer exists and a process is running, just switch to buffer `*SQL*'.
3596 To specify the SQL product, prefix the call with
3597 \\[universal-argument]. To set the buffer name as well, prefix
3598 the call to \\[sql-product-interactive] with
3599 \\[universal-argument] \\[universal-argument].
3601 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
3602 (interactive "P")
3604 ;; Handle universal arguments if specified
3605 (when (not (or executing-kbd-macro noninteractive))
3606 (when (and (consp product)
3607 (not (cdr product))
3608 (numberp (car product)))
3609 (when (>= (prefix-numeric-value product) 16)
3610 (when (not new-name)
3611 (setq new-name '(4)))
3612 (setq product '(4)))))
3614 ;; Get the value of product that we need
3615 (setq product
3616 (cond
3617 ((and product ; Product specified
3618 (symbolp product)) product)
3619 ((= (prefix-numeric-value product) 4) ; C-u, prompt for product
3620 (sql-read-product "SQL product: " sql-product))
3621 (t sql-product))) ; Default to sql-product
3623 ;; If we have a product and it has a interactive mode
3624 (if product
3625 (when (sql-get-product-feature product :sqli-comint-func)
3626 ;; If no new name specified, try to pop to an active SQL
3627 ;; interactive for the same product
3628 (let ((buf (sql-find-sqli-buffer product)))
3629 (if (and (not new-name) buf)
3630 (pop-to-buffer buf)
3632 ;; We have a new name or sql-buffer doesn't exist or match
3633 ;; Start by remembering where we start
3634 (let ((start-buffer (current-buffer))
3635 new-sqli-buffer)
3637 ;; Get credentials.
3638 (apply 'sql-get-login (sql-get-product-feature product :sqli-login))
3640 ;; Connect to database.
3641 (message "Login...")
3642 (funcall (sql-get-product-feature product :sqli-comint-func)
3643 product
3644 (sql-get-product-feature product :sqli-options))
3646 ;; Set SQLi mode.
3647 (setq new-sqli-buffer (current-buffer))
3648 (let ((sql-interactive-product product))
3649 (sql-interactive-mode))
3651 ;; Set the new buffer name
3652 (when new-name
3653 (sql-rename-buffer new-name))
3655 ;; Set `sql-buffer' in the new buffer and the start buffer
3656 (setq sql-buffer (buffer-name new-sqli-buffer))
3657 (with-current-buffer start-buffer
3658 (setq sql-buffer (buffer-name new-sqli-buffer))
3659 (run-hooks 'sql-set-sqli-hook))
3661 ;; All done.
3662 (message "Login...done")
3663 (pop-to-buffer sql-buffer)))))
3664 (message "No default SQL product defined. Set `sql-product'.")))
3666 (defun sql-comint (product params)
3667 "Set up a comint buffer to run the SQL processor.
3669 PRODUCT is the SQL product. PARAMS is a list of strings which are
3670 passed as command line arguments."
3671 (let ((program (sql-get-product-feature product :sqli-program))
3672 (buf-name "SQL"))
3673 ;; make sure we can find the program
3674 (unless (executable-find program)
3675 (error "Unable to locate SQL program \'%s\'" program))
3676 ;; Make sure buffer name is unique
3677 (when (sql-buffer-live-p (format "*%s*" buf-name))
3678 (setq buf-name (format "SQL-%s" product))
3679 (when (sql-buffer-live-p (format "*%s*" buf-name))
3680 (let ((i 1))
3681 (while (sql-buffer-live-p
3682 (format "*%s*"
3683 (setq buf-name (format "SQL-%s%d" product i))))
3684 (setq i (1+ i))))))
3685 (set-buffer
3686 (apply 'make-comint buf-name program nil params))))
3688 ;;;###autoload
3689 (defun sql-oracle (&optional buffer)
3690 "Run sqlplus by Oracle as an inferior process.
3692 If buffer `*SQL*' exists but no process is running, make a new process.
3693 If buffer exists and a process is running, just switch to buffer
3694 `*SQL*'.
3696 Interpreter used comes from variable `sql-oracle-program'. Login uses
3697 the variables `sql-user', `sql-password', and `sql-database' as
3698 defaults, if set. Additional command line parameters can be stored in
3699 the list `sql-oracle-options'.
3701 The buffer is put in SQL interactive mode, giving commands for sending
3702 input. See `sql-interactive-mode'.
3704 To set the buffer name directly, use \\[universal-argument]
3705 before \\[sql-oracle]. Once session has started,
3706 \\[sql-rename-buffer] can be called separately to rename the
3707 buffer.
3709 To specify a coding system for converting non-ASCII characters
3710 in the input and output to the process, use \\[universal-coding-system-argument]
3711 before \\[sql-oracle]. You can also specify this with \\[set-buffer-process-coding-system]
3712 in the SQL buffer, after you start the process.
3713 The default comes from `process-coding-system-alist' and
3714 `default-process-coding-system'.
3716 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
3717 (interactive "P")
3718 (sql-product-interactive 'oracle buffer))
3720 (defun sql-comint-oracle (product options)
3721 "Create comint buffer and connect to Oracle."
3722 ;; Produce user/password@database construct. Password without user
3723 ;; is meaningless; database without user/password is meaningless,
3724 ;; because "@param" will ask sqlplus to interpret the script
3725 ;; "param".
3726 (let ((parameter nil))
3727 (if (not (string= "" sql-user))
3728 (if (not (string= "" sql-password))
3729 (setq parameter (concat sql-user "/" sql-password))
3730 (setq parameter sql-user)))
3731 (if (and parameter (not (string= "" sql-database)))
3732 (setq parameter (concat parameter "@" sql-database)))
3733 (if parameter
3734 (setq parameter (nconc (list parameter) options))
3735 (setq parameter options))
3736 (sql-comint product parameter)))
3740 ;;;###autoload
3741 (defun sql-sybase (&optional buffer)
3742 "Run isql by Sybase as an inferior process.
3744 If buffer `*SQL*' exists but no process is running, make a new process.
3745 If buffer exists and a process is running, just switch to buffer
3746 `*SQL*'.
3748 Interpreter used comes from variable `sql-sybase-program'. Login uses
3749 the variables `sql-server', `sql-user', `sql-password', and
3750 `sql-database' as defaults, if set. Additional command line parameters
3751 can be stored in the list `sql-sybase-options'.
3753 The buffer is put in SQL interactive mode, giving commands for sending
3754 input. See `sql-interactive-mode'.
3756 To set the buffer name directly, use \\[universal-argument]
3757 before \\[sql-sybase]. Once session has started,
3758 \\[sql-rename-buffer] can be called separately to rename the
3759 buffer.
3761 To specify a coding system for converting non-ASCII characters
3762 in the input and output to the process, use \\[universal-coding-system-argument]
3763 before \\[sql-sybase]. You can also specify this with \\[set-buffer-process-coding-system]
3764 in the SQL buffer, after you start the process.
3765 The default comes from `process-coding-system-alist' and
3766 `default-process-coding-system'.
3768 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
3769 (interactive "P")
3770 (sql-product-interactive 'sybase buffer))
3772 (defun sql-comint-sybase (product options)
3773 "Create comint buffer and connect to Sybase."
3774 ;; Put all parameters to the program (if defined) in a list and call
3775 ;; make-comint.
3776 (let ((params options))
3777 (if (not (string= "" sql-server))
3778 (setq params (append (list "-S" sql-server) params)))
3779 (if (not (string= "" sql-database))
3780 (setq params (append (list "-D" sql-database) params)))
3781 (if (not (string= "" sql-password))
3782 (setq params (append (list "-P" sql-password) params)))
3783 (if (not (string= "" sql-user))
3784 (setq params (append (list "-U" sql-user) params)))
3785 (sql-comint product params)))
3789 ;;;###autoload
3790 (defun sql-informix (&optional buffer)
3791 "Run dbaccess by Informix as an inferior process.
3793 If buffer `*SQL*' exists but no process is running, make a new process.
3794 If buffer exists and a process is running, just switch to buffer
3795 `*SQL*'.
3797 Interpreter used comes from variable `sql-informix-program'. Login uses
3798 the variable `sql-database' as default, if set.
3800 The buffer is put in SQL interactive mode, giving commands for sending
3801 input. See `sql-interactive-mode'.
3803 To set the buffer name directly, use \\[universal-argument]
3804 before \\[sql-informix]. Once session has started,
3805 \\[sql-rename-buffer] can be called separately to rename the
3806 buffer.
3808 To specify a coding system for converting non-ASCII characters
3809 in the input and output to the process, use \\[universal-coding-system-argument]
3810 before \\[sql-informix]. You can also specify this with \\[set-buffer-process-coding-system]
3811 in the SQL buffer, after you start the process.
3812 The default comes from `process-coding-system-alist' and
3813 `default-process-coding-system'.
3815 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
3816 (interactive "P")
3817 (sql-product-interactive 'informix buffer))
3819 (defun sql-comint-informix (product options)
3820 "Create comint buffer and connect to Informix."
3821 ;; username and password are ignored.
3822 (let ((db (if (string= "" sql-database)
3824 (if (string= "" sql-server)
3825 sql-database
3826 (concat sql-database "@" sql-server)))))
3827 (sql-comint product (append `(,db "-") options))))
3831 ;;;###autoload
3832 (defun sql-sqlite (&optional buffer)
3833 "Run sqlite as an inferior process.
3835 SQLite is free software.
3837 If buffer `*SQL*' exists but no process is running, make a new process.
3838 If buffer exists and a process is running, just switch to buffer
3839 `*SQL*'.
3841 Interpreter used comes from variable `sql-sqlite-program'. Login uses
3842 the variables `sql-user', `sql-password', `sql-database', and
3843 `sql-server' as defaults, if set. Additional command line parameters
3844 can be stored in the list `sql-sqlite-options'.
3846 The buffer is put in SQL interactive mode, giving commands for sending
3847 input. See `sql-interactive-mode'.
3849 To set the buffer name directly, use \\[universal-argument]
3850 before \\[sql-sqlite]. Once session has started,
3851 \\[sql-rename-buffer] can be called separately to rename the
3852 buffer.
3854 To specify a coding system for converting non-ASCII characters
3855 in the input and output to the process, use \\[universal-coding-system-argument]
3856 before \\[sql-sqlite]. You can also specify this with \\[set-buffer-process-coding-system]
3857 in the SQL buffer, after you start the process.
3858 The default comes from `process-coding-system-alist' and
3859 `default-process-coding-system'.
3861 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
3862 (interactive "P")
3863 (sql-product-interactive 'sqlite buffer))
3865 (defun sql-comint-sqlite (product options)
3866 "Create comint buffer and connect to SQLite."
3867 ;; Put all parameters to the program (if defined) in a list and call
3868 ;; make-comint.
3869 (let ((params))
3870 (if (not (string= "" sql-database))
3871 (setq params (append (list (expand-file-name sql-database))
3872 params)))
3873 (setq params (append options params))
3874 (sql-comint product params)))
3878 ;;;###autoload
3879 (defun sql-mysql (&optional buffer)
3880 "Run mysql by TcX as an inferior process.
3882 Mysql versions 3.23 and up are free software.
3884 If buffer `*SQL*' exists but no process is running, make a new process.
3885 If buffer exists and a process is running, just switch to buffer
3886 `*SQL*'.
3888 Interpreter used comes from variable `sql-mysql-program'. Login uses
3889 the variables `sql-user', `sql-password', `sql-database', and
3890 `sql-server' as defaults, if set. Additional command line parameters
3891 can be stored in the list `sql-mysql-options'.
3893 The buffer is put in SQL interactive mode, giving commands for sending
3894 input. See `sql-interactive-mode'.
3896 To set the buffer name directly, use \\[universal-argument]
3897 before \\[sql-mysql]. Once session has started,
3898 \\[sql-rename-buffer] can be called separately to rename the
3899 buffer.
3901 To specify a coding system for converting non-ASCII characters
3902 in the input and output to the process, use \\[universal-coding-system-argument]
3903 before \\[sql-mysql]. You can also specify this with \\[set-buffer-process-coding-system]
3904 in the SQL buffer, after you start the process.
3905 The default comes from `process-coding-system-alist' and
3906 `default-process-coding-system'.
3908 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
3909 (interactive "P")
3910 (sql-product-interactive 'mysql buffer))
3912 (defun sql-comint-mysql (product options)
3913 "Create comint buffer and connect to MySQL."
3914 ;; Put all parameters to the program (if defined) in a list and call
3915 ;; make-comint.
3916 (let ((params))
3917 (if (not (string= "" sql-database))
3918 (setq params (append (list sql-database) params)))
3919 (if (not (string= "" sql-server))
3920 (setq params (append (list (concat "--host=" sql-server)) params)))
3921 (if (not (= 0 sql-port))
3922 (setq params (append (list (concat "--port=" (number-to-string sql-port))) params)))
3923 (if (not (string= "" sql-password))
3924 (setq params (append (list (concat "--password=" sql-password)) params)))
3925 (if (not (string= "" sql-user))
3926 (setq params (append (list (concat "--user=" sql-user)) params)))
3927 (setq params (append options params))
3928 (sql-comint product params)))
3932 ;;;###autoload
3933 (defun sql-solid (&optional buffer)
3934 "Run solsql by Solid as an inferior process.
3936 If buffer `*SQL*' exists but no process is running, make a new process.
3937 If buffer exists and a process is running, just switch to buffer
3938 `*SQL*'.
3940 Interpreter used comes from variable `sql-solid-program'. Login uses
3941 the variables `sql-user', `sql-password', and `sql-server' as
3942 defaults, if set.
3944 The buffer is put in SQL interactive mode, giving commands for sending
3945 input. See `sql-interactive-mode'.
3947 To set the buffer name directly, use \\[universal-argument]
3948 before \\[sql-solid]. Once session has started,
3949 \\[sql-rename-buffer] can be called separately to rename the
3950 buffer.
3952 To specify a coding system for converting non-ASCII characters
3953 in the input and output to the process, use \\[universal-coding-system-argument]
3954 before \\[sql-solid]. You can also specify this with \\[set-buffer-process-coding-system]
3955 in the SQL buffer, after you start the process.
3956 The default comes from `process-coding-system-alist' and
3957 `default-process-coding-system'.
3959 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
3960 (interactive "P")
3961 (sql-product-interactive 'solid buffer))
3963 (defun sql-comint-solid (product options)
3964 "Create comint buffer and connect to Solid."
3965 ;; Put all parameters to the program (if defined) in a list and call
3966 ;; make-comint.
3967 (let ((params options))
3968 ;; It only makes sense if both username and password are there.
3969 (if (not (or (string= "" sql-user)
3970 (string= "" sql-password)))
3971 (setq params (append (list sql-user sql-password) params)))
3972 (if (not (string= "" sql-server))
3973 (setq params (append (list sql-server) params)))
3974 (sql-comint product params)))
3978 ;;;###autoload
3979 (defun sql-ingres (&optional buffer)
3980 "Run sql by Ingres as an inferior process.
3982 If buffer `*SQL*' exists but no process is running, make a new process.
3983 If buffer exists and a process is running, just switch to buffer
3984 `*SQL*'.
3986 Interpreter used comes from variable `sql-ingres-program'. Login uses
3987 the variable `sql-database' as default, if set.
3989 The buffer is put in SQL interactive mode, giving commands for sending
3990 input. See `sql-interactive-mode'.
3992 To set the buffer name directly, use \\[universal-argument]
3993 before \\[sql-ingres]. Once session has started,
3994 \\[sql-rename-buffer] can be called separately to rename the
3995 buffer.
3997 To specify a coding system for converting non-ASCII characters
3998 in the input and output to the process, use \\[universal-coding-system-argument]
3999 before \\[sql-ingres]. You can also specify this with \\[set-buffer-process-coding-system]
4000 in the SQL buffer, after you start the process.
4001 The default comes from `process-coding-system-alist' and
4002 `default-process-coding-system'.
4004 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4005 (interactive "P")
4006 (sql-product-interactive 'ingres buffer))
4008 (defun sql-comint-ingres (product options)
4009 "Create comint buffer and connect to Ingres."
4010 ;; username and password are ignored.
4011 (sql-comint product
4012 (append (if (string= "" sql-database)
4014 (list sql-database))
4015 options)))
4019 ;;;###autoload
4020 (defun sql-ms (&optional buffer)
4021 "Run osql by Microsoft as an inferior process.
4023 If buffer `*SQL*' exists but no process is running, make a new process.
4024 If buffer exists and a process is running, just switch to buffer
4025 `*SQL*'.
4027 Interpreter used comes from variable `sql-ms-program'. Login uses the
4028 variables `sql-user', `sql-password', `sql-database', and `sql-server'
4029 as defaults, if set. Additional command line parameters can be stored
4030 in the list `sql-ms-options'.
4032 The buffer is put in SQL interactive mode, giving commands for sending
4033 input. See `sql-interactive-mode'.
4035 To set the buffer name directly, use \\[universal-argument]
4036 before \\[sql-ms]. Once session has started,
4037 \\[sql-rename-buffer] can be called separately to rename the
4038 buffer.
4040 To specify a coding system for converting non-ASCII characters
4041 in the input and output to the process, use \\[universal-coding-system-argument]
4042 before \\[sql-ms]. You can also specify this with \\[set-buffer-process-coding-system]
4043 in the SQL buffer, after you start the process.
4044 The default comes from `process-coding-system-alist' and
4045 `default-process-coding-system'.
4047 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4048 (interactive "P")
4049 (sql-product-interactive 'ms buffer))
4051 (defun sql-comint-ms (product options)
4052 "Create comint buffer and connect to Microsoft SQL Server."
4053 ;; Put all parameters to the program (if defined) in a list and call
4054 ;; make-comint.
4055 (let ((params options))
4056 (if (not (string= "" sql-server))
4057 (setq params (append (list "-S" sql-server) params)))
4058 (if (not (string= "" sql-database))
4059 (setq params (append (list "-d" sql-database) params)))
4060 (if (not (string= "" sql-user))
4061 (setq params (append (list "-U" sql-user) params)))
4062 (if (not (string= "" sql-password))
4063 (setq params (append (list "-P" sql-password) params))
4064 (if (string= "" sql-user)
4065 ;; if neither user nor password is provided, use system
4066 ;; credentials.
4067 (setq params (append (list "-E") params))
4068 ;; If -P is passed to ISQL as the last argument without a
4069 ;; password, it's considered null.
4070 (setq params (append params (list "-P")))))
4071 (sql-comint product params)))
4075 ;;;###autoload
4076 (defun sql-postgres (&optional buffer)
4077 "Run psql by Postgres as an inferior process.
4079 If buffer `*SQL*' exists but no process is running, make a new process.
4080 If buffer exists and a process is running, just switch to buffer
4081 `*SQL*'.
4083 Interpreter used comes from variable `sql-postgres-program'. Login uses
4084 the variables `sql-database' and `sql-server' as default, if set.
4085 Additional command line parameters can be stored in the list
4086 `sql-postgres-options'.
4088 The buffer is put in SQL interactive mode, giving commands for sending
4089 input. See `sql-interactive-mode'.
4091 To set the buffer name directly, use \\[universal-argument]
4092 before \\[sql-postgres]. Once session has started,
4093 \\[sql-rename-buffer] can be called separately to rename the
4094 buffer.
4096 To specify a coding system for converting non-ASCII characters
4097 in the input and output to the process, use \\[universal-coding-system-argument]
4098 before \\[sql-postgres]. You can also specify this with \\[set-buffer-process-coding-system]
4099 in the SQL buffer, after you start the process.
4100 The default comes from `process-coding-system-alist' and
4101 `default-process-coding-system'. If your output lines end with ^M,
4102 your might try undecided-dos as a coding system. If this doesn't help,
4103 Try to set `comint-output-filter-functions' like this:
4105 \(setq comint-output-filter-functions (append comint-output-filter-functions
4106 '(comint-strip-ctrl-m)))
4108 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4109 (interactive "P")
4110 (sql-product-interactive 'postgres buffer))
4112 (defun sql-comint-postgres (product options)
4113 "Create comint buffer and connect to Postgres."
4114 ;; username and password are ignored. Mark Stosberg suggest to add
4115 ;; the database at the end. Jason Beegan suggest using --pset and
4116 ;; pager=off instead of \\o|cat. The later was the solution by
4117 ;; Gregor Zych. Jason's suggestion is the default value for
4118 ;; sql-postgres-options.
4119 (let ((params options))
4120 (if (not (string= "" sql-database))
4121 (setq params (append params (list sql-database))))
4122 (if (not (string= "" sql-server))
4123 (setq params (append (list "-h" sql-server) params)))
4124 (if (not (string= "" sql-user))
4125 (setq params (append (list "-U" sql-user) params)))
4126 (if (not (= 0 sql-port))
4127 (setq params (append (list "-p" sql-port) params)))
4128 (sql-comint product params)))
4132 ;;;###autoload
4133 (defun sql-interbase (&optional buffer)
4134 "Run isql by Interbase as an inferior process.
4136 If buffer `*SQL*' exists but no process is running, make a new process.
4137 If buffer exists and a process is running, just switch to buffer
4138 `*SQL*'.
4140 Interpreter used comes from variable `sql-interbase-program'. Login
4141 uses the variables `sql-user', `sql-password', and `sql-database' as
4142 defaults, if set.
4144 The buffer is put in SQL interactive mode, giving commands for sending
4145 input. See `sql-interactive-mode'.
4147 To set the buffer name directly, use \\[universal-argument]
4148 before \\[sql-interbase]. Once session has started,
4149 \\[sql-rename-buffer] can be called separately to rename the
4150 buffer.
4152 To specify a coding system for converting non-ASCII characters
4153 in the input and output to the process, use \\[universal-coding-system-argument]
4154 before \\[sql-interbase]. You can also specify this with \\[set-buffer-process-coding-system]
4155 in the SQL buffer, after you start the process.
4156 The default comes from `process-coding-system-alist' and
4157 `default-process-coding-system'.
4159 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4160 (interactive "P")
4161 (sql-product-interactive 'interbase buffer))
4163 (defun sql-comint-interbase (product options)
4164 "Create comint buffer and connect to Interbase."
4165 ;; Put all parameters to the program (if defined) in a list and call
4166 ;; make-comint.
4167 (let ((params options))
4168 (if (not (string= "" sql-user))
4169 (setq params (append (list "-u" sql-user) params)))
4170 (if (not (string= "" sql-password))
4171 (setq params (append (list "-p" sql-password) params)))
4172 (if (not (string= "" sql-database))
4173 (setq params (cons sql-database params))) ; add to the front!
4174 (sql-comint product params)))
4178 ;;;###autoload
4179 (defun sql-db2 (&optional buffer)
4180 "Run db2 by IBM as an inferior process.
4182 If buffer `*SQL*' exists but no process is running, make a new process.
4183 If buffer exists and a process is running, just switch to buffer
4184 `*SQL*'.
4186 Interpreter used comes from variable `sql-db2-program'. There is not
4187 automatic login.
4189 The buffer is put in SQL interactive mode, giving commands for sending
4190 input. See `sql-interactive-mode'.
4192 If you use \\[sql-accumulate-and-indent] to send multiline commands to
4193 db2, newlines will be escaped if necessary. If you don't want that, set
4194 `comint-input-sender' back to `comint-simple-send' by writing an after
4195 advice. See the elisp manual for more information.
4197 To set the buffer name directly, use \\[universal-argument]
4198 before \\[sql-db2]. Once session has started,
4199 \\[sql-rename-buffer] can be called separately to rename the
4200 buffer.
4202 To specify a coding system for converting non-ASCII characters
4203 in the input and output to the process, use \\[universal-coding-system-argument]
4204 before \\[sql-db2]. You can also specify this with \\[set-buffer-process-coding-system]
4205 in the SQL buffer, after you start the process.
4206 The default comes from `process-coding-system-alist' and
4207 `default-process-coding-system'.
4209 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4210 (interactive "P")
4211 (sql-product-interactive 'db2 buffer))
4213 (defun sql-comint-db2 (product options)
4214 "Create comint buffer and connect to DB2."
4215 ;; Put all parameters to the program (if defined) in a list and call
4216 ;; make-comint.
4217 (sql-comint product options)
4220 ;;;###autoload
4221 (defun sql-linter (&optional buffer)
4222 "Run inl by RELEX as an inferior process.
4224 If buffer `*SQL*' exists but no process is running, make a new process.
4225 If buffer exists and a process is running, just switch to buffer
4226 `*SQL*'.
4228 Interpreter used comes from variable `sql-linter-program' - usually `inl'.
4229 Login uses the variables `sql-user', `sql-password', `sql-database' and
4230 `sql-server' as defaults, if set. Additional command line parameters
4231 can be stored in the list `sql-linter-options'. Run inl -h to get help on
4232 parameters.
4234 `sql-database' is used to set the LINTER_MBX environment variable for
4235 local connections, `sql-server' refers to the server name from the
4236 `nodetab' file for the network connection (dbc_tcp or friends must run
4237 for this to work). If `sql-password' is an empty string, inl will use
4238 an empty password.
4240 The buffer is put in SQL interactive mode, giving commands for sending
4241 input. See `sql-interactive-mode'.
4243 To set the buffer name directly, use \\[universal-argument]
4244 before \\[sql-linter]. Once session has started,
4245 \\[sql-rename-buffer] can be called separately to rename the
4246 buffer.
4248 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
4249 (interactive "P")
4250 (sql-product-interactive 'linter buffer))
4252 (defun sql-comint-linter (product options)
4253 "Create comint buffer and connect to Linter."
4254 ;; Put all parameters to the program (if defined) in a list and call
4255 ;; make-comint.
4256 (let ((params options)
4257 (login nil)
4258 (old-mbx (getenv "LINTER_MBX")))
4259 (if (not (string= "" sql-user))
4260 (setq login (concat sql-user "/" sql-password)))
4261 (setq params (append (list "-u" login) params))
4262 (if (not (string= "" sql-server))
4263 (setq params (append (list "-n" sql-server) params)))
4264 (if (string= "" sql-database)
4265 (setenv "LINTER_MBX" nil)
4266 (setenv "LINTER_MBX" sql-database))
4267 (sql-comint product params)
4268 (setenv "LINTER_MBX" old-mbx)))
4272 (provide 'sql)
4274 ;;; sql.el ends here