.
[emacs.git] / lisp / progmodes / sql.el
blob00b779ba7413f38c75bb520938880036acb85f8b
1 ;;; sql.el --- specialized comint.el for SQL interpreters
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Alex Schroeder <alex@gnu.org>
7 ;; Version: 1.7.0
8 ;; Keywords: comm languages processes
9 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlMode
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
30 ;; Please send bug reports and bug fixes to the mailing list at
31 ;; sql.el@gnu.org. If you want to subscribe to the mailing list, send
32 ;; mail to sql.el-request@gnu.org with `subscribe sql.el FIRSTNAME
33 ;; LASTNAME' in the mail body.
35 ;; This file provides a sql-mode and a sql-interactive-mode. My goals
36 ;; were two simple modes providing syntactic hilighting. The
37 ;; interactive mode had to provide a command-line history; the other
38 ;; mode had to provide "send region/buffer to SQL interpreter"
39 ;; functions. "simple" in this context means easy to use, easy to
40 ;; maintain and little or no bells and whistles.
42 ;; If anybody feels like extending this sql mode, take a look at the
43 ;; above mentioned modes and write a sqlx-mode on top of this one. If
44 ;; this proves to be difficult, please suggest changes that will
45 ;; facilitate your plans.
47 ;; sql-interactive-mode is used to interact with a SQL interpreter
48 ;; process in a SQLi buffer (usually called `*SQL*'). The SQLi buffer
49 ;; is created by calling a SQL interpreter-specific entry function. Do
50 ;; *not* call sql-interactive-mode by itself.
52 ;; The list of currently supported interpreters and the corresponding
53 ;; entry function used to create the SQLi buffers is shown with
54 ;; `sql-help' (M-x sql-help).
56 ;; Since sql-interactive-mode is built on top of the general
57 ;; command-interpreter-in-a-buffer mode (comint mode), it shares a
58 ;; common base functionality, and a common set of bindings, with all
59 ;; modes derived from comint mode. This makes these modes easier to
60 ;; use.
62 ;; sql-mode can be used to keep editing SQL statements. The SQL
63 ;; statements can be sent to the SQL process in the SQLi buffer.
65 ;; For documentation on the functionality provided by comint mode, and
66 ;; the hooks available for customizing it, see the file `comint.el'.
68 ;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
69 ;; `imenu-add-menubar-index'.
71 ;;; Requirements for Emacs 19.34:
73 ;; If you are using Emacs 19.34, you will have to get and install
74 ;; the file regexp-opt.el
75 ;; <URL:ftp://ftp.ifi.uio.no/pub/emacs/emacs-20.3/lisp/emacs-lisp/regexp-opt.el>
76 ;; and the custom package
77 ;; <URL:http://www.dina.kvl.dk/~abraham/custom/>.
79 ;;; Bugs:
81 ;; Using sql-ms (isql by Microsoft): When commands with syntax errors
82 ;; or execution errors are executed, there is no server feedback.
83 ;; This happens in stored procedures for example. The server messages
84 ;; only appear after the process is exited. This makes things
85 ;; somewhat unreliable.
87 ;; ChangeLog available on request.
89 ;;; To Do:
91 ;; Add better hilight support for other brands; there is a bias towards
92 ;; Oracle because that's what I use at work. Anybody else just send in
93 ;; your lists of reserved words, keywords and builtin functions! As
94 ;; long as I don't receive any feedback, everything is hilighted with
95 ;; ANSI keywords only. I received the list of ANSI keywords from a
96 ;; user; if you know of any changes, let me know.
98 ;; Add different hilighting levels.
100 ;;; Thanks to all the people who helped me out:
102 ;; Kai Blauberg <kai.blauberg@metla.fi>
103 ;; <ibalaban@dalet.com>
104 ;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
105 ;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
106 ;; nino <nino@inform.dk>
107 ;; Berend de Boer <berend@pobox.com>
111 ;;; Code:
113 (require 'comint)
114 ;; Need the following to allow GNU Emacs 19 to compile the file.
115 (require 'regexp-opt)
116 (require 'custom)
118 ;;; Allow customization
120 (defgroup SQL nil
121 "Running a SQL interpreter from within Emacs buffers"
122 :version "20.4"
123 :group 'processes)
125 ;; These three variables will be used as defaults, if set.
127 (defcustom sql-user ""
128 "*Default username."
129 :type 'string
130 :group 'SQL)
132 (defcustom sql-password ""
133 "*Default password.
135 Storing your password in a textfile such as ~/.emacs could be dangerous.
136 Customizing your password will store it in your ~/.emacs file."
137 :type 'string
138 :group 'SQL)
140 (defcustom sql-database ""
141 "*Default database."
142 :type 'string
143 :group 'SQL)
145 (defcustom sql-server ""
146 "*Default server or host."
147 :type 'string
148 :group 'SQL)
150 ;; misc customization of sql.el behaviour
152 (defcustom sql-electric-stuff nil
153 "Treat some input as electric.
154 If set to the symbol `semicolon', then hitting `;' will send current
155 input in the SQLi buffer to the process.
156 If set to the symbol `go', then hitting `go' on a line by itself will
157 send current input in the SQLi buffer to the process.
158 If set to nil, then you must use \\[comint-send-input] in order to send
159 current input in the SQLi buffer to the process."
160 :type '(choice (const :tag "Nothing" nil)
161 (const :tag "The semikolon `;'" semicolon)
162 (const :tag "The string `go' by itself" go))
163 :version "20.8"
164 :group 'SQL)
166 (defcustom sql-pop-to-buffer-after-send-region nil
167 "*If t, pop to the buffer SQL statements are sent to.
169 After a call to `sql-send-region' or `sql-send-buffer',
170 the window is split and the SQLi buffer is shown. If this
171 variable is not nil, that buffer's window will be selected
172 by calling `pop-to-buffer'. If this variable is nil, that
173 buffer is shown using `display-buffer'."
174 :type 'boolean
175 :group 'SQL)
177 ;; imenu support for sql-mode.
179 (defvar sql-imenu-generic-expression
180 '(("Tables" "^\\s-*create\\s-+table\\s-+\\(\\w+\\)" 1)
181 ("Indexes" "^\\s-*create\\s-+index\\s-+\\(\\w+\\)" 1))
182 "Define interesting points in the SQL buffer for `imenu'.
184 This is used to set `imenu-generic-expression' when SQL mode is
185 entered. Subsequent changes to sql-imenu-generic-expression will not
186 affect existing SQL buffers because imenu-generic-expression is a
187 local variable.")
189 ;; history file
191 (defcustom sql-input-ring-file-name nil
192 "*If non-nil, name of the file to read/write input history.
194 You have to set this variable if you want the history of your commands
195 saved from one Emacs session to the next. If this variable is set,
196 exiting the SQL interpreter in an SQLi buffer will write the input
197 history to the specified file. Starting a new process in a SQLi buffer
198 will read the input history from the specified file.
200 This is used to initialize `comint-input-ring-file-name'.
202 Note that the size of the input history is determined by the variable
203 `comint-input-ring-size'."
204 :type '(choice (const :tag "none" nil)
205 (file))
206 :group 'SQL)
208 (defcustom sql-input-ring-separator "\n--\n"
209 "*Separator between commands in the history file.
211 If set to \"\\n\", each line in the history file will be interpreted as
212 one command. Multi-line commands are split into several commands when
213 the input ring is initialized from a history file.
215 This variable used to initialize `comint-input-ring-separator'.
216 `comint-input-ring-separator' is part of Emacs 21; if your Emacs
217 does not have it, setting `sql-input-ring-separator' will have no
218 effect. In that case multiline commands will be split into several
219 commands when the input history is read, as if you had set
220 `sql-input-ring-separator' to \"\\n\"."
221 :type 'string
222 :group 'SQL)
224 ;; The usual hooks
226 (defcustom sql-interactive-mode-hook '()
227 "*Hook for customizing `sql-interactive-mode'."
228 :type 'hook
229 :group 'SQL)
231 (defcustom sql-mode-hook '()
232 "*Hook for customizing `sql-mode'."
233 :type 'hook
234 :group 'SQL)
236 (defcustom sql-set-sqli-hook '()
237 "*Hook for reacting to changes of `sql-buffer'.
239 This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
240 is changed."
241 :type 'hook
242 :group 'SQL)
244 ;; Customization for Oracle
246 (defcustom sql-oracle-program "sqlplus"
247 "*Command to start sqlplus by Oracle.
249 Starts `sql-interactive-mode' after doing some setup.
251 Under NT, \"sqlplus\" usually starts the sqlplus \"GUI\". In order to
252 start the sqlplus console, use \"plus33\" or something similar. You
253 will find the file in your Orant\\bin directory.
255 The program can also specify a TCP connection. See `make-comint'."
256 :type 'file
257 :group 'SQL)
259 (defcustom sql-oracle-options nil
260 "*List of additional options for `sql-oracle-program'."
261 :type '(repeat string)
262 :version "20.8"
263 :group 'SQL)
265 ;; Customization for MySql
267 (defcustom sql-mysql-program "mysql"
268 "*Command to start mysql by TcX.
270 Starts `sql-interactive-mode' after doing some setup.
272 The program can also specify a TCP connection. See `make-comint'."
273 :type 'file
274 :group 'SQL)
276 (defcustom sql-mysql-options nil
277 "*List of additional options for `sql-mysql-program'.
278 The following list of options is reported to make things work
279 on Windows: \"-C\" \"-t\" \"-f\" \"-n\"."
280 :type '(repeat string)
281 :version "20.8"
282 :group 'SQL)
284 ;; Customization for Solid
286 (defcustom sql-solid-program "solsql"
287 "*Command to start SOLID SQL Editor.
289 Starts `sql-interactive-mode' after doing some setup.
291 The program can also specify a TCP connection. See `make-comint'."
292 :type 'file
293 :group 'SQL)
295 ;; Customization for SyBase
297 (defcustom sql-sybase-program "isql"
298 "*Command to start isql by SyBase.
300 Starts `sql-interactive-mode' after doing some setup.
302 The program can also specify a TCP connection. See `make-comint'."
303 :type 'file
304 :group 'SQL)
306 (defcustom sql-sybase-options nil
307 "*List of additional options for `sql-sybase-program'.
308 Some versions of isql might require the -n option in order to work."
309 :type '(repeat string)
310 :version "20.8"
311 :group 'SQL)
313 ;; Customization for Informix
315 (defcustom sql-informix-program "dbaccess"
316 "*Command to start dbaccess by Informix.
318 Starts `sql-interactive-mode' after doing some setup.
320 The program can also specify a TCP connection. See `make-comint'."
321 :type 'file
322 :group 'SQL)
324 ;; Customization for Ingres
326 (defcustom sql-ingres-program "sql"
327 "*Command to start sql by Ingres.
329 Starts `sql-interactive-mode' after doing some setup.
331 The program can also specify a TCP connection. See `make-comint'."
332 :type 'file
333 :group 'SQL)
335 ;; Customization for Microsoft
337 (defcustom sql-ms-program "isql"
338 "*Command to start isql by Microsoft.
340 Starts `sql-interactive-mode' after doing some setup.
342 The program can also specify a TCP connection. See `make-comint'."
343 :type 'file
344 :group 'SQL)
346 (defcustom sql-ms-options '("-w" "300" "-n")
347 ;; -w is the linesize
348 "*List of additional options for `sql-ms-program'."
349 :type '(repeat string)
350 :version "21.4"
351 :group 'SQL)
353 ;; Customization for Postgres
355 (defcustom sql-postgres-program "psql"
356 "Command to start psql by Postgres.
358 Starts `sql-interactive-mode' after doing some setup.
360 The program can also specify a TCP connection. See `make-comint'."
361 :type 'file
362 :group 'SQL)
364 (defcustom sql-postgres-options '("-P" "pager=off")
365 "*List of additional options for `sql-postgres-program'.
366 The default setting includes the -P option which breaks older versions
367 of the psql client (such as version 6.5.3). The -P option is equivalent
368 to the --pset option. If you want the psql to prompt you for a user
369 name, add the string \"-u\" to the list of options. If you want to
370 provide a user name on the command line (newer versions such as 7.1),
371 add your name with a \"-U\" prefix (such as \"-Umark\") to the list."
372 :type '(repeat string)
373 :version "20.8"
374 :group 'SQL)
376 ;; Customization for Interbase
378 (defcustom sql-interbase-program "isql"
379 "*Command to start isql by Interbase.
381 Starts `sql-interactive-mode' after doing some setup.
383 The program can also specify a TCP connection. See `make-comint'."
384 :type 'file
385 :group 'SQL)
387 (defcustom sql-interbase-options nil
388 "*List of additional options for `sql-interbase-program'."
389 :type '(repeat string)
390 :version "20.8"
391 :group 'SQL)
393 ;; Customization for DB2
395 (defcustom sql-db2-program "db2"
396 "*Command to start db2 by IBM.
398 Starts `sql-interactive-mode' after doing some setup.
400 The program can also specify a TCP connection. See `make-comint'."
401 :type 'file
402 :group 'SQL)
404 (defcustom sql-db2-options nil
405 "*List of additional options for `sql-db2-program'."
406 :type '(repeat string)
407 :version "20.8"
408 :group 'SQL)
410 ;; Customization for Linter
412 (defcustom sql-linter-program "inl"
413 "*Command to start inl by RELEX.
415 Starts `sql-interactive-mode' after doing some setup."
416 :type 'file
417 :group 'SQL)
419 (defcustom sql-linter-options nil
420 "*List of additional options for `sql-linter-program'."
421 :type '(repeat string)
422 :version "21.3"
423 :group 'SQL)
427 ;;; Variables which do not need customization
429 (defvar sql-user-history nil
430 "History of usernames used.")
432 (defvar sql-database-history nil
433 "History of databases used.")
435 (defvar sql-server-history nil
436 "History of servers used.")
438 ;; Passwords are not kept in a history.
440 (defvar sql-buffer nil
441 "Current SQLi buffer.
443 The global value of sql-buffer is the name of the latest SQLi buffer
444 created. Any SQL buffer created will make a local copy of this value.
445 See `sql-interactive-mode' for more on multiple sessions. If you want
446 to change the SQLi buffer a SQL mode sends its SQL strings to, change
447 the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
449 (defvar sql-prompt-regexp nil
450 "Prompt used to initialize `comint-prompt-regexp'.
452 You can change `comint-prompt-regexp' on `sql-interactive-mode-hook'.")
454 (defvar sql-prompt-length 0
455 "Prompt used to set `left-margin' in `sql-interactive-mode'.
457 You can change it on `sql-interactive-mode-hook'.")
459 (defvar sql-alternate-buffer-name nil
460 "Buffer-local string used to possibly rename the SQLi buffer.
462 Used by `sql-rename-buffer'.")
464 ;; Keymap for sql-interactive-mode.
466 (defvar sql-interactive-mode-map
467 (let ((map (make-sparse-keymap)))
468 (if (functionp 'set-keymap-parent)
469 (set-keymap-parent map comint-mode-map); Emacs
470 (set-keymap-parents map (list comint-mode-map))); XEmacs
471 (if (functionp 'set-keymap-name)
472 (set-keymap-name map 'sql-interactive-mode-map)); XEmacs
473 (define-key map (kbd "C-j") 'sql-accumulate-and-indent)
474 (define-key map (kbd "C-c C-w") 'sql-copy-column)
475 (define-key map (kbd "O") 'sql-magic-go)
476 (define-key map (kbd "o") 'sql-magic-go)
477 (define-key map (kbd ";") 'sql-magic-semicolon)
478 map)
479 "Mode map used for `sql-interactive-mode'.
480 Based on `comint-mode-map'.")
482 ;; Keymap for sql-mode.
484 (defvar sql-mode-map
485 (let ((map (make-sparse-keymap)))
486 (define-key map (kbd "C-c C-c") 'sql-send-paragraph)
487 (define-key map (kbd "C-c C-r") 'sql-send-region)
488 (define-key map (kbd "C-c C-b") 'sql-send-buffer)
489 map)
490 "Mode map used for `sql-mode'.")
492 ;; easy menu for sql-mode.
494 (easy-menu-define
495 sql-mode-menu sql-mode-map
496 "Menu for `sql-mode'."
497 '("SQL"
498 ["Send Paragraph" sql-send-paragraph (and (buffer-live-p sql-buffer)
499 (get-buffer-process sql-buffer))]
500 ["Send Region" sql-send-region (and (or (and (boundp 'mark-active); Emacs
501 mark-active)
502 (mark)); XEmacs
503 (buffer-live-p sql-buffer)
504 (get-buffer-process sql-buffer))]
505 ["Send Buffer" sql-send-buffer (and (buffer-live-p sql-buffer)
506 (get-buffer-process sql-buffer))]
507 ["Show SQLi buffer" sql-show-sqli-buffer t]
508 ["Set SQLi buffer" sql-set-sqli-buffer t]
509 ["Pop to SQLi buffer after send"
510 sql-toggle-pop-to-buffer-after-send-region
511 :style toggle
512 :selected sql-pop-to-buffer-after-send-region]
513 ("Highlighting"
514 ["ANSI SQL keywords" sql-highlight-ansi-keywords t]
515 ["Oracle keywords" sql-highlight-oracle-keywords t]
516 ["Postgres keywords" sql-highlight-postgres-keywords t]
517 ["Linter keywords" sql-highlight-linter-keywords t]
520 ;; easy menu for sql-interactive-mode.
522 (easy-menu-define
523 sql-interactive-mode-menu sql-interactive-mode-map
524 "Menu for `sql-interactive-mode'."
525 '("SQL"
526 ["Rename Buffer" sql-rename-buffer t]))
528 ;; Abbreviations -- if you want more of them, define them in your
529 ;; ~/.emacs file. Abbrevs have to be enabled in your ~/.emacs, too.
531 (defvar sql-mode-abbrev-table nil
532 "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
533 (if sql-mode-abbrev-table
535 (let ((wrapper))
536 (define-abbrev-table 'sql-mode-abbrev-table ())
537 (define-abbrev sql-mode-abbrev-table "ins" "insert" nil 0 t)
538 (define-abbrev sql-mode-abbrev-table "upd" "update" nil 0 t)
539 (define-abbrev sql-mode-abbrev-table "del" "delete" nil 0 t)
540 (define-abbrev sql-mode-abbrev-table "sel" "select" nil 0 t)))
542 ;; Syntax Table
544 (defvar sql-mode-syntax-table
545 (let ((table (make-syntax-table)))
546 ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
547 (modify-syntax-entry ?/ ". 14" table)
548 (modify-syntax-entry ?* ". 23" table)
549 ;; double-dash starts comment
550 (if (string-match "XEmacs\\|Lucid" emacs-version)
551 (modify-syntax-entry ?- ". 56" table)
552 (modify-syntax-entry ?- ". 12b" table))
553 ;; newline and formfeed end coments
554 (modify-syntax-entry ?\n "> b" table)
555 (modify-syntax-entry ?\f "> b" table)
556 ;; single quotes (') quotes delimit strings
557 (modify-syntax-entry ?' "\"" table)
558 ;; backslash is no escape character
559 (modify-syntax-entry ?\\ "." table)
560 table)
561 "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
563 ;; Font lock support
565 (defvar sql-mode-ansi-font-lock-keywords nil
566 "ANSI SQL keywords used by font-lock.
568 This variable is used by `sql-mode' and `sql-interactive-mode'. The
569 regular expressions are created during compilation by calling the
570 function `regexp-opt'. Therefore, take a look at the source before
571 you define your own sql-mode-ansi-font-lock-keywords. You may want to
572 add functions and PL/SQL keywords.")
573 (if sql-mode-ansi-font-lock-keywords
575 (let ((ansi-keywords (eval-when-compile
576 (concat "\\b"
577 (regexp-opt '(
578 "authorization" "avg" "begin" "close" "cobol" "commit"
579 "continue" "count" "declare" "double" "end" "escape"
580 "exec" "fetch" "foreign" "fortran" "found" "go" "goto" "indicator"
581 "key" "language" "max" "min" "module" "numeric" "open" "pascal" "pli"
582 "precision" "primary" "procedure" "references" "rollback"
583 "schema" "section" "some" "sqlcode" "sqlerror" "sum" "work") t) "\\b")))
584 (ansi-reserved-words (eval-when-compile
585 (concat "\\b"
586 (regexp-opt '(
587 "all" "and" "any" "as" "asc" "between" "by" "check" "create"
588 "current" "default" "delete" "desc" "distinct" "exists" "float" "for"
589 "from" "grant" "group" "having" "in" "insert" "into" "is"
590 "like" "not" "null" "of" "on" "option" "or" "order" "privileges"
591 "public" "select" "set" "table" "to" "union" "unique"
592 "update" "user" "values" "view" "where" "with") t) "\\b")))
593 (ansi-types (eval-when-compile
594 (concat "\\b"
595 (regexp-opt '(
596 ;; ANSI Keywords that look like types
597 "character" "cursor" "dec" "int" "real"
598 ;; ANSI Reserved Word that look like types
599 "char" "integer" "smallint" ) t) "\\b"))))
600 (setq sql-mode-ansi-font-lock-keywords
601 (list (cons ansi-keywords 'font-lock-function-name-face)
602 (cons ansi-reserved-words 'font-lock-keyword-face)
603 (cons ansi-types 'font-lock-type-face)))))
605 (defvar sql-mode-oracle-font-lock-keywords nil
606 "Oracle SQL keywords used by font-lock.
608 This variable is used by `sql-mode' and `sql-interactive-mode'. The
609 regular expressions are created during compilation by calling the
610 function `regexp-opt'. Therefore, take a look at the source before
611 you define your own sql-mode-oracle-font-lock-keywords. You may want
612 to add functions and PL/SQL keywords.")
613 (if sql-mode-oracle-font-lock-keywords
615 (let ((oracle-keywords (eval-when-compile
616 (concat "\\b"
617 (regexp-opt '(
618 "admin" "after" "allocate" "analyze" "archive" "archivelog" "backup"
619 "become" "before" "block" "body" "cache" "cancel" "cascade" "change"
620 "checkpoint" "compile" "constraint" "constraints" "contents"
621 "controlfile" "cycle" "database" "datafile" "dba" "disable" "dismount"
622 "dump" "each" "else" "elsif" "enable" "events" "except" "exceptions"
623 "execute" "exit" "explain" "extent" "externally" "false" "flush" "force"
624 "freelist" "freelists" "function" "groups" "if" "including" "initrans"
625 "instance" "layer" "link" "lists" "logfile" "loop" "manage" "manual"
626 "maxdatafiles" "maxinistances" "maxlogfiles" "maxloghistory"
627 "maxlogmembers" "maxtrans" "maxvalue" "minextents" "minvalue" "mount"
628 "new" "next" "noarchivelog" "nocache" "nocycle" "nomaxvalue"
629 "nominvalue" "none" "noorder" "noresetlogs" "normal" "nosort" "off"
630 "old" "only" "optimal" "others" "out" "own" "package" "parallel"
631 "pctincrease" "pctused" "plan" "pragma" "private" "profile" "quota"
632 "raise" "read" "recover" "referencing" "resetlogs" "restrict_references"
633 "restricted" "return" "returning" "reuse" "rnds" "rnps" "role" "roles"
634 "savepoint" "scn" "segment" "sequence" "shared" "snapshot" "sort"
635 "statement_id" "statistics" "stop" "storage" "subtype" "switch" "system"
636 "tables" "tablespace" "temporary" "thread" "time" "tracing"
637 "transaction" "triggers" "true" "truncate" "type" "under" "unlimited"
638 "until" "use" "using" "when" "while" "wnds" "wnps" "write") t) "\\b")))
639 (oracle-warning-words (eval-when-compile
640 (concat "\\b"
641 (regexp-opt '(
642 "cursor_already_open" "dup_val_on_index" "exception" "invalid_cursor"
643 "invalid_number" "login_denied" "no_data_found" "not_logged_on"
644 "notfound" "others" "pragma" "program_error" "storage_error"
645 "timeout_on_resource" "too_many_rows" "transaction_backed_out"
646 "value_error" "zero_divide") t) "\\b")))
647 (oracle-reserved-words (eval-when-compile
648 (concat "\\b"
649 (regexp-opt '(
650 "access" "add" "alter" "audit" "cluster" "column" "comment" "compress"
651 "connect" "drop" "else" "exclusive" "file" "grant"
652 "identified" "immediate" "increment" "index" "initial" "intersect"
653 "level" "lock" "long" "maxextents" "minus" "mode" "modify" "noaudit"
654 "nocompress" "nowait" "number" "offline" "online" "pctfree" "prior"
655 "raw" "rename" "resource" "revoke" "row" "rowlabel" "rownum"
656 "rows" "session" "share" "size" "start" "successful" "synonym" "sysdate"
657 "then" "trigger" "uid" "validate" "whenever") t) "\\b")))
658 (oracle-types (eval-when-compile
659 (concat "\\b"
660 (regexp-opt '(
661 ;; Oracle Keywords that look like types
662 ;; Oracle Reserved Words that look like types
663 "binary_integer" "blob" "boolean" "constant" "date" "decimal" "rowid"
664 "varchar" "varchar2") t) "\\b")))
665 (oracle-builtin-functions (eval-when-compile
666 (concat "\\b"
667 (regexp-opt '(
668 ;; Misc Oracle builtin functions
669 "abs" "add_months" "ascii" "avg" "ceil" "chartorowid" "chr" "concat"
670 "convert" "cos" "cosh" "count" "currval" "decode" "dump" "exp" "floor"
671 "glb" "greatest" "greatest_lb" "hextoraw" "initcap" "instr" "instrb"
672 "last_day" "least" "least_ub" "length" "lengthb" "ln" "log" "lower"
673 "lpad" "ltrim" "lub" "max" "min" "mod" "months_between" "new_time"
674 "next_day" "nextval" "nls_initcap" "nls_lower" "nls_upper" "nlssort"
675 "nvl" "power" "rawtohex" "replace" "round" "rowidtochar" "rpad"
676 "rtrim" "sign" "sin" "sinh" "soundex" "sqlcode" "sqlerrm" "sqrt"
677 "stddev" "sum" "substr" "substrb" "tan" "tanh" "to_char"
678 "to_date" "to_label" "to_multi_byte" "to_number" "to_single_byte"
679 "translate" "trim" "trunc" "uid" "upper" "userenv" "variance" "vsize") t) "\\b"))))
680 (setq sql-mode-oracle-font-lock-keywords
681 (append sql-mode-ansi-font-lock-keywords
682 (list (cons oracle-keywords 'font-lock-function-name-face)
683 (cons oracle-warning-words 'font-lock-warning-face)
684 (cons oracle-reserved-words 'font-lock-keyword-face)
685 ;; XEmacs doesn't have font-lock-builtin-face
686 (if (string-match "XEmacs\\|Lucid" emacs-version)
687 (cons oracle-builtin-functions 'font-lock-preprocessor-face)
688 ;; GNU Emacs 19 doesn't have it either
689 (if (string-match "GNU Emacs 19" emacs-version)
690 (cons oracle-builtin-functions 'font-lock-function-name-face)
691 ;; Emacs
692 (cons oracle-builtin-functions 'font-lock-builtin-face)))
693 (cons oracle-types 'font-lock-type-face))))))
695 (defvar sql-mode-postgres-font-lock-keywords nil
696 "Postgres SQL keywords used by font-lock.
698 This variable is used by `sql-mode' and `sql-interactive-mode'. The
699 regular expressions are created during compilation by calling the
700 function `regexp-opt'. Therefore, take a look at the source before
701 you define your own sql-mode-postgres-font-lock-keywords.")
703 (if sql-mode-postgres-font-lock-keywords
705 (let ((postgres-reserved-words (eval-when-compile
706 (concat "\\b"
707 (regexp-opt '(
708 "language"
709 ) t) "\\b")))
710 (postgres-types (eval-when-compile
711 (concat "\\b"
712 (regexp-opt '(
713 "bool" "box" "circle" "char" "char2" "char4" "char8" "char16" "date"
714 "float4" "float8" "int2" "int4" "int8" "line" "lseg" "money" "path"
715 "point" "polygon" "serial" "text" "time" "timespan" "timestamp" "varchar"
716 ) t)"\\b")))
717 (postgres-builtin-functions (eval-when-compile
718 (concat "\\b"
719 (regexp-opt '(
720 ;; Misc Postgres builtin functions
721 "abstime" "age" "area" "box" "center" "date_part" "date_trunc"
722 "datetime" "dexp" "diameter" "dpow" "float" "float4" "height"
723 "initcap" "integer" "isclosed" "isfinite" "isoldpath" "isopen"
724 "length" "lower" "lpad" "ltrim" "pclose" "point" "points" "popen"
725 "position" "radius" "reltime" "revertpoly" "rpad" "rtrim" "substr"
726 "substring" "text" "timespan" "translate" "trim" "upgradepath"
727 "upgradepoly" "upper" "varchar" "width"
728 ) t) "\\b"))))
729 (setq sql-mode-postgres-font-lock-keywords
730 (append sql-mode-ansi-font-lock-keywords
731 (list (cons postgres-reserved-words 'font-lock-keyword-face)
732 ;; XEmacs doesn't have 'font-lock-builtin-face
733 (if (string-match "XEmacs\\|Lucid" emacs-version)
734 (cons postgres-builtin-functions 'font-lock-preprocessor-face)
735 ;; Emacs
736 (cons postgres-builtin-functions 'font-lock-builtin-face))
737 (cons postgres-types 'font-lock-type-face))))))
740 (defvar sql-mode-linter-font-lock-keywords nil
741 "Linter SQL keywords used by font-lock.
743 This variable is used by `sql-mode' and `sql-interactive-mode'. The
744 regular expressions are created during compilation by calling the
745 function `regexp-opt'.")
747 (if sql-mode-linter-font-lock-keywords
749 (let ((linter-keywords (eval-when-compile
750 (concat "\\b"
751 (regexp-opt '(
752 "autocommit" "autoinc" "autorowid" "cancel" "cascade" "channel"
753 "committed" "count" "countblob" "cross" "current" "data" "database"
754 "datafile" "datafiles" "datesplit" "dba" "dbname" "default" "deferred"
755 "denied" "description" "device" "difference" "directory" "error"
756 "escape" "euc" "exclusive" "external" "extfile" "false" "file"
757 "filename" "filesize" "filetime" "filter" "findblob" "first" "foreign"
758 "full" "fuzzy" "global" "granted" "ignore" "immediate" "increment"
759 "indexes" "indexfile" "indexfiles" "indextime" "initial" "integrity"
760 "internal" "key" "last_autoinc" "last_rowid" "limit" "linter"
761 "linter_file_device" "linter_file_size" "linter_name_length" "ln"
762 "local" "login" "maxisn" "maxrow" "maxrowid" "maxvalue" "message"
763 "minvalue" "module" "names" "national" "natural" "new" "new_table"
764 "no" "node" "noneuc" "nulliferror" "numbers" "off" "old" "old_table"
765 "only" "operation" "optimistic" "option" "page" "partially" "password"
766 "phrase" "plan" "precision" "primary" "priority" "privileges"
767 "proc_info_size" "proc_par_name_len" "protocol" "quant" "range" "raw"
768 "read" "record" "records" "references" "remote" "rename" "replication"
769 "restart" "rewrite" "root" "row" "rule" "savepoint" "security"
770 "sensitive" "sequence" "serializable" "server" "since" "size" "some"
771 "startup" "statement" "station" "success" "sys_guid" "tables" "test"
772 "timeout" "trace" "transaction" "translation" "trigger"
773 "trigger_info_size" "true" "trunc" "uncommitted" "unicode" "unknown"
774 "unlimited" "unlisted" "user" "utf8" "value" "varying" "volumes"
775 "wait" "windows_code" "workspace" "write" "xml"
776 ) t) "\\b")))
777 (linter-reserved-words (eval-when-compile
778 (concat "\\b"
779 (regexp-opt '(
780 "access" "action" "add" "address" "after" "all" "alter" "always" "and"
781 "any" "append" "as" "asc" "ascic" "async" "at_begin" "at_end" "audit"
782 "aud_obj_name_len" "backup" "base" "before" "between" "blobfile"
783 "blobfiles" "blobpct" "brief" "browse" "by" "case" "cast" "check"
784 "clear" "close" "column" "comment" "commit" "connect" "contains"
785 "correct" "create" "delete" "desc" "disable" "disconnect" "distinct"
786 "drop" "each" "ef" "else" "enable" "end" "event" "except" "exclude"
787 "execute" "exists" "extract" "fetch" "finish" "for" "from" "get"
788 "grant" "group" "having" "identified" "in" "index" "inner" "insert"
789 "instead" "intersect" "into" "is" "isolation" "join" "left" "level"
790 "like" "lock" "mode" "modify" "not" "nowait" "null" "of" "on" "open"
791 "or" "order" "outer" "owner" "press" "prior" "procedure" "public"
792 "purge" "rebuild" "resource" "restrict" "revoke" "right" "role"
793 "rollback" "rownum" "select" "session" "set" "share" "shutdown"
794 "start" "stop" "sync" "synchronize" "synonym" "sysdate" "table" "then"
795 "to" "union" "unique" "unlock" "until" "update" "using" "values"
796 "view" "when" "where" "with" "without"
797 ) t) "\\b")))
798 (linter-types (eval-when-compile
799 (concat "\\b"
800 (regexp-opt '(
801 "bigint" "bitmap" "blob" "boolean" "char" "character" "date"
802 "datetime" "dec" "decimal" "double" "float" "int" "integer" "nchar"
803 "number" "numeric" "real" "smallint" "varbyte" "varchar" "byte"
804 "cursor" "long"
805 ) t) "\\b")))
806 (linter-builtin-functions (eval-when-compile
807 (concat "\\b"
808 (regexp-opt '(
809 "abs" "acos" "asin" "atan" "atan2" "avg" "ceil" "cos" "cosh" "divtime"
810 "exp" "floor" "getbits" "getblob" "getbyte" "getlong" "getraw"
811 "getstr" "gettext" "getword" "hextoraw" "lenblob" "length" "log"
812 "lower" "lpad" "ltrim" "max" "min" "mod" "monthname" "nvl"
813 "octet_length" "power" "rand" "rawtohex" "repeat_string"
814 "right_substr" "round" "rpad" "rtrim" "sign" "sin" "sinh" "soundex"
815 "sqrt" "sum" "tan" "tanh" "timeint_to_days" "to_char" "to_date"
816 "to_gmtime" "to_localtime" "to_number" "trim" "upper" "decode"
817 "substr" "substring" "chr" "dayname" "days" "greatest" "hex" "initcap"
818 "instr" "least" "multime" "replace" "width"
819 ) t) "\\b"))))
820 (setq sql-mode-linter-font-lock-keywords
821 (append sql-mode-ansi-font-lock-keywords
822 (list (cons linter-keywords 'font-lock-function-name-face)
823 (cons linter-reserved-words 'font-lock-keyword-face)
824 ;; XEmacs doesn't have font-lock-builtin-face
825 (if (string-match "XEmacs\\|Lucid" emacs-version)
826 (cons linter-builtin-functions 'font-lock-preprocessor-face)
827 ;; GNU Emacs 19 doesn't have it either
828 (if (string-match "GNU Emacs 19" emacs-version)
829 (cons linter-builtin-functions 'font-lock-function-name-face)
830 ;; Emacs
831 (cons linter-builtin-functions 'font-lock-builtin-face)))
832 (cons linter-types 'font-lock-type-face))))))
834 (defvar sql-mode-font-lock-keywords sql-mode-ansi-font-lock-keywords
835 "SQL keywords used by font-lock.
837 This variable defaults to `sql-mode-ansi-font-lock-keywords'. This is
838 used for the default `font-lock-defaults' value in `sql-mode'. This
839 can be changed by some entry functions to provide more hilighting.")
843 ;;; Functions to switch highlighting
845 (defun sql-highlight-oracle-keywords ()
846 "Highlight Oracle keywords.
847 Basically, this just sets `font-lock-keywords' appropriately."
848 (interactive)
849 (setq font-lock-keywords sql-mode-oracle-font-lock-keywords)
850 (font-lock-fontify-buffer))
852 (defun sql-highlight-postgres-keywords ()
853 "Highlight Postgres keywords.
854 Basically, this just sets `font-lock-keywords' appropriately."
855 (interactive)
856 (setq font-lock-keywords sql-mode-postgres-font-lock-keywords)
857 (font-lock-fontify-buffer))
859 (defun sql-highlight-linter-keywords ()
860 "Highlight LINTER keywords.
861 Basically, this just sets `font-lock-keywords' appropriately."
862 (interactive)
863 (setq font-lock-keywords sql-mode-linter-font-lock-keywords)
864 (font-lock-fontify-buffer))
866 (defun sql-highlight-ansi-keywords ()
867 "Highlight ANSI SQL keywords.
868 Basically, this just sets `font-lock-keywords' appropriately."
869 (interactive)
870 (setq font-lock-keywords sql-mode-ansi-font-lock-keywords)
871 (font-lock-fontify-buffer))
875 ;;; Compatibility functions
877 (if (not (fboundp 'comint-line-beginning-position))
878 ;; comint-line-beginning-position is defined in Emacs 21
879 (defun comint-line-beginning-position ()
880 "Returns the buffer position of the beginning of the line, after any prompt.
881 The prompt is assumed to be any text at the beginning of the line matching
882 the regular expression `comint-prompt-regexp', a buffer local variable."
883 (save-excursion (comint-bol nil) (point))))
887 ;;; Small functions
889 (defun sql-magic-go (arg)
890 "Insert \"o\" and call `comint-send-input'.
891 `sql-electric-stuff' must be the symbol `go'."
892 (interactive "P")
893 (self-insert-command (prefix-numeric-value arg))
894 (if (and (equal sql-electric-stuff 'go)
895 (save-excursion
896 (comint-bol nil)
897 (looking-at "go\\b")))
898 (comint-send-input)))
900 (defun sql-magic-semicolon (arg)
901 "Insert semicolon and call `comint-send-input'.
902 `sql-electric-stuff' must be the symbol `semicolon'."
903 (interactive "P")
904 (self-insert-command (prefix-numeric-value arg))
905 (if (equal sql-electric-stuff 'semicolon)
906 (comint-send-input)))
908 (defun sql-accumulate-and-indent ()
909 "Continue SQL statement on the next line."
910 (interactive)
911 (if (fboundp 'comint-accumulate)
912 (comint-accumulate)
913 (newline))
914 (indent-according-to-mode))
916 ;;;###autoload
917 (defun sql-help ()
918 "Show short help for the SQL modes.
920 Use an entry function to open an interactive SQL buffer. This buffer is
921 usually named `*SQL*'. The name of the major mode is SQLi.
923 Use the following commands to start a specific SQL interpreter:
925 PostGres: \\[sql-postgres]
926 MySQL: \\[sql-mysql]
928 Other non-free SQL implementations are also supported:
930 Solid: \\[sql-solid]
931 Oracle: \\[sql-oracle]
932 Informix: \\[sql-informix]
933 Sybase: \\[sql-sybase]
934 Ingres: \\[sql-ingres]
935 Microsoft: \\[sql-ms]
936 Interbase: \\[sql-interbase]
937 Linter: \\[sql-linter]
939 But we urge you to choose a free implementation instead of these.
941 Once you have the SQLi buffer, you can enter SQL statements in the
942 buffer. The output generated is appended to the buffer and a new prompt
943 is generated. See the In/Out menu in the SQLi buffer for some functions
944 that help you navigate through the buffer, the input history, etc.
946 If you have a really complex SQL statement or if you are writing a
947 procedure, you can do this in a separate buffer. Put the new buffer in
948 `sql-mode' by calling \\[sql-mode]. The name of this buffer can be
949 anything. The name of the major mode is SQL.
951 In this SQL buffer (SQL mode), you can send the region or the entire
952 buffer to the interactive SQL buffer (SQLi mode). The results are
953 appended to the SQLi buffer without disturbing your SQL buffer."
954 (interactive)
955 (describe-function 'sql-help))
957 (defun sql-read-passwd (prompt &optional default)
958 "Read a password using PROMPT.
959 Optional DEFAULT is password to start with. This function calls
960 `read-passwd' if it is available. If not, function
961 `ange-ftp-read-passwd' is called. This should always be available,
962 even in old versions of Emacs."
963 (if (fboundp 'read-passwd)
964 (read-passwd prompt nil default)
965 (unless (fboundp 'ange-ftp-read-passwd)
966 (autoload 'ange-ftp-read-passwd "ange-ftp"))
967 (ange-ftp-read-passwd prompt default)))
969 (defun sql-get-login (&rest what)
970 "Get username, password and database from the user.
972 The variables `sql-user', `sql-password', `sql-server', and
973 `sql-database' can be customized. They are used as the default values.
974 Usernames, servers and databases are stored in `sql-user-history',
975 `sql-server-history' and `database-history'. Passwords are not stored
976 in a history.
978 Parameter WHAT is a list of the arguments passed to this function.
979 The function asks for the username if WHAT contains symbol `user', for
980 the password if it contains symbol `password', for the server if it
981 contains symbol `server', and for the database if it contains symbol
982 `database'.
984 In order to ask the user for username, password and database, call the
985 function like this: (sql-get-login 'user 'password 'database)."
986 (interactive)
987 (if (memq 'user what)
988 (setq sql-user
989 (read-from-minibuffer "User: " sql-user nil nil
990 sql-user-history)))
991 (if (memq 'password what)
992 (setq sql-password
993 (sql-read-passwd "Password: " sql-password)))
994 (if (memq 'server what)
995 (setq sql-server
996 (read-from-minibuffer "Server: " sql-server nil nil
997 sql-server-history)))
998 (if (memq 'database what)
999 (setq sql-database
1000 (read-from-minibuffer "Database: " sql-database nil nil
1001 sql-database-history))))
1003 (defun sql-find-sqli-buffer ()
1004 "Return the current default SQLi buffer or nil.
1005 In order to qualify, the SQLi buffer must be alive,
1006 be in `sql-interactive-mode' and have a process."
1007 (let ((default-buffer (default-value 'sql-buffer)))
1008 (if (and (buffer-live-p default-buffer)
1009 (get-buffer-process default-buffer))
1010 default-buffer
1011 (save-excursion
1012 (let ((buflist (buffer-list))
1013 (found))
1014 (while (not (or (null buflist)
1015 found))
1016 (let ((candidate (car buflist)))
1017 (set-buffer candidate)
1018 (if (and (equal major-mode 'sql-interactive-mode)
1019 (get-buffer-process candidate))
1020 (setq found candidate))
1021 (setq buflist (cdr buflist))))
1022 found)))))
1024 (defun sql-set-sqli-buffer-generally ()
1025 "Set SQLi buffer for all SQL buffers that have none.
1026 This function checks all SQL buffers for their SQLi buffer. If their
1027 SQLi buffer is nonexistent or has no process, it is set to the current
1028 default SQLi buffer. The current default SQLi buffer is determined
1029 using `sql-find-sqli-buffer'. If `sql-buffer' is set,
1030 `sql-set-sqli-hook' is run."
1031 (interactive)
1032 (save-excursion
1033 (let ((buflist (buffer-list))
1034 (default-sqli-buffer (sql-find-sqli-buffer)))
1035 (setq-default sql-buffer default-sqli-buffer)
1036 (while (not (null buflist))
1037 (let ((candidate (car buflist)))
1038 (set-buffer candidate)
1039 (if (and (equal major-mode 'sql-mode)
1040 (not (buffer-live-p sql-buffer)))
1041 (progn
1042 (setq sql-buffer default-sqli-buffer)
1043 (run-hooks 'sql-set-sqli-hook))))
1044 (setq buflist (cdr buflist))))))
1046 (defun sql-set-sqli-buffer ()
1047 "Set the SQLi buffer SQL strings are sent to.
1049 Call this function in a SQL buffer in order to set the SQLi buffer SQL
1050 strings are sent to. Calling this function sets `sql-buffer' and runs
1051 `sql-set-sqli-hook'.
1053 If you call it from a SQL buffer, this sets the local copy of
1054 `sql-buffer'.
1056 If you call it from anywhere else, it sets the global copy of
1057 `sql-buffer'."
1058 (interactive)
1059 (let ((default-buffer (sql-find-sqli-buffer)))
1060 (if (null default-buffer)
1061 (error "There is no suitable SQLi buffer"))
1062 (let ((new-buffer
1063 (get-buffer
1064 (read-buffer "New SQLi buffer: " default-buffer t))))
1065 (if (null (get-buffer-process new-buffer))
1066 (error "Buffer %s has no process" (buffer-name new-buffer)))
1067 (if (null (save-excursion
1068 (set-buffer new-buffer)
1069 (equal major-mode 'sql-interactive-mode)))
1070 (error "Buffer %s is no SQLi buffer" (buffer-name new-buffer)))
1071 (if new-buffer
1072 (progn
1073 (setq sql-buffer new-buffer)
1074 (run-hooks 'sql-set-sqli-hook))))))
1076 (defun sql-show-sqli-buffer ()
1077 "Show the name of current SQLi buffer.
1079 This is the buffer SQL strings are sent to. It is stored in the
1080 variable `sql-buffer'. See `sql-help' on how to create such a buffer."
1081 (interactive)
1082 (if (null (buffer-live-p sql-buffer))
1083 (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
1084 (if (null (get-buffer-process sql-buffer))
1085 (message "Buffer %s has no process." (buffer-name sql-buffer))
1086 (message "Current SQLi buffer is %s." (buffer-name sql-buffer)))))
1088 (defun sql-make-alternate-buffer-name ()
1089 "Return a string that can be used to rename a SQLi buffer.
1091 This is used to set `sql-alternate-buffer-name' within
1092 `sql-interactive-mode'."
1093 (concat (if (string= "" sql-user)
1094 (if (string= "" (user-login-name))
1096 (concat (user-login-name) "/"))
1097 (concat sql-user "/"))
1098 (if (string= "" sql-database)
1099 (if (string= "" sql-server)
1100 (system-name)
1101 sql-server)
1102 sql-database)))
1104 (defun sql-rename-buffer ()
1105 "Renames a SQLi buffer."
1106 (interactive)
1107 (rename-buffer (format "*SQL: %s*" sql-alternate-buffer-name) t))
1109 (defun sql-copy-column ()
1110 "Copy current column to the end of buffer.
1111 Inserts SELECT or commas if appropriate."
1112 (interactive)
1113 (let ((column))
1114 (save-excursion
1115 (setq column (buffer-substring
1116 (progn (forward-char 1) (backward-sexp 1) (point))
1117 (progn (forward-sexp 1) (point))))
1118 (goto-char (point-max))
1119 (let ((bol (comint-line-beginning-position)))
1120 (cond
1121 ;; if empty command line, insert SELECT
1122 ((= bol (point))
1123 (insert "SELECT "))
1124 ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
1125 ((save-excursion
1126 (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
1127 bol t))
1128 (insert ", "))
1129 ;; else insert a space
1131 (if (eq (preceding-char) ? )
1133 (insert " ")))))
1134 ;; in any case, insert the column
1135 (insert column)
1136 (message "%s" column))))
1138 ;; On NT, SQL*Plus for Oracle turns on full buffering for stdout if it
1139 ;; is not attached to a character device; therefore placeholder
1140 ;; replacement by SQL*Plus is fully buffered. The workaround lets
1141 ;; Emacs query for the placeholders.
1143 (defvar sql-placeholder-history nil
1144 "History of placeholder values used.")
1146 (defun sql-query-placeholders-and-send (proc string)
1147 "Send to PROC input STRING, maybe replacing placeholders.
1148 Placeholders are words starting with and ampersand like &this.
1149 This function is used for `comint-input-sender' if using `sql-oracle' on NT."
1150 (while (string-match "&\\(\\sw+\\)" string)
1151 (setq string (replace-match
1152 (read-from-minibuffer
1153 (format "Enter value for %s: " (match-string 1 string))
1154 nil nil nil sql-placeholder-history)
1155 t t string)))
1156 (comint-send-string proc string)
1157 (if comint-input-sender-no-newline
1158 (if (not (string-equal string ""))
1159 (process-send-eof))
1160 (comint-send-string proc "\n")))
1162 ;; Using DB2 interactively, newlines must be escaped with " \".
1163 ;; The space before the backslash is relevant.
1164 (defun sql-escape-newlines-and-send (proc string)
1165 "Send to PROC input STRING, escaping newlines if necessary.
1166 Every newline in STRING will be preceded with a space and a backslash."
1167 (let ((result "") (start 0) mb me)
1168 (while (string-match "\n" string start)
1169 (setq mb (match-beginning 0)
1170 me (match-end 0))
1171 (if (and (> mb 1)
1172 (string-equal " \\" (substring string (- mb 2) mb)))
1173 (setq result (concat result (substring string start me)))
1174 (setq result (concat result (substring string start mb) " \\\n")))
1175 (setq start me))
1176 (setq result (concat result (substring string start)))
1177 (comint-send-string proc result)
1178 (if comint-input-sender-no-newline
1179 (if (not (string-equal string ""))
1180 (process-send-eof))
1181 (comint-send-string proc "\n"))))
1185 ;;; Sending the region to the SQLi buffer.
1187 (defun sql-send-region (start end)
1188 "Send a region to the SQL process."
1189 (interactive "r")
1190 (if (buffer-live-p sql-buffer)
1191 (save-excursion
1192 (comint-send-region sql-buffer start end)
1193 (if (string-match "\n$" (buffer-substring start end))
1195 (comint-send-string sql-buffer "\n"))
1196 (message "Sent string to buffer %s." (buffer-name sql-buffer))
1197 (if sql-pop-to-buffer-after-send-region
1198 (pop-to-buffer sql-buffer)
1199 (display-buffer sql-buffer)))
1200 (message "No SQL process started.")))
1202 (defun sql-send-paragraph ()
1203 "Send the current paragraph to the SQL process."
1204 (interactive)
1205 (let ((start (save-excursion
1206 (backward-paragraph)
1207 (point)))
1208 (end (save-excursion
1209 (forward-paragraph)
1210 (point))))
1211 (sql-send-region start end)))
1213 (defun sql-send-buffer ()
1214 "Send the buffer contents to the SQL process."
1215 (interactive)
1216 (sql-send-region (point-min) (point-max)))
1218 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
1219 "Toggle `sql-pop-to-buffer-after-send-region'.
1221 If given the optional parameter VALUE, sets
1222 sql-toggle-pop-to-buffer-after-send-region to VALUE."
1223 (interactive "P")
1224 (if value
1225 (setq sql-pop-to-buffer-after-send-region value)
1226 (setq sql-pop-to-buffer-after-send-region
1227 (null sql-pop-to-buffer-after-send-region ))))
1231 ;;; SQL mode -- uses SQL interactive mode
1233 ;;;###autoload
1234 (defun sql-mode ()
1235 "Major mode to edit SQL.
1237 You can send SQL statements to the SQLi buffer using
1238 \\[sql-send-region]. Such a buffer must exist before you can do this.
1239 See `sql-help' on how to create SQLi buffers.
1241 \\{sql-mode-map}
1242 Customization: Entry to this mode runs the `sql-mode-hook'.
1244 When you put a buffer in SQL mode, the buffer stores the last SQLi
1245 buffer created as its destination in the variable `sql-buffer'. This
1246 will be the buffer \\[sql-send-region] sends the region to. If this
1247 SQLi buffer is killed, \\[sql-send-region] is no longer able to
1248 determine where the strings should be sent to. You can set the
1249 value of `sql-buffer' using \\[sql-set-sqli-buffer].
1251 For information on how to create multiple SQLi buffers, see
1252 `sql-interactive-mode'.
1254 Note that SQL doesn't have an escape character unless you specify
1255 one. If you specify backslash as escape character in SQL,
1256 you must tell Emacs. Here's how to do that in your `~/.emacs' file:
1258 \(add-hook 'sql-mode-hook
1259 (lambda ()
1260 (modify-syntax-entry ?\\\\ \".\" sql-mode-syntax-table)))"
1261 (interactive)
1262 (kill-all-local-variables)
1263 (setq major-mode 'sql-mode)
1264 (setq mode-name "SQL")
1265 (use-local-map sql-mode-map)
1266 (if sql-mode-menu
1267 (easy-menu-add sql-mode-menu)); XEmacs
1268 (set-syntax-table sql-mode-syntax-table)
1269 (make-local-variable 'font-lock-defaults)
1270 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1271 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1272 ;; will have just one quote. Therefore syntactic hilighting is
1273 ;; disabled for interactive buffers. `_' and `.' are considered part
1274 ;; of words.
1275 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1276 nil t ((?_ . "w") (?. . "w"))))
1277 (make-local-variable 'comment-start)
1278 (setq comment-start "--")
1279 ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
1280 (make-local-variable 'sql-buffer)
1281 ;; Add imenu support for sql-mode. Note that imenu-generic-expression
1282 ;; is buffer-local, so we don't need a local-variable for it. SQL is
1283 ;; case-insensitive, that's why we have to set imenu-case-fold-search.
1284 ;; imenu-syntax-alist makes sure that `_' is considered part of object
1285 ;; names.
1286 (setq imenu-generic-expression sql-imenu-generic-expression
1287 imenu-case-fold-search t
1288 imenu-syntax-alist '(("_" . "w")))
1289 ;; Make `sql-send-paragraph' work on paragraphs that contain indented
1290 ;; lines.
1291 (make-local-variable 'paragraph-separate)
1292 (make-local-variable 'paragraph-start)
1293 (setq paragraph-separate "[\f]*$"
1294 paragraph-start "[\n\f]")
1295 ;; Abbrevs
1296 (setq local-abbrev-table sql-mode-abbrev-table)
1297 (setq abbrev-all-caps 1)
1298 ;; Run hook
1299 (run-hooks 'sql-mode-hook))
1303 ;;; SQL interactive mode
1305 (put 'sql-interactive-mode 'mode-class 'special)
1307 (defun sql-interactive-mode ()
1308 "Major mode to use a SQL interpreter interactively.
1310 Do not call this function by yourself. The environment must be
1311 initialized by an entry function specific for the SQL interpreter. See
1312 `sql-help' for a list of available entry functions.
1314 \\[comint-send-input] after the end of the process' output sends the
1315 text from the end of process to the end of the current line.
1316 \\[comint-send-input] before end of process output copies the current
1317 line minus the prompt to the end of the buffer and sends it.
1318 \\[comint-copy-old-input] just copies the current line.
1319 Use \\[sql-accumulate-and-indent] to enter multi-line statements.
1321 If you want to make multiple SQL buffers, rename the `*SQL*' buffer
1322 using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
1323 See `sql-help' for a list of available entry functions. The last buffer
1324 created by such an entry function is the current SQLi buffer. SQL
1325 buffers will send strings to the SQLi buffer current at the time of
1326 their creation. See `sql-mode' for details.
1328 Sample session using two connections:
1330 1. Create first SQLi buffer by calling an entry function.
1331 2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
1332 3. Create a SQL buffer \"test1.sql\".
1333 4. Create second SQLi buffer by calling an entry function.
1334 5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
1335 6. Create a SQL buffer \"test2.sql\".
1337 Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
1338 buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
1339 will send the region to buffer \"*Connection 2*\".
1341 If you accidentally suspend your process, use \\[comint-continue-subjob]
1342 to continue it. On some operating systems, this will not work because
1343 the signals are not supported.
1345 \\{sql-interactive-mode-map}
1346 Customization: Entry to this mode runs the hooks on `comint-mode-hook'
1347 and `sql-interactive-mode-hook' (in that order). Before each input, the
1348 hooks on `comint-input-filter-functions' are run. After each SQL
1349 interpreter output, the hooks on `comint-output-filter-functions' are
1350 run.
1352 Variable `sql-input-ring-file-name' controls the initialisation of the
1353 input ring history.
1355 Variables `comint-output-filter-functions', a hook, and
1356 `comint-scroll-to-bottom-on-input' and
1357 `comint-scroll-to-bottom-on-output' control whether input and output
1358 cause the window to scroll to the end of the buffer.
1360 If you want to make SQL buffers limited in length, add the function
1361 `comint-truncate-buffer' to `comint-output-filter-functions'.
1363 Here is an example for your .emacs file. It keeps the SQLi buffer a
1364 certain length.
1366 \(add-hook 'sql-interactive-mode-hook
1367 \(function (lambda ()
1368 \(setq comint-output-filter-functions 'comint-truncate-buffer))))
1370 Here is another example. It will always put point back to the statement
1371 you entered, right above the output it created.
1373 \(setq comint-output-filter-functions
1374 \(function (lambda (STR) (comint-show-output))))"
1375 (comint-mode)
1376 (setq comint-prompt-regexp sql-prompt-regexp)
1377 (setq left-margin sql-prompt-length)
1378 (setq major-mode 'sql-interactive-mode)
1379 (setq mode-name "SQLi")
1380 (use-local-map sql-interactive-mode-map)
1381 (if sql-interactive-mode-menu
1382 (easy-menu-add sql-interactive-mode-menu)); XEmacs
1383 (set-syntax-table sql-mode-syntax-table)
1384 (make-local-variable 'font-lock-defaults)
1385 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1386 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1387 ;; will have just one quote. Therefore syntactic hilighting is
1388 ;; disabled for interactive buffers. `_' and `.' are considered part
1389 ;; of words.
1390 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1391 t t ((?_ . "w") (?. . "w"))))
1392 ;; Enable commenting and uncommenting of the region.
1393 (make-local-variable 'comment-start)
1394 (setq comment-start "--")
1395 ;; Abbreviation table init and case-insensitive. It is not activatet
1396 ;; by default.
1397 (setq local-abbrev-table sql-mode-abbrev-table)
1398 (setq abbrev-all-caps 1)
1399 ;; Exiting the process will call sql-stop.
1400 (set-process-sentinel (get-buffer-process sql-buffer) 'sql-stop)
1401 ;; People wanting a different history file for each
1402 ;; buffer/process/client/whatever can change separator and file-name
1403 ;; on the sql-interactive-mode-hook.
1404 (setq comint-input-ring-separator sql-input-ring-separator
1405 comint-input-ring-file-name sql-input-ring-file-name)
1406 ;; Create a usefull name for renaming this buffer later.
1407 (make-local-variable 'sql-alternate-buffer-name)
1408 (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
1409 ;; User stuff.
1410 (run-hooks 'sql-interactive-mode-hook)
1411 ;; Calling the hook before calling comint-read-input-ring allows users
1412 ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
1413 (comint-read-input-ring t))
1415 (defun sql-stop (process event)
1416 "Called when the SQL process is stopped.
1418 Writes the input history to a history file using
1419 `comint-write-input-ring' and inserts a short message in the SQL buffer.
1421 This function is a sentinel watching the SQL interpreter process.
1422 Sentinels will always get the two parameters PROCESS and EVENT."
1423 (comint-write-input-ring)
1424 (if (and (eq (current-buffer) sql-buffer)
1425 (not buffer-read-only))
1426 (insert (format "\nProcess %s %s\n" process event))
1427 (message "Process %s %s" process event)))
1431 ;;; Entry functions for different SQL interpreters.
1433 ;;;###autoload
1434 (defun sql-oracle ()
1435 "Run sqlplus by Oracle as an inferior process.
1437 If buffer `*SQL*' exists but no process is running, make a new process.
1438 If buffer exists and a process is running, just switch to buffer
1439 `*SQL*'.
1441 Interpreter used comes from variable `sql-oracle-program'. Login uses
1442 the variables `sql-user', `sql-password', and `sql-database' as
1443 defaults, if set. Additional command line parameters can be stored in
1444 the list `sql-oracle-options'.
1446 The buffer is put in sql-interactive-mode, giving commands for sending
1447 input. See `sql-interactive-mode'.
1449 To specify a coding system for converting non-ASCII characters
1450 in the input and output to the process, use \\[universal-coding-system-argument]
1451 before \\[sql-oracle]. You can also specify this with \\[set-buffer-process-coding-system]
1452 in the SQL buffer, after you start the process.
1453 The default comes from `process-coding-system-alist' and
1454 `default-process-coding-system'.
1456 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1457 (interactive)
1458 (if (comint-check-proc "*SQL*")
1459 (pop-to-buffer "*SQL*")
1460 (sql-get-login 'user 'password 'database)
1461 (message "Login...")
1462 ;; Produce user/password@database construct. Password without user
1463 ;; is meaningless; database without user/password is meaningless,
1464 ;; because "@param" will ask sqlplus to interpret the script
1465 ;; "param".
1466 (let ((parameter nil))
1467 (if (not (string= "" sql-user))
1468 (if (not (string= "" sql-password))
1469 (setq parameter (concat sql-user "/" sql-password))
1470 (setq parameter sql-user)))
1471 (if (and parameter (not (string= "" sql-database)))
1472 (setq parameter (concat parameter "@" sql-database)))
1473 (if parameter
1474 (setq parameter (nconc (list parameter) sql-oracle-options))
1475 (setq parameter sql-oracle-options))
1476 (if parameter
1477 (set-buffer (apply 'make-comint "SQL" sql-oracle-program nil
1478 parameter))
1479 (set-buffer (make-comint "SQL" sql-oracle-program nil))))
1480 (setq sql-prompt-regexp "^SQL> ")
1481 (setq sql-prompt-length 5)
1482 (setq sql-buffer (current-buffer))
1483 ;; set sql-mode-font-lock-keywords to something different before
1484 ;; calling sql-interactive-mode.
1485 (setq sql-mode-font-lock-keywords sql-mode-oracle-font-lock-keywords)
1486 (sql-interactive-mode)
1487 ;; If running on NT, make sure we do placeholder replacement
1488 ;; ourselves. This must come after sql-interactive-mode because all
1489 ;; local variables will be killed, there.
1490 (if (eq window-system 'w32)
1491 (setq comint-input-sender 'sql-query-placeholders-and-send))
1492 (message "Login...done")
1493 (pop-to-buffer sql-buffer)))
1497 ;;;###autoload
1498 (defun sql-sybase ()
1499 "Run isql by SyBase as an inferior process.
1501 If buffer `*SQL*' exists but no process is running, make a new process.
1502 If buffer exists and a process is running, just switch to buffer
1503 `*SQL*'.
1505 Interpreter used comes from variable `sql-sybase-program'. Login uses
1506 the variables `sql-server', `sql-user', `sql-password', and
1507 `sql-database' as defaults, if set. Additional command line parameters
1508 can be stored in the list `sql-sybase-options'.
1510 The buffer is put in sql-interactive-mode, giving commands for sending
1511 input. See `sql-interactive-mode'.
1513 To specify a coding system for converting non-ASCII characters
1514 in the input and output to the process, use \\[universal-coding-system-argument]
1515 before \\[sql-sybase]. You can also specify this with \\[set-buffer-process-coding-system]
1516 in the SQL buffer, after you start the process.
1517 The default comes from `process-coding-system-alist' and
1518 `default-process-coding-system'.
1520 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1521 (interactive)
1522 (if (comint-check-proc "*SQL*")
1523 (pop-to-buffer "*SQL*")
1524 (sql-get-login 'server 'user 'password 'database)
1525 (message "Login...")
1526 ;; Put all parameters to the program (if defined) in a list and call
1527 ;; make-comint.
1528 (let ((params sql-sybase-options))
1529 (if (not (string= "" sql-server))
1530 (setq params (append (list "-S" sql-server) params)))
1531 (if (not (string= "" sql-database))
1532 (setq params (append (list "-D" sql-database) params)))
1533 (if (not (string= "" sql-password))
1534 (setq params (append (list "-P" sql-password) params)))
1535 (if (not (string= "" sql-user))
1536 (setq params (append (list "-U" sql-user) params)))
1537 (set-buffer (apply 'make-comint "SQL" sql-sybase-program
1538 nil params)))
1539 (setq sql-prompt-regexp "^SQL> ")
1540 (setq sql-prompt-length 5)
1541 (setq sql-buffer (current-buffer))
1542 (sql-interactive-mode)
1543 (message "Login...done")
1544 (pop-to-buffer sql-buffer)))
1548 ;;;###autoload
1549 (defun sql-informix ()
1550 "Run dbaccess by Informix as an inferior process.
1552 If buffer `*SQL*' exists but no process is running, make a new process.
1553 If buffer exists and a process is running, just switch to buffer
1554 `*SQL*'.
1556 Interpreter used comes from variable `sql-informix-program'. Login uses
1557 the variable `sql-database' as default, if set.
1559 The buffer is put in sql-interactive-mode, giving commands for sending
1560 input. See `sql-interactive-mode'.
1562 To specify a coding system for converting non-ASCII characters
1563 in the input and output to the process, use \\[universal-coding-system-argument]
1564 before \\[sql-informix]. You can also specify this with \\[set-buffer-process-coding-system]
1565 in the SQL buffer, after you start the process.
1566 The default comes from `process-coding-system-alist' and
1567 `default-process-coding-system'.
1569 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1570 (interactive)
1571 (if (comint-check-proc "*SQL*")
1572 (pop-to-buffer "*SQL*")
1573 (sql-get-login 'database)
1574 (message "Login...")
1575 ;; username and password are ignored.
1576 (if (string= "" sql-database)
1577 (set-buffer (make-comint "SQL" sql-informix-program nil))
1578 (set-buffer (make-comint "SQL" sql-informix-program nil sql-database "-")))
1579 (setq sql-prompt-regexp "^SQL> ")
1580 (setq sql-prompt-length 5)
1581 (setq sql-buffer (current-buffer))
1582 (sql-interactive-mode)
1583 (message "Login...done")
1584 (pop-to-buffer sql-buffer)))
1588 ;;;###autoload
1589 (defun sql-mysql ()
1590 "Run mysql by TcX as an inferior process.
1592 Mysql versions 3.23 and up are free software.
1594 If buffer `*SQL*' exists but no process is running, make a new process.
1595 If buffer exists and a process is running, just switch to buffer
1596 `*SQL*'.
1598 Interpreter used comes from variable `sql-mysql-program'. Login uses
1599 the variables `sql-user', `sql-password', `sql-database', and
1600 `sql-server' as defaults, if set. Additional command line parameters
1601 can be stored in the list `sql-mysql-options'.
1603 The buffer is put in sql-interactive-mode, giving commands for sending
1604 input. See `sql-interactive-mode'.
1606 To specify a coding system for converting non-ASCII characters
1607 in the input and output to the process, use \\[universal-coding-system-argument]
1608 before \\[sql-mysql]. You can also specify this with \\[set-buffer-process-coding-system]
1609 in the SQL buffer, after you start the process.
1610 The default comes from `process-coding-system-alist' and
1611 `default-process-coding-system'.
1613 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1614 (interactive)
1615 (if (comint-check-proc "*SQL*")
1616 (pop-to-buffer "*SQL*")
1617 (sql-get-login 'user 'password 'database 'server)
1618 (message "Login...")
1619 ;; Put all parameters to the program (if defined) in a list and call
1620 ;; make-comint.
1621 (let ((params))
1622 (if (not (string= "" sql-database))
1623 (setq params (append (list sql-database) params)))
1624 (if (not (string= "" sql-server))
1625 (setq params (append (list (concat "--host=" sql-server)) params)))
1626 (if (not (string= "" sql-password))
1627 (setq params (append (list (concat "--password=" sql-password)) params)))
1628 (if (not (string= "" sql-user))
1629 (setq params (append (list (concat "--user=" sql-user)) params)))
1630 (if (not (null sql-mysql-options))
1631 (setq params (append sql-mysql-options params)))
1632 (set-buffer (apply 'make-comint "SQL" sql-mysql-program
1633 nil params)))
1634 (setq sql-prompt-regexp "^mysql>")
1635 (setq sql-prompt-length 6)
1636 (setq sql-buffer (current-buffer))
1637 (sql-interactive-mode)
1638 (message "Login...done")
1639 (pop-to-buffer sql-buffer)))
1643 ;;;###autoload
1644 (defun sql-solid ()
1645 "Run solsql by Solid as an inferior process.
1647 If buffer `*SQL*' exists but no process is running, make a new process.
1648 If buffer exists and a process is running, just switch to buffer
1649 `*SQL*'.
1651 Interpreter used comes from variable `sql-solid-program'. Login uses
1652 the variables `sql-user', `sql-password', and `sql-server' as
1653 defaults, if set.
1655 The buffer is put in sql-interactive-mode, giving commands for sending
1656 input. See `sql-interactive-mode'.
1658 To specify a coding system for converting non-ASCII characters
1659 in the input and output to the process, use \\[universal-coding-system-argument]
1660 before \\[sql-solid]. You can also specify this with \\[set-buffer-process-coding-system]
1661 in the SQL buffer, after you start the process.
1662 The default comes from `process-coding-system-alist' and
1663 `default-process-coding-system'.
1665 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1666 (interactive)
1667 (if (comint-check-proc "*SQL*")
1668 (pop-to-buffer "*SQL*")
1669 (sql-get-login 'user 'password 'server)
1670 (message "Login...")
1671 ;; Put all parameters to the program (if defined) in a list and call
1672 ;; make-comint.
1673 (let ((params))
1674 ;; It only makes sense if both username and password are there.
1675 (if (not (or (string= "" sql-user)
1676 (string= "" sql-password)))
1677 (setq params (append (list sql-user sql-password) params)))
1678 (if (not (string= "" sql-server))
1679 (setq params (append (list sql-server) params)))
1680 (set-buffer (apply 'make-comint "SQL" sql-solid-program
1681 nil params)))
1682 (setq sql-prompt-regexp "^")
1683 (setq sql-prompt-length 0)
1684 (setq sql-buffer (current-buffer))
1685 (sql-interactive-mode)
1686 (message "Login...done")
1687 (pop-to-buffer sql-buffer)))
1691 ;;;###autoload
1692 (defun sql-ingres ()
1693 "Run sql by Ingres as an inferior process.
1695 If buffer `*SQL*' exists but no process is running, make a new process.
1696 If buffer exists and a process is running, just switch to buffer
1697 `*SQL*'.
1699 Interpreter used comes from variable `sql-ingres-program'. Login uses
1700 the variable `sql-database' as default, if set.
1702 The buffer is put in sql-interactive-mode, giving commands for sending
1703 input. See `sql-interactive-mode'.
1705 To specify a coding system for converting non-ASCII characters
1706 in the input and output to the process, use \\[universal-coding-system-argument]
1707 before \\[sql-ingres]. You can also specify this with \\[set-buffer-process-coding-system]
1708 in the SQL buffer, after you start the process.
1709 The default comes from `process-coding-system-alist' and
1710 `default-process-coding-system'.
1712 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1713 (interactive)
1714 (if (comint-check-proc "*SQL*")
1715 (pop-to-buffer "*SQL*")
1716 (sql-get-login 'database)
1717 (message "Login...")
1718 ;; username and password are ignored.
1719 (if (string= "" sql-database)
1720 (set-buffer (make-comint "SQL" sql-ingres-program nil))
1721 (set-buffer (make-comint "SQL" sql-ingres-program nil sql-database)))
1722 (setq sql-prompt-regexp "^\* ")
1723 (setq sql-prompt-length 2)
1724 (setq sql-buffer (current-buffer))
1725 (sql-interactive-mode)
1726 (message "Login...done")
1727 (pop-to-buffer sql-buffer)))
1731 ;;;###autoload
1732 (defun sql-ms ()
1733 "Run isql by Microsoft as an inferior process.
1735 If buffer `*SQL*' exists but no process is running, make a new process.
1736 If buffer exists and a process is running, just switch to buffer
1737 `*SQL*'.
1739 Interpreter used comes from variable `sql-ms-program'. Login uses the
1740 variables `sql-user', `sql-password', `sql-database', and `sql-server'
1741 as defaults, if set. Additional command line parameters can be stored
1742 in the list `sql-ms-options'.
1744 The buffer is put in sql-interactive-mode, giving commands for sending
1745 input. See `sql-interactive-mode'.
1747 To specify a coding system for converting non-ASCII characters
1748 in the input and output to the process, use \\[universal-coding-system-argument]
1749 before \\[sql-ms]. You can also specify this with \\[set-buffer-process-coding-system]
1750 in the SQL buffer, after you start the process.
1751 The default comes from `process-coding-system-alist' and
1752 `default-process-coding-system'.
1754 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1755 (interactive)
1756 (if (comint-check-proc "*SQL*")
1757 (pop-to-buffer "*SQL*")
1758 (sql-get-login 'user 'password 'database 'server)
1759 (message "Login...")
1760 ;; Put all parameters to the program (if defined) in a list and call
1761 ;; make-comint.
1762 (let ((params sql-ms-options))
1763 (if (not (string= "" sql-server))
1764 (setq params (append (list "-S" sql-server) params)))
1765 (if (not (string= "" sql-database))
1766 (setq params (append (list "-d" sql-database) params)))
1767 (if (not (string= "" sql-user))
1768 (setq params (append (list "-U" sql-user) params)))
1769 (if (not (string= "" sql-password))
1770 (setq params (append (list "-P" sql-password) params))
1771 ;; If -P is passed to ISQL as the last argument without a password,
1772 ;; it's considered null.
1773 (setq params (append params (list "-P"))))
1774 (set-buffer (apply 'make-comint "SQL" sql-ms-program
1775 nil params)))
1776 (setq sql-prompt-regexp "^[0-9]*>")
1777 (setq sql-prompt-length 5)
1778 (setq sql-buffer (current-buffer))
1779 (sql-interactive-mode)
1780 (message "Login...done")
1781 (pop-to-buffer sql-buffer)))
1785 ;;;###autoload
1786 (defun sql-postgres ()
1787 "Run psql by Postgres as an inferior process.
1789 If buffer `*SQL*' exists but no process is running, make a new process.
1790 If buffer exists and a process is running, just switch to buffer
1791 `*SQL*'.
1793 Interpreter used comes from variable `sql-postgres-program'. Login uses
1794 the variables `sql-database' and `sql-server' as default, if set.
1795 Additional command line parameters can be stored in the list
1796 `sql-postgres-options'.
1798 The buffer is put in sql-interactive-mode, giving commands for sending
1799 input. See `sql-interactive-mode'.
1801 To specify a coding system for converting non-ASCII characters
1802 in the input and output to the process, use \\[universal-coding-system-argument]
1803 before \\[sql-postgres]. You can also specify this with \\[set-buffer-process-coding-system]
1804 in the SQL buffer, after you start the process.
1805 The default comes from `process-coding-system-alist' and
1806 `default-process-coding-system'. If your output lines end with ^M,
1807 your might try undecided-dos as a coding system. If this doesn't help,
1808 Try to set `comint-output-filter-functions' like this:
1810 \(setq comint-output-filter-functions (append comint-output-filter-functions
1811 '(comint-strip-ctrl-m)))
1813 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1814 (interactive)
1815 (if (comint-check-proc "*SQL*")
1816 (pop-to-buffer "*SQL*")
1817 (sql-get-login 'database 'server)
1818 (message "Login...")
1819 ;; username and password are ignored. Mark Stosberg suggest to add
1820 ;; the database at the end. Jason Beegan suggest using --pset and
1821 ;; pager=off instead of \\o|cat. The later was the solution by
1822 ;; Gregor Zych. Jason's suggestion is the default value for
1823 ;; sql-postgres-options.
1824 (let ((params sql-postgres-options))
1825 (if (not (string= "" sql-database))
1826 (setq params (append params (list sql-database))))
1827 (if (not (string= "" sql-server))
1828 (setq params (append (list "-h" sql-server) params)))
1829 (set-buffer (apply 'make-comint "SQL" sql-postgres-program
1830 nil params)))
1831 (setq sql-prompt-regexp "^.*> *")
1832 (setq sql-prompt-length 5)
1833 ;; This is a lousy hack to prevent psql from truncating it's output
1834 ;; and giving stupid warnings. If s.o. knows a way to prevent psql
1835 ;; from acting this way, then I would be very thankful to
1836 ;; incorporate this (Gregor Zych <zych@pool.informatik.rwth-aachen.de>)
1837 ;; (comint-send-string "*SQL*" "\\o \| cat\n")
1838 (setq sql-mode-font-lock-keywords sql-mode-postgres-font-lock-keywords)
1839 (setq sql-buffer (current-buffer))
1840 (sql-interactive-mode)
1841 (message "Login...done")
1842 (pop-to-buffer sql-buffer)))
1846 ;;;###autoload
1847 (defun sql-interbase ()
1848 "Run isql by Interbase as an inferior process.
1850 If buffer `*SQL*' exists but no process is running, make a new process.
1851 If buffer exists and a process is running, just switch to buffer
1852 `*SQL*'.
1854 Interpreter used comes from variable `sql-interbase-program'. Login
1855 uses the variables `sql-user', `sql-password', and `sql-database' as
1856 defaults, if set.
1858 The buffer is put in sql-interactive-mode, giving commands for sending
1859 input. See `sql-interactive-mode'.
1861 To specify a coding system for converting non-ASCII characters
1862 in the input and output to the process, use \\[universal-coding-system-argument]
1863 before \\[sql-interbase]. You can also specify this with \\[set-buffer-process-coding-system]
1864 in the SQL buffer, after you start the process.
1865 The default comes from `process-coding-system-alist' and
1866 `default-process-coding-system'.
1868 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1869 (interactive)
1870 (if (comint-check-proc "*SQL*")
1871 (pop-to-buffer "*SQL*")
1872 (sql-get-login 'user 'password 'database)
1873 (message "Login...")
1874 ;; Put all parameters to the program (if defined) in a list and call
1875 ;; make-comint.
1876 (let ((params sql-interbase-options))
1877 (if (not (string= "" sql-user))
1878 (setq params (append (list "-u" sql-user) params)))
1879 (if (not (string= "" sql-password))
1880 (setq params (append (list "-p" sql-password) params)))
1881 (if (not (string= "" sql-database))
1882 (setq params (cons sql-database params))); add to the front!
1883 (set-buffer (apply 'make-comint "SQL" sql-interbase-program
1884 nil params)))
1885 (setq sql-prompt-regexp "^SQL> ")
1886 (setq sql-prompt-length 5)
1887 (setq sql-buffer (current-buffer))
1888 (sql-interactive-mode)
1889 (message "Login...done")
1890 (pop-to-buffer sql-buffer)))
1894 ;;;###autoload
1895 (defun sql-db2 ()
1896 "Run db2 by IBM as an inferior process.
1898 If buffer `*SQL*' exists but no process is running, make a new process.
1899 If buffer exists and a process is running, just switch to buffer
1900 `*SQL*'.
1902 Interpreter used comes from variable `sql-db2-program'. There is not
1903 automatic login.
1905 The buffer is put in sql-interactive-mode, giving commands for sending
1906 input. See `sql-interactive-mode'.
1908 If you use \\[sql-accumulate-and-indent] to send multiline commands to
1909 db2, newlines will be escaped if necessary. If you don't want that, set
1910 `comint-input-sender' back to `comint-simple-send' by writing an after
1911 advice. See the elisp manual for more information.
1913 To specify a coding system for converting non-ASCII characters
1914 in the input and output to the process, use \\[universal-coding-system-argument]
1915 before \\[sql-db2]. You can also specify this with \\[set-buffer-process-coding-system]
1916 in the SQL buffer, after you start the process.
1917 The default comes from `process-coding-system-alist' and
1918 `default-process-coding-system'.
1920 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1921 (interactive)
1922 (if (comint-check-proc "*SQL*")
1923 (pop-to-buffer "*SQL*")
1924 (message "Login...")
1925 ;; Put all parameters to the program (if defined) in a list and call
1926 ;; make-comint.
1927 (set-buffer (apply 'make-comint "SQL" sql-db2-program
1928 nil sql-db2-options))
1929 (setq sql-prompt-regexp "^db2 => ")
1930 (setq sql-prompt-length 7)
1931 (setq sql-buffer (current-buffer))
1932 (sql-interactive-mode)
1933 ;; Escape newlines. This must come after sql-interactive-mode
1934 ;; because all local variables will be killed, there.
1935 (setq comint-input-sender 'sql-escape-newlines-and-send)
1936 (message "Login...done")
1937 (pop-to-buffer sql-buffer)))
1939 ;;;###autoload
1940 (defun sql-linter ()
1941 "Run inl by RELEX as an inferior process.
1943 If buffer `*SQL*' exists but no process is running, make a new process.
1944 If buffer exists and a process is running, just switch to buffer
1945 `*SQL*'.
1947 Interpreter used comes from variable `sql-linter-program' - usually `inl'.
1948 Login uses the variables `sql-user', `sql-password', `sql-database' and
1949 `sql-server' as defaults, if set. Additional command line parameters
1950 can be stored in the list `sql-linter-options'. Run inl -h to get help on
1951 parameters.
1953 `sql-database' is used to set the LINTER_MBX environment variable for
1954 local connections, `sql-server' refers to the server name from the
1955 `nodetab' file for the network connection (dbc_tcp or friends must run
1956 for this to work). If `sql-password' is an empty string, inl will use
1957 an empty password.
1959 The buffer is put in sql-interactive-mode, giving commands for sending
1960 input. See `sql-interactive-mode'.
1962 To use LINTER font locking by default, put this line into your .emacs :
1963 (setq sql-mode-font-lock-keywords sql-mode-linter-font-lock-keywords)
1965 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1966 (interactive)
1967 (if (comint-check-proc "*SQL*")
1968 (pop-to-buffer "*SQL*")
1969 (sql-get-login 'user 'password 'database 'server)
1970 (message "Login...")
1971 ;; Put all parameters to the program (if defined) in a list and call
1972 ;; make-comint.
1973 (let ((params sql-linter-options) (login nil) (old-mbx (getenv "LINTER_MBX")))
1974 (if (not (string= "" sql-user))
1975 (setq login (concat sql-user "/" sql-password)))
1976 (setq params (append (list "-u" login) params))
1977 (if (not (string= "" sql-server))
1978 (setq params (append (list "-n" sql-server) params)))
1979 (if (string= "" sql-database)
1980 (setenv "LINTER_MBX" nil)
1981 (setenv "LINTER_MBX" sql-database))
1982 (set-buffer (apply 'make-comint "SQL" sql-linter-program nil
1983 params))
1984 (setenv "LINTER_MBX" old-mbx)
1986 (setq sql-prompt-regexp "^SQL>")
1987 (setq sql-prompt-length 4)
1988 (setq sql-buffer (current-buffer))
1989 (sql-interactive-mode)
1990 (message "Login...done")
1991 (pop-to-buffer sql-buffer)))
1995 (provide 'sql)
1997 ;;; sql.el ends here