1 ;;; sql.el --- specialized comint.el for SQL interpreters
3 ;; Copyright (C) 1998, 1999 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Alex Schroeder <alex@gnu.org>
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)
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.
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 ;; You can get the latest version of this file from my homepage
35 ;; <URL:http://www.geocities.com/TimesSquare/6120/emacs.html>.
37 ;; This file provides a sql-mode and a sql-interactive-mode. My goals
38 ;; were two simple modes providing syntactic hilighting. The
39 ;; interactive mode had to provide a command-line history; the other
40 ;; mode had to provide "send region/buffer to SQL interpreter"
41 ;; functions. "simple" in this context means easy to use, easy to
42 ;; maintain and little or no bells and whistles.
44 ;; If anybody feels like extending this sql mode, take a look at the
45 ;; above mentioned modes and write a sqlx-mode on top of this one. If
46 ;; this proves to be difficult, please suggest changes that will
47 ;; facilitate your plans.
49 ;; sql-interactive-mode is used to interact with a SQL interpreter
50 ;; process in a SQLi buffer (usually called `*SQL*'). The SQLi buffer
51 ;; is created by calling a SQL interpreter-specific entry function. Do
52 ;; *not* call sql-interactive-mode by itself.
54 ;; The list of currently supported interpreters and the corresponding
55 ;; entry function used to create the SQLi buffers is shown with
56 ;; `sql-help' (M-x sql-help).
58 ;; Since sql-interactive-mode is built on top of the general
59 ;; command-interpreter-in-a-buffer mode (comint mode), it shares a
60 ;; common base functionality, and a common set of bindings, with all
61 ;; modes derived from comint mode. This makes these modes easier to
64 ;; sql-mode can be used to enable syntactic hilighting for SQL
65 ;; statements in another buffer. SQL statements can then be sent to
66 ;; the SQL process in the SQLi buffer. sql-mode has already been
67 ;; used as a template to a simple PL/SQL mode.
69 ;; For documentation on the functionality provided by comint mode, and
70 ;; the hooks available for customising it, see the file `comint.el'.
72 ;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
73 ;; `imenu-add-menubar-index'.
75 ;;; Requirements for Emacs 19.34:
77 ;; If you are using Emacs 19.34, you will have to get and install
78 ;; the file regexp-opt.el
79 ;; <URL:ftp://ftp.ifi.uio.no/pub/emacs/emacs-20.3/lisp/emacs-lisp/regexp-opt.el>
80 ;; and the custom package
81 ;; <URL:http://www.dina.kvl.dk/~abraham/custom/>.
85 ;; Using sql-ms (isql by Microsoft): When commands with syntax errors
86 ;; or execution errors are executed, there is no server feedback.
87 ;; This happens in stored procedures for example. The server messages
88 ;; only appear after the process is exited. This makes things
89 ;; somewhat unreliable.
93 ;; Add better hilight support for other brands; there is a bias towards
94 ;; Oracle because that's what I use at work. Anybody else just send in
95 ;; your lists of reserved words, keywords and builtin functions! As
96 ;; long as I don't receive any feedback, everything is hilighted with
97 ;; ANSI keywords only. I received the list of ANSI keywords from a
98 ;; user; if you know of any changes, let me know.
100 ;; Add different hilighting levels.
102 ;;; Thanks to all the people who helped me out:
104 ;; Kai Blauberg <kai.blauberg@metla.fi>
105 ;; <ibalaban@dalet.com>
106 ;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
107 ;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
108 ;; nino <nino@inform.dk>
109 ;; Berend de Boer <berend@pobox.com>
116 ;; Need the following to allow GNU Emacs 19 to compile the file.
117 (require 'regexp-opt)
120 ;;; Allow customization
123 "Running a SQL interpreter from within Emacs buffers"
127 ;; These three variables will be used as defaults, if set.
129 (defcustom sql-user ""
134 (defcustom sql-password ""
137 Storing your password in a textfile such as ~/.emacs could be dangerous.
138 Customizing your password will store it in your ~/.emacs file."
142 (defcustom sql-database ""
147 (defcustom sql-server ""
148 "*Default server or host."
152 ;; misc customization of sql.el behaviour
154 (defcustom sql-pop-to-buffer-after-send-region nil
155 "*If t, pop to the buffer SQL statements are sent to.
157 After a call to `sql-send-region' or `sql-send-buffer',
158 the window is split and the SQLi buffer is shown. If this
159 variable is not nil, that buffer's window will be selected
160 by calling `pop-to-buffer'. If this variable is nil, that
161 buffer is shown using `display-buffer'."
165 ;; imenu support for sql-mode.
167 (defvar sql-imenu-generic-expression
168 '(("Tables" "^\\s-*create\\s-+table\\s-+\\(\\w+\\)" 1)
169 ("Indexes" "^\\s-*create\\s-+index\\s-+\\(\\w+\\)" 1))
170 "Define interesting points in the SQL buffer for `imenu'.
172 This is used to rebind `imenu-generic-expression'.")
176 (defcustom sql-input-ring-file-name nil
177 "*If non-nil, name of the file to read/write input history.
179 You have to set this variable if you want the history of your commands
180 saved from one Emacs session to the next. If this variable is set,
181 exiting the SQL interpreter in an SQLi buffer will write the input
182 history to the specified file. Starting a new process in a SQLi buffer
183 will read the input history from the specified file.
185 This is used to locally set `comint-input-ring-file-name' when reading
186 or writing the input history."
187 :type '(choice (const :tag "none" nil)
191 (defcustom sql-input-ring-separator "\n--\n"
192 "*Separator between commands in the history file.
194 If set to \"\\n\", each line in the history file will be interpreted as
195 one command. Multi-line commands are split into several commands when
196 the input ring is initialized from a history file.
198 This variable used to locally set `comint-input-ring-separator' when
199 reading or writing the history file. `comint-input-ring-separator' is
200 not yet part of Emacs; if your Emacs does not have it, setting
201 `sql-input-ring-separator' will have no effect. In that case multiline
202 commands will be split into several commands when the input history is
203 read, as if you had set `sql-input-ring-separator' to \"\\n\".
205 The source code contains a link to a homepage that might have a patch
206 for comint.el to download."
212 (defcustom sql-interactive-mode-hook '()
213 "*Hook for customising `sql-interactive-mode'."
217 (defcustom sql-mode-hook '()
218 "*Hook for customising `sql-mode'."
222 (defcustom sql-set-sqli-hook '()
223 "*Hook for reacting to changes of `sql-buffer'.
225 This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
230 ;; Customisation for Oracle
232 (defcustom sql-oracle-program "sqlplus"
233 "*Command to start sqlplus by Oracle.
235 Starts `sql-interactive-mode' after doing some setup.
237 Under NT, \"sqlplus\" usually starts the sqlplus \"GUI\". In order to
238 start the sqlplus console, use \"plus33\" or something similar. You
239 will find the file in your Orant\\bin directory.
241 The program can also specify a TCP connection. See `make-comint'."
245 ;; Customisation for MySql
247 (defcustom sql-mysql-program "mysql"
248 "*Command to start mysql by TcX.
250 Starts `sql-interactive-mode' after doing some setup.
252 The program can also specify a TCP connection. See `make-comint'."
256 ;; Customisation for Solid
258 (defcustom sql-solid-program "solsql"
259 "*Command to start SOLID SQL Editor.
261 Starts `sql-interactive-mode' after doing some setup.
263 The program can also specify a TCP connection. See `make-comint'."
267 ;; Customisation for SyBase
269 (defcustom sql-sybase-program "isql"
270 "*Command to start isql by SyBase.
272 Starts `sql-interactive-mode' after doing some setup.
274 The program can also specify a TCP connection. See `make-comint'."
278 ;; Customisation for Informix
280 (defcustom sql-informix-program "dbaccess"
281 "*Command to start dbaccess by Informix.
283 Starts `sql-interactive-mode' after doing some setup.
285 The program can also specify a TCP connection. See `make-comint'."
289 ;; Customisation for Ingres
291 (defcustom sql-ingres-program "sql"
292 "*Command to start sql by Ingres.
294 Starts `sql-interactive-mode' after doing some setup.
296 The program can also specify a TCP connection. See `make-comint'."
300 ;; Customisation for Microsoft
302 (defcustom sql-ms-program "isql"
303 "*Command to start isql by Microsoft.
305 Starts `sql-interactive-mode' after doing some setup.
307 The program can also specify a TCP connection. See `make-comint'."
311 ;; Customisation for Postgres
313 (defcustom sql-postgres-program "psql"
314 "Command to start psql by Postgres.
316 Starts `sql-interactive-mode' after doing some setup.
318 The program can also specify a TCP connection. See `make-comint'."
324 ;;; Variables which do not need customization
326 (defvar sql-user-history nil
327 "History of usernames used.")
329 (defvar sql-database-history nil
330 "History of databases used.")
332 (defvar sql-server-history nil
333 "History of servers used.")
335 ;; Passwords are not kept in a history.
337 (defvar sql-buffer nil
338 "Current SQLi buffer.
340 The global value of sql-buffer is the name of the latest SQLi buffer
341 created. Any SQL buffer created will make a local copy of this value.
342 See `sql-interactive-mode' for more on multiple sessions. If you want
343 to change the SQLi buffer a SQL mode sends its SQL strings to, change
344 the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
346 (defvar sql-prompt-regexp nil
347 "Prompt used to initialize `comint-prompt-regexp'.
349 You can change `comint-prompt-regexp' on `sql-interactive-mode-hook'.")
351 (defvar sql-prompt-length 0
352 "Prompt used to set `left-margin' in `sql-interactive-mode'.
354 You can change it on `sql-interactive-mode-hook'.")
356 (defvar sql-alternate-buffer-name nil
357 "Buffer-local string used to possibly rename the SQLi buffer.
359 Used by `sql-rename-buffer'.")
361 ;; Keymap for sql-interactive-mode, based on comint-mode-map.
363 (if (not (string-match "XEmacs\\|Lucid" emacs-version))
364 (defvar sql-interactive-mode-map
365 (let ((map (nconc (make-sparse-keymap) comint-mode-map)))
366 (define-key map "\C-j" 'sql-accumulate-and-indent)
367 (define-key map "\C-c\C-w" 'sql-copy-column)
369 "Mode map used for `sql-interactive-mode'.")
371 (defvar sql-interactive-mode-map nil)
372 (if (not sql-interactive-mode-map)
373 (let ((map (make-keymap)))
374 (set-keymap-parents map (list comint-mode-map))
375 (set-keymap-name map 'sql-interactive-mode-map)
376 (define-key map "\C-j" 'sql-accumulate-and-indent)
377 (define-key map "\C-c\C-w" 'sql-copy-column)
378 (setq sql-interactive-mode-map map))))
380 ;; Keymap for sql-mode.
383 (let ((map (make-sparse-keymap)))
384 (define-key map "\C-c\C-c" 'sql-send-paragraph)
385 (define-key map "\C-c\C-r" 'sql-send-region)
386 (define-key map "\C-c\C-b" 'sql-send-buffer)
387 (define-key map "\t" 'indent-relative)
389 "Mode map used for `sql-mode'.")
391 ;; easy menu for sql-mode.
394 sql-mode-menu sql-mode-map
395 "Menu for `sql-mode'."
397 ["Send Paragraph" sql-send-paragraph (and (buffer-live-p sql-buffer)
398 (get-buffer-process sql-buffer))]
399 ["Send Region" sql-send-region (and mark-active
400 (buffer-live-p sql-buffer)
401 (get-buffer-process sql-buffer))]
402 ["Send Buffer" sql-send-buffer (and (buffer-live-p sql-buffer)
403 (get-buffer-process sql-buffer))]
404 ["Show SQLi buffer" sql-show-sqli-buffer t]
405 ["Set SQLi buffer" sql-set-sqli-buffer t]
406 ["Pop to SQLi buffer after send"
407 sql-toggle-pop-to-buffer-after-send-region
409 :selected sql-pop-to-buffer-after-send-region]))
411 ;; easy menu for sql-interactive-mode.
414 sql-interactive-mode-menu sql-interactive-mode-map
415 "Menu for `sql-interactive-mode'."
417 ["Rename Buffer" sql-rename-buffer t]))
419 ;; Abbreviations -- if you want more of them, define them in your
420 ;; ~/.emacs file. Abbrevs have to be enabled in your ~/.emacs, too.
422 (defvar sql-mode-abbrev-table nil
423 "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
424 (if sql-mode-abbrev-table
427 (define-abbrev-table 'sql-mode-abbrev-table ())
428 (define-abbrev sql-mode-abbrev-table "ins" "insert" nil)
429 (define-abbrev sql-mode-abbrev-table "upd" "update" nil)
430 (define-abbrev sql-mode-abbrev-table "del" "delete" nil)
431 (define-abbrev sql-mode-abbrev-table "sel" "select" nil)))
435 (defvar sql-mode-syntax-table
436 (let ((table (make-syntax-table)))
437 ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
438 (modify-syntax-entry ?/ ". 14" table)
439 (modify-syntax-entry ?* ". 23" table)
440 ;; double-dash starts comment
441 (if (string-match "XEmacs\\|Lucid" emacs-version)
442 (modify-syntax-entry ?- ". 56" table)
443 (modify-syntax-entry ?- ". 12b" table))
444 ;; newline and formfeed end coments
445 (modify-syntax-entry ?\n "> b" table)
446 (modify-syntax-entry ?\f "> b" table)
447 ;; single quotes (') quotes delimit strings
448 (modify-syntax-entry ?' "\"" table)
450 "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
454 (defvar sql-mode-ansi-font-lock-keywords nil
455 "ANSI SQL keywords used by font-lock.
457 This variable is used by `sql-mode' and `sql-interactive-mode'. The
458 regular expressions are created during compilation by calling the
459 function `regexp-opt'. Therefore, take a look at the source before
460 you define your own sql-mode-ansi-font-lock-keywords. You may want to
461 add functions and PL/SQL keywords.")
462 (if sql-mode-ansi-font-lock-keywords
464 (let ((ansi-keywords (eval-when-compile
467 "authorization" "avg" "begin" "close" "cobol" "commit"
468 "continue" "count" "declare" "double" "end" "escape"
469 "exec" "fetch" "foreign" "fortran" "found" "go" "goto" "indicator"
470 "key" "language" "max" "min" "module" "numeric" "open" "pascal" "pli"
471 "precision" "primary" "procedure" "references" "rollback"
472 "schema" "section" "some" "sqlcode" "sqlerror" "sum" "work") t) "\\b")))
473 (ansi-reserved-words (eval-when-compile
476 "all" "and" "any" "as" "asc" "between" "by" "check" "create"
477 "current" "default" "delete" "desc" "distinct" "exists" "float" "for"
478 "from" "grant" "group" "having" "in" "insert" "into" "is"
479 "like" "not" "null" "of" "on" "option" "or" "order" "privileges"
480 "public" "select" "set" "table" "to" "union" "unique"
481 "update" "user" "values" "view" "where" "with") t) "\\b")))
482 (ansi-types (eval-when-compile
485 ;; ANSI Keywords that look like types
486 "character" "cursor" "dec" "int" "real"
487 ;; ANSI Reserved Word that look like types
488 "char" "integer" "smallint" ) t) "\\b"))))
489 (setq sql-mode-ansi-font-lock-keywords
490 (list (cons ansi-keywords 'font-lock-function-name-face)
491 (cons ansi-reserved-words 'font-lock-keyword-face)
492 (cons ansi-types 'font-lock-type-face)))))
494 (defvar sql-mode-oracle-font-lock-keywords nil
495 "Oracle SQL keywords used by font-lock.
497 This variable is used by `sql-mode' and `sql-interactive-mode'. The
498 regular expressions are created during compilation by calling the
499 function `regexp-opt'. Therefore, take a look at the source before
500 you define your own sql-mode-oracle-font-lock-keywords. You may want
501 to add functions and PL/SQL keywords.")
502 (if sql-mode-oracle-font-lock-keywords
504 (let ((oracle-keywords (eval-when-compile
507 "admin" "after" "allocate" "analyze" "archive" "archivelog" "backup"
508 "become" "before" "block" "body" "cache" "cancel" "cascade" "change"
509 "checkpoint" "compile" "constraint" "constraints" "contents"
510 "controlfile" "cycle" "database" "datafile" "dba" "disable" "dismount"
511 "dump" "each" "enable" "events" "except" "exceptions" "execute"
512 "explain" "extent" "externally" "flush" "force" "freelist" "freelists"
513 "function" "groups" "including" "initrans" "instance" "layer" "link"
514 "lists" "logfile" "manage" "manual" "maxdatafiles" "maxinistances"
515 "maxlogfiles" "maxloghistory" "maxlogmembers" "maxtrans" "maxvalue"
516 "minextents" "minvalue" "mount" "new" "next" "noarchivelog" "nocache"
517 "nocycle" "nomaxvalue" "nominvalue" "none" "noorder" "noresetlogs"
518 "normal" "nosort" "off" "old" "only" "optimal" "own" "package"
519 "parallel" "pctincrease" "pctused" "plan" "private" "profile" "quota"
520 "read" "recover" "referencing" "resetlogs" "restricted" "reuse" "role"
521 "roles" "savepoint" "scn" "segment" "sequence" "shared" "snapshot"
522 "sort" "statement_id" "statistics" "stop" "storage" "switch" "system"
523 "tables" "tablespace" "temporary" "thread" "time" "tracing"
524 "transaction" "triggers" "truncate" "under" "unlimited" "until" "use"
525 "using" "when" "write") t) "\\b")))
526 (oracle-reserved-words (eval-when-compile
529 "access" "add" "alter" "audit" "cluster" "column" "comment" "compress"
530 "connect" "drop" "else" "exclusive" "file" "grant"
531 "identified" "immediate" "increment" "index" "initial" "intersect"
532 "level" "lock" "long" "maxextents" "minus" "mode" "modify" "noaudit"
533 "nocompress" "nowait" "number" "offline" "online" "pctfree" "prior"
534 "raw" "rename" "resource" "revoke" "row" "rowlabel" "rownum"
535 "rows" "session" "share" "size" "start" "successful" "synonym" "sysdate"
536 "then" "trigger" "uid" "validate" "whenever") t) "\\b")))
537 (oracle-types (eval-when-compile
540 ;; Oracle Keywords that look like types
541 ;; Oracle Reserved Words that look like types
542 "date" "decimal" "rowid" "varchar" "varchar2") t) "\\b")))
543 (oracle-builtin-functions (eval-when-compile
546 ;; Misc Oracle builtin functions
547 "abs" "add_months" "ascii" "avg" "ceil" "chartorowid" "chr" "concat"
548 "convert" "cos" "cosh" "count" "currval" "decode" "dump" "exp" "floor"
549 "glb" "greatest" "greatest_lb" "hextoraw" "initcap" "instr" "instrb"
550 "last_day" "least" "least_ub" "length" "lengthb" "ln" "log" "lower"
551 "lpad" "ltrim" "lub" "max" "min" "mod" "months_between" "new_time"
552 "next_day" "nextval" "nls_initcap" "nls_lower" "nls_upper" "nlssort"
553 "nvl" "power" "rawtohex" "replace" "round" "rowidtochar" "rpad"
554 "rtrim" "sign" "sin" "sinh" "soundex" "sqlcode" "sqlerrm" "sqrt"
555 "stddev" "sum" "substr" "substrb" "tan" "tanh" "to_char"
556 "to_date" "to_label" "to_multi_byte" "to_number" "to_single_byte"
557 "translate" "trunc" "uid" "upper" "userenv" "variance" "vsize") t) "\\b"))))
558 (setq sql-mode-oracle-font-lock-keywords
559 (append sql-mode-ansi-font-lock-keywords
560 (list (cons oracle-keywords 'font-lock-function-name-face)
561 (cons oracle-reserved-words 'font-lock-keyword-face)
562 ;; XEmacs doesn't have font-lock-builtin-face
563 (if (string-match "XEmacs\\|Lucid" emacs-version)
564 (cons oracle-builtin-functions 'font-lock-preprocessor-face)
565 ;; GNU Emacs 19 doesn't have it either
566 (if (string-match "GNU Emacs 19" emacs-version)
567 (cons oracle-builtin-functions 'font-lock-function-name-face)
569 (cons oracle-builtin-functions 'font-lock-builtin-face)))
570 (cons oracle-types 'font-lock-type-face))))))
572 (defvar sql-mode-postgres-font-lock-keywords nil
573 "Postgres SQL keywords used by font-lock.
575 This variable is used by `sql-mode' and `sql-interactive-mode'. The
576 regular expressions are created during compilation by calling the
577 function `regexp-opt'. Therefore, take a look at the source before
578 you define your own sql-mode-postgres-font-lock-keywords.")
580 (if sql-mode-postgres-font-lock-keywords
582 (let ((postgres-reserved-words (eval-when-compile
587 (postgres-types (eval-when-compile
590 "bool" "box" "circle" "char" "char2" "char4" "char8" "char16" "date"
591 "float4" "float8" "int2" "int4" "int8" "line" "lseg" "money" "path"
592 "point" "polygon" "serial" "text" "time" "timespan" "timestamp" "varchar"
594 (postgres-builtin-functions (eval-when-compile
597 ;; Misc Postgres builtin functions
598 "abstime" "age" "area" "box" "center" "date_part" "date_trunc"
599 "datetime" "dexp" "diameter" "dpow" "float" "float4" "height"
600 "initcap" "integer" "isclosed" "isfinite" "isoldpath" "isopen"
601 "length" "lower" "lpad" "ltrim" "pclose" "point" "points" "popen"
602 "position" "radius" "reltime" "revertpoly" "rpad" "rtrim" "substr"
603 "substring" "text" "timespan" "translate" "trim" "upgradepath"
604 "upgradepoly" "upper" "varchar" "width"
606 (setq sql-mode-postgres-font-lock-keywords
607 (append sql-mode-ansi-font-lock-keywords
608 (list (cons postgres-reserved-words 'font-lock-keyword-face)
609 ;; XEmacs doesn't have 'font-lock-builtin-face
610 (if (string-match "XEmacs\\|Lucid" emacs-version)
611 (cons postgres-builtin-functions 'font-lock-preprocessor-face)
613 (cons postgres-builtin-functions 'font-lock-builtin-face))
614 (cons postgres-types 'font-lock-type-face))))))
617 (defvar sql-mode-font-lock-keywords sql-mode-ansi-font-lock-keywords
618 "SQL keywords used by font-lock.
620 This variable defaults to `sql-mode-ansi-font-lock-keywords'. This is
621 used for the default `font-lock-defaults' value in `sql-mode'. This
622 can be changed by some entry functions to provide more hilighting.")
628 (defun sql-accumulate-and-indent ()
629 "Continue SQL statement on the next line."
631 (if (fboundp 'comint-accumulate) (comint-accumulate))
632 (indent-according-to-mode))
636 "Show short help for the SQL modes.
638 Use an entry function to open an interactive SQL buffer. This buffer is
639 usually named `*SQL*'. The name of the major mode is SQLi.
641 Use the following commands to start a specific SQL interpreter:
643 PostGres: \\[sql-postgres]
645 Other non-free SQL implementations are also supported:
649 Oracle: \\[sql-oracle]
650 Informix: \\[sql-informix]
651 Sybase: \\[sql-sybase]
652 Ingres: \\[sql-ingres]
653 Microsoft: \\[sql-ms]
655 But we urge you to choose a free implementation instead of these.
657 Once you have the SQLi buffer, you can enter SQL statements in the
658 buffer. The output generated is appended to the buffer and a new prompt
659 is generated. See the In/Out menu in the SQLi buffer for some functions
660 that help you navigate through the buffer, the input history, etc.
662 Put a line with a call to autoload into your `~/.emacs' file for each
663 entry function you want to use regularly:
665 \(autoload 'sql-postgres \"sql\" \"Interactive SQL mode.\" t)
667 If you have a really complex SQL statement or if you are writing a
668 procedure, you can do this in a separate buffer. Put the new buffer in
669 `sql-mode' by calling \\[sql-mode]. The name of this buffer can be
670 anything. The name of the major mode is SQL.
672 In this SQL buffer (SQL mode), you can send the region or the entire
673 buffer to the interactive SQL buffer (SQLi mode). The results are
674 appended to the SQLi buffer without disturbing your SQL buffer."
676 (describe-function 'sql-help))
678 (defun sql-read-passwd (prompt &optional default)
679 "Read a password using PROMPT.
680 Optional DEFAULT is password to start with. This function calls
681 `read-passwd' if it is available. If not, function
682 `ange-ftp-read-passwd' is called. This should always be available,
683 even in old versions of Emacs."
684 (if (fboundp 'read-passwd)
685 (read-passwd prompt nil default)
686 (unless (fboundp 'ange-ftp-read-passwd)
687 (autoload 'ange-ftp-read-passwd "ange-ftp"))
688 (ange-ftp-read-passwd prompt default)))
690 (defun sql-get-login (&rest what)
691 "Get username, password and database from the user.
693 The variables `sql-user', `sql-password', `sql-server', and
694 `sql-database' can be customised. They are used as the default values.
695 Usernames, servers and databases are stored in `sql-user-history',
696 `sql-server-history' and `database-history'. Passwords are not stored
699 Parameter WHAT is a list of the arguments passed to this function.
700 The function asks for the username if WHAT contains symbol `user', for
701 the password if it contains symbol `password', for the server if it
702 contains symbol `server', and for the database if it contains symbol
705 In order to ask the user for username, password and database, call the
706 function like this: (sql-get-login 'user 'password 'database)."
708 (if (memq 'user what)
710 (read-from-minibuffer "User: " sql-user nil nil
712 (if (memq 'password what)
714 (sql-read-passwd "Password: " sql-password)))
715 (if (memq 'server what)
717 (read-from-minibuffer "Server: " sql-server nil nil
718 sql-server-history)))
719 (if (memq 'database what)
721 (read-from-minibuffer "Database: " sql-database nil nil
722 sql-database-history))))
724 (defun sql-find-sqli-buffer ()
725 "Return the current default SQLi buffer or nil.
726 In order to qualify, the SQLi buffer must be alive,
727 be in `sql-interactive-mode' and have a process."
728 (if (and (buffer-live-p (default-value 'sql-buffer))
729 (get-buffer-process (default-value 'sql-buffer)))
732 (let ((buflist (buffer-list))
734 (while (not (or (null buflist)
736 (let ((candidate (car buflist)))
737 (set-buffer candidate)
738 (if (and (equal major-mode 'sql-interactive-mode)
739 (get-buffer-process candidate))
740 (setq found candidate))
741 (setq buflist (cdr buflist))))
744 (defun sql-set-sqli-buffer-generally ()
745 "Set SQLi buffer for all SQL buffers that have none.
746 This function checks all SQL buffers for their SQLi buffer. If their
747 SQLi buffer is nonexistent or has no process, it is set to the current
748 default SQLi buffer. The current default SQLi buffer is determined
749 using `sql-find-sqli-buffer'. If `sql-buffer' is set,
750 `sql-set-sqli-hook' is run."
753 (let ((buflist (buffer-list))
754 (default-sqli-buffer (sql-find-sqli-buffer)))
755 (setq-default sql-buffer default-sqli-buffer)
756 (while (not (null buflist))
757 (let ((candidate (car buflist)))
758 (set-buffer candidate)
759 (if (and (equal major-mode 'sql-mode)
760 (not (buffer-live-p sql-buffer)))
762 (setq sql-buffer default-sqli-buffer)
763 (run-hooks 'sql-set-sqli-hook))))
764 (setq buflist (cdr buflist))))))
766 (defun sql-set-sqli-buffer ()
767 "Set the SQLi buffer SQL strings are sent to.
769 Call this function in a SQL buffer in order to set the SQLi buffer SQL
770 strings are sent to. Calling this function sets `sql-buffer' and runs
773 If you call it from a SQL buffer, this sets the local copy of
776 If you call it from anywhere else, it sets the global copy of
779 (let ((default-buffer (sql-find-sqli-buffer)))
780 (if (null default-buffer)
781 (error "There is no suitable SQLi buffer"))
784 (read-buffer "New SQLi buffer: " default-buffer t))))
785 (if (null (get-buffer-process new-buffer))
786 (error "Buffer %s has no process" (buffer-name new-buffer)))
787 (if (null (save-excursion
788 (set-buffer new-buffer)
789 (equal major-mode 'sql-interactive-mode)))
790 (error "Buffer %s is no SQLi buffer" (buffer-name new-buffer)))
793 (setq sql-buffer new-buffer)
794 (run-hooks 'sql-set-sqli-hook))))))
796 (defun sql-show-sqli-buffer ()
797 "Show the name of current SQLi buffer.
799 This is the buffer SQL strings are sent to. It is stored in the
800 variable `sql-buffer'. See `sql-help' on how to create such a buffer."
802 (if (null (buffer-live-p sql-buffer))
803 (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
804 (if (null (get-buffer-process sql-buffer))
805 (message "Buffer %s has no process." (buffer-name sql-buffer))
806 (message "Current SQLi buffer is %s." (buffer-name sql-buffer)))))
808 (defun sql-make-alternate-buffer-name ()
809 "Return a string that can be used to rename a SQLi buffer.
811 This is used to set `sql-alternate-buffer-name' within
812 `sql-interactive-mode'."
813 (concat (if (string= "" sql-user)
814 (if (string= "" user-login-name)
816 (concat user-login-name "/"))
817 (concat sql-user "/"))
818 (if (string= "" sql-database)
819 (if (string= "" sql-server)
824 (defun sql-rename-buffer ()
825 "Renames a SQLi buffer."
827 (rename-buffer (format "*SQL: %s*" sql-alternate-buffer-name) t))
829 (defun sql-copy-column ()
830 "Copy current column to the end of buffer.
831 Inserts SELECT or commas if appropriate."
835 (setq column (buffer-substring
836 (progn (forward-char 1) (backward-sexp 1) (point))
837 (progn (forward-sexp 1) (point))))
838 (goto-char (point-max))
840 ;; if empty command line, insert SELECT
841 ((save-excursion (beginning-of-line)
842 (looking-at (concat comint-prompt-regexp "$")))
844 ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
846 (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
847 (save-excursion (beginning-of-line) (point)) t))
849 ;; else insert a space
851 (if (eq (preceding-char) ? )
854 ;; in any case, insert the column
856 (message "%s" column))))
860 ;;; Sending the region to the SQLi buffer.
862 (defun sql-send-region (start end)
863 "Send a region to the SQL process."
865 (if (buffer-live-p sql-buffer)
867 (comint-send-region sql-buffer start end)
868 (if (string-match "\n$" (buffer-substring start end))
870 (comint-send-string sql-buffer "\n"))
871 (message "Sent string to buffer %s." (buffer-name sql-buffer))
872 (if sql-pop-to-buffer-after-send-region
873 (pop-to-buffer sql-buffer)
874 (display-buffer sql-buffer)))
875 (message "No SQL process started.")))
877 (defun sql-send-paragraph ()
878 "Send the current paragraph to the SQL process."
880 (let ((start (save-excursion
886 (sql-send-region start end)))
888 (defun sql-send-buffer ()
889 "Send the buffer contents to the SQL process."
891 (sql-send-region (point-min) (point-max)))
893 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
894 "Toggle `sql-pop-to-buffer-after-send-region'.
896 If given the optional parameter VALUE, sets
897 sql-toggle-pop-to-buffer-after-send-region to VALUE."
900 (setq sql-pop-to-buffer-after-send-region value)
901 (setq sql-pop-to-buffer-after-send-region
902 (null sql-pop-to-buffer-after-send-region ))))
906 ;;; SQL mode -- uses SQL interactive mode
910 "Major mode to edit SQL.
912 You can send SQL statements to the SQLi buffer using
913 \\[sql-send-region]. Such a buffer must exist before you can do this.
914 See `sql-help' on how to create SQLi buffers.
917 Customization: Entry to this mode runs the `sql-mode-hook'.
919 When you put a buffer in SQL mode, the buffer stores the last SQLi
920 buffer created as its destination in the variable `sql-buffer'. This
921 will be the buffer \\[sql-send-region] sends the region to. If this
922 SQLi buffer is killed, \\[sql-send-region] is no longer able to
923 determine where the strings should be sent to. You can set the
924 value of `sql-buffer' using \\[sql-set-sqli-buffer].
926 For information on how to create multiple SQLi buffers, see
927 `sql-interactive-mode'."
929 (kill-all-local-variables)
930 (setq major-mode 'sql-mode)
931 (setq mode-name "SQL")
932 (use-local-map sql-mode-map)
933 (set-syntax-table sql-mode-syntax-table)
934 (make-local-variable 'font-lock-defaults)
935 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
936 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
937 ;; will have just one quote. Therefore syntactic hilighting is
938 ;; disabled for interactive buffers. `_' and `.' are considered part
940 (setq font-lock-defaults '(sql-mode-font-lock-keywords
941 nil t ((?_ . "w") (?. . "w"))))
942 (make-local-variable 'comment-start)
943 (setq comment-start "--")
944 ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
945 (make-local-variable 'sql-buffer)
946 ;; Add imenu support for sql-mode. Note that imenu-generic-expression
947 ;; is buffer-local, so we don't need a local-variable for it. SQL is
948 ;; case-insensitive, that's why we have to set imenu-case-fold-search.
949 ;; imenu-syntax-alist makes sure that `_' is considered part of object
951 (setq imenu-generic-expression sql-imenu-generic-expression
952 imenu-case-fold-search t
953 imenu-syntax-alist '(("_" . "w")))
954 ;; Make `sql-send-paragraph' work on paragraphs that contain indented
956 (make-local-variable 'paragraph-separate)
957 (make-local-variable 'paragraph-start)
958 (setq paragraph-separate "[\f]*$"
959 paragraph-start "[\n\f]")
961 (setq local-abbrev-table sql-mode-abbrev-table)
962 (setq abbrev-all-caps 1)
964 (run-hooks 'sql-mode-hook))
968 ;;; SQL interactive mode
970 (put 'sql-interactive-mode 'mode-class 'special)
972 (defun sql-interactive-mode ()
973 "Major mode to use a SQL interpreter interactively.
975 Do not call this function by yourself. The environment must be
976 initialized by an entry function specific for the SQL interpreter. See
977 `sql-help' for a list of available entry functions.
979 \\[comint-send-input] after the end of the process' output sends the
980 text from the end of process to the end of the current line.
981 \\[comint-send-input] before end of process output copies the current
982 line minus the prompt to the end of the buffer and sends it.
983 \\[comint-copy-old-input] just copies the current line.
984 Use \\[sql-accumulate-and-indent] to enter multi-line statements.
986 If you want to make multiple SQL buffers, rename the `*SQL*' buffer
987 using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
988 See `sql-help' for a list of available entry functions. The last buffer
989 created by such an entry function is the current SQLi buffer. SQL
990 buffers will send strings to the SQLi buffer current at the time of
991 their creation. See `sql-mode' for details.
993 Sample session using two connections:
995 1. Create first SQLi buffer by calling an entry function.
996 2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
997 3. Create a SQL buffer \"test1.sql\".
998 4. Create second SQLi buffer by calling an entry function.
999 5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
1000 6. Create a SQL buffer \"test2.sql\".
1002 Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
1003 buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
1004 will send the region to buffer \"*Connection 2*\".
1006 If you accidentally suspend your process, use \\[comint-continue-subjob]
1007 to continue it. On some operating systems, this will not work because
1008 the signals are not supported.
1010 \\{sql-interactive-mode-map}
1011 Customization: Entry to this mode runs the hooks on `comint-mode-hook'
1012 and `sql-interactive-mode-hook' (in that order). Before each input, the
1013 hooks on `comint-input-filter-functions' are run. After each SQL
1014 interpreter output, the hooks on `comint-output-filter-functions' are
1017 Variable `sql-input-ring-file-name' controls the initialisation of the
1018 input ring history. `comint-input-ring-file-name' is temporarily bound
1019 to `sql-input-ring-file-name' when reading the input history.
1021 Variables `comint-output-filter-functions', a hook, and
1022 `comint-scroll-to-bottom-on-input' and
1023 `comint-scroll-to-bottom-on-output' control whether input and output
1024 cause the window to scroll to the end of the buffer.
1026 If you want to make SQL buffers limited in length, add the function
1027 `comint-truncate-buffer' to `comint-output-filter-functions'.
1029 Here is an example for your .emacs file. It keeps the SQLi buffer a
1032 \(add-hook 'sql-interactive-mode-hook
1033 \(function (lambda ()
1034 \(setq comint-output-filter-functions 'comint-truncate-buffer))))
1036 Here is another example. It will always put point back to the statement
1037 you entered, right above the output it created.
1039 \(setq comint-output-filter-functions
1040 \(function (lambda (STR) (comint-show-output))))"
1042 (setq comint-prompt-regexp sql-prompt-regexp)
1043 (setq left-margin sql-prompt-length)
1044 (setq major-mode 'sql-interactive-mode)
1045 (setq mode-name "SQLi")
1046 (use-local-map sql-interactive-mode-map)
1047 (set-syntax-table sql-mode-syntax-table)
1048 (make-local-variable 'font-lock-defaults)
1049 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1050 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1051 ;; will have just one quote. Therefore syntactic hilighting is
1052 ;; disabled for interactive buffers. `_' and `.' are considered part
1054 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1055 t t ((?_ . "w") (?. . "w"))))
1056 ;; Enable commenting and uncommenting of the region.
1057 (make-local-variable 'comment-start)
1058 (setq comment-start "--")
1059 ;; Abbreviation table init and case-insensitive. It is not activatet
1061 (setq local-abbrev-table sql-mode-abbrev-table)
1062 (setq abbrev-all-caps 1)
1063 ;; Exiting the process will call sql-stop.
1064 (set-process-sentinel (get-buffer-process sql-buffer) 'sql-stop)
1065 ;; Make input-ring stuff buffer local so that people who want a
1066 ;; different history file for each buffer/process/client/whatever can
1067 ;; change separator and file-name on the sql-interactive-mode-hook.
1068 (make-local-variable 'sql-input-ring-separator)
1069 (make-local-variable 'sql-input-ring-file-name)
1070 ;; Create a usefull name for renaming this buffer later.
1071 (make-local-variable 'sql-alternate-buffer-name)
1072 (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
1074 (run-hooks 'sql-interactive-mode-hook)
1075 ;; Calling the hook before calling comint-read-input-ring allows users
1076 ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
1077 ;; While reading the history, file-name and history are rebound...
1078 (let ((comint-input-ring-file-name sql-input-ring-file-name)
1079 (comint-input-ring-separator sql-input-ring-separator))
1080 (comint-read-input-ring t)))
1082 (defun sql-stop (process event)
1083 "Called when the SQL process is stopped.
1085 Writes the input history to a history file using
1086 `comint-write-input-ring' and inserts a short message in the SQL buffer.
1087 `comint-comint-input-ring-file-name' is temporarily bound to
1088 `sql-input-ring-file-name'.
1090 This function is a sentinel watching the SQL interpreter process.
1091 Sentinels will always get the two parameters PROCESS and EVENT."
1093 ;; While reading the history, file-name and history are rebound...
1094 (let ((comint-input-ring-file-name sql-input-ring-file-name)
1095 (comint-input-ring-separator sql-input-ring-separator))
1096 (comint-write-input-ring))
1097 (if (buffer-live-p sql-buffer)
1098 (insert (format "\nProcess %s %s\n" process event))))
1102 ;;; Entry functions for different SQL interpreters.
1104 (defun sql-oracle ()
1105 "Run sqlplus by Oracle as an inferior process.
1107 If buffer `*SQL*' exists but no process is running, make a new process.
1108 If buffer exists and a process is running, just switch to buffer
1111 Interpreter used comes from variable `sql-oracle-program'. Login uses
1112 the variables `sql-user', `sql-password', and `sql-database' as
1115 The buffer is put in sql-interactive-mode, giving commands for sending
1116 input. See `sql-interactive-mode'.
1118 To specify a coding system for converting non-ASCII characters
1119 in the input and output to the process, use \\[universal-coding-system-argument]
1120 before \\[sql-oracle]. You can also specify this with \\[set-buffer-process-coding-system]
1121 in the SQL buffer, after you start the process.
1122 The default comes from `process-coding-system-alist' and
1123 `default-process-coding-system'.
1125 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1127 (if (comint-check-proc "*SQL*")
1128 (pop-to-buffer "*SQL*")
1129 (sql-get-login 'user 'password 'database)
1130 (message "Login...")
1131 ;; Produce user/password@database construct. Password without user
1132 ;; is meaningless; database without user/password is meaningless,
1133 ;; because "@param" will ask sqlplus to interpret the script
1135 (let ((parameter nil))
1136 (if (not (string= "" sql-user))
1137 (if (not (string= "" sql-password))
1138 (setq parameter (concat sql-user "/" sql-password))
1139 (setq parameter sql-user)))
1140 (if (and parameter (not (string= "" sql-database)))
1141 (setq parameter (concat parameter "@" sql-database)))
1143 (set-buffer (make-comint "SQL" sql-oracle-program nil parameter))
1144 (set-buffer (make-comint "SQL" sql-oracle-program nil))))
1145 (setq sql-prompt-regexp "^SQL> ")
1146 (setq sql-prompt-length 5)
1147 (setq sql-buffer (current-buffer))
1148 ;; set sql-mode-font-lock-keywords to something different before
1149 ;; calling sql-interactive-mode.
1150 (setq sql-mode-font-lock-keywords sql-mode-oracle-font-lock-keywords)
1151 (sql-interactive-mode)
1152 (message "Login...done")
1153 (pop-to-buffer sql-buffer)))
1157 (defun sql-sybase ()
1158 "Run isql by SyBase as an inferior process.
1160 If buffer `*SQL*' exists but no process is running, make a new process.
1161 If buffer exists and a process is running, just switch to buffer
1164 Interpreter used comes from variable `sql-sybase-program'. Login uses
1165 the variables `sql-user', `sql-password', and `sql-server' as
1168 The buffer is put in sql-interactive-mode, giving commands for sending
1169 input. See `sql-interactive-mode'.
1171 To specify a coding system for converting non-ASCII characters
1172 in the input and output to the process, use \\[universal-coding-system-argument]
1173 before \\[sql-sybase]. You can also specify this with \\[set-buffer-process-coding-system]
1174 in the SQL buffer, after you start the process.
1175 The default comes from `process-coding-system-alist' and
1176 `default-process-coding-system'.
1178 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1180 (if (comint-check-proc "*SQL*")
1181 (pop-to-buffer "*SQL*")
1182 (sql-get-login 'user 'password 'server)
1183 (message "Login...")
1184 ;; Put all parameters to the program (if defined) in a list and call
1186 (let ((params '("-w" "2048" "-n")))
1187 ;; There is no way to specify the database via command line
1188 ;; parameters. The -S option specifies the server.
1189 (if (not (string= "" sql-server))
1190 (setq params (append (list "-S" sql-server) params)))
1191 (if (not (string= "" sql-password))
1192 (setq params (append (list "-P" sql-password) params)))
1193 (if (not (string= "" sql-user))
1194 (setq params (append (list "-U" sql-user) params)))
1195 (set-buffer (apply 'make-comint "SQL" sql-sybase-program
1197 (setq sql-prompt-regexp "^SQL> ")
1198 (setq sql-prompt-length 5)
1199 (setq sql-buffer (current-buffer))
1200 (sql-interactive-mode)
1201 (message "Login...done")
1202 (pop-to-buffer sql-buffer)))
1206 (defun sql-informix ()
1207 "Run dbaccess by Informix as an inferior process.
1209 If buffer `*SQL*' exists but no process is running, make a new process.
1210 If buffer exists and a process is running, just switch to buffer
1213 Interpreter used comes from variable `sql-informix-program'. Login uses
1214 the variable `sql-database' as default, if set.
1216 The buffer is put in sql-interactive-mode, giving commands for sending
1217 input. See `sql-interactive-mode'.
1219 To specify a coding system for converting non-ASCII characters
1220 in the input and output to the process, use \\[universal-coding-system-argument]
1221 before \\[sql-informix]. You can also specify this with \\[set-buffer-process-coding-system]
1222 in the SQL buffer, after you start the process.
1223 The default comes from `process-coding-system-alist' and
1224 `default-process-coding-system'.
1226 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1228 (if (comint-check-proc "*SQL*")
1229 (pop-to-buffer "*SQL*")
1230 (sql-get-login 'database)
1231 (message "Login...")
1232 ;; username and password are ignored.
1233 (if (string= "" sql-database)
1234 (set-buffer (make-comint "SQL" sql-informix-program nil))
1235 (set-buffer (make-comint "SQL" sql-informix-program nil sql-database)))
1236 (setq sql-prompt-regexp "^SQL> ")
1237 (setq sql-prompt-length 5)
1238 (setq sql-buffer (current-buffer))
1239 (sql-interactive-mode)
1240 (message "Login...done")
1241 (pop-to-buffer sql-buffer)))
1246 "Run mysql by TcX as an inferior process.
1248 Note that the widespread idea that mysql is free software is inaccurate;
1249 its license is too restrictive. We urge you to use PostGres instead.
1251 If buffer `*SQL*' exists but no process is running, make a new process.
1252 If buffer exists and a process is running, just switch to buffer
1255 Interpreter used comes from variable `sql-mysql-program'. Login uses
1256 the variables `sql-user', `sql-password', `sql-database', and
1257 `sql-server' as defaults, if set.
1259 The buffer is put in sql-interactive-mode, giving commands for sending
1260 input. See `sql-interactive-mode'.
1262 To specify a coding system for converting non-ASCII characters
1263 in the input and output to the process, use \\[universal-coding-system-argument]
1264 before \\[sql-mysql]. You can also specify this with \\[set-buffer-process-coding-system]
1265 in the SQL buffer, after you start the process.
1266 The default comes from `process-coding-system-alist' and
1267 `default-process-coding-system'.
1269 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1271 (if (comint-check-proc "*SQL*")
1272 (pop-to-buffer "*SQL*")
1273 (sql-get-login 'user 'password 'database 'server)
1274 (message "Login...")
1275 ;; Put all parameters to the program (if defined) in a list and call
1278 (if (not (string= "" sql-database))
1279 (setq params (append (list sql-database) params)))
1280 (if (not (string= "" sql-server))
1281 (setq params (append (list (concat "--host=" sql-server)) params)))
1282 (if (not (string= "" sql-password))
1283 (setq params (append (list (concat "--password=" sql-password)) params)))
1284 (if (not (string= "" sql-user))
1285 (setq params (append (list (concat "--user=" sql-user)) params)))
1286 (set-buffer (apply 'make-comint "SQL" sql-mysql-program
1288 (setq sql-prompt-regexp "^mysql>")
1289 (setq sql-prompt-length 6)
1290 (setq sql-buffer (current-buffer))
1291 (sql-interactive-mode)
1292 (message "Login...done")
1293 (pop-to-buffer sql-buffer)))
1298 "Run solsql by Solid as an inferior process.
1300 If buffer `*SQL*' exists but no process is running, make a new process.
1301 If buffer exists and a process is running, just switch to buffer
1304 Interpreter used comes from variable `sql-solid-program'. Login uses
1305 the variables `sql-user', `sql-password', and `sql-server' as
1308 The buffer is put in sql-interactive-mode, giving commands for sending
1309 input. See `sql-interactive-mode'.
1311 To specify a coding system for converting non-ASCII characters
1312 in the input and output to the process, use \\[universal-coding-system-argument]
1313 before \\[sql-solid]. You can also specify this with \\[set-buffer-process-coding-system]
1314 in the SQL buffer, after you start the process.
1315 The default comes from `process-coding-system-alist' and
1316 `default-process-coding-system'.
1318 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1320 (if (comint-check-proc "*SQL*")
1321 (pop-to-buffer "*SQL*")
1322 (sql-get-login 'user 'password 'server)
1323 (message "Login...")
1324 ;; Put all parameters to the program (if defined) in a list and call
1327 ;; It only makes sense if both username and password are there.
1328 (if (not (or (string= "" sql-user)
1329 (string= "" sql-password)))
1330 (setq params (append (list sql-user sql-password) params)))
1331 (if (not (string= "" sql-server))
1332 (setq params (append (list sql-server) params)))
1333 (set-buffer (apply 'make-comint "SQL" sql-solid-program
1335 (setq sql-prompt-regexp "^")
1336 (setq sql-prompt-length 0)
1337 (setq sql-buffer (current-buffer))
1338 (sql-interactive-mode)
1339 (message "Login...done")
1340 (pop-to-buffer sql-buffer)))
1344 (defun sql-ingres ()
1345 "Run sql by Ingres as an inferior process.
1347 If buffer `*SQL*' exists but no process is running, make a new process.
1348 If buffer exists and a process is running, just switch to buffer
1351 Interpreter used comes from variable `sql-ingres-program'. Login uses
1352 the variable `sql-database' as default, if set.
1354 The buffer is put in sql-interactive-mode, giving commands for sending
1355 input. See `sql-interactive-mode'.
1357 To specify a coding system for converting non-ASCII characters
1358 in the input and output to the process, use \\[universal-coding-system-argument]
1359 before \\[sql-ingres]. You can also specify this with \\[set-buffer-process-coding-system]
1360 in the SQL buffer, after you start the process.
1361 The default comes from `process-coding-system-alist' and
1362 `default-process-coding-system'.
1364 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1366 (if (comint-check-proc "*SQL*")
1367 (pop-to-buffer "*SQL*")
1368 (sql-get-login 'database)
1369 (message "Login...")
1370 ;; username and password are ignored.
1371 (if (string= "" sql-database)
1372 (set-buffer (make-comint "SQL" sql-ingres-program nil))
1373 (set-buffer (make-comint "SQL" sql-ingres-program nil sql-database)))
1374 (setq sql-prompt-regexp "^\* ")
1375 (setq sql-prompt-length 2)
1376 (setq sql-buffer (current-buffer))
1377 (sql-interactive-mode)
1378 (message "Login...done")
1379 (pop-to-buffer sql-buffer)))
1384 "Run isql by Microsoft as an inferior process.
1386 If buffer `*SQL*' exists but no process is running, make a new process.
1387 If buffer exists and a process is running, just switch to buffer
1390 Interpreter used comes from variable `sql-ms-program'. Login uses the
1391 variables `sql-user', `sql-password', `sql-database', and `sql-server'
1392 as defaults, if set.
1394 The buffer is put in sql-interactive-mode, giving commands for sending
1395 input. See `sql-interactive-mode'.
1397 To specify a coding system for converting non-ASCII characters
1398 in the input and output to the process, use \\[universal-coding-system-argument]
1399 before \\[sql-ms]. You can also specify this with \\[set-buffer-process-coding-system]
1400 in the SQL buffer, after you start the process.
1401 The default comes from `process-coding-system-alist' and
1402 `default-process-coding-system'.
1404 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1406 (if (comint-check-proc "*SQL*")
1407 (pop-to-buffer "*SQL*")
1408 (sql-get-login 'user 'password 'database 'server)
1409 (message "Login...")
1410 ;; Put all parameters to the program (if defined) in a list and call
1412 (let ((params '("-w 300")))
1413 (if (not (string= "" sql-server))
1414 (setq params (append (list "-S" sql-server) params)))
1415 (if (not (string= "" sql-database))
1416 (setq params (append (list "-d" sql-database) params)))
1417 (if (not (string= "" sql-user))
1418 (setq params (append (list "-U" sql-user) params)))
1419 (if (not (string= "" sql-password))
1420 (setq params (append (list "-P" sql-password) params))
1421 ;; If -P is passed to ISQL as the last argument without a password,
1422 ;; it's considered null.
1423 (setq params (append params (list "-P"))))
1424 (set-buffer (apply 'make-comint "SQL" sql-ms-program
1426 (setq sql-prompt-regexp "^[0-9]*>")
1427 (setq sql-prompt-length 5)
1428 (setq sql-buffer (current-buffer))
1429 (sql-interactive-mode)
1430 (message "Login...done")
1431 (pop-to-buffer sql-buffer)))
1437 (defun sql-postgres ()
1438 "Run psql by Postgres as an inferior process.
1440 If buffer `*SQL*' exists but no process is running, make a new process.
1441 If buffer exists and a process is running, just switch to buffer
1444 Interpreter used comes from variable `sql-postgres-program'. Login uses
1445 the variables `sql-database' and `sql-server' as default, if set.
1447 The buffer is put in sql-interactive-mode, giving commands for sending
1448 input. See `sql-interactive-mode'.
1450 To specify a coding system for converting non-ASCII characters
1451 in the input and output to the process, use \\[universal-coding-system-argument]
1452 before \\[sql-postgres]. You can also specify this with \\[set-buffer-process-coding-system]
1453 in the SQL buffer, after you start the process.
1454 The default comes from `process-coding-system-alist' and
1455 `default-process-coding-system'. If your output lines end with ^M,
1456 your might try undecided-dos as a coding system. If this doesn't help,
1457 Try to set `comint-output-filter-functions' like this:
1459 \(setq comint-output-filter-functions (append comint-output-filter-functions
1460 '(comint-strip-ctrl-m)))
1462 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1464 (if (comint-check-proc "*SQL*")
1465 (pop-to-buffer "*SQL*")
1466 (sql-get-login 'database 'server)
1467 (message "Login...")
1468 ;; username and password are ignored.
1470 (if (not (string= "" sql-database))
1471 (setq params (append (list sql-database) params)))
1472 (if (not (string= "" sql-server))
1473 (setq params (append (list "-h" sql-server) params)))
1474 (set-buffer (apply 'make-comint "SQL" sql-postgres-program
1476 (setq sql-prompt-regexp "^.*> *")
1477 (setq sql-prompt-length 5)
1478 ;; This is a lousy hack to prevent psql from truncating it's output
1479 ;; and giving stupid warnings. If s.o. knows a way to prevent psql
1480 ;; from acting this way, then I would be very thankful to
1481 ;; incorporate this (Gregor Zych <zych@pool.informatik.rwth-aachen.de>)
1482 (comint-send-string "*SQL*" "\\o \| cat\n")
1483 (setq sql-mode-font-lock-keywords sql-mode-postgres-font-lock-keywords)
1484 (setq sql-buffer (current-buffer))
1485 (sql-interactive-mode)
1486 (message "Login...done")
1487 (pop-to-buffer sql-buffer)))
1491 ;;; sql.el ends here