(custom-set-variables): Print message about errors in
[emacs.git] / lisp / progmodes / sql.el
blob0485517bae22f572821419421e8b3b4ce4a1d91f
1 ;;; sql.el --- specialized comint.el for SQL interpreters
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Alex Schroeder <alex@gnu.org>
7 ;; Version: 1.4.19
8 ;; Keywords: comm languages processes
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Please send bug reports and bug fixes to the mailing list at
30 ;; sql.el@gnu.org. If you want to subscribe to the mailing list, send
31 ;; mail to sql.el-request@gnu.org with `subscribe sql.el FIRSTNAME
32 ;; LASTNAME' in the mail body.
34 ;; This file provides a sql-mode and a sql-interactive-mode. My goals
35 ;; were two simple modes providing syntactic hilighting. The
36 ;; interactive mode had to provide a command-line history; the other
37 ;; mode had to provide "send region/buffer to SQL interpreter"
38 ;; functions. "simple" in this context means easy to use, easy to
39 ;; maintain and little or no bells and whistles.
41 ;; If anybody feels like extending this sql mode, take a look at the
42 ;; above mentioned modes and write a sqlx-mode on top of this one. If
43 ;; this proves to be difficult, please suggest changes that will
44 ;; facilitate your plans.
46 ;; sql-interactive-mode is used to interact with a SQL interpreter
47 ;; process in a SQLi buffer (usually called `*SQL*'). The SQLi buffer
48 ;; is created by calling a SQL interpreter-specific entry function. Do
49 ;; *not* call sql-interactive-mode by itself.
51 ;; The list of currently supported interpreters and the corresponding
52 ;; entry function used to create the SQLi buffers is shown with
53 ;; `sql-help' (M-x sql-help).
55 ;; Since sql-interactive-mode is built on top of the general
56 ;; command-interpreter-in-a-buffer mode (comint mode), it shares a
57 ;; common base functionality, and a common set of bindings, with all
58 ;; modes derived from comint mode. This makes these modes easier to
59 ;; use.
61 ;; sql-mode can be used to enable syntactic hilighting for SQL
62 ;; statements in another buffer. SQL statements can then be sent to
63 ;; the SQL process in the SQLi buffer. sql-mode has already been
64 ;; used as a template to a simple PL/SQL mode.
66 ;; For documentation on the functionality provided by comint mode, and
67 ;; the hooks available for customising it, see the file `comint.el'.
69 ;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
70 ;; `imenu-add-menubar-index'.
72 ;;; Requirements for Emacs 19.34:
74 ;; If you are using Emacs 19.34, you will have to get and install
75 ;; the file regexp-opt.el
76 ;; <URL:ftp://ftp.ifi.uio.no/pub/emacs/emacs-20.3/lisp/emacs-lisp/regexp-opt.el>
77 ;; and the custom package
78 ;; <URL:http://www.dina.kvl.dk/~abraham/custom/>.
80 ;;; Bugs:
82 ;; Using sql-ms (isql by Microsoft): When commands with syntax errors
83 ;; or execution errors are executed, there is no server feedback.
84 ;; This happens in stored procedures for example. The server messages
85 ;; only appear after the process is exited. This makes things
86 ;; somewhat unreliable.
88 ;; ChangeLog available on request.
90 ;;; To Do:
92 ;; Add better hilight support for other brands; there is a bias towards
93 ;; Oracle because that's what I use at work. Anybody else just send in
94 ;; your lists of reserved words, keywords and builtin functions! As
95 ;; long as I don't receive any feedback, everything is hilighted with
96 ;; ANSI keywords only. I received the list of ANSI keywords from a
97 ;; user; if you know of any changes, let me know.
99 ;; Add different hilighting levels.
101 ;;; Thanks to all the people who helped me out:
103 ;; Kai Blauberg <kai.blauberg@metla.fi>
104 ;; <ibalaban@dalet.com>
105 ;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
106 ;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
107 ;; nino <nino@inform.dk>
108 ;; Berend de Boer <berend@pobox.com>
112 ;;; Code:
114 (require 'comint)
115 ;; Need the following to allow GNU Emacs 19 to compile the file.
116 (require 'regexp-opt)
117 (require 'custom)
119 ;;; Allow customization
121 (defgroup SQL nil
122 "Running a SQL interpreter from within Emacs buffers"
123 :version "20.4"
124 :group 'processes)
126 ;; These three variables will be used as defaults, if set.
128 (defcustom sql-user ""
129 "*Default username."
130 :type 'string
131 :group 'SQL)
133 (defcustom sql-password ""
134 "*Default password.
136 Storing your password in a textfile such as ~/.emacs could be dangerous.
137 Customizing your password will store it in your ~/.emacs file."
138 :type 'string
139 :group 'SQL)
141 (defcustom sql-database ""
142 "*Default database."
143 :type 'string
144 :group 'SQL)
146 (defcustom sql-server ""
147 "*Default server or host."
148 :type 'string
149 :group 'SQL)
151 ;; misc customization of sql.el behaviour
153 (defcustom sql-electric-stuff nil
154 "Treat some input as electric.
155 If set to the symbol `semicolon', then hitting `;' will send current
156 input in the SQLi buffer to the process.
157 If set to the symbol `go', then hitting `go' on a line by itself will
158 send current input in the SQLi buffer to the process.
159 If set to nil, then you must use \\[comint-send-input] in order to send
160 current input in the SQLi buffer to the process."
161 :type '(choice (const :tag "Nothing" nil)
162 (const :tag "The semikolon `;'" semicolon)
163 (const :tag "The string `go' by itself" go))
164 :version "20.8"
165 :group 'SQL)
167 (defcustom sql-pop-to-buffer-after-send-region nil
168 "*If t, pop to the buffer SQL statements are sent to.
170 After a call to `sql-send-region' or `sql-send-buffer',
171 the window is split and the SQLi buffer is shown. If this
172 variable is not nil, that buffer's window will be selected
173 by calling `pop-to-buffer'. If this variable is nil, that
174 buffer is shown using `display-buffer'."
175 :type 'boolean
176 :group 'SQL)
178 ;; imenu support for sql-mode.
180 (defvar sql-imenu-generic-expression
181 '(("Tables" "^\\s-*create\\s-+table\\s-+\\(\\w+\\)" 1)
182 ("Indexes" "^\\s-*create\\s-+index\\s-+\\(\\w+\\)" 1))
183 "Define interesting points in the SQL buffer for `imenu'.
185 This is used to set `imenu-generic-expression' when SQL mode is
186 entered. Subsequent changes to sql-imenu-generic-expression will not
187 affect existing SQL buffers because imenu-generic-expression is a
188 local variable.")
190 ;; history file
192 (defcustom sql-input-ring-file-name nil
193 "*If non-nil, name of the file to read/write input history.
195 You have to set this variable if you want the history of your commands
196 saved from one Emacs session to the next. If this variable is set,
197 exiting the SQL interpreter in an SQLi buffer will write the input
198 history to the specified file. Starting a new process in a SQLi buffer
199 will read the input history from the specified file.
201 This is used to initialize `comint-input-ring-file-name'.
203 Note that the size of the input history is determined by the variable
204 `comint-input-ring-size'."
205 :type '(choice (const :tag "none" nil)
206 (file))
207 :group 'SQL)
209 (defcustom sql-input-ring-separator "\n--\n"
210 "*Separator between commands in the history file.
212 If set to \"\\n\", each line in the history file will be interpreted as
213 one command. Multi-line commands are split into several commands when
214 the input ring is initialized from a history file.
216 This variable used to initialize `comint-input-ring-separator'.
217 `comint-input-ring-separator' is part of Emacs 21; if your Emacs
218 does not have it, setting `sql-input-ring-separator' will have no
219 effect. In that case multiline commands will be split into several
220 commands when the input history is read, as if you had set
221 `sql-input-ring-separator' to \"\\n\"."
222 :type 'string
223 :group 'SQL)
225 ;; The usual hooks
227 (defcustom sql-interactive-mode-hook '()
228 "*Hook for customising `sql-interactive-mode'."
229 :type 'hook
230 :group 'SQL)
232 (defcustom sql-mode-hook '()
233 "*Hook for customising `sql-mode'."
234 :type 'hook
235 :group 'SQL)
237 (defcustom sql-set-sqli-hook '()
238 "*Hook for reacting to changes of `sql-buffer'.
240 This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
241 is changed."
242 :type 'hook
243 :group 'SQL)
245 ;; Customisation for Oracle
247 (defcustom sql-oracle-program "sqlplus"
248 "*Command to start sqlplus by Oracle.
250 Starts `sql-interactive-mode' after doing some setup.
252 Under NT, \"sqlplus\" usually starts the sqlplus \"GUI\". In order to
253 start the sqlplus console, use \"plus33\" or something similar. You
254 will find the file in your Orant\\bin directory.
256 The program can also specify a TCP connection. See `make-comint'."
257 :type 'file
258 :group 'SQL)
260 (defcustom sql-oracle-options nil
261 "*List of additional options for `sql-oracle-program'."
262 :type '(repeat string)
263 :version "20.8"
264 :group 'SQL)
266 ;; Customisation for MySql
268 (defcustom sql-mysql-program "mysql"
269 "*Command to start mysql by TcX.
271 Starts `sql-interactive-mode' after doing some setup.
273 The program can also specify a TCP connection. See `make-comint'."
274 :type 'file
275 :group 'SQL)
277 ;; Customisation for Solid
279 (defcustom sql-solid-program "solsql"
280 "*Command to start SOLID SQL Editor.
282 Starts `sql-interactive-mode' after doing some setup.
284 The program can also specify a TCP connection. See `make-comint'."
285 :type 'file
286 :group 'SQL)
288 ;; Customisation for SyBase
290 (defcustom sql-sybase-program "isql"
291 "*Command to start isql by SyBase.
293 Starts `sql-interactive-mode' after doing some setup.
295 The program can also specify a TCP connection. See `make-comint'."
296 :type 'file
297 :group 'SQL)
299 ;; Customisation for Informix
301 (defcustom sql-informix-program "dbaccess"
302 "*Command to start dbaccess by Informix.
304 Starts `sql-interactive-mode' after doing some setup.
306 The program can also specify a TCP connection. See `make-comint'."
307 :type 'file
308 :group 'SQL)
310 ;; Customisation for Ingres
312 (defcustom sql-ingres-program "sql"
313 "*Command to start sql by Ingres.
315 Starts `sql-interactive-mode' after doing some setup.
317 The program can also specify a TCP connection. See `make-comint'."
318 :type 'file
319 :group 'SQL)
321 ;; Customisation for Microsoft
323 (defcustom sql-ms-program "isql"
324 "*Command to start isql by Microsoft.
326 Starts `sql-interactive-mode' after doing some setup.
328 The program can also specify a TCP connection. See `make-comint'."
329 :type 'file
330 :group 'SQL)
332 ;; Customisation for Postgres
334 (defcustom sql-postgres-program "psql"
335 "Command to start psql by Postgres.
337 Starts `sql-interactive-mode' after doing some setup.
339 The program can also specify a TCP connection. See `make-comint'."
340 :type 'file
341 :group 'SQL)
345 ;;; Variables which do not need customization
347 (defvar sql-user-history nil
348 "History of usernames used.")
350 (defvar sql-database-history nil
351 "History of databases used.")
353 (defvar sql-server-history nil
354 "History of servers used.")
356 ;; Passwords are not kept in a history.
358 (defvar sql-buffer nil
359 "Current SQLi buffer.
361 The global value of sql-buffer is the name of the latest SQLi buffer
362 created. Any SQL buffer created will make a local copy of this value.
363 See `sql-interactive-mode' for more on multiple sessions. If you want
364 to change the SQLi buffer a SQL mode sends its SQL strings to, change
365 the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
367 (defvar sql-prompt-regexp nil
368 "Prompt used to initialize `comint-prompt-regexp'.
370 You can change `comint-prompt-regexp' on `sql-interactive-mode-hook'.")
372 (defvar sql-prompt-length 0
373 "Prompt used to set `left-margin' in `sql-interactive-mode'.
375 You can change it on `sql-interactive-mode-hook'.")
377 (defvar sql-alternate-buffer-name nil
378 "Buffer-local string used to possibly rename the SQLi buffer.
380 Used by `sql-rename-buffer'.")
382 ;; Keymap for sql-interactive-mode.
384 (defvar sql-interactive-mode-map
385 (let ((map (make-sparse-keymap)))
386 (if (functionp 'set-keymap-parent)
387 (set-keymap-parent map comint-mode-map); Emacs
388 (set-keymap-parents map (list comint-mode-map))); XEmacs
389 (if (functionp 'set-keymap-name)
390 (set-keymap-name map 'sql-interactive-mode-map)); XEmacs
391 (define-key map (kbd "C-j") 'sql-accumulate-and-indent)
392 (define-key map (kbd "C-c C-w") 'sql-copy-column)
393 (define-key map (kbd "O") 'sql-magic-go)
394 (define-key map (kbd "o") 'sql-magic-go)
395 (define-key map (kbd ";") 'sql-magic-semicolon)
396 map)
397 "Mode map used for `sql-interactive-mode'.
398 Based on `comint-mode-map'.")
400 ;; Keymap for sql-mode.
402 (defvar sql-mode-map
403 (let ((map (make-sparse-keymap)))
404 (define-key map (kbd "C-c C-c") 'sql-send-paragraph)
405 (define-key map (kbd "C-c C-r") 'sql-send-region)
406 (define-key map (kbd "C-c C-b") 'sql-send-buffer)
407 map)
408 "Mode map used for `sql-mode'.")
410 ;; easy menu for sql-mode.
412 (easy-menu-define
413 sql-mode-menu sql-mode-map
414 "Menu for `sql-mode'."
415 '("SQL"
416 ["Send Paragraph" sql-send-paragraph (and (buffer-live-p sql-buffer)
417 (get-buffer-process sql-buffer))]
418 ["Send Region" sql-send-region (and mark-active
419 (buffer-live-p sql-buffer)
420 (get-buffer-process sql-buffer))]
421 ["Send Buffer" sql-send-buffer (and (buffer-live-p sql-buffer)
422 (get-buffer-process sql-buffer))]
423 ["Show SQLi buffer" sql-show-sqli-buffer t]
424 ["Set SQLi buffer" sql-set-sqli-buffer t]
425 ["Pop to SQLi buffer after send"
426 sql-toggle-pop-to-buffer-after-send-region
427 :style toggle
428 :selected sql-pop-to-buffer-after-send-region]))
430 ;; easy menu for sql-interactive-mode.
432 (easy-menu-define
433 sql-interactive-mode-menu sql-interactive-mode-map
434 "Menu for `sql-interactive-mode'."
435 '("SQL"
436 ["Rename Buffer" sql-rename-buffer t]))
438 ;; Abbreviations -- if you want more of them, define them in your
439 ;; ~/.emacs file. Abbrevs have to be enabled in your ~/.emacs, too.
441 (defvar sql-mode-abbrev-table nil
442 "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
443 (if sql-mode-abbrev-table
445 (let ((wrapper))
446 (define-abbrev-table 'sql-mode-abbrev-table ())
447 (define-abbrev sql-mode-abbrev-table "ins" "insert" nil)
448 (define-abbrev sql-mode-abbrev-table "upd" "update" nil)
449 (define-abbrev sql-mode-abbrev-table "del" "delete" nil)
450 (define-abbrev sql-mode-abbrev-table "sel" "select" nil)))
452 ;; Syntax Table
454 (defvar sql-mode-syntax-table
455 (let ((table (make-syntax-table)))
456 ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
457 (modify-syntax-entry ?/ ". 14" table)
458 (modify-syntax-entry ?* ". 23" table)
459 ;; double-dash starts comment
460 (if (string-match "XEmacs\\|Lucid" emacs-version)
461 (modify-syntax-entry ?- ". 56" table)
462 (modify-syntax-entry ?- ". 12b" table))
463 ;; newline and formfeed end coments
464 (modify-syntax-entry ?\n "> b" table)
465 (modify-syntax-entry ?\f "> b" table)
466 ;; single quotes (') quotes delimit strings
467 (modify-syntax-entry ?' "\"" table)
468 table)
469 "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
471 ;; Font lock support
473 (defvar sql-mode-ansi-font-lock-keywords nil
474 "ANSI SQL keywords used by font-lock.
476 This variable is used by `sql-mode' and `sql-interactive-mode'. The
477 regular expressions are created during compilation by calling the
478 function `regexp-opt'. Therefore, take a look at the source before
479 you define your own sql-mode-ansi-font-lock-keywords. You may want to
480 add functions and PL/SQL keywords.")
481 (if sql-mode-ansi-font-lock-keywords
483 (let ((ansi-keywords (eval-when-compile
484 (concat "\\b"
485 (regexp-opt '(
486 "authorization" "avg" "begin" "close" "cobol" "commit"
487 "continue" "count" "declare" "double" "end" "escape"
488 "exec" "fetch" "foreign" "fortran" "found" "go" "goto" "indicator"
489 "key" "language" "max" "min" "module" "numeric" "open" "pascal" "pli"
490 "precision" "primary" "procedure" "references" "rollback"
491 "schema" "section" "some" "sqlcode" "sqlerror" "sum" "work") t) "\\b")))
492 (ansi-reserved-words (eval-when-compile
493 (concat "\\b"
494 (regexp-opt '(
495 "all" "and" "any" "as" "asc" "between" "by" "check" "create"
496 "current" "default" "delete" "desc" "distinct" "exists" "float" "for"
497 "from" "grant" "group" "having" "in" "insert" "into" "is"
498 "like" "not" "null" "of" "on" "option" "or" "order" "privileges"
499 "public" "select" "set" "table" "to" "union" "unique"
500 "update" "user" "values" "view" "where" "with") t) "\\b")))
501 (ansi-types (eval-when-compile
502 (concat "\\b"
503 (regexp-opt '(
504 ;; ANSI Keywords that look like types
505 "character" "cursor" "dec" "int" "real"
506 ;; ANSI Reserved Word that look like types
507 "char" "integer" "smallint" ) t) "\\b"))))
508 (setq sql-mode-ansi-font-lock-keywords
509 (list (cons ansi-keywords 'font-lock-function-name-face)
510 (cons ansi-reserved-words 'font-lock-keyword-face)
511 (cons ansi-types 'font-lock-type-face)))))
513 (defvar sql-mode-oracle-font-lock-keywords nil
514 "Oracle SQL keywords used by font-lock.
516 This variable is used by `sql-mode' and `sql-interactive-mode'. The
517 regular expressions are created during compilation by calling the
518 function `regexp-opt'. Therefore, take a look at the source before
519 you define your own sql-mode-oracle-font-lock-keywords. You may want
520 to add functions and PL/SQL keywords.")
521 (if sql-mode-oracle-font-lock-keywords
523 (let ((oracle-keywords (eval-when-compile
524 (concat "\\b"
525 (regexp-opt '(
526 "admin" "after" "allocate" "analyze" "archive" "archivelog" "backup"
527 "become" "before" "block" "body" "cache" "cancel" "cascade" "change"
528 "checkpoint" "compile" "constraint" "constraints" "contents"
529 "controlfile" "cycle" "database" "datafile" "dba" "disable" "dismount"
530 "dump" "each" "else" "elsif" "enable" "events" "except" "exceptions"
531 "execute" "exit" "explain" "extent" "externally" "false" "flush" "force"
532 "freelist" "freelists" "function" "groups" "if" "including" "initrans"
533 "instance" "layer" "link" "lists" "logfile" "loop" "manage" "manual"
534 "maxdatafiles" "maxinistances" "maxlogfiles" "maxloghistory"
535 "maxlogmembers" "maxtrans" "maxvalue" "minextents" "minvalue" "mount"
536 "new" "next" "noarchivelog" "nocache" "nocycle" "nomaxvalue"
537 "nominvalue" "none" "noorder" "noresetlogs" "normal" "nosort" "off"
538 "old" "only" "optimal" "others" "out" "own" "package" "parallel"
539 "pctincrease" "pctused" "plan" "pragma" "private" "profile" "quota"
540 "raise" "read" "recover" "referencing" "resetlogs" "restrict_references"
541 "restricted" "return" "returning" "reuse" "rnds" "rnps" "role" "roles"
542 "savepoint" "scn" "segment" "sequence" "shared" "snapshot" "sort"
543 "statement_id" "statistics" "stop" "storage" "subtype" "switch" "system"
544 "tables" "tablespace" "temporary" "thread" "time" "tracing"
545 "transaction" "triggers" "true" "truncate" "type" "under" "unlimited"
546 "until" "use" "using" "when" "while" "wnds" "wnps" "write") t) "\\b")))
547 (oracle-warning-words (eval-when-compile
548 (concat "\\b"
549 (regexp-opt '(
550 "cursor_already_open" "dup_val_on_index" "exception" "invalid_cursor"
551 "invalid_number" "login_denied" "no_data_found" "not_logged_on"
552 "notfound" "others" "pragma" "program_error" "storage_error"
553 "timeout_on_resource" "too_many_rows" "transaction_backed_out"
554 "value_error" "zero_divide") t) "\\b")))
555 (oracle-reserved-words (eval-when-compile
556 (concat "\\b"
557 (regexp-opt '(
558 "access" "add" "alter" "audit" "cluster" "column" "comment" "compress"
559 "connect" "drop" "else" "exclusive" "file" "grant"
560 "identified" "immediate" "increment" "index" "initial" "intersect"
561 "level" "lock" "long" "maxextents" "minus" "mode" "modify" "noaudit"
562 "nocompress" "nowait" "number" "offline" "online" "pctfree" "prior"
563 "raw" "rename" "resource" "revoke" "row" "rowlabel" "rownum"
564 "rows" "session" "share" "size" "start" "successful" "synonym" "sysdate"
565 "then" "trigger" "uid" "validate" "whenever") t) "\\b")))
566 (oracle-types (eval-when-compile
567 (concat "\\b"
568 (regexp-opt '(
569 ;; Oracle Keywords that look like types
570 ;; Oracle Reserved Words that look like types
571 "binary_integer" "blob" "boolean" "constant" "date" "decimal" "rowid"
572 "varchar" "varchar2") t) "\\b")))
573 (oracle-builtin-functions (eval-when-compile
574 (concat "\\b"
575 (regexp-opt '(
576 ;; Misc Oracle builtin functions
577 "abs" "add_months" "ascii" "avg" "ceil" "chartorowid" "chr" "concat"
578 "convert" "cos" "cosh" "count" "currval" "decode" "dump" "exp" "floor"
579 "glb" "greatest" "greatest_lb" "hextoraw" "initcap" "instr" "instrb"
580 "last_day" "least" "least_ub" "length" "lengthb" "ln" "log" "lower"
581 "lpad" "ltrim" "lub" "max" "min" "mod" "months_between" "new_time"
582 "next_day" "nextval" "nls_initcap" "nls_lower" "nls_upper" "nlssort"
583 "nvl" "power" "rawtohex" "replace" "round" "rowidtochar" "rpad"
584 "rtrim" "sign" "sin" "sinh" "soundex" "sqlcode" "sqlerrm" "sqrt"
585 "stddev" "sum" "substr" "substrb" "tan" "tanh" "to_char"
586 "to_date" "to_label" "to_multi_byte" "to_number" "to_single_byte"
587 "translate" "trim" "trunc" "uid" "upper" "userenv" "variance" "vsize") t) "\\b"))))
588 (setq sql-mode-oracle-font-lock-keywords
589 (append sql-mode-ansi-font-lock-keywords
590 (list (cons oracle-keywords 'font-lock-function-name-face)
591 (cons oracle-warning-words 'font-lock-warning-face)
592 (cons oracle-reserved-words 'font-lock-keyword-face)
593 ;; XEmacs doesn't have font-lock-builtin-face
594 (if (string-match "XEmacs\\|Lucid" emacs-version)
595 (cons oracle-builtin-functions 'font-lock-preprocessor-face)
596 ;; GNU Emacs 19 doesn't have it either
597 (if (string-match "GNU Emacs 19" emacs-version)
598 (cons oracle-builtin-functions 'font-lock-function-name-face)
599 ;; Emacs
600 (cons oracle-builtin-functions 'font-lock-builtin-face)))
601 (cons oracle-types 'font-lock-type-face))))))
603 (defvar sql-mode-postgres-font-lock-keywords nil
604 "Postgres SQL keywords used by font-lock.
606 This variable is used by `sql-mode' and `sql-interactive-mode'. The
607 regular expressions are created during compilation by calling the
608 function `regexp-opt'. Therefore, take a look at the source before
609 you define your own sql-mode-postgres-font-lock-keywords.")
611 (if sql-mode-postgres-font-lock-keywords
613 (let ((postgres-reserved-words (eval-when-compile
614 (concat "\\b"
615 (regexp-opt '(
616 "language"
617 ) t) "\\b")))
618 (postgres-types (eval-when-compile
619 (concat "\\b"
620 (regexp-opt '(
621 "bool" "box" "circle" "char" "char2" "char4" "char8" "char16" "date"
622 "float4" "float8" "int2" "int4" "int8" "line" "lseg" "money" "path"
623 "point" "polygon" "serial" "text" "time" "timespan" "timestamp" "varchar"
624 ) t)"\\b")))
625 (postgres-builtin-functions (eval-when-compile
626 (concat "\\b"
627 (regexp-opt '(
628 ;; Misc Postgres builtin functions
629 "abstime" "age" "area" "box" "center" "date_part" "date_trunc"
630 "datetime" "dexp" "diameter" "dpow" "float" "float4" "height"
631 "initcap" "integer" "isclosed" "isfinite" "isoldpath" "isopen"
632 "length" "lower" "lpad" "ltrim" "pclose" "point" "points" "popen"
633 "position" "radius" "reltime" "revertpoly" "rpad" "rtrim" "substr"
634 "substring" "text" "timespan" "translate" "trim" "upgradepath"
635 "upgradepoly" "upper" "varchar" "width"
636 ) t) "\\b"))))
637 (setq sql-mode-postgres-font-lock-keywords
638 (append sql-mode-ansi-font-lock-keywords
639 (list (cons postgres-reserved-words 'font-lock-keyword-face)
640 ;; XEmacs doesn't have 'font-lock-builtin-face
641 (if (string-match "XEmacs\\|Lucid" emacs-version)
642 (cons postgres-builtin-functions 'font-lock-preprocessor-face)
643 ;; Emacs
644 (cons postgres-builtin-functions 'font-lock-builtin-face))
645 (cons postgres-types 'font-lock-type-face))))))
648 (defvar sql-mode-font-lock-keywords sql-mode-ansi-font-lock-keywords
649 "SQL keywords used by font-lock.
651 This variable defaults to `sql-mode-ansi-font-lock-keywords'. This is
652 used for the default `font-lock-defaults' value in `sql-mode'. This
653 can be changed by some entry functions to provide more hilighting.")
657 ;;; Compatibility functions
659 (if (not (fboundp 'comint-line-beginning-position))
660 ;; comint-line-beginning-position is defined in Emacs 21
661 (defun comint-line-beginning-position ()
662 "Returns the buffer position of the beginning of the line, after any prompt.
663 The prompt is assumed to be any text at the beginning of the line matching
664 the regular expression `comint-prompt-regexp', a buffer local variable."
665 (save-excursion (comint-bol nil) (point))))
669 ;;; Small functions
671 (defun sql-magic-go (arg)
672 "Insert \"o\" and call `comint-send-input'.
673 `sql-electric-stuff' must be the symbol `go'."
674 (interactive "P")
675 (self-insert-command (prefix-numeric-value arg))
676 (if (and (equal sql-electric-stuff 'go)
677 (save-excursion
678 (comint-bol nil)
679 (looking-at "go\\b")))
680 (comint-send-input)))
682 (defun sql-magic-semicolon (arg)
683 "Insert semicolon and call `comint-send-input'.
684 `sql-electric-stuff' must be the symbol `semicolon'."
685 (interactive "P")
686 (self-insert-command (prefix-numeric-value arg))
687 (if (equal sql-electric-stuff 'semicolon)
688 (comint-send-input)))
690 (defun sql-accumulate-and-indent ()
691 "Continue SQL statement on the next line."
692 (interactive)
693 (if (fboundp 'comint-accumulate)
694 (comint-accumulate)
695 (newline))
696 (indent-according-to-mode))
698 ;;;###autoload
699 (defun sql-help ()
700 "Show short help for the SQL modes.
702 Use an entry function to open an interactive SQL buffer. This buffer is
703 usually named `*SQL*'. The name of the major mode is SQLi.
705 Use the following commands to start a specific SQL interpreter:
707 PostGres: \\[sql-postgres]
709 Other non-free SQL implementations are also supported:
711 MySQL: \\[sql-mysql]
712 Solid: \\[sql-solid]
713 Oracle: \\[sql-oracle]
714 Informix: \\[sql-informix]
715 Sybase: \\[sql-sybase]
716 Ingres: \\[sql-ingres]
717 Microsoft: \\[sql-ms]
719 But we urge you to choose a free implementation instead of these.
721 Once you have the SQLi buffer, you can enter SQL statements in the
722 buffer. The output generated is appended to the buffer and a new prompt
723 is generated. See the In/Out menu in the SQLi buffer for some functions
724 that help you navigate through the buffer, the input history, etc.
726 If you have a really complex SQL statement or if you are writing a
727 procedure, you can do this in a separate buffer. Put the new buffer in
728 `sql-mode' by calling \\[sql-mode]. The name of this buffer can be
729 anything. The name of the major mode is SQL.
731 In this SQL buffer (SQL mode), you can send the region or the entire
732 buffer to the interactive SQL buffer (SQLi mode). The results are
733 appended to the SQLi buffer without disturbing your SQL buffer."
734 (interactive)
735 (describe-function 'sql-help))
737 (defun sql-read-passwd (prompt &optional default)
738 "Read a password using PROMPT.
739 Optional DEFAULT is password to start with. This function calls
740 `read-passwd' if it is available. If not, function
741 `ange-ftp-read-passwd' is called. This should always be available,
742 even in old versions of Emacs."
743 (if (fboundp 'read-passwd)
744 (read-passwd prompt nil default)
745 (unless (fboundp 'ange-ftp-read-passwd)
746 (autoload 'ange-ftp-read-passwd "ange-ftp"))
747 (ange-ftp-read-passwd prompt default)))
749 (defun sql-get-login (&rest what)
750 "Get username, password and database from the user.
752 The variables `sql-user', `sql-password', `sql-server', and
753 `sql-database' can be customised. They are used as the default values.
754 Usernames, servers and databases are stored in `sql-user-history',
755 `sql-server-history' and `database-history'. Passwords are not stored
756 in a history.
758 Parameter WHAT is a list of the arguments passed to this function.
759 The function asks for the username if WHAT contains symbol `user', for
760 the password if it contains symbol `password', for the server if it
761 contains symbol `server', and for the database if it contains symbol
762 `database'.
764 In order to ask the user for username, password and database, call the
765 function like this: (sql-get-login 'user 'password 'database)."
766 (interactive)
767 (if (memq 'user what)
768 (setq sql-user
769 (read-from-minibuffer "User: " sql-user nil nil
770 sql-user-history)))
771 (if (memq 'password what)
772 (setq sql-password
773 (sql-read-passwd "Password: " sql-password)))
774 (if (memq 'server what)
775 (setq sql-server
776 (read-from-minibuffer "Server: " sql-server nil nil
777 sql-server-history)))
778 (if (memq 'database what)
779 (setq sql-database
780 (read-from-minibuffer "Database: " sql-database nil nil
781 sql-database-history))))
783 (defun sql-find-sqli-buffer ()
784 "Return the current default SQLi buffer or nil.
785 In order to qualify, the SQLi buffer must be alive,
786 be in `sql-interactive-mode' and have a process."
787 (let ((default-buffer (default-value 'sql-buffer)))
788 (if (and (buffer-live-p default-buffer)
789 (get-buffer-process default-buffer))
790 default-buffer
791 (save-excursion
792 (let ((buflist (buffer-list))
793 (found))
794 (while (not (or (null buflist)
795 found))
796 (let ((candidate (car buflist)))
797 (set-buffer candidate)
798 (if (and (equal major-mode 'sql-interactive-mode)
799 (get-buffer-process candidate))
800 (setq found candidate))
801 (setq buflist (cdr buflist))))
802 found)))))
804 (defun sql-set-sqli-buffer-generally ()
805 "Set SQLi buffer for all SQL buffers that have none.
806 This function checks all SQL buffers for their SQLi buffer. If their
807 SQLi buffer is nonexistent or has no process, it is set to the current
808 default SQLi buffer. The current default SQLi buffer is determined
809 using `sql-find-sqli-buffer'. If `sql-buffer' is set,
810 `sql-set-sqli-hook' is run."
811 (interactive)
812 (save-excursion
813 (let ((buflist (buffer-list))
814 (default-sqli-buffer (sql-find-sqli-buffer)))
815 (setq-default sql-buffer default-sqli-buffer)
816 (while (not (null buflist))
817 (let ((candidate (car buflist)))
818 (set-buffer candidate)
819 (if (and (equal major-mode 'sql-mode)
820 (not (buffer-live-p sql-buffer)))
821 (progn
822 (setq sql-buffer default-sqli-buffer)
823 (run-hooks 'sql-set-sqli-hook))))
824 (setq buflist (cdr buflist))))))
826 (defun sql-set-sqli-buffer ()
827 "Set the SQLi buffer SQL strings are sent to.
829 Call this function in a SQL buffer in order to set the SQLi buffer SQL
830 strings are sent to. Calling this function sets `sql-buffer' and runs
831 `sql-set-sqli-hook'.
833 If you call it from a SQL buffer, this sets the local copy of
834 `sql-buffer'.
836 If you call it from anywhere else, it sets the global copy of
837 `sql-buffer'."
838 (interactive)
839 (let ((default-buffer (sql-find-sqli-buffer)))
840 (if (null default-buffer)
841 (error "There is no suitable SQLi buffer"))
842 (let ((new-buffer
843 (get-buffer
844 (read-buffer "New SQLi buffer: " default-buffer t))))
845 (if (null (get-buffer-process new-buffer))
846 (error "Buffer %s has no process" (buffer-name new-buffer)))
847 (if (null (save-excursion
848 (set-buffer new-buffer)
849 (equal major-mode 'sql-interactive-mode)))
850 (error "Buffer %s is no SQLi buffer" (buffer-name new-buffer)))
851 (if new-buffer
852 (progn
853 (setq sql-buffer new-buffer)
854 (run-hooks 'sql-set-sqli-hook))))))
856 (defun sql-show-sqli-buffer ()
857 "Show the name of current SQLi buffer.
859 This is the buffer SQL strings are sent to. It is stored in the
860 variable `sql-buffer'. See `sql-help' on how to create such a buffer."
861 (interactive)
862 (if (null (buffer-live-p sql-buffer))
863 (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
864 (if (null (get-buffer-process sql-buffer))
865 (message "Buffer %s has no process." (buffer-name sql-buffer))
866 (message "Current SQLi buffer is %s." (buffer-name sql-buffer)))))
868 (defun sql-make-alternate-buffer-name ()
869 "Return a string that can be used to rename a SQLi buffer.
871 This is used to set `sql-alternate-buffer-name' within
872 `sql-interactive-mode'."
873 (concat (if (string= "" sql-user)
874 (if (string= "" (user-login-name))
876 (concat (user-login-name) "/"))
877 (concat sql-user "/"))
878 (if (string= "" sql-database)
879 (if (string= "" sql-server)
880 (system-name)
881 sql-server)
882 sql-database)))
884 (defun sql-rename-buffer ()
885 "Renames a SQLi buffer."
886 (interactive)
887 (rename-buffer (format "*SQL: %s*" sql-alternate-buffer-name) t))
889 (defun sql-copy-column ()
890 "Copy current column to the end of buffer.
891 Inserts SELECT or commas if appropriate."
892 (interactive)
893 (let ((column))
894 (save-excursion
895 (setq column (buffer-substring
896 (progn (forward-char 1) (backward-sexp 1) (point))
897 (progn (forward-sexp 1) (point))))
898 (goto-char (point-max))
899 (let ((bol (comint-line-beginning-position)))
900 (cond
901 ;; if empty command line, insert SELECT
902 ((= bol (point))
903 (insert "SELECT "))
904 ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
905 ((save-excursion
906 (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
907 bol t))
908 (insert ", "))
909 ;; else insert a space
911 (if (eq (preceding-char) ? )
913 (insert " ")))))
914 ;; in any case, insert the column
915 (insert column)
916 (message "%s" column))))
918 ;; On NT, SQL*Plus for Oracle turns on full buffering for stdout if it
919 ;; is not attached to a character device; therefore placeholder
920 ;; replacement by SQL*Plus is fully buffered. The workaround lets
921 ;; Emacs query for the placeholders.
923 (defvar sql-placeholder-history nil
924 "History of placeholder values used.")
926 (defun sql-query-placeholders-and-send (proc string)
927 "Send to PROC input STRING, maybe replacing placeholders.
928 Placeholders are words starting with and ampersand like &this.
929 This function is used for `comint-input-sender' if using `sql-oracle' on NT."
930 (while (string-match "&\\(\\sw+\\)" string)
931 (setq string (replace-match
932 (read-from-minibuffer
933 (format "Enter value for %s: " (match-string 1 string))
934 nil nil nil sql-placeholder-history)
935 t t string)))
936 (comint-send-string proc string)
937 (comint-send-string proc "\n"))
941 ;;; Sending the region to the SQLi buffer.
943 (defun sql-send-region (start end)
944 "Send a region to the SQL process."
945 (interactive "r")
946 (if (buffer-live-p sql-buffer)
947 (save-excursion
948 (comint-send-region sql-buffer start end)
949 (if (string-match "\n$" (buffer-substring start end))
951 (comint-send-string sql-buffer "\n"))
952 (message "Sent string to buffer %s." (buffer-name sql-buffer))
953 (if sql-pop-to-buffer-after-send-region
954 (pop-to-buffer sql-buffer)
955 (display-buffer sql-buffer)))
956 (message "No SQL process started.")))
958 (defun sql-send-paragraph ()
959 "Send the current paragraph to the SQL process."
960 (interactive)
961 (let ((start (save-excursion
962 (backward-paragraph)
963 (point)))
964 (end (save-excursion
965 (forward-paragraph)
966 (point))))
967 (sql-send-region start end)))
969 (defun sql-send-buffer ()
970 "Send the buffer contents to the SQL process."
971 (interactive)
972 (sql-send-region (point-min) (point-max)))
974 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
975 "Toggle `sql-pop-to-buffer-after-send-region'.
977 If given the optional parameter VALUE, sets
978 sql-toggle-pop-to-buffer-after-send-region to VALUE."
979 (interactive "P")
980 (if value
981 (setq sql-pop-to-buffer-after-send-region value)
982 (setq sql-pop-to-buffer-after-send-region
983 (null sql-pop-to-buffer-after-send-region ))))
987 ;;; SQL mode -- uses SQL interactive mode
989 ;;;###autoload
990 (defun sql-mode ()
991 "Major mode to edit SQL.
993 You can send SQL statements to the SQLi buffer using
994 \\[sql-send-region]. Such a buffer must exist before you can do this.
995 See `sql-help' on how to create SQLi buffers.
997 \\{sql-mode-map}
998 Customization: Entry to this mode runs the `sql-mode-hook'.
1000 When you put a buffer in SQL mode, the buffer stores the last SQLi
1001 buffer created as its destination in the variable `sql-buffer'. This
1002 will be the buffer \\[sql-send-region] sends the region to. If this
1003 SQLi buffer is killed, \\[sql-send-region] is no longer able to
1004 determine where the strings should be sent to. You can set the
1005 value of `sql-buffer' using \\[sql-set-sqli-buffer].
1007 For information on how to create multiple SQLi buffers, see
1008 `sql-interactive-mode'."
1009 (interactive)
1010 (kill-all-local-variables)
1011 (setq major-mode 'sql-mode)
1012 (setq mode-name "SQL")
1013 (use-local-map sql-mode-map)
1014 (set-syntax-table sql-mode-syntax-table)
1015 (make-local-variable 'font-lock-defaults)
1016 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1017 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1018 ;; will have just one quote. Therefore syntactic hilighting is
1019 ;; disabled for interactive buffers. `_' and `.' are considered part
1020 ;; of words.
1021 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1022 nil t ((?_ . "w") (?. . "w"))))
1023 (make-local-variable 'comment-start)
1024 (setq comment-start "--")
1025 ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
1026 (make-local-variable 'sql-buffer)
1027 ;; Add imenu support for sql-mode. Note that imenu-generic-expression
1028 ;; is buffer-local, so we don't need a local-variable for it. SQL is
1029 ;; case-insensitive, that's why we have to set imenu-case-fold-search.
1030 ;; imenu-syntax-alist makes sure that `_' is considered part of object
1031 ;; names.
1032 (setq imenu-generic-expression sql-imenu-generic-expression
1033 imenu-case-fold-search t
1034 imenu-syntax-alist '(("_" . "w")))
1035 ;; Make `sql-send-paragraph' work on paragraphs that contain indented
1036 ;; lines.
1037 (make-local-variable 'paragraph-separate)
1038 (make-local-variable 'paragraph-start)
1039 (setq paragraph-separate "[\f]*$"
1040 paragraph-start "[\n\f]")
1041 ;; Abbrevs
1042 (setq local-abbrev-table sql-mode-abbrev-table)
1043 (setq abbrev-all-caps 1)
1044 ;; Run hook
1045 (run-hooks 'sql-mode-hook))
1049 ;;; SQL interactive mode
1051 (put 'sql-interactive-mode 'mode-class 'special)
1053 (defun sql-interactive-mode ()
1054 "Major mode to use a SQL interpreter interactively.
1056 Do not call this function by yourself. The environment must be
1057 initialized by an entry function specific for the SQL interpreter. See
1058 `sql-help' for a list of available entry functions.
1060 \\[comint-send-input] after the end of the process' output sends the
1061 text from the end of process to the end of the current line.
1062 \\[comint-send-input] before end of process output copies the current
1063 line minus the prompt to the end of the buffer and sends it.
1064 \\[comint-copy-old-input] just copies the current line.
1065 Use \\[sql-accumulate-and-indent] to enter multi-line statements.
1067 If you want to make multiple SQL buffers, rename the `*SQL*' buffer
1068 using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
1069 See `sql-help' for a list of available entry functions. The last buffer
1070 created by such an entry function is the current SQLi buffer. SQL
1071 buffers will send strings to the SQLi buffer current at the time of
1072 their creation. See `sql-mode' for details.
1074 Sample session using two connections:
1076 1. Create first SQLi buffer by calling an entry function.
1077 2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
1078 3. Create a SQL buffer \"test1.sql\".
1079 4. Create second SQLi buffer by calling an entry function.
1080 5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
1081 6. Create a SQL buffer \"test2.sql\".
1083 Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
1084 buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
1085 will send the region to buffer \"*Connection 2*\".
1087 If you accidentally suspend your process, use \\[comint-continue-subjob]
1088 to continue it. On some operating systems, this will not work because
1089 the signals are not supported.
1091 \\{sql-interactive-mode-map}
1092 Customization: Entry to this mode runs the hooks on `comint-mode-hook'
1093 and `sql-interactive-mode-hook' (in that order). Before each input, the
1094 hooks on `comint-input-filter-functions' are run. After each SQL
1095 interpreter output, the hooks on `comint-output-filter-functions' are
1096 run.
1098 Variable `sql-input-ring-file-name' controls the initialisation of the
1099 input ring history.
1101 Variables `comint-output-filter-functions', a hook, and
1102 `comint-scroll-to-bottom-on-input' and
1103 `comint-scroll-to-bottom-on-output' control whether input and output
1104 cause the window to scroll to the end of the buffer.
1106 If you want to make SQL buffers limited in length, add the function
1107 `comint-truncate-buffer' to `comint-output-filter-functions'.
1109 Here is an example for your .emacs file. It keeps the SQLi buffer a
1110 certain length.
1112 \(add-hook 'sql-interactive-mode-hook
1113 \(function (lambda ()
1114 \(setq comint-output-filter-functions 'comint-truncate-buffer))))
1116 Here is another example. It will always put point back to the statement
1117 you entered, right above the output it created.
1119 \(setq comint-output-filter-functions
1120 \(function (lambda (STR) (comint-show-output))))"
1121 (comint-mode)
1122 (setq comint-prompt-regexp sql-prompt-regexp)
1123 (setq left-margin sql-prompt-length)
1124 (setq major-mode 'sql-interactive-mode)
1125 (setq mode-name "SQLi")
1126 (use-local-map sql-interactive-mode-map)
1127 (set-syntax-table sql-mode-syntax-table)
1128 (make-local-variable 'font-lock-defaults)
1129 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1130 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1131 ;; will have just one quote. Therefore syntactic hilighting is
1132 ;; disabled for interactive buffers. `_' and `.' are considered part
1133 ;; of words.
1134 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1135 t t ((?_ . "w") (?. . "w"))))
1136 ;; Enable commenting and uncommenting of the region.
1137 (make-local-variable 'comment-start)
1138 (setq comment-start "--")
1139 ;; Abbreviation table init and case-insensitive. It is not activatet
1140 ;; by default.
1141 (setq local-abbrev-table sql-mode-abbrev-table)
1142 (setq abbrev-all-caps 1)
1143 ;; Exiting the process will call sql-stop.
1144 (set-process-sentinel (get-buffer-process sql-buffer) 'sql-stop)
1145 ;; People wanting a different history file for each
1146 ;; buffer/process/client/whatever can change separator and file-name
1147 ;; on the sql-interactive-mode-hook.
1148 (setq comint-input-ring-separator sql-input-ring-separator
1149 comint-input-ring-file-name sql-input-ring-file-name)
1150 ;; Create a usefull name for renaming this buffer later.
1151 (make-local-variable 'sql-alternate-buffer-name)
1152 (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
1153 ;; User stuff.
1154 (run-hooks 'sql-interactive-mode-hook)
1155 ;; Calling the hook before calling comint-read-input-ring allows users
1156 ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
1157 (comint-read-input-ring t))
1159 (defun sql-stop (process event)
1160 "Called when the SQL process is stopped.
1162 Writes the input history to a history file using
1163 `comint-write-input-ring' and inserts a short message in the SQL buffer.
1164 `comint-comint-input-ring-file-name' is temporarily bound to
1165 `sql-input-ring-file-name'.
1167 This function is a sentinel watching the SQL interpreter process.
1168 Sentinels will always get the two parameters PROCESS and EVENT."
1169 (comint-write-input-ring)
1170 (if (and (eq (current-buffer) sql-buffer)
1171 (not buffer-read-only))
1172 (insert (format "\nProcess %s %s\n" process event))
1173 (message "Process %s %s" process event)))
1177 ;;; Entry functions for different SQL interpreters.
1179 ;;;###autoload
1180 (defun sql-oracle ()
1181 "Run sqlplus by Oracle as an inferior process.
1183 If buffer `*SQL*' exists but no process is running, make a new process.
1184 If buffer exists and a process is running, just switch to buffer
1185 `*SQL*'.
1187 Interpreter used comes from variable `sql-oracle-program'. Login uses
1188 the variables `sql-user', `sql-password', and `sql-database' as
1189 defaults, if set. Additional command line parameters can be stored in
1190 the list `sql-oracle-options'.
1192 The buffer is put in sql-interactive-mode, giving commands for sending
1193 input. See `sql-interactive-mode'.
1195 To specify a coding system for converting non-ASCII characters
1196 in the input and output to the process, use \\[universal-coding-system-argument]
1197 before \\[sql-oracle]. You can also specify this with \\[set-buffer-process-coding-system]
1198 in the SQL buffer, after you start the process.
1199 The default comes from `process-coding-system-alist' and
1200 `default-process-coding-system'.
1202 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1203 (interactive)
1204 (if (comint-check-proc "*SQL*")
1205 (pop-to-buffer "*SQL*")
1206 (sql-get-login 'user 'password 'database)
1207 (message "Login...")
1208 ;; Produce user/password@database construct. Password without user
1209 ;; is meaningless; database without user/password is meaningless,
1210 ;; because "@param" will ask sqlplus to interpret the script
1211 ;; "param".
1212 (let ((parameter nil))
1213 (if (not (string= "" sql-user))
1214 (if (not (string= "" sql-password))
1215 (setq parameter (concat sql-user "/" sql-password))
1216 (setq parameter sql-user)))
1217 (if (and parameter (not (string= "" sql-database)))
1218 (setq parameter (concat parameter "@" sql-database)))
1219 (if parameter
1220 (setq parameter (nconc (list parameter) sql-oracle-options))
1221 (setq parameter sql-oracle-options))
1222 (if parameter
1223 (set-buffer (apply 'make-comint "SQL" sql-oracle-program nil
1224 parameter))
1225 (set-buffer (make-comint "SQL" sql-oracle-program nil))))
1226 (setq sql-prompt-regexp "^SQL> ")
1227 (setq sql-prompt-length 5)
1228 (setq sql-buffer (current-buffer))
1229 ;; set sql-mode-font-lock-keywords to something different before
1230 ;; calling sql-interactive-mode.
1231 (setq sql-mode-font-lock-keywords sql-mode-oracle-font-lock-keywords)
1232 (sql-interactive-mode)
1233 ;; If running on NT, make sure we do placeholder replacement ourselves.
1234 (if (eq window-system 'w32)
1235 (setq comint-input-sender 'sql-query-placeholders-and-send))
1236 (message "Login...done")
1237 (pop-to-buffer sql-buffer)))
1241 ;;;###autoload
1242 (defun sql-sybase ()
1243 "Run isql by SyBase as an inferior process.
1245 If buffer `*SQL*' exists but no process is running, make a new process.
1246 If buffer exists and a process is running, just switch to buffer
1247 `*SQL*'.
1249 Interpreter used comes from variable `sql-sybase-program'. Login uses
1250 the variables `sql-user', `sql-password', and `sql-server' as
1251 defaults, if set.
1253 The buffer is put in sql-interactive-mode, giving commands for sending
1254 input. See `sql-interactive-mode'.
1256 To specify a coding system for converting non-ASCII characters
1257 in the input and output to the process, use \\[universal-coding-system-argument]
1258 before \\[sql-sybase]. You can also specify this with \\[set-buffer-process-coding-system]
1259 in the SQL buffer, after you start the process.
1260 The default comes from `process-coding-system-alist' and
1261 `default-process-coding-system'.
1263 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1264 (interactive)
1265 (if (comint-check-proc "*SQL*")
1266 (pop-to-buffer "*SQL*")
1267 (sql-get-login 'user 'password 'server)
1268 (message "Login...")
1269 ;; Put all parameters to the program (if defined) in a list and call
1270 ;; make-comint.
1271 (let ((params '("-w" "2048" "-n")))
1272 ;; There is no way to specify the database via command line
1273 ;; parameters. The -S option specifies the server.
1274 (if (not (string= "" sql-server))
1275 (setq params (append (list "-S" sql-server) params)))
1276 (if (not (string= "" sql-password))
1277 (setq params (append (list "-P" sql-password) params)))
1278 (if (not (string= "" sql-user))
1279 (setq params (append (list "-U" sql-user) params)))
1280 (set-buffer (apply 'make-comint "SQL" sql-sybase-program
1281 nil params)))
1282 (setq sql-prompt-regexp "^SQL> ")
1283 (setq sql-prompt-length 5)
1284 (setq sql-buffer (current-buffer))
1285 (sql-interactive-mode)
1286 (message "Login...done")
1287 (pop-to-buffer sql-buffer)))
1291 ;;;###autoload
1292 (defun sql-informix ()
1293 "Run dbaccess by Informix as an inferior process.
1295 If buffer `*SQL*' exists but no process is running, make a new process.
1296 If buffer exists and a process is running, just switch to buffer
1297 `*SQL*'.
1299 Interpreter used comes from variable `sql-informix-program'. Login uses
1300 the variable `sql-database' as default, if set.
1302 The buffer is put in sql-interactive-mode, giving commands for sending
1303 input. See `sql-interactive-mode'.
1305 To specify a coding system for converting non-ASCII characters
1306 in the input and output to the process, use \\[universal-coding-system-argument]
1307 before \\[sql-informix]. You can also specify this with \\[set-buffer-process-coding-system]
1308 in the SQL buffer, after you start the process.
1309 The default comes from `process-coding-system-alist' and
1310 `default-process-coding-system'.
1312 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1313 (interactive)
1314 (if (comint-check-proc "*SQL*")
1315 (pop-to-buffer "*SQL*")
1316 (sql-get-login 'database)
1317 (message "Login...")
1318 ;; username and password are ignored.
1319 (if (string= "" sql-database)
1320 (set-buffer (make-comint "SQL" sql-informix-program nil))
1321 (set-buffer (make-comint "SQL" sql-informix-program nil sql-database "-")))
1322 (setq sql-prompt-regexp "^SQL> ")
1323 (setq sql-prompt-length 5)
1324 (setq sql-buffer (current-buffer))
1325 (sql-interactive-mode)
1326 (message "Login...done")
1327 (pop-to-buffer sql-buffer)))
1331 ;;;###autoload
1332 (defun sql-mysql ()
1333 "Run mysql by TcX as an inferior process.
1335 Note that the widespread idea that mysql is free software is inaccurate;
1336 its license is too restrictive. We urge you to use PostGres instead.
1338 If buffer `*SQL*' exists but no process is running, make a new process.
1339 If buffer exists and a process is running, just switch to buffer
1340 `*SQL*'.
1342 Interpreter used comes from variable `sql-mysql-program'. Login uses
1343 the variables `sql-user', `sql-password', `sql-database', and
1344 `sql-server' as defaults, if set.
1346 The buffer is put in sql-interactive-mode, giving commands for sending
1347 input. See `sql-interactive-mode'.
1349 To specify a coding system for converting non-ASCII characters
1350 in the input and output to the process, use \\[universal-coding-system-argument]
1351 before \\[sql-mysql]. You can also specify this with \\[set-buffer-process-coding-system]
1352 in the SQL buffer, after you start the process.
1353 The default comes from `process-coding-system-alist' and
1354 `default-process-coding-system'.
1356 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1357 (interactive)
1358 (if (comint-check-proc "*SQL*")
1359 (pop-to-buffer "*SQL*")
1360 (sql-get-login 'user 'password 'database 'server)
1361 (message "Login...")
1362 ;; Put all parameters to the program (if defined) in a list and call
1363 ;; make-comint.
1364 (let ((params))
1365 (if (not (string= "" sql-database))
1366 (setq params (append (list sql-database) params)))
1367 (if (not (string= "" sql-server))
1368 (setq params (append (list (concat "--host=" sql-server)) params)))
1369 (if (not (string= "" sql-password))
1370 (setq params (append (list (concat "--password=" sql-password)) params)))
1371 (if (not (string= "" sql-user))
1372 (setq params (append (list (concat "--user=" sql-user)) params)))
1373 (set-buffer (apply 'make-comint "SQL" sql-mysql-program
1374 nil params)))
1375 (setq sql-prompt-regexp "^mysql>")
1376 (setq sql-prompt-length 6)
1377 (setq sql-buffer (current-buffer))
1378 (sql-interactive-mode)
1379 (message "Login...done")
1380 (pop-to-buffer sql-buffer)))
1384 ;;;###autoload
1385 (defun sql-solid ()
1386 "Run solsql by Solid as an inferior process.
1388 If buffer `*SQL*' exists but no process is running, make a new process.
1389 If buffer exists and a process is running, just switch to buffer
1390 `*SQL*'.
1392 Interpreter used comes from variable `sql-solid-program'. Login uses
1393 the variables `sql-user', `sql-password', and `sql-server' as
1394 defaults, if set.
1396 The buffer is put in sql-interactive-mode, giving commands for sending
1397 input. See `sql-interactive-mode'.
1399 To specify a coding system for converting non-ASCII characters
1400 in the input and output to the process, use \\[universal-coding-system-argument]
1401 before \\[sql-solid]. You can also specify this with \\[set-buffer-process-coding-system]
1402 in the SQL buffer, after you start the process.
1403 The default comes from `process-coding-system-alist' and
1404 `default-process-coding-system'.
1406 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1407 (interactive)
1408 (if (comint-check-proc "*SQL*")
1409 (pop-to-buffer "*SQL*")
1410 (sql-get-login 'user 'password 'server)
1411 (message "Login...")
1412 ;; Put all parameters to the program (if defined) in a list and call
1413 ;; make-comint.
1414 (let ((params))
1415 ;; It only makes sense if both username and password are there.
1416 (if (not (or (string= "" sql-user)
1417 (string= "" sql-password)))
1418 (setq params (append (list sql-user sql-password) params)))
1419 (if (not (string= "" sql-server))
1420 (setq params (append (list sql-server) params)))
1421 (set-buffer (apply 'make-comint "SQL" sql-solid-program
1422 nil params)))
1423 (setq sql-prompt-regexp "^")
1424 (setq sql-prompt-length 0)
1425 (setq sql-buffer (current-buffer))
1426 (sql-interactive-mode)
1427 (message "Login...done")
1428 (pop-to-buffer sql-buffer)))
1432 ;;;###autoload
1433 (defun sql-ingres ()
1434 "Run sql by Ingres as an inferior process.
1436 If buffer `*SQL*' exists but no process is running, make a new process.
1437 If buffer exists and a process is running, just switch to buffer
1438 `*SQL*'.
1440 Interpreter used comes from variable `sql-ingres-program'. Login uses
1441 the variable `sql-database' as default, if set.
1443 The buffer is put in sql-interactive-mode, giving commands for sending
1444 input. See `sql-interactive-mode'.
1446 To specify a coding system for converting non-ASCII characters
1447 in the input and output to the process, use \\[universal-coding-system-argument]
1448 before \\[sql-ingres]. You can also specify this with \\[set-buffer-process-coding-system]
1449 in the SQL buffer, after you start the process.
1450 The default comes from `process-coding-system-alist' and
1451 `default-process-coding-system'.
1453 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1454 (interactive)
1455 (if (comint-check-proc "*SQL*")
1456 (pop-to-buffer "*SQL*")
1457 (sql-get-login 'database)
1458 (message "Login...")
1459 ;; username and password are ignored.
1460 (if (string= "" sql-database)
1461 (set-buffer (make-comint "SQL" sql-ingres-program nil))
1462 (set-buffer (make-comint "SQL" sql-ingres-program nil sql-database)))
1463 (setq sql-prompt-regexp "^\* ")
1464 (setq sql-prompt-length 2)
1465 (setq sql-buffer (current-buffer))
1466 (sql-interactive-mode)
1467 (message "Login...done")
1468 (pop-to-buffer sql-buffer)))
1472 ;;;###autoload
1473 (defun sql-ms ()
1474 "Run isql by Microsoft as an inferior process.
1476 If buffer `*SQL*' exists but no process is running, make a new process.
1477 If buffer exists and a process is running, just switch to buffer
1478 `*SQL*'.
1480 Interpreter used comes from variable `sql-ms-program'. Login uses the
1481 variables `sql-user', `sql-password', `sql-database', and `sql-server'
1482 as defaults, if set.
1484 The buffer is put in sql-interactive-mode, giving commands for sending
1485 input. See `sql-interactive-mode'.
1487 To specify a coding system for converting non-ASCII characters
1488 in the input and output to the process, use \\[universal-coding-system-argument]
1489 before \\[sql-ms]. You can also specify this with \\[set-buffer-process-coding-system]
1490 in the SQL buffer, after you start the process.
1491 The default comes from `process-coding-system-alist' and
1492 `default-process-coding-system'.
1494 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1495 (interactive)
1496 (if (comint-check-proc "*SQL*")
1497 (pop-to-buffer "*SQL*")
1498 (sql-get-login 'user 'password 'database 'server)
1499 (message "Login...")
1500 ;; Put all parameters to the program (if defined) in a list and call
1501 ;; make-comint.
1502 (let ((params '("-w 300")))
1503 (if (not (string= "" sql-server))
1504 (setq params (append (list "-S" sql-server) params)))
1505 (if (not (string= "" sql-database))
1506 (setq params (append (list "-d" sql-database) params)))
1507 (if (not (string= "" sql-user))
1508 (setq params (append (list "-U" sql-user) params)))
1509 (if (not (string= "" sql-password))
1510 (setq params (append (list "-P" sql-password) params))
1511 ;; If -P is passed to ISQL as the last argument without a password,
1512 ;; it's considered null.
1513 (setq params (append params (list "-P"))))
1514 (set-buffer (apply 'make-comint "SQL" sql-ms-program
1515 nil params)))
1516 (setq sql-prompt-regexp "^[0-9]*>")
1517 (setq sql-prompt-length 5)
1518 (setq sql-buffer (current-buffer))
1519 (sql-interactive-mode)
1520 (message "Login...done")
1521 (pop-to-buffer sql-buffer)))
1526 ;;;###autoload
1527 (defun sql-postgres ()
1528 "Run psql by Postgres as an inferior process.
1530 If buffer `*SQL*' exists but no process is running, make a new process.
1531 If buffer exists and a process is running, just switch to buffer
1532 `*SQL*'.
1534 Interpreter used comes from variable `sql-postgres-program'. Login uses
1535 the variables `sql-database' and `sql-server' as default, if set.
1537 The buffer is put in sql-interactive-mode, giving commands for sending
1538 input. See `sql-interactive-mode'.
1540 To specify a coding system for converting non-ASCII characters
1541 in the input and output to the process, use \\[universal-coding-system-argument]
1542 before \\[sql-postgres]. You can also specify this with \\[set-buffer-process-coding-system]
1543 in the SQL buffer, after you start the process.
1544 The default comes from `process-coding-system-alist' and
1545 `default-process-coding-system'. If your output lines end with ^M,
1546 your might try undecided-dos as a coding system. If this doesn't help,
1547 Try to set `comint-output-filter-functions' like this:
1549 \(setq comint-output-filter-functions (append comint-output-filter-functions
1550 '(comint-strip-ctrl-m)))
1552 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1553 (interactive)
1554 (if (comint-check-proc "*SQL*")
1555 (pop-to-buffer "*SQL*")
1556 (sql-get-login 'database 'server)
1557 (message "Login...")
1558 ;; username and password are ignored. Jason Beegan suggest using
1559 ;; --pset and pager=off instead of \\o|cat. The later was the
1560 ;; solution by Gregor Zych. If you find that your postgres doesn't
1561 ;; like Jason Beegans's solution and prefers Gregor Zych's solution,
1562 ;; then I'd love to hear from you. Send your comments to the
1563 ;; mailing list.
1564 (let ((params (list "--pset" "pager=off")))
1565 (if (not (string= "" sql-database))
1566 (setq params (append (list sql-database) params)))
1567 (if (not (string= "" sql-server))
1568 (setq params (append (list "-h" sql-server) params)))
1569 (set-buffer (apply 'make-comint "SQL" sql-postgres-program
1570 nil params)))
1571 (setq sql-prompt-regexp "^.*> *")
1572 (setq sql-prompt-length 5)
1573 ;; This is a lousy hack to prevent psql from truncating it's output
1574 ;; and giving stupid warnings. If s.o. knows a way to prevent psql
1575 ;; from acting this way, then I would be very thankful to
1576 ;; incorporate this (Gregor Zych <zych@pool.informatik.rwth-aachen.de>)
1577 ;; (comint-send-string "*SQL*" "\\o \| cat\n")
1578 (setq sql-mode-font-lock-keywords sql-mode-postgres-font-lock-keywords)
1579 (setq sql-buffer (current-buffer))
1580 (sql-interactive-mode)
1581 (message "Login...done")
1582 (pop-to-buffer sql-buffer)))
1584 (provide 'sql)
1586 ;;; sql.el ends here