]> code.delx.au - gnu-emacs/blob - lisp/progmodes/sql.el
(f90-mode-abbrev-table): Mark all the predefined abbrevs as "system"
[gnu-emacs] / lisp / progmodes / sql.el
1 ;;; sql.el --- specialized comint.el for SQL interpreters
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Alex Schroeder <alex@gnu.org>
7 ;; Version: 1.6.5
8 ;; Keywords: comm languages processes
9 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlMode
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; 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.
27
28 ;;; Commentary:
29
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.
34
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.
41
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.
46
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.
51
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).
55
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.
61
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.
64
65 ;; For documentation on the functionality provided by comint mode, and
66 ;; the hooks available for customizing it, see the file `comint.el'.
67
68 ;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
69 ;; `imenu-add-menubar-index'.
70
71 ;;; Requirements for Emacs 19.34:
72
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/>.
78
79 ;;; Bugs:
80
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.
86
87 ;; ChangeLog available on request.
88
89 ;;; To Do:
90
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.
97
98 ;; Add different hilighting levels.
99
100 ;;; Thanks to all the people who helped me out:
101
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>
108
109 \f
110
111 ;;; Code:
112
113 (require 'comint)
114 ;; Need the following to allow GNU Emacs 19 to compile the file.
115 (require 'regexp-opt)
116 (require 'custom)
117
118 ;;; Allow customization
119
120 (defgroup SQL nil
121 "Running a SQL interpreter from within Emacs buffers"
122 :version "20.4"
123 :group 'processes)
124
125 ;; These three variables will be used as defaults, if set.
126
127 (defcustom sql-user ""
128 "*Default username."
129 :type 'string
130 :group 'SQL)
131
132 (defcustom sql-password ""
133 "*Default password.
134
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)
139
140 (defcustom sql-database ""
141 "*Default database."
142 :type 'string
143 :group 'SQL)
144
145 (defcustom sql-server ""
146 "*Default server or host."
147 :type 'string
148 :group 'SQL)
149
150 ;; misc customization of sql.el behaviour
151
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)
165
166 (defcustom sql-pop-to-buffer-after-send-region nil
167 "*If t, pop to the buffer SQL statements are sent to.
168
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)
176
177 ;; imenu support for sql-mode.
178
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'.
183
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.")
188
189 ;; history file
190
191 (defcustom sql-input-ring-file-name nil
192 "*If non-nil, name of the file to read/write input history.
193
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.
199
200 This is used to initialize `comint-input-ring-file-name'.
201
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)
207
208 (defcustom sql-input-ring-separator "\n--\n"
209 "*Separator between commands in the history file.
210
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.
214
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)
223
224 ;; The usual hooks
225
226 (defcustom sql-interactive-mode-hook '()
227 "*Hook for customizing `sql-interactive-mode'."
228 :type 'hook
229 :group 'SQL)
230
231 (defcustom sql-mode-hook '()
232 "*Hook for customizing `sql-mode'."
233 :type 'hook
234 :group 'SQL)
235
236 (defcustom sql-set-sqli-hook '()
237 "*Hook for reacting to changes of `sql-buffer'.
238
239 This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
240 is changed."
241 :type 'hook
242 :group 'SQL)
243
244 ;; Customization for Oracle
245
246 (defcustom sql-oracle-program "sqlplus"
247 "*Command to start sqlplus by Oracle.
248
249 Starts `sql-interactive-mode' after doing some setup.
250
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.
254
255 The program can also specify a TCP connection. See `make-comint'."
256 :type 'file
257 :group 'SQL)
258
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)
264
265 ;; Customization for MySql
266
267 (defcustom sql-mysql-program "mysql"
268 "*Command to start mysql by TcX.
269
270 Starts `sql-interactive-mode' after doing some setup.
271
272 The program can also specify a TCP connection. See `make-comint'."
273 :type 'file
274 :group 'SQL)
275
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)
283
284 ;; Customization for Solid
285
286 (defcustom sql-solid-program "solsql"
287 "*Command to start SOLID SQL Editor.
288
289 Starts `sql-interactive-mode' after doing some setup.
290
291 The program can also specify a TCP connection. See `make-comint'."
292 :type 'file
293 :group 'SQL)
294
295 ;; Customization for SyBase
296
297 (defcustom sql-sybase-program "isql"
298 "*Command to start isql by SyBase.
299
300 Starts `sql-interactive-mode' after doing some setup.
301
302 The program can also specify a TCP connection. See `make-comint'."
303 :type 'file
304 :group 'SQL)
305
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)
312
313 ;; Customization for Informix
314
315 (defcustom sql-informix-program "dbaccess"
316 "*Command to start dbaccess by Informix.
317
318 Starts `sql-interactive-mode' after doing some setup.
319
320 The program can also specify a TCP connection. See `make-comint'."
321 :type 'file
322 :group 'SQL)
323
324 ;; Customization for Ingres
325
326 (defcustom sql-ingres-program "sql"
327 "*Command to start sql by Ingres.
328
329 Starts `sql-interactive-mode' after doing some setup.
330
331 The program can also specify a TCP connection. See `make-comint'."
332 :type 'file
333 :group 'SQL)
334
335 ;; Customization for Microsoft
336
337 (defcustom sql-ms-program "isql"
338 "*Command to start isql by Microsoft.
339
340 Starts `sql-interactive-mode' after doing some setup.
341
342 The program can also specify a TCP connection. See `make-comint'."
343 :type 'file
344 :group 'SQL)
345
346 ;; Customization for Postgres
347
348 (defcustom sql-postgres-program "psql"
349 "Command to start psql by Postgres.
350
351 Starts `sql-interactive-mode' after doing some setup.
352
353 The program can also specify a TCP connection. See `make-comint'."
354 :type 'file
355 :group 'SQL)
356
357 (defcustom sql-postgres-options '("-P" "pager=off")
358 "*List of additional options for `sql-postgres-program'.
359 The default setting includes the -P option which breaks older versions
360 of the psql client (such as version 6.5.3). The -P option is equivalent
361 to the --pset option. If you want the psql to prompt you for a user
362 name, add the string \"-u\" to the list of options. If you want to
363 provide a user name on the command line (newer versions such as 7.1),
364 add your name with a \"-U\" prefix (such as \"-Umark\") to the list."
365 :type '(repeat string)
366 :version "20.8"
367 :group 'SQL)
368
369 ;; Customization for Interbase
370
371 (defcustom sql-interbase-program "isql"
372 "*Command to start isql by Interbase.
373
374 Starts `sql-interactive-mode' after doing some setup.
375
376 The program can also specify a TCP connection. See `make-comint'."
377 :type 'file
378 :group 'SQL)
379
380 (defcustom sql-interbase-options nil
381 "*List of additional options for `sql-interbase-program'."
382 :type '(repeat string)
383 :version "20.8"
384 :group 'SQL)
385
386 ;; Customization for DB2
387
388 (defcustom sql-db2-program "db2"
389 "*Command to start db2 by IBM.
390
391 Starts `sql-interactive-mode' after doing some setup.
392
393 The program can also specify a TCP connection. See `make-comint'."
394 :type 'file
395 :group 'SQL)
396
397 (defcustom sql-db2-options nil
398 "*List of additional options for `sql-db2-program'."
399 :type '(repeat string)
400 :version "20.8"
401 :group 'SQL)
402
403 \f
404
405 ;;; Variables which do not need customization
406
407 (defvar sql-user-history nil
408 "History of usernames used.")
409
410 (defvar sql-database-history nil
411 "History of databases used.")
412
413 (defvar sql-server-history nil
414 "History of servers used.")
415
416 ;; Passwords are not kept in a history.
417
418 (defvar sql-buffer nil
419 "Current SQLi buffer.
420
421 The global value of sql-buffer is the name of the latest SQLi buffer
422 created. Any SQL buffer created will make a local copy of this value.
423 See `sql-interactive-mode' for more on multiple sessions. If you want
424 to change the SQLi buffer a SQL mode sends its SQL strings to, change
425 the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
426
427 (defvar sql-prompt-regexp nil
428 "Prompt used to initialize `comint-prompt-regexp'.
429
430 You can change `comint-prompt-regexp' on `sql-interactive-mode-hook'.")
431
432 (defvar sql-prompt-length 0
433 "Prompt used to set `left-margin' in `sql-interactive-mode'.
434
435 You can change it on `sql-interactive-mode-hook'.")
436
437 (defvar sql-alternate-buffer-name nil
438 "Buffer-local string used to possibly rename the SQLi buffer.
439
440 Used by `sql-rename-buffer'.")
441
442 ;; Keymap for sql-interactive-mode.
443
444 (defvar sql-interactive-mode-map
445 (let ((map (make-sparse-keymap)))
446 (if (functionp 'set-keymap-parent)
447 (set-keymap-parent map comint-mode-map); Emacs
448 (set-keymap-parents map (list comint-mode-map))); XEmacs
449 (if (functionp 'set-keymap-name)
450 (set-keymap-name map 'sql-interactive-mode-map)); XEmacs
451 (define-key map (kbd "C-j") 'sql-accumulate-and-indent)
452 (define-key map (kbd "C-c C-w") 'sql-copy-column)
453 (define-key map (kbd "O") 'sql-magic-go)
454 (define-key map (kbd "o") 'sql-magic-go)
455 (define-key map (kbd ";") 'sql-magic-semicolon)
456 map)
457 "Mode map used for `sql-interactive-mode'.
458 Based on `comint-mode-map'.")
459
460 ;; Keymap for sql-mode.
461
462 (defvar sql-mode-map
463 (let ((map (make-sparse-keymap)))
464 (define-key map (kbd "C-c C-c") 'sql-send-paragraph)
465 (define-key map (kbd "C-c C-r") 'sql-send-region)
466 (define-key map (kbd "C-c C-b") 'sql-send-buffer)
467 map)
468 "Mode map used for `sql-mode'.")
469
470 ;; easy menu for sql-mode.
471
472 (easy-menu-define
473 sql-mode-menu sql-mode-map
474 "Menu for `sql-mode'."
475 '("SQL"
476 ["Send Paragraph" sql-send-paragraph (and (buffer-live-p sql-buffer)
477 (get-buffer-process sql-buffer))]
478 ["Send Region" sql-send-region (and (or (and (boundp 'mark-active); Emacs
479 mark-active)
480 (mark)); XEmacs
481 (buffer-live-p sql-buffer)
482 (get-buffer-process sql-buffer))]
483 ["Send Buffer" sql-send-buffer (and (buffer-live-p sql-buffer)
484 (get-buffer-process sql-buffer))]
485 ["Show SQLi buffer" sql-show-sqli-buffer t]
486 ["Set SQLi buffer" sql-set-sqli-buffer t]
487 ["Pop to SQLi buffer after send"
488 sql-toggle-pop-to-buffer-after-send-region
489 :style toggle
490 :selected sql-pop-to-buffer-after-send-region]
491 ("Highlighting"
492 ["ANSI SQL keywords" sql-highlight-ansi-keywords t]
493 ["Oracle keywords" sql-highlight-oracle-keywords t]
494 ["Postgres keywords" sql-highlight-postgres-keywords t])))
495
496 ;; easy menu for sql-interactive-mode.
497
498 (easy-menu-define
499 sql-interactive-mode-menu sql-interactive-mode-map
500 "Menu for `sql-interactive-mode'."
501 '("SQL"
502 ["Rename Buffer" sql-rename-buffer t]))
503
504 ;; Abbreviations -- if you want more of them, define them in your
505 ;; ~/.emacs file. Abbrevs have to be enabled in your ~/.emacs, too.
506
507 (defvar sql-mode-abbrev-table nil
508 "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
509 (if sql-mode-abbrev-table
510 ()
511 (let ((wrapper))
512 (define-abbrev-table 'sql-mode-abbrev-table ())
513 (define-abbrev sql-mode-abbrev-table "ins" "insert" nil 0 t)
514 (define-abbrev sql-mode-abbrev-table "upd" "update" nil 0 t)
515 (define-abbrev sql-mode-abbrev-table "del" "delete" nil 0 t)
516 (define-abbrev sql-mode-abbrev-table "sel" "select" nil 0 t)))
517
518 ;; Syntax Table
519
520 (defvar sql-mode-syntax-table
521 (let ((table (make-syntax-table)))
522 ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
523 (modify-syntax-entry ?/ ". 14" table)
524 (modify-syntax-entry ?* ". 23" table)
525 ;; double-dash starts comment
526 (if (string-match "XEmacs\\|Lucid" emacs-version)
527 (modify-syntax-entry ?- ". 56" table)
528 (modify-syntax-entry ?- ". 12b" table))
529 ;; newline and formfeed end coments
530 (modify-syntax-entry ?\n "> b" table)
531 (modify-syntax-entry ?\f "> b" table)
532 ;; single quotes (') quotes delimit strings
533 (modify-syntax-entry ?' "\"" table)
534 ;; backslash is no escape character
535 (modify-syntax-entry ?\\ "." table)
536 table)
537 "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
538
539 ;; Font lock support
540
541 (defvar sql-mode-ansi-font-lock-keywords nil
542 "ANSI SQL keywords used by font-lock.
543
544 This variable is used by `sql-mode' and `sql-interactive-mode'. The
545 regular expressions are created during compilation by calling the
546 function `regexp-opt'. Therefore, take a look at the source before
547 you define your own sql-mode-ansi-font-lock-keywords. You may want to
548 add functions and PL/SQL keywords.")
549 (if sql-mode-ansi-font-lock-keywords
550 ()
551 (let ((ansi-keywords (eval-when-compile
552 (concat "\\b"
553 (regexp-opt '(
554 "authorization" "avg" "begin" "close" "cobol" "commit"
555 "continue" "count" "declare" "double" "end" "escape"
556 "exec" "fetch" "foreign" "fortran" "found" "go" "goto" "indicator"
557 "key" "language" "max" "min" "module" "numeric" "open" "pascal" "pli"
558 "precision" "primary" "procedure" "references" "rollback"
559 "schema" "section" "some" "sqlcode" "sqlerror" "sum" "work") t) "\\b")))
560 (ansi-reserved-words (eval-when-compile
561 (concat "\\b"
562 (regexp-opt '(
563 "all" "and" "any" "as" "asc" "between" "by" "check" "create"
564 "current" "default" "delete" "desc" "distinct" "exists" "float" "for"
565 "from" "grant" "group" "having" "in" "insert" "into" "is"
566 "like" "not" "null" "of" "on" "option" "or" "order" "privileges"
567 "public" "select" "set" "table" "to" "union" "unique"
568 "update" "user" "values" "view" "where" "with") t) "\\b")))
569 (ansi-types (eval-when-compile
570 (concat "\\b"
571 (regexp-opt '(
572 ;; ANSI Keywords that look like types
573 "character" "cursor" "dec" "int" "real"
574 ;; ANSI Reserved Word that look like types
575 "char" "integer" "smallint" ) t) "\\b"))))
576 (setq sql-mode-ansi-font-lock-keywords
577 (list (cons ansi-keywords 'font-lock-function-name-face)
578 (cons ansi-reserved-words 'font-lock-keyword-face)
579 (cons ansi-types 'font-lock-type-face)))))
580
581 (defvar sql-mode-oracle-font-lock-keywords nil
582 "Oracle SQL keywords used by font-lock.
583
584 This variable is used by `sql-mode' and `sql-interactive-mode'. The
585 regular expressions are created during compilation by calling the
586 function `regexp-opt'. Therefore, take a look at the source before
587 you define your own sql-mode-oracle-font-lock-keywords. You may want
588 to add functions and PL/SQL keywords.")
589 (if sql-mode-oracle-font-lock-keywords
590 ()
591 (let ((oracle-keywords (eval-when-compile
592 (concat "\\b"
593 (regexp-opt '(
594 "admin" "after" "allocate" "analyze" "archive" "archivelog" "backup"
595 "become" "before" "block" "body" "cache" "cancel" "cascade" "change"
596 "checkpoint" "compile" "constraint" "constraints" "contents"
597 "controlfile" "cycle" "database" "datafile" "dba" "disable" "dismount"
598 "dump" "each" "else" "elsif" "enable" "events" "except" "exceptions"
599 "execute" "exit" "explain" "extent" "externally" "false" "flush" "force"
600 "freelist" "freelists" "function" "groups" "if" "including" "initrans"
601 "instance" "layer" "link" "lists" "logfile" "loop" "manage" "manual"
602 "maxdatafiles" "maxinistances" "maxlogfiles" "maxloghistory"
603 "maxlogmembers" "maxtrans" "maxvalue" "minextents" "minvalue" "mount"
604 "new" "next" "noarchivelog" "nocache" "nocycle" "nomaxvalue"
605 "nominvalue" "none" "noorder" "noresetlogs" "normal" "nosort" "off"
606 "old" "only" "optimal" "others" "out" "own" "package" "parallel"
607 "pctincrease" "pctused" "plan" "pragma" "private" "profile" "quota"
608 "raise" "read" "recover" "referencing" "resetlogs" "restrict_references"
609 "restricted" "return" "returning" "reuse" "rnds" "rnps" "role" "roles"
610 "savepoint" "scn" "segment" "sequence" "shared" "snapshot" "sort"
611 "statement_id" "statistics" "stop" "storage" "subtype" "switch" "system"
612 "tables" "tablespace" "temporary" "thread" "time" "tracing"
613 "transaction" "triggers" "true" "truncate" "type" "under" "unlimited"
614 "until" "use" "using" "when" "while" "wnds" "wnps" "write") t) "\\b")))
615 (oracle-warning-words (eval-when-compile
616 (concat "\\b"
617 (regexp-opt '(
618 "cursor_already_open" "dup_val_on_index" "exception" "invalid_cursor"
619 "invalid_number" "login_denied" "no_data_found" "not_logged_on"
620 "notfound" "others" "pragma" "program_error" "storage_error"
621 "timeout_on_resource" "too_many_rows" "transaction_backed_out"
622 "value_error" "zero_divide") t) "\\b")))
623 (oracle-reserved-words (eval-when-compile
624 (concat "\\b"
625 (regexp-opt '(
626 "access" "add" "alter" "audit" "cluster" "column" "comment" "compress"
627 "connect" "drop" "else" "exclusive" "file" "grant"
628 "identified" "immediate" "increment" "index" "initial" "intersect"
629 "level" "lock" "long" "maxextents" "minus" "mode" "modify" "noaudit"
630 "nocompress" "nowait" "number" "offline" "online" "pctfree" "prior"
631 "raw" "rename" "resource" "revoke" "row" "rowlabel" "rownum"
632 "rows" "session" "share" "size" "start" "successful" "synonym" "sysdate"
633 "then" "trigger" "uid" "validate" "whenever") t) "\\b")))
634 (oracle-types (eval-when-compile
635 (concat "\\b"
636 (regexp-opt '(
637 ;; Oracle Keywords that look like types
638 ;; Oracle Reserved Words that look like types
639 "binary_integer" "blob" "boolean" "constant" "date" "decimal" "rowid"
640 "varchar" "varchar2") t) "\\b")))
641 (oracle-builtin-functions (eval-when-compile
642 (concat "\\b"
643 (regexp-opt '(
644 ;; Misc Oracle builtin functions
645 "abs" "add_months" "ascii" "avg" "ceil" "chartorowid" "chr" "concat"
646 "convert" "cos" "cosh" "count" "currval" "decode" "dump" "exp" "floor"
647 "glb" "greatest" "greatest_lb" "hextoraw" "initcap" "instr" "instrb"
648 "last_day" "least" "least_ub" "length" "lengthb" "ln" "log" "lower"
649 "lpad" "ltrim" "lub" "max" "min" "mod" "months_between" "new_time"
650 "next_day" "nextval" "nls_initcap" "nls_lower" "nls_upper" "nlssort"
651 "nvl" "power" "rawtohex" "replace" "round" "rowidtochar" "rpad"
652 "rtrim" "sign" "sin" "sinh" "soundex" "sqlcode" "sqlerrm" "sqrt"
653 "stddev" "sum" "substr" "substrb" "tan" "tanh" "to_char"
654 "to_date" "to_label" "to_multi_byte" "to_number" "to_single_byte"
655 "translate" "trim" "trunc" "uid" "upper" "userenv" "variance" "vsize") t) "\\b"))))
656 (setq sql-mode-oracle-font-lock-keywords
657 (append sql-mode-ansi-font-lock-keywords
658 (list (cons oracle-keywords 'font-lock-function-name-face)
659 (cons oracle-warning-words 'font-lock-warning-face)
660 (cons oracle-reserved-words 'font-lock-keyword-face)
661 ;; XEmacs doesn't have font-lock-builtin-face
662 (if (string-match "XEmacs\\|Lucid" emacs-version)
663 (cons oracle-builtin-functions 'font-lock-preprocessor-face)
664 ;; GNU Emacs 19 doesn't have it either
665 (if (string-match "GNU Emacs 19" emacs-version)
666 (cons oracle-builtin-functions 'font-lock-function-name-face)
667 ;; Emacs
668 (cons oracle-builtin-functions 'font-lock-builtin-face)))
669 (cons oracle-types 'font-lock-type-face))))))
670
671 (defvar sql-mode-postgres-font-lock-keywords nil
672 "Postgres SQL keywords used by font-lock.
673
674 This variable is used by `sql-mode' and `sql-interactive-mode'. The
675 regular expressions are created during compilation by calling the
676 function `regexp-opt'. Therefore, take a look at the source before
677 you define your own sql-mode-postgres-font-lock-keywords.")
678
679 (if sql-mode-postgres-font-lock-keywords
680 ()
681 (let ((postgres-reserved-words (eval-when-compile
682 (concat "\\b"
683 (regexp-opt '(
684 "language"
685 ) t) "\\b")))
686 (postgres-types (eval-when-compile
687 (concat "\\b"
688 (regexp-opt '(
689 "bool" "box" "circle" "char" "char2" "char4" "char8" "char16" "date"
690 "float4" "float8" "int2" "int4" "int8" "line" "lseg" "money" "path"
691 "point" "polygon" "serial" "text" "time" "timespan" "timestamp" "varchar"
692 ) t)"\\b")))
693 (postgres-builtin-functions (eval-when-compile
694 (concat "\\b"
695 (regexp-opt '(
696 ;; Misc Postgres builtin functions
697 "abstime" "age" "area" "box" "center" "date_part" "date_trunc"
698 "datetime" "dexp" "diameter" "dpow" "float" "float4" "height"
699 "initcap" "integer" "isclosed" "isfinite" "isoldpath" "isopen"
700 "length" "lower" "lpad" "ltrim" "pclose" "point" "points" "popen"
701 "position" "radius" "reltime" "revertpoly" "rpad" "rtrim" "substr"
702 "substring" "text" "timespan" "translate" "trim" "upgradepath"
703 "upgradepoly" "upper" "varchar" "width"
704 ) t) "\\b"))))
705 (setq sql-mode-postgres-font-lock-keywords
706 (append sql-mode-ansi-font-lock-keywords
707 (list (cons postgres-reserved-words 'font-lock-keyword-face)
708 ;; XEmacs doesn't have 'font-lock-builtin-face
709 (if (string-match "XEmacs\\|Lucid" emacs-version)
710 (cons postgres-builtin-functions 'font-lock-preprocessor-face)
711 ;; Emacs
712 (cons postgres-builtin-functions 'font-lock-builtin-face))
713 (cons postgres-types 'font-lock-type-face))))))
714
715
716 (defvar sql-mode-font-lock-keywords sql-mode-ansi-font-lock-keywords
717 "SQL keywords used by font-lock.
718
719 This variable defaults to `sql-mode-ansi-font-lock-keywords'. This is
720 used for the default `font-lock-defaults' value in `sql-mode'. This
721 can be changed by some entry functions to provide more hilighting.")
722
723 \f
724
725 ;;; Functions to switch highlighting
726
727 (defun sql-highlight-oracle-keywords ()
728 "Highlight Oracle keywords.
729 Basically, this just sets `font-lock-keywords' appropriately."
730 (interactive)
731 (setq font-lock-keywords sql-mode-oracle-font-lock-keywords)
732 (font-lock-fontify-buffer))
733
734 (defun sql-highlight-postgres-keywords ()
735 "Highlight Postgres keywords.
736 Basically, this just sets `font-lock-keywords' appropriately."
737 (interactive)
738 (setq font-lock-keywords sql-mode-postgres-font-lock-keywords)
739 (font-lock-fontify-buffer))
740
741 (defun sql-highlight-ansi-keywords ()
742 "Highlight ANSI SQL keywords.
743 Basically, this just sets `font-lock-keywords' appropriately."
744 (interactive)
745 (setq font-lock-keywords sql-mode-ansi-font-lock-keywords)
746 (font-lock-fontify-buffer))
747
748 \f
749
750 ;;; Compatibility functions
751
752 (if (not (fboundp 'comint-line-beginning-position))
753 ;; comint-line-beginning-position is defined in Emacs 21
754 (defun comint-line-beginning-position ()
755 "Returns the buffer position of the beginning of the line, after any prompt.
756 The prompt is assumed to be any text at the beginning of the line matching
757 the regular expression `comint-prompt-regexp', a buffer local variable."
758 (save-excursion (comint-bol nil) (point))))
759
760 \f
761
762 ;;; Small functions
763
764 (defun sql-magic-go (arg)
765 "Insert \"o\" and call `comint-send-input'.
766 `sql-electric-stuff' must be the symbol `go'."
767 (interactive "P")
768 (self-insert-command (prefix-numeric-value arg))
769 (if (and (equal sql-electric-stuff 'go)
770 (save-excursion
771 (comint-bol nil)
772 (looking-at "go\\b")))
773 (comint-send-input)))
774
775 (defun sql-magic-semicolon (arg)
776 "Insert semicolon and call `comint-send-input'.
777 `sql-electric-stuff' must be the symbol `semicolon'."
778 (interactive "P")
779 (self-insert-command (prefix-numeric-value arg))
780 (if (equal sql-electric-stuff 'semicolon)
781 (comint-send-input)))
782
783 (defun sql-accumulate-and-indent ()
784 "Continue SQL statement on the next line."
785 (interactive)
786 (if (fboundp 'comint-accumulate)
787 (comint-accumulate)
788 (newline))
789 (indent-according-to-mode))
790
791 ;;;###autoload
792 (defun sql-help ()
793 "Show short help for the SQL modes.
794
795 Use an entry function to open an interactive SQL buffer. This buffer is
796 usually named `*SQL*'. The name of the major mode is SQLi.
797
798 Use the following commands to start a specific SQL interpreter:
799
800 PostGres: \\[sql-postgres]
801 MySQL: \\[sql-mysql]
802
803 Other non-free SQL implementations are also supported:
804
805 Solid: \\[sql-solid]
806 Oracle: \\[sql-oracle]
807 Informix: \\[sql-informix]
808 Sybase: \\[sql-sybase]
809 Ingres: \\[sql-ingres]
810 Microsoft: \\[sql-ms]
811 Interbase: \\[sql-interbase]
812
813 But we urge you to choose a free implementation instead of these.
814
815 Once you have the SQLi buffer, you can enter SQL statements in the
816 buffer. The output generated is appended to the buffer and a new prompt
817 is generated. See the In/Out menu in the SQLi buffer for some functions
818 that help you navigate through the buffer, the input history, etc.
819
820 If you have a really complex SQL statement or if you are writing a
821 procedure, you can do this in a separate buffer. Put the new buffer in
822 `sql-mode' by calling \\[sql-mode]. The name of this buffer can be
823 anything. The name of the major mode is SQL.
824
825 In this SQL buffer (SQL mode), you can send the region or the entire
826 buffer to the interactive SQL buffer (SQLi mode). The results are
827 appended to the SQLi buffer without disturbing your SQL buffer."
828 (interactive)
829 (describe-function 'sql-help))
830
831 (defun sql-read-passwd (prompt &optional default)
832 "Read a password using PROMPT.
833 Optional DEFAULT is password to start with. This function calls
834 `read-passwd' if it is available. If not, function
835 `ange-ftp-read-passwd' is called. This should always be available,
836 even in old versions of Emacs."
837 (if (fboundp 'read-passwd)
838 (read-passwd prompt nil default)
839 (unless (fboundp 'ange-ftp-read-passwd)
840 (autoload 'ange-ftp-read-passwd "ange-ftp"))
841 (ange-ftp-read-passwd prompt default)))
842
843 (defun sql-get-login (&rest what)
844 "Get username, password and database from the user.
845
846 The variables `sql-user', `sql-password', `sql-server', and
847 `sql-database' can be customized. They are used as the default values.
848 Usernames, servers and databases are stored in `sql-user-history',
849 `sql-server-history' and `database-history'. Passwords are not stored
850 in a history.
851
852 Parameter WHAT is a list of the arguments passed to this function.
853 The function asks for the username if WHAT contains symbol `user', for
854 the password if it contains symbol `password', for the server if it
855 contains symbol `server', and for the database if it contains symbol
856 `database'.
857
858 In order to ask the user for username, password and database, call the
859 function like this: (sql-get-login 'user 'password 'database)."
860 (interactive)
861 (if (memq 'user what)
862 (setq sql-user
863 (read-from-minibuffer "User: " sql-user nil nil
864 sql-user-history)))
865 (if (memq 'password what)
866 (setq sql-password
867 (sql-read-passwd "Password: " sql-password)))
868 (if (memq 'server what)
869 (setq sql-server
870 (read-from-minibuffer "Server: " sql-server nil nil
871 sql-server-history)))
872 (if (memq 'database what)
873 (setq sql-database
874 (read-from-minibuffer "Database: " sql-database nil nil
875 sql-database-history))))
876
877 (defun sql-find-sqli-buffer ()
878 "Return the current default SQLi buffer or nil.
879 In order to qualify, the SQLi buffer must be alive,
880 be in `sql-interactive-mode' and have a process."
881 (let ((default-buffer (default-value 'sql-buffer)))
882 (if (and (buffer-live-p default-buffer)
883 (get-buffer-process default-buffer))
884 default-buffer
885 (save-excursion
886 (let ((buflist (buffer-list))
887 (found))
888 (while (not (or (null buflist)
889 found))
890 (let ((candidate (car buflist)))
891 (set-buffer candidate)
892 (if (and (equal major-mode 'sql-interactive-mode)
893 (get-buffer-process candidate))
894 (setq found candidate))
895 (setq buflist (cdr buflist))))
896 found)))))
897
898 (defun sql-set-sqli-buffer-generally ()
899 "Set SQLi buffer for all SQL buffers that have none.
900 This function checks all SQL buffers for their SQLi buffer. If their
901 SQLi buffer is nonexistent or has no process, it is set to the current
902 default SQLi buffer. The current default SQLi buffer is determined
903 using `sql-find-sqli-buffer'. If `sql-buffer' is set,
904 `sql-set-sqli-hook' is run."
905 (interactive)
906 (save-excursion
907 (let ((buflist (buffer-list))
908 (default-sqli-buffer (sql-find-sqli-buffer)))
909 (setq-default sql-buffer default-sqli-buffer)
910 (while (not (null buflist))
911 (let ((candidate (car buflist)))
912 (set-buffer candidate)
913 (if (and (equal major-mode 'sql-mode)
914 (not (buffer-live-p sql-buffer)))
915 (progn
916 (setq sql-buffer default-sqli-buffer)
917 (run-hooks 'sql-set-sqli-hook))))
918 (setq buflist (cdr buflist))))))
919
920 (defun sql-set-sqli-buffer ()
921 "Set the SQLi buffer SQL strings are sent to.
922
923 Call this function in a SQL buffer in order to set the SQLi buffer SQL
924 strings are sent to. Calling this function sets `sql-buffer' and runs
925 `sql-set-sqli-hook'.
926
927 If you call it from a SQL buffer, this sets the local copy of
928 `sql-buffer'.
929
930 If you call it from anywhere else, it sets the global copy of
931 `sql-buffer'."
932 (interactive)
933 (let ((default-buffer (sql-find-sqli-buffer)))
934 (if (null default-buffer)
935 (error "There is no suitable SQLi buffer"))
936 (let ((new-buffer
937 (get-buffer
938 (read-buffer "New SQLi buffer: " default-buffer t))))
939 (if (null (get-buffer-process new-buffer))
940 (error "Buffer %s has no process" (buffer-name new-buffer)))
941 (if (null (save-excursion
942 (set-buffer new-buffer)
943 (equal major-mode 'sql-interactive-mode)))
944 (error "Buffer %s is no SQLi buffer" (buffer-name new-buffer)))
945 (if new-buffer
946 (progn
947 (setq sql-buffer new-buffer)
948 (run-hooks 'sql-set-sqli-hook))))))
949
950 (defun sql-show-sqli-buffer ()
951 "Show the name of current SQLi buffer.
952
953 This is the buffer SQL strings are sent to. It is stored in the
954 variable `sql-buffer'. See `sql-help' on how to create such a buffer."
955 (interactive)
956 (if (null (buffer-live-p sql-buffer))
957 (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
958 (if (null (get-buffer-process sql-buffer))
959 (message "Buffer %s has no process." (buffer-name sql-buffer))
960 (message "Current SQLi buffer is %s." (buffer-name sql-buffer)))))
961
962 (defun sql-make-alternate-buffer-name ()
963 "Return a string that can be used to rename a SQLi buffer.
964
965 This is used to set `sql-alternate-buffer-name' within
966 `sql-interactive-mode'."
967 (concat (if (string= "" sql-user)
968 (if (string= "" (user-login-name))
969 ()
970 (concat (user-login-name) "/"))
971 (concat sql-user "/"))
972 (if (string= "" sql-database)
973 (if (string= "" sql-server)
974 (system-name)
975 sql-server)
976 sql-database)))
977
978 (defun sql-rename-buffer ()
979 "Renames a SQLi buffer."
980 (interactive)
981 (rename-buffer (format "*SQL: %s*" sql-alternate-buffer-name) t))
982
983 (defun sql-copy-column ()
984 "Copy current column to the end of buffer.
985 Inserts SELECT or commas if appropriate."
986 (interactive)
987 (let ((column))
988 (save-excursion
989 (setq column (buffer-substring
990 (progn (forward-char 1) (backward-sexp 1) (point))
991 (progn (forward-sexp 1) (point))))
992 (goto-char (point-max))
993 (let ((bol (comint-line-beginning-position)))
994 (cond
995 ;; if empty command line, insert SELECT
996 ((= bol (point))
997 (insert "SELECT "))
998 ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
999 ((save-excursion
1000 (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
1001 bol t))
1002 (insert ", "))
1003 ;; else insert a space
1004 (t
1005 (if (eq (preceding-char) ? )
1006 nil
1007 (insert " ")))))
1008 ;; in any case, insert the column
1009 (insert column)
1010 (message "%s" column))))
1011
1012 ;; On NT, SQL*Plus for Oracle turns on full buffering for stdout if it
1013 ;; is not attached to a character device; therefore placeholder
1014 ;; replacement by SQL*Plus is fully buffered. The workaround lets
1015 ;; Emacs query for the placeholders.
1016
1017 (defvar sql-placeholder-history nil
1018 "History of placeholder values used.")
1019
1020 (defun sql-query-placeholders-and-send (proc string)
1021 "Send to PROC input STRING, maybe replacing placeholders.
1022 Placeholders are words starting with and ampersand like &this.
1023 This function is used for `comint-input-sender' if using `sql-oracle' on NT."
1024 (while (string-match "&\\(\\sw+\\)" string)
1025 (setq string (replace-match
1026 (read-from-minibuffer
1027 (format "Enter value for %s: " (match-string 1 string))
1028 nil nil nil sql-placeholder-history)
1029 t t string)))
1030 (comint-send-string proc string)
1031 (comint-send-string proc "\n"))
1032
1033 ;; Using DB2 interactively, newlines must be escaped with " \".
1034 ;; The space before the backslash is relevant.
1035 (defun sql-escape-newlines-and-send (proc string)
1036 "Send to PROC input STRING, escaping newlines if necessary.
1037 Every newline in STRING will be preceded with a space and a backslash."
1038 (let ((result "") (start 0) mb me)
1039 (while (string-match "\n" string start)
1040 (setq mb (match-beginning 0)
1041 me (match-end 0))
1042 (if (and (> mb 1)
1043 (string-equal " \\" (substring string (- mb 2) mb)))
1044 (setq result (concat result (substring string start me)))
1045 (setq result (concat result (substring string start mb) " \\\n")))
1046 (setq start me))
1047 (setq result (concat result (substring string start)))
1048 (comint-send-string proc result)
1049 (comint-send-string proc "\n")))
1050
1051 \f
1052
1053 ;;; Sending the region to the SQLi buffer.
1054
1055 (defun sql-send-region (start end)
1056 "Send a region to the SQL process."
1057 (interactive "r")
1058 (if (buffer-live-p sql-buffer)
1059 (save-excursion
1060 (comint-send-region sql-buffer start end)
1061 (if (string-match "\n$" (buffer-substring start end))
1062 ()
1063 (comint-send-string sql-buffer "\n"))
1064 (message "Sent string to buffer %s." (buffer-name sql-buffer))
1065 (if sql-pop-to-buffer-after-send-region
1066 (pop-to-buffer sql-buffer)
1067 (display-buffer sql-buffer)))
1068 (message "No SQL process started.")))
1069
1070 (defun sql-send-paragraph ()
1071 "Send the current paragraph to the SQL process."
1072 (interactive)
1073 (let ((start (save-excursion
1074 (backward-paragraph)
1075 (point)))
1076 (end (save-excursion
1077 (forward-paragraph)
1078 (point))))
1079 (sql-send-region start end)))
1080
1081 (defun sql-send-buffer ()
1082 "Send the buffer contents to the SQL process."
1083 (interactive)
1084 (sql-send-region (point-min) (point-max)))
1085
1086 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
1087 "Toggle `sql-pop-to-buffer-after-send-region'.
1088
1089 If given the optional parameter VALUE, sets
1090 sql-toggle-pop-to-buffer-after-send-region to VALUE."
1091 (interactive "P")
1092 (if value
1093 (setq sql-pop-to-buffer-after-send-region value)
1094 (setq sql-pop-to-buffer-after-send-region
1095 (null sql-pop-to-buffer-after-send-region ))))
1096
1097 \f
1098
1099 ;;; SQL mode -- uses SQL interactive mode
1100
1101 ;;;###autoload
1102 (defun sql-mode ()
1103 "Major mode to edit SQL.
1104
1105 You can send SQL statements to the SQLi buffer using
1106 \\[sql-send-region]. Such a buffer must exist before you can do this.
1107 See `sql-help' on how to create SQLi buffers.
1108
1109 \\{sql-mode-map}
1110 Customization: Entry to this mode runs the `sql-mode-hook'.
1111
1112 When you put a buffer in SQL mode, the buffer stores the last SQLi
1113 buffer created as its destination in the variable `sql-buffer'. This
1114 will be the buffer \\[sql-send-region] sends the region to. If this
1115 SQLi buffer is killed, \\[sql-send-region] is no longer able to
1116 determine where the strings should be sent to. You can set the
1117 value of `sql-buffer' using \\[sql-set-sqli-buffer].
1118
1119 For information on how to create multiple SQLi buffers, see
1120 `sql-interactive-mode'.
1121
1122 Note that SQL doesn't have an escape character unless you specify
1123 one. If you specify backslash as escape character in SQL,
1124 you must tell Emacs. Here's how to do that in your `~/.emacs' file:
1125
1126 \(add-hook 'sql-mode-hook
1127 (lambda ()
1128 (modify-syntax-entry ?\\\\ \".\" sql-mode-syntax-table)))"
1129 (interactive)
1130 (kill-all-local-variables)
1131 (setq major-mode 'sql-mode)
1132 (setq mode-name "SQL")
1133 (use-local-map sql-mode-map)
1134 (if sql-mode-menu
1135 (easy-menu-add sql-mode-menu)); XEmacs
1136 (set-syntax-table sql-mode-syntax-table)
1137 (make-local-variable 'font-lock-defaults)
1138 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1139 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1140 ;; will have just one quote. Therefore syntactic hilighting is
1141 ;; disabled for interactive buffers. `_' and `.' are considered part
1142 ;; of words.
1143 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1144 nil t ((?_ . "w") (?. . "w"))))
1145 (make-local-variable 'comment-start)
1146 (setq comment-start "--")
1147 ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
1148 (make-local-variable 'sql-buffer)
1149 ;; Add imenu support for sql-mode. Note that imenu-generic-expression
1150 ;; is buffer-local, so we don't need a local-variable for it. SQL is
1151 ;; case-insensitive, that's why we have to set imenu-case-fold-search.
1152 ;; imenu-syntax-alist makes sure that `_' is considered part of object
1153 ;; names.
1154 (setq imenu-generic-expression sql-imenu-generic-expression
1155 imenu-case-fold-search t
1156 imenu-syntax-alist '(("_" . "w")))
1157 ;; Make `sql-send-paragraph' work on paragraphs that contain indented
1158 ;; lines.
1159 (make-local-variable 'paragraph-separate)
1160 (make-local-variable 'paragraph-start)
1161 (setq paragraph-separate "[\f]*$"
1162 paragraph-start "[\n\f]")
1163 ;; Abbrevs
1164 (setq local-abbrev-table sql-mode-abbrev-table)
1165 (setq abbrev-all-caps 1)
1166 ;; Run hook
1167 (run-hooks 'sql-mode-hook))
1168
1169 \f
1170
1171 ;;; SQL interactive mode
1172
1173 (put 'sql-interactive-mode 'mode-class 'special)
1174
1175 (defun sql-interactive-mode ()
1176 "Major mode to use a SQL interpreter interactively.
1177
1178 Do not call this function by yourself. The environment must be
1179 initialized by an entry function specific for the SQL interpreter. See
1180 `sql-help' for a list of available entry functions.
1181
1182 \\[comint-send-input] after the end of the process' output sends the
1183 text from the end of process to the end of the current line.
1184 \\[comint-send-input] before end of process output copies the current
1185 line minus the prompt to the end of the buffer and sends it.
1186 \\[comint-copy-old-input] just copies the current line.
1187 Use \\[sql-accumulate-and-indent] to enter multi-line statements.
1188
1189 If you want to make multiple SQL buffers, rename the `*SQL*' buffer
1190 using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
1191 See `sql-help' for a list of available entry functions. The last buffer
1192 created by such an entry function is the current SQLi buffer. SQL
1193 buffers will send strings to the SQLi buffer current at the time of
1194 their creation. See `sql-mode' for details.
1195
1196 Sample session using two connections:
1197
1198 1. Create first SQLi buffer by calling an entry function.
1199 2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
1200 3. Create a SQL buffer \"test1.sql\".
1201 4. Create second SQLi buffer by calling an entry function.
1202 5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
1203 6. Create a SQL buffer \"test2.sql\".
1204
1205 Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
1206 buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
1207 will send the region to buffer \"*Connection 2*\".
1208
1209 If you accidentally suspend your process, use \\[comint-continue-subjob]
1210 to continue it. On some operating systems, this will not work because
1211 the signals are not supported.
1212
1213 \\{sql-interactive-mode-map}
1214 Customization: Entry to this mode runs the hooks on `comint-mode-hook'
1215 and `sql-interactive-mode-hook' (in that order). Before each input, the
1216 hooks on `comint-input-filter-functions' are run. After each SQL
1217 interpreter output, the hooks on `comint-output-filter-functions' are
1218 run.
1219
1220 Variable `sql-input-ring-file-name' controls the initialisation of the
1221 input ring history.
1222
1223 Variables `comint-output-filter-functions', a hook, and
1224 `comint-scroll-to-bottom-on-input' and
1225 `comint-scroll-to-bottom-on-output' control whether input and output
1226 cause the window to scroll to the end of the buffer.
1227
1228 If you want to make SQL buffers limited in length, add the function
1229 `comint-truncate-buffer' to `comint-output-filter-functions'.
1230
1231 Here is an example for your .emacs file. It keeps the SQLi buffer a
1232 certain length.
1233
1234 \(add-hook 'sql-interactive-mode-hook
1235 \(function (lambda ()
1236 \(setq comint-output-filter-functions 'comint-truncate-buffer))))
1237
1238 Here is another example. It will always put point back to the statement
1239 you entered, right above the output it created.
1240
1241 \(setq comint-output-filter-functions
1242 \(function (lambda (STR) (comint-show-output))))"
1243 (comint-mode)
1244 (setq comint-prompt-regexp sql-prompt-regexp)
1245 (setq left-margin sql-prompt-length)
1246 (setq major-mode 'sql-interactive-mode)
1247 (setq mode-name "SQLi")
1248 (use-local-map sql-interactive-mode-map)
1249 (if sql-interactive-mode-menu
1250 (easy-menu-add sql-interactive-mode-menu)); XEmacs
1251 (set-syntax-table sql-mode-syntax-table)
1252 (make-local-variable 'font-lock-defaults)
1253 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1254 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1255 ;; will have just one quote. Therefore syntactic hilighting is
1256 ;; disabled for interactive buffers. `_' and `.' are considered part
1257 ;; of words.
1258 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1259 t t ((?_ . "w") (?. . "w"))))
1260 ;; Enable commenting and uncommenting of the region.
1261 (make-local-variable 'comment-start)
1262 (setq comment-start "--")
1263 ;; Abbreviation table init and case-insensitive. It is not activatet
1264 ;; by default.
1265 (setq local-abbrev-table sql-mode-abbrev-table)
1266 (setq abbrev-all-caps 1)
1267 ;; Exiting the process will call sql-stop.
1268 (set-process-sentinel (get-buffer-process sql-buffer) 'sql-stop)
1269 ;; People wanting a different history file for each
1270 ;; buffer/process/client/whatever can change separator and file-name
1271 ;; on the sql-interactive-mode-hook.
1272 (setq comint-input-ring-separator sql-input-ring-separator
1273 comint-input-ring-file-name sql-input-ring-file-name)
1274 ;; Create a usefull name for renaming this buffer later.
1275 (make-local-variable 'sql-alternate-buffer-name)
1276 (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
1277 ;; User stuff.
1278 (run-hooks 'sql-interactive-mode-hook)
1279 ;; Calling the hook before calling comint-read-input-ring allows users
1280 ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
1281 (comint-read-input-ring t))
1282
1283 (defun sql-stop (process event)
1284 "Called when the SQL process is stopped.
1285
1286 Writes the input history to a history file using
1287 `comint-write-input-ring' and inserts a short message in the SQL buffer.
1288
1289 This function is a sentinel watching the SQL interpreter process.
1290 Sentinels will always get the two parameters PROCESS and EVENT."
1291 (comint-write-input-ring)
1292 (if (and (eq (current-buffer) sql-buffer)
1293 (not buffer-read-only))
1294 (insert (format "\nProcess %s %s\n" process event))
1295 (message "Process %s %s" process event)))
1296
1297 \f
1298
1299 ;;; Entry functions for different SQL interpreters.
1300
1301 ;;;###autoload
1302 (defun sql-oracle ()
1303 "Run sqlplus by Oracle as an inferior process.
1304
1305 If buffer `*SQL*' exists but no process is running, make a new process.
1306 If buffer exists and a process is running, just switch to buffer
1307 `*SQL*'.
1308
1309 Interpreter used comes from variable `sql-oracle-program'. Login uses
1310 the variables `sql-user', `sql-password', and `sql-database' as
1311 defaults, if set. Additional command line parameters can be stored in
1312 the list `sql-oracle-options'.
1313
1314 The buffer is put in sql-interactive-mode, giving commands for sending
1315 input. See `sql-interactive-mode'.
1316
1317 To specify a coding system for converting non-ASCII characters
1318 in the input and output to the process, use \\[universal-coding-system-argument]
1319 before \\[sql-oracle]. You can also specify this with \\[set-buffer-process-coding-system]
1320 in the SQL buffer, after you start the process.
1321 The default comes from `process-coding-system-alist' and
1322 `default-process-coding-system'.
1323
1324 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1325 (interactive)
1326 (if (comint-check-proc "*SQL*")
1327 (pop-to-buffer "*SQL*")
1328 (sql-get-login 'user 'password 'database)
1329 (message "Login...")
1330 ;; Produce user/password@database construct. Password without user
1331 ;; is meaningless; database without user/password is meaningless,
1332 ;; because "@param" will ask sqlplus to interpret the script
1333 ;; "param".
1334 (let ((parameter nil))
1335 (if (not (string= "" sql-user))
1336 (if (not (string= "" sql-password))
1337 (setq parameter (concat sql-user "/" sql-password))
1338 (setq parameter sql-user)))
1339 (if (and parameter (not (string= "" sql-database)))
1340 (setq parameter (concat parameter "@" sql-database)))
1341 (if parameter
1342 (setq parameter (nconc (list parameter) sql-oracle-options))
1343 (setq parameter sql-oracle-options))
1344 (if parameter
1345 (set-buffer (apply 'make-comint "SQL" sql-oracle-program nil
1346 parameter))
1347 (set-buffer (make-comint "SQL" sql-oracle-program nil))))
1348 (setq sql-prompt-regexp "^SQL> ")
1349 (setq sql-prompt-length 5)
1350 (setq sql-buffer (current-buffer))
1351 ;; set sql-mode-font-lock-keywords to something different before
1352 ;; calling sql-interactive-mode.
1353 (setq sql-mode-font-lock-keywords sql-mode-oracle-font-lock-keywords)
1354 (sql-interactive-mode)
1355 ;; If running on NT, make sure we do placeholder replacement
1356 ;; ourselves. This must come after sql-interactive-mode because all
1357 ;; local variables will be killed, there.
1358 (if (eq window-system 'w32)
1359 (setq comint-input-sender 'sql-query-placeholders-and-send))
1360 (message "Login...done")
1361 (pop-to-buffer sql-buffer)))
1362
1363 \f
1364
1365 ;;;###autoload
1366 (defun sql-sybase ()
1367 "Run isql by SyBase as an inferior process.
1368
1369 If buffer `*SQL*' exists but no process is running, make a new process.
1370 If buffer exists and a process is running, just switch to buffer
1371 `*SQL*'.
1372
1373 Interpreter used comes from variable `sql-sybase-program'. Login uses
1374 the variables `sql-server', `sql-user', `sql-password', and
1375 `sql-database' as defaults, if set. Additional command line parameters
1376 can be stored in the list `sql-sybase-options'.
1377
1378 The buffer is put in sql-interactive-mode, giving commands for sending
1379 input. See `sql-interactive-mode'.
1380
1381 To specify a coding system for converting non-ASCII characters
1382 in the input and output to the process, use \\[universal-coding-system-argument]
1383 before \\[sql-sybase]. You can also specify this with \\[set-buffer-process-coding-system]
1384 in the SQL buffer, after you start the process.
1385 The default comes from `process-coding-system-alist' and
1386 `default-process-coding-system'.
1387
1388 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1389 (interactive)
1390 (if (comint-check-proc "*SQL*")
1391 (pop-to-buffer "*SQL*")
1392 (sql-get-login 'server 'user 'password 'database)
1393 (message "Login...")
1394 ;; Put all parameters to the program (if defined) in a list and call
1395 ;; make-comint.
1396 (let ((params sql-sybase-options))
1397 (if (not (string= "" sql-server))
1398 (setq params (append (list "-S" sql-server) params)))
1399 (if (not (string= "" sql-database))
1400 (setq params (append (list "-D" sql-database) params)))
1401 (if (not (string= "" sql-password))
1402 (setq params (append (list "-P" sql-password) params)))
1403 (if (not (string= "" sql-user))
1404 (setq params (append (list "-U" sql-user) params)))
1405 (set-buffer (apply 'make-comint "SQL" sql-sybase-program
1406 nil params)))
1407 (setq sql-prompt-regexp "^SQL> ")
1408 (setq sql-prompt-length 5)
1409 (setq sql-buffer (current-buffer))
1410 (sql-interactive-mode)
1411 (message "Login...done")
1412 (pop-to-buffer sql-buffer)))
1413
1414 \f
1415
1416 ;;;###autoload
1417 (defun sql-informix ()
1418 "Run dbaccess by Informix as an inferior process.
1419
1420 If buffer `*SQL*' exists but no process is running, make a new process.
1421 If buffer exists and a process is running, just switch to buffer
1422 `*SQL*'.
1423
1424 Interpreter used comes from variable `sql-informix-program'. Login uses
1425 the variable `sql-database' as default, if set.
1426
1427 The buffer is put in sql-interactive-mode, giving commands for sending
1428 input. See `sql-interactive-mode'.
1429
1430 To specify a coding system for converting non-ASCII characters
1431 in the input and output to the process, use \\[universal-coding-system-argument]
1432 before \\[sql-informix]. You can also specify this with \\[set-buffer-process-coding-system]
1433 in the SQL buffer, after you start the process.
1434 The default comes from `process-coding-system-alist' and
1435 `default-process-coding-system'.
1436
1437 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1438 (interactive)
1439 (if (comint-check-proc "*SQL*")
1440 (pop-to-buffer "*SQL*")
1441 (sql-get-login 'database)
1442 (message "Login...")
1443 ;; username and password are ignored.
1444 (if (string= "" sql-database)
1445 (set-buffer (make-comint "SQL" sql-informix-program nil))
1446 (set-buffer (make-comint "SQL" sql-informix-program nil sql-database "-")))
1447 (setq sql-prompt-regexp "^SQL> ")
1448 (setq sql-prompt-length 5)
1449 (setq sql-buffer (current-buffer))
1450 (sql-interactive-mode)
1451 (message "Login...done")
1452 (pop-to-buffer sql-buffer)))
1453
1454 \f
1455
1456 ;;;###autoload
1457 (defun sql-mysql ()
1458 "Run mysql by TcX as an inferior process.
1459
1460 Mysql versions 3.23 and up are free software.
1461
1462 If buffer `*SQL*' exists but no process is running, make a new process.
1463 If buffer exists and a process is running, just switch to buffer
1464 `*SQL*'.
1465
1466 Interpreter used comes from variable `sql-mysql-program'. Login uses
1467 the variables `sql-user', `sql-password', `sql-database', and
1468 `sql-server' as defaults, if set. Additional command line parameters
1469 can be stored in the list `sql-mysql-options'.
1470
1471 The buffer is put in sql-interactive-mode, giving commands for sending
1472 input. See `sql-interactive-mode'.
1473
1474 To specify a coding system for converting non-ASCII characters
1475 in the input and output to the process, use \\[universal-coding-system-argument]
1476 before \\[sql-mysql]. You can also specify this with \\[set-buffer-process-coding-system]
1477 in the SQL buffer, after you start the process.
1478 The default comes from `process-coding-system-alist' and
1479 `default-process-coding-system'.
1480
1481 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1482 (interactive)
1483 (if (comint-check-proc "*SQL*")
1484 (pop-to-buffer "*SQL*")
1485 (sql-get-login 'user 'password 'database 'server)
1486 (message "Login...")
1487 ;; Put all parameters to the program (if defined) in a list and call
1488 ;; make-comint.
1489 (let ((params))
1490 (if (not (string= "" sql-database))
1491 (setq params (append (list sql-database) params)))
1492 (if (not (string= "" sql-server))
1493 (setq params (append (list (concat "--host=" sql-server)) params)))
1494 (if (not (string= "" sql-password))
1495 (setq params (append (list (concat "--password=" sql-password)) params)))
1496 (if (not (string= "" sql-user))
1497 (setq params (append (list (concat "--user=" sql-user)) params)))
1498 (if (not (null sql-mysql-options))
1499 (setq params (append sql-mysql-options params)))
1500 (set-buffer (apply 'make-comint "SQL" sql-mysql-program
1501 nil params)))
1502 (setq sql-prompt-regexp "^mysql>")
1503 (setq sql-prompt-length 6)
1504 (setq sql-buffer (current-buffer))
1505 (sql-interactive-mode)
1506 (message "Login...done")
1507 (pop-to-buffer sql-buffer)))
1508
1509 \f
1510
1511 ;;;###autoload
1512 (defun sql-solid ()
1513 "Run solsql by Solid as an inferior process.
1514
1515 If buffer `*SQL*' exists but no process is running, make a new process.
1516 If buffer exists and a process is running, just switch to buffer
1517 `*SQL*'.
1518
1519 Interpreter used comes from variable `sql-solid-program'. Login uses
1520 the variables `sql-user', `sql-password', and `sql-server' as
1521 defaults, if set.
1522
1523 The buffer is put in sql-interactive-mode, giving commands for sending
1524 input. See `sql-interactive-mode'.
1525
1526 To specify a coding system for converting non-ASCII characters
1527 in the input and output to the process, use \\[universal-coding-system-argument]
1528 before \\[sql-solid]. You can also specify this with \\[set-buffer-process-coding-system]
1529 in the SQL buffer, after you start the process.
1530 The default comes from `process-coding-system-alist' and
1531 `default-process-coding-system'.
1532
1533 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1534 (interactive)
1535 (if (comint-check-proc "*SQL*")
1536 (pop-to-buffer "*SQL*")
1537 (sql-get-login 'user 'password 'server)
1538 (message "Login...")
1539 ;; Put all parameters to the program (if defined) in a list and call
1540 ;; make-comint.
1541 (let ((params))
1542 ;; It only makes sense if both username and password are there.
1543 (if (not (or (string= "" sql-user)
1544 (string= "" sql-password)))
1545 (setq params (append (list sql-user sql-password) params)))
1546 (if (not (string= "" sql-server))
1547 (setq params (append (list sql-server) params)))
1548 (set-buffer (apply 'make-comint "SQL" sql-solid-program
1549 nil params)))
1550 (setq sql-prompt-regexp "^")
1551 (setq sql-prompt-length 0)
1552 (setq sql-buffer (current-buffer))
1553 (sql-interactive-mode)
1554 (message "Login...done")
1555 (pop-to-buffer sql-buffer)))
1556
1557 \f
1558
1559 ;;;###autoload
1560 (defun sql-ingres ()
1561 "Run sql by Ingres as an inferior process.
1562
1563 If buffer `*SQL*' exists but no process is running, make a new process.
1564 If buffer exists and a process is running, just switch to buffer
1565 `*SQL*'.
1566
1567 Interpreter used comes from variable `sql-ingres-program'. Login uses
1568 the variable `sql-database' as default, if set.
1569
1570 The buffer is put in sql-interactive-mode, giving commands for sending
1571 input. See `sql-interactive-mode'.
1572
1573 To specify a coding system for converting non-ASCII characters
1574 in the input and output to the process, use \\[universal-coding-system-argument]
1575 before \\[sql-ingres]. You can also specify this with \\[set-buffer-process-coding-system]
1576 in the SQL buffer, after you start the process.
1577 The default comes from `process-coding-system-alist' and
1578 `default-process-coding-system'.
1579
1580 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1581 (interactive)
1582 (if (comint-check-proc "*SQL*")
1583 (pop-to-buffer "*SQL*")
1584 (sql-get-login 'database)
1585 (message "Login...")
1586 ;; username and password are ignored.
1587 (if (string= "" sql-database)
1588 (set-buffer (make-comint "SQL" sql-ingres-program nil))
1589 (set-buffer (make-comint "SQL" sql-ingres-program nil sql-database)))
1590 (setq sql-prompt-regexp "^\* ")
1591 (setq sql-prompt-length 2)
1592 (setq sql-buffer (current-buffer))
1593 (sql-interactive-mode)
1594 (message "Login...done")
1595 (pop-to-buffer sql-buffer)))
1596
1597 \f
1598
1599 ;;;###autoload
1600 (defun sql-ms ()
1601 "Run isql by Microsoft as an inferior process.
1602
1603 If buffer `*SQL*' exists but no process is running, make a new process.
1604 If buffer exists and a process is running, just switch to buffer
1605 `*SQL*'.
1606
1607 Interpreter used comes from variable `sql-ms-program'. Login uses the
1608 variables `sql-user', `sql-password', `sql-database', and `sql-server'
1609 as defaults, if set.
1610
1611 The buffer is put in sql-interactive-mode, giving commands for sending
1612 input. See `sql-interactive-mode'.
1613
1614 To specify a coding system for converting non-ASCII characters
1615 in the input and output to the process, use \\[universal-coding-system-argument]
1616 before \\[sql-ms]. You can also specify this with \\[set-buffer-process-coding-system]
1617 in the SQL buffer, after you start the process.
1618 The default comes from `process-coding-system-alist' and
1619 `default-process-coding-system'.
1620
1621 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1622 (interactive)
1623 (if (comint-check-proc "*SQL*")
1624 (pop-to-buffer "*SQL*")
1625 (sql-get-login 'user 'password 'database 'server)
1626 (message "Login...")
1627 ;; Put all parameters to the program (if defined) in a list and call
1628 ;; make-comint.
1629 (let ((params '("-w 300")))
1630 (if (not (string= "" sql-server))
1631 (setq params (append (list "-S" sql-server) params)))
1632 (if (not (string= "" sql-database))
1633 (setq params (append (list "-d" sql-database) params)))
1634 (if (not (string= "" sql-user))
1635 (setq params (append (list "-U" sql-user) params)))
1636 (if (not (string= "" sql-password))
1637 (setq params (append (list "-P" sql-password) params))
1638 ;; If -P is passed to ISQL as the last argument without a password,
1639 ;; it's considered null.
1640 (setq params (append params (list "-P"))))
1641 (set-buffer (apply 'make-comint "SQL" sql-ms-program
1642 nil params)))
1643 (setq sql-prompt-regexp "^[0-9]*>")
1644 (setq sql-prompt-length 5)
1645 (setq sql-buffer (current-buffer))
1646 (sql-interactive-mode)
1647 (message "Login...done")
1648 (pop-to-buffer sql-buffer)))
1649
1650 \f
1651
1652 ;;;###autoload
1653 (defun sql-postgres ()
1654 "Run psql by Postgres as an inferior process.
1655
1656 If buffer `*SQL*' exists but no process is running, make a new process.
1657 If buffer exists and a process is running, just switch to buffer
1658 `*SQL*'.
1659
1660 Interpreter used comes from variable `sql-postgres-program'. Login uses
1661 the variables `sql-database' and `sql-server' as default, if set.
1662 Additional command line parameters can be stored in the list
1663 `sql-postgres-options'.
1664
1665 The buffer is put in sql-interactive-mode, giving commands for sending
1666 input. See `sql-interactive-mode'.
1667
1668 To specify a coding system for converting non-ASCII characters
1669 in the input and output to the process, use \\[universal-coding-system-argument]
1670 before \\[sql-postgres]. You can also specify this with \\[set-buffer-process-coding-system]
1671 in the SQL buffer, after you start the process.
1672 The default comes from `process-coding-system-alist' and
1673 `default-process-coding-system'. If your output lines end with ^M,
1674 your might try undecided-dos as a coding system. If this doesn't help,
1675 Try to set `comint-output-filter-functions' like this:
1676
1677 \(setq comint-output-filter-functions (append comint-output-filter-functions
1678 '(comint-strip-ctrl-m)))
1679
1680 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1681 (interactive)
1682 (if (comint-check-proc "*SQL*")
1683 (pop-to-buffer "*SQL*")
1684 (sql-get-login 'database 'server)
1685 (message "Login...")
1686 ;; username and password are ignored. Mark Stosberg suggest to add
1687 ;; the database at the end. Jason Beegan suggest using --pset and
1688 ;; pager=off instead of \\o|cat. The later was the solution by
1689 ;; Gregor Zych. Jason's suggestion is the default value for
1690 ;; sql-postgres-options.
1691 (let ((params sql-postgres-options))
1692 (if (not (string= "" sql-database))
1693 (setq params (append params (list sql-database))))
1694 (if (not (string= "" sql-server))
1695 (setq params (append (list "-h" sql-server) params)))
1696 (set-buffer (apply 'make-comint "SQL" sql-postgres-program
1697 nil params)))
1698 (setq sql-prompt-regexp "^.*> *")
1699 (setq sql-prompt-length 5)
1700 ;; This is a lousy hack to prevent psql from truncating it's output
1701 ;; and giving stupid warnings. If s.o. knows a way to prevent psql
1702 ;; from acting this way, then I would be very thankful to
1703 ;; incorporate this (Gregor Zych <zych@pool.informatik.rwth-aachen.de>)
1704 ;; (comint-send-string "*SQL*" "\\o \| cat\n")
1705 (setq sql-mode-font-lock-keywords sql-mode-postgres-font-lock-keywords)
1706 (setq sql-buffer (current-buffer))
1707 (sql-interactive-mode)
1708 (message "Login...done")
1709 (pop-to-buffer sql-buffer)))
1710
1711 \f
1712
1713 ;;;###autoload
1714 (defun sql-interbase ()
1715 "Run isql by Interbase as an inferior process.
1716
1717 If buffer `*SQL*' exists but no process is running, make a new process.
1718 If buffer exists and a process is running, just switch to buffer
1719 `*SQL*'.
1720
1721 Interpreter used comes from variable `sql-interbase-program'. Login
1722 uses the variables `sql-user', `sql-password', and `sql-database' as
1723 defaults, if set.
1724
1725 The buffer is put in sql-interactive-mode, giving commands for sending
1726 input. See `sql-interactive-mode'.
1727
1728 To specify a coding system for converting non-ASCII characters
1729 in the input and output to the process, use \\[universal-coding-system-argument]
1730 before \\[sql-interbase]. You can also specify this with \\[set-buffer-process-coding-system]
1731 in the SQL buffer, after you start the process.
1732 The default comes from `process-coding-system-alist' and
1733 `default-process-coding-system'.
1734
1735 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1736 (interactive)
1737 (if (comint-check-proc "*SQL*")
1738 (pop-to-buffer "*SQL*")
1739 (sql-get-login 'user 'password 'database)
1740 (message "Login...")
1741 ;; Put all parameters to the program (if defined) in a list and call
1742 ;; make-comint.
1743 (let ((params sql-interbase-options))
1744 (if (not (string= "" sql-user))
1745 (setq params (append (list "-u" sql-user) params)))
1746 (if (not (string= "" sql-password))
1747 (setq params (append (list "-p" sql-password) params)))
1748 (if (not (string= "" sql-database))
1749 (setq params (cons sql-database params))); add to the front!
1750 (set-buffer (apply 'make-comint "SQL" sql-interbase-program
1751 nil params)))
1752 (setq sql-prompt-regexp "^SQL> ")
1753 (setq sql-prompt-length 5)
1754 (setq sql-buffer (current-buffer))
1755 (sql-interactive-mode)
1756 (message "Login...done")
1757 (pop-to-buffer sql-buffer)))
1758
1759 \f
1760
1761 ;;;###autoload
1762 (defun sql-db2 ()
1763 "Run db2 by IBM as an inferior process.
1764
1765 If buffer `*SQL*' exists but no process is running, make a new process.
1766 If buffer exists and a process is running, just switch to buffer
1767 `*SQL*'.
1768
1769 Interpreter used comes from variable `sql-db2-program'. There is not
1770 automatic login.
1771
1772 The buffer is put in sql-interactive-mode, giving commands for sending
1773 input. See `sql-interactive-mode'.
1774
1775 If you use \\[sql-accumulate-and-indent] to send multiline commands to
1776 db2, newlines will be escaped if necessary. If you don't want that, set
1777 `comint-input-sender' back to `comint-simple-send' by writing an after
1778 advice. See the elisp manual for more information.
1779
1780 To specify a coding system for converting non-ASCII characters
1781 in the input and output to the process, use \\[universal-coding-system-argument]
1782 before \\[sql-db2]. You can also specify this with \\[set-buffer-process-coding-system]
1783 in the SQL buffer, after you start the process.
1784 The default comes from `process-coding-system-alist' and
1785 `default-process-coding-system'.
1786
1787 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1788 (interactive)
1789 (if (comint-check-proc "*SQL*")
1790 (pop-to-buffer "*SQL*")
1791 (message "Login...")
1792 ;; Put all parameters to the program (if defined) in a list and call
1793 ;; make-comint.
1794 (set-buffer (apply 'make-comint "SQL" sql-db2-program
1795 nil sql-db2-options))
1796 (setq sql-prompt-regexp "^db2 => ")
1797 (setq sql-prompt-length 7)
1798 (setq sql-buffer (current-buffer))
1799 (sql-interactive-mode)
1800 ;; Escape newlines. This must come after sql-interactive-mode
1801 ;; because all local variables will be killed, there.
1802 (setq comint-input-sender 'sql-escape-newlines-and-send)
1803 (message "Login...done")
1804 (pop-to-buffer sql-buffer)))
1805
1806 (provide 'sql)
1807
1808 ;;; sql.el ends here