*** empty log message ***
[bpt/emacs.git] / lisp / progmodes / sql.el
1 ;;; sql.el --- specialized comint.el for SQL interpreters
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4
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
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 SQLite
266
267 (defcustom sql-sqlite-program "sqlite"
268 "*Command to start SQLite.
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-sqlite-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 MySql
285
286 (defcustom sql-mysql-program "mysql"
287 "*Command to start mysql by TcX.
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 (defcustom sql-mysql-options nil
296 "*List of additional options for `sql-mysql-program'.
297 The following list of options is reported to make things work
298 on Windows: \"-C\" \"-t\" \"-f\" \"-n\"."
299 :type '(repeat string)
300 :version "20.8"
301 :group 'SQL)
302
303 ;; Customization for Solid
304
305 (defcustom sql-solid-program "solsql"
306 "*Command to start SOLID SQL Editor.
307
308 Starts `sql-interactive-mode' after doing some setup.
309
310 The program can also specify a TCP connection. See `make-comint'."
311 :type 'file
312 :group 'SQL)
313
314 ;; Customization for SyBase
315
316 (defcustom sql-sybase-program "isql"
317 "*Command to start isql by SyBase.
318
319 Starts `sql-interactive-mode' after doing some setup.
320
321 The program can also specify a TCP connection. See `make-comint'."
322 :type 'file
323 :group 'SQL)
324
325 (defcustom sql-sybase-options nil
326 "*List of additional options for `sql-sybase-program'.
327 Some versions of isql might require the -n option in order to work."
328 :type '(repeat string)
329 :version "20.8"
330 :group 'SQL)
331
332 ;; Customization for Informix
333
334 (defcustom sql-informix-program "dbaccess"
335 "*Command to start dbaccess by Informix.
336
337 Starts `sql-interactive-mode' after doing some setup.
338
339 The program can also specify a TCP connection. See `make-comint'."
340 :type 'file
341 :group 'SQL)
342
343 ;; Customization for Ingres
344
345 (defcustom sql-ingres-program "sql"
346 "*Command to start sql by Ingres.
347
348 Starts `sql-interactive-mode' after doing some setup.
349
350 The program can also specify a TCP connection. See `make-comint'."
351 :type 'file
352 :group 'SQL)
353
354 ;; Customization for Microsoft
355
356 (defcustom sql-ms-program "isql"
357 "*Command to start isql by Microsoft.
358
359 Starts `sql-interactive-mode' after doing some setup.
360
361 The program can also specify a TCP connection. See `make-comint'."
362 :type 'file
363 :group 'SQL)
364
365 (defcustom sql-ms-options '("-w" "300" "-n")
366 ;; -w is the linesize
367 "*List of additional options for `sql-ms-program'."
368 :type '(repeat string)
369 :version "21.4"
370 :group 'SQL)
371
372 ;; Customization for Postgres
373
374 (defcustom sql-postgres-program "psql"
375 "Command to start psql by Postgres.
376
377 Starts `sql-interactive-mode' after doing some setup.
378
379 The program can also specify a TCP connection. See `make-comint'."
380 :type 'file
381 :group 'SQL)
382
383 (defcustom sql-postgres-options '("-P" "pager=off")
384 "*List of additional options for `sql-postgres-program'.
385 The default setting includes the -P option which breaks older versions
386 of the psql client (such as version 6.5.3). The -P option is equivalent
387 to the --pset option. If you want the psql to prompt you for a user
388 name, add the string \"-u\" to the list of options. If you want to
389 provide a user name on the command line (newer versions such as 7.1),
390 add your name with a \"-U\" prefix (such as \"-Umark\") to the list."
391 :type '(repeat string)
392 :version "20.8"
393 :group 'SQL)
394
395 ;; Customization for Interbase
396
397 (defcustom sql-interbase-program "isql"
398 "*Command to start isql by Interbase.
399
400 Starts `sql-interactive-mode' after doing some setup.
401
402 The program can also specify a TCP connection. See `make-comint'."
403 :type 'file
404 :group 'SQL)
405
406 (defcustom sql-interbase-options nil
407 "*List of additional options for `sql-interbase-program'."
408 :type '(repeat string)
409 :version "20.8"
410 :group 'SQL)
411
412 ;; Customization for DB2
413
414 (defcustom sql-db2-program "db2"
415 "*Command to start db2 by IBM.
416
417 Starts `sql-interactive-mode' after doing some setup.
418
419 The program can also specify a TCP connection. See `make-comint'."
420 :type 'file
421 :group 'SQL)
422
423 (defcustom sql-db2-options nil
424 "*List of additional options for `sql-db2-program'."
425 :type '(repeat string)
426 :version "20.8"
427 :group 'SQL)
428
429 ;; Customization for Linter
430
431 (defcustom sql-linter-program "inl"
432 "*Command to start inl by RELEX.
433
434 Starts `sql-interactive-mode' after doing some setup."
435 :type 'file
436 :group 'SQL)
437
438 (defcustom sql-linter-options nil
439 "*List of additional options for `sql-linter-program'."
440 :type '(repeat string)
441 :version "21.3"
442 :group 'SQL)
443
444 \f
445
446 ;;; Variables which do not need customization
447
448 (defvar sql-user-history nil
449 "History of usernames used.")
450
451 (defvar sql-database-history nil
452 "History of databases used.")
453
454 (defvar sql-server-history nil
455 "History of servers used.")
456
457 ;; Passwords are not kept in a history.
458
459 (defvar sql-buffer nil
460 "Current SQLi buffer.
461
462 The global value of sql-buffer is the name of the latest SQLi buffer
463 created. Any SQL buffer created will make a local copy of this value.
464 See `sql-interactive-mode' for more on multiple sessions. If you want
465 to change the SQLi buffer a SQL mode sends its SQL strings to, change
466 the local value of `sql-buffer' using \\[sql-set-sqli-buffer].")
467
468 (defvar sql-prompt-regexp nil
469 "Prompt used to initialize `comint-prompt-regexp'.
470
471 You can change `comint-prompt-regexp' on `sql-interactive-mode-hook'.")
472
473 (defvar sql-prompt-length 0
474 "Prompt used to set `left-margin' in `sql-interactive-mode'.
475
476 You can change it on `sql-interactive-mode-hook'.")
477
478 (defvar sql-alternate-buffer-name nil
479 "Buffer-local string used to possibly rename the SQLi buffer.
480
481 Used by `sql-rename-buffer'.")
482
483 ;; Keymap for sql-interactive-mode.
484
485 (defvar sql-interactive-mode-map
486 (let ((map (make-sparse-keymap)))
487 (if (functionp 'set-keymap-parent)
488 (set-keymap-parent map comint-mode-map); Emacs
489 (set-keymap-parents map (list comint-mode-map))); XEmacs
490 (if (functionp 'set-keymap-name)
491 (set-keymap-name map 'sql-interactive-mode-map)); XEmacs
492 (define-key map (kbd "C-j") 'sql-accumulate-and-indent)
493 (define-key map (kbd "C-c C-w") 'sql-copy-column)
494 (define-key map (kbd "O") 'sql-magic-go)
495 (define-key map (kbd "o") 'sql-magic-go)
496 (define-key map (kbd ";") 'sql-magic-semicolon)
497 map)
498 "Mode map used for `sql-interactive-mode'.
499 Based on `comint-mode-map'.")
500
501 ;; Keymap for sql-mode.
502
503 (defvar sql-mode-map
504 (let ((map (make-sparse-keymap)))
505 (define-key map (kbd "C-c C-c") 'sql-send-paragraph)
506 (define-key map (kbd "C-c C-r") 'sql-send-region)
507 (define-key map (kbd "C-c C-b") 'sql-send-buffer)
508 map)
509 "Mode map used for `sql-mode'.")
510
511 ;; easy menu for sql-mode.
512
513 (easy-menu-define
514 sql-mode-menu sql-mode-map
515 "Menu for `sql-mode'."
516 '("SQL"
517 ["Send Paragraph" sql-send-paragraph (and (buffer-live-p sql-buffer)
518 (get-buffer-process sql-buffer))]
519 ["Send Region" sql-send-region (and (or (and (boundp 'mark-active); Emacs
520 mark-active)
521 (mark)); XEmacs
522 (buffer-live-p sql-buffer)
523 (get-buffer-process sql-buffer))]
524 ["Send Buffer" sql-send-buffer (and (buffer-live-p sql-buffer)
525 (get-buffer-process sql-buffer))]
526 ["Show SQLi buffer" sql-show-sqli-buffer t]
527 ["Set SQLi buffer" sql-set-sqli-buffer t]
528 ["Pop to SQLi buffer after send"
529 sql-toggle-pop-to-buffer-after-send-region
530 :style toggle
531 :selected sql-pop-to-buffer-after-send-region]
532 ("Highlighting"
533 ["ANSI SQL keywords" sql-highlight-ansi-keywords t]
534 ["Oracle keywords" sql-highlight-oracle-keywords t]
535 ["Postgres keywords" sql-highlight-postgres-keywords t]
536 ["Linter keywords" sql-highlight-linter-keywords t]
537 )))
538
539 ;; easy menu for sql-interactive-mode.
540
541 (easy-menu-define
542 sql-interactive-mode-menu sql-interactive-mode-map
543 "Menu for `sql-interactive-mode'."
544 '("SQL"
545 ["Rename Buffer" sql-rename-buffer t]))
546
547 ;; Abbreviations -- if you want more of them, define them in your
548 ;; ~/.emacs file. Abbrevs have to be enabled in your ~/.emacs, too.
549
550 (defvar sql-mode-abbrev-table nil
551 "Abbrev table used in `sql-mode' and `sql-interactive-mode'.")
552 (if sql-mode-abbrev-table
553 ()
554 (let ((wrapper))
555 (define-abbrev-table 'sql-mode-abbrev-table ())
556 (define-abbrev sql-mode-abbrev-table "ins" "insert")
557 (define-abbrev sql-mode-abbrev-table "upd" "update")
558 (define-abbrev sql-mode-abbrev-table "del" "delete")
559 (define-abbrev sql-mode-abbrev-table "sel" "select")))
560
561 ;; Syntax Table
562
563 (defvar sql-mode-syntax-table
564 (let ((table (make-syntax-table)))
565 ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
566 (modify-syntax-entry ?/ ". 14" table)
567 (modify-syntax-entry ?* ". 23" table)
568 ;; double-dash starts comment
569 (if (string-match "XEmacs\\|Lucid" emacs-version)
570 (modify-syntax-entry ?- ". 56" table)
571 (modify-syntax-entry ?- ". 12b" table))
572 ;; newline and formfeed end coments
573 (modify-syntax-entry ?\n "> b" table)
574 (modify-syntax-entry ?\f "> b" table)
575 ;; single quotes (') quotes delimit strings
576 (modify-syntax-entry ?' "\"" table)
577 ;; backslash is no escape character
578 (modify-syntax-entry ?\\ "." table)
579 table)
580 "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
581
582 ;; Font lock support
583
584 (defvar sql-mode-ansi-font-lock-keywords nil
585 "ANSI SQL keywords used by font-lock.
586
587 This variable is used by `sql-mode' and `sql-interactive-mode'. The
588 regular expressions are created during compilation by calling the
589 function `regexp-opt'. Therefore, take a look at the source before
590 you define your own sql-mode-ansi-font-lock-keywords. You may want to
591 add functions and PL/SQL keywords.")
592 (if sql-mode-ansi-font-lock-keywords
593 ()
594 (let ((ansi-keywords (eval-when-compile
595 (concat "\\b"
596 (regexp-opt '(
597 "authorization" "avg" "begin" "close" "cobol" "commit"
598 "continue" "count" "declare" "double" "end" "escape"
599 "exec" "fetch" "foreign" "fortran" "found" "go" "goto" "indicator"
600 "key" "language" "max" "min" "module" "numeric" "open" "pascal" "pli"
601 "precision" "primary" "procedure" "references" "rollback"
602 "schema" "section" "some" "sqlcode" "sqlerror" "sum" "work") t) "\\b")))
603 (ansi-reserved-words (eval-when-compile
604 (concat "\\b"
605 (regexp-opt '(
606 "all" "and" "any" "as" "asc" "between" "by" "check" "create"
607 "current" "default" "delete" "desc" "distinct" "exists" "float" "for"
608 "from" "grant" "group" "having" "in" "insert" "into" "is"
609 "like" "not" "null" "of" "on" "option" "or" "order" "privileges"
610 "public" "select" "set" "table" "to" "union" "unique"
611 "update" "user" "values" "view" "where" "with") t) "\\b")))
612 (ansi-types (eval-when-compile
613 (concat "\\b"
614 (regexp-opt '(
615 ;; ANSI Keywords that look like types
616 "character" "cursor" "dec" "int" "real"
617 ;; ANSI Reserved Word that look like types
618 "char" "integer" "smallint" ) t) "\\b"))))
619 (setq sql-mode-ansi-font-lock-keywords
620 (list (cons ansi-keywords 'font-lock-function-name-face)
621 (cons ansi-reserved-words 'font-lock-keyword-face)
622 (cons ansi-types 'font-lock-type-face)))))
623
624 (defvar sql-mode-oracle-font-lock-keywords nil
625 "Oracle SQL keywords used by font-lock.
626
627 This variable is used by `sql-mode' and `sql-interactive-mode'. The
628 regular expressions are created during compilation by calling the
629 function `regexp-opt'. Therefore, take a look at the source before
630 you define your own sql-mode-oracle-font-lock-keywords. You may want
631 to add functions and PL/SQL keywords.")
632 (if sql-mode-oracle-font-lock-keywords
633 ()
634 (let ((oracle-keywords (eval-when-compile
635 (concat "\\b"
636 (regexp-opt '(
637 "admin" "after" "allocate" "analyze" "archive" "archivelog" "backup"
638 "become" "before" "block" "body" "cache" "cancel" "cascade" "change"
639 "checkpoint" "compile" "constraint" "constraints" "contents"
640 "controlfile" "cycle" "database" "datafile" "dba" "disable" "dismount"
641 "dump" "each" "else" "elsif" "enable" "events" "except" "exceptions"
642 "execute" "exit" "explain" "extent" "externally" "false" "flush" "force"
643 "freelist" "freelists" "function" "groups" "if" "including" "initrans"
644 "instance" "layer" "link" "lists" "logfile" "loop" "manage" "manual"
645 "maxdatafiles" "maxinistances" "maxlogfiles" "maxloghistory"
646 "maxlogmembers" "maxtrans" "maxvalue" "minextents" "minvalue" "mount"
647 "new" "next" "noarchivelog" "nocache" "nocycle" "nomaxvalue"
648 "nominvalue" "none" "noorder" "noresetlogs" "normal" "nosort" "off"
649 "old" "only" "optimal" "others" "out" "own" "package" "parallel"
650 "pctincrease" "pctused" "plan" "pragma" "private" "profile" "quota"
651 "raise" "read" "recover" "referencing" "resetlogs" "restrict_references"
652 "restricted" "return" "returning" "reuse" "rnds" "rnps" "role" "roles"
653 "savepoint" "scn" "segment" "sequence" "shared" "snapshot" "sort"
654 "statement_id" "statistics" "stop" "storage" "subtype" "switch" "system"
655 "tables" "tablespace" "temporary" "thread" "time" "tracing"
656 "transaction" "triggers" "true" "truncate" "type" "under" "unlimited"
657 "until" "use" "using" "when" "while" "wnds" "wnps" "write") t) "\\b")))
658 (oracle-warning-words (eval-when-compile
659 (concat "\\b"
660 (regexp-opt '(
661 "cursor_already_open" "dup_val_on_index" "exception" "invalid_cursor"
662 "invalid_number" "login_denied" "no_data_found" "not_logged_on"
663 "notfound" "others" "pragma" "program_error" "storage_error"
664 "timeout_on_resource" "too_many_rows" "transaction_backed_out"
665 "value_error" "zero_divide") t) "\\b")))
666 (oracle-reserved-words (eval-when-compile
667 (concat "\\b"
668 (regexp-opt '(
669 "access" "add" "alter" "audit" "cluster" "column" "comment" "compress"
670 "connect" "drop" "else" "exclusive" "file" "grant"
671 "identified" "immediate" "increment" "index" "initial" "intersect"
672 "level" "lock" "long" "maxextents" "minus" "mode" "modify" "noaudit"
673 "nocompress" "nowait" "number" "offline" "online" "pctfree" "prior"
674 "raw" "rename" "resource" "revoke" "row" "rowlabel" "rownum"
675 "rows" "session" "share" "size" "start" "successful" "synonym" "sysdate"
676 "then" "trigger" "uid" "validate" "whenever") t) "\\b")))
677 (oracle-types (eval-when-compile
678 (concat "\\b"
679 (regexp-opt '(
680 ;; Oracle Keywords that look like types
681 ;; Oracle Reserved Words that look like types
682 "binary_integer" "blob" "boolean" "constant" "date" "decimal" "rowid"
683 "varchar" "varchar2") t) "\\b")))
684 (oracle-builtin-functions (eval-when-compile
685 (concat "\\b"
686 (regexp-opt '(
687 ;; Misc Oracle builtin functions
688 "abs" "add_months" "ascii" "avg" "ceil" "chartorowid" "chr" "concat"
689 "convert" "cos" "cosh" "count" "currval" "decode" "dump" "exp" "floor"
690 "glb" "greatest" "greatest_lb" "hextoraw" "initcap" "instr" "instrb"
691 "last_day" "least" "least_ub" "length" "lengthb" "ln" "log" "lower"
692 "lpad" "ltrim" "lub" "max" "min" "mod" "months_between" "new_time"
693 "next_day" "nextval" "nls_initcap" "nls_lower" "nls_upper" "nlssort"
694 "nvl" "power" "rawtohex" "replace" "round" "rowidtochar" "rpad"
695 "rtrim" "sign" "sin" "sinh" "soundex" "sqlcode" "sqlerrm" "sqrt"
696 "stddev" "sum" "substr" "substrb" "tan" "tanh" "to_char"
697 "to_date" "to_label" "to_multi_byte" "to_number" "to_single_byte"
698 "translate" "trim" "trunc" "uid" "upper" "userenv" "variance" "vsize") t) "\\b"))))
699 (setq sql-mode-oracle-font-lock-keywords
700 (append sql-mode-ansi-font-lock-keywords
701 (list (cons oracle-keywords 'font-lock-function-name-face)
702 (cons oracle-warning-words 'font-lock-warning-face)
703 (cons oracle-reserved-words 'font-lock-keyword-face)
704 ;; XEmacs doesn't have font-lock-builtin-face
705 (if (string-match "XEmacs\\|Lucid" emacs-version)
706 (cons oracle-builtin-functions 'font-lock-preprocessor-face)
707 ;; GNU Emacs 19 doesn't have it either
708 (if (string-match "GNU Emacs 19" emacs-version)
709 (cons oracle-builtin-functions 'font-lock-function-name-face)
710 ;; Emacs
711 (cons oracle-builtin-functions 'font-lock-builtin-face)))
712 (cons oracle-types 'font-lock-type-face))))))
713
714 (defvar sql-mode-postgres-font-lock-keywords nil
715 "Postgres SQL keywords used by font-lock.
716
717 This variable is used by `sql-mode' and `sql-interactive-mode'. The
718 regular expressions are created during compilation by calling the
719 function `regexp-opt'. Therefore, take a look at the source before
720 you define your own sql-mode-postgres-font-lock-keywords.")
721
722 (if sql-mode-postgres-font-lock-keywords
723 ()
724 (let ((postgres-reserved-words (eval-when-compile
725 (concat "\\b"
726 (regexp-opt '(
727 "language"
728 ) t) "\\b")))
729 (postgres-types (eval-when-compile
730 (concat "\\b"
731 (regexp-opt '(
732 "bool" "box" "circle" "char" "char2" "char4" "char8" "char16" "date"
733 "float4" "float8" "int2" "int4" "int8" "line" "lseg" "money" "path"
734 "point" "polygon" "serial" "text" "time" "timespan" "timestamp" "varchar"
735 ) t)"\\b")))
736 (postgres-builtin-functions (eval-when-compile
737 (concat "\\b"
738 (regexp-opt '(
739 ;; Misc Postgres builtin functions
740 "abstime" "age" "area" "box" "center" "date_part" "date_trunc"
741 "datetime" "dexp" "diameter" "dpow" "float" "float4" "height"
742 "initcap" "integer" "isclosed" "isfinite" "isoldpath" "isopen"
743 "length" "lower" "lpad" "ltrim" "pclose" "point" "points" "popen"
744 "position" "radius" "reltime" "revertpoly" "rpad" "rtrim" "substr"
745 "substring" "text" "timespan" "translate" "trim" "upgradepath"
746 "upgradepoly" "upper" "varchar" "width"
747 ) t) "\\b"))))
748 (setq sql-mode-postgres-font-lock-keywords
749 (append sql-mode-ansi-font-lock-keywords
750 (list (cons postgres-reserved-words 'font-lock-keyword-face)
751 ;; XEmacs doesn't have 'font-lock-builtin-face
752 (if (string-match "XEmacs\\|Lucid" emacs-version)
753 (cons postgres-builtin-functions 'font-lock-preprocessor-face)
754 ;; Emacs
755 (cons postgres-builtin-functions 'font-lock-builtin-face))
756 (cons postgres-types 'font-lock-type-face))))))
757
758
759 (defvar sql-mode-linter-font-lock-keywords nil
760 "Linter SQL keywords used by font-lock.
761
762 This variable is used by `sql-mode' and `sql-interactive-mode'. The
763 regular expressions are created during compilation by calling the
764 function `regexp-opt'.")
765
766 (if sql-mode-linter-font-lock-keywords
767 ()
768 (let ((linter-keywords (eval-when-compile
769 (concat "\\b"
770 (regexp-opt '(
771 "autocommit" "autoinc" "autorowid" "cancel" "cascade" "channel"
772 "committed" "count" "countblob" "cross" "current" "data" "database"
773 "datafile" "datafiles" "datesplit" "dba" "dbname" "default" "deferred"
774 "denied" "description" "device" "difference" "directory" "error"
775 "escape" "euc" "exclusive" "external" "extfile" "false" "file"
776 "filename" "filesize" "filetime" "filter" "findblob" "first" "foreign"
777 "full" "fuzzy" "global" "granted" "ignore" "immediate" "increment"
778 "indexes" "indexfile" "indexfiles" "indextime" "initial" "integrity"
779 "internal" "key" "last_autoinc" "last_rowid" "limit" "linter"
780 "linter_file_device" "linter_file_size" "linter_name_length" "ln"
781 "local" "login" "maxisn" "maxrow" "maxrowid" "maxvalue" "message"
782 "minvalue" "module" "names" "national" "natural" "new" "new_table"
783 "no" "node" "noneuc" "nulliferror" "numbers" "off" "old" "old_table"
784 "only" "operation" "optimistic" "option" "page" "partially" "password"
785 "phrase" "plan" "precision" "primary" "priority" "privileges"
786 "proc_info_size" "proc_par_name_len" "protocol" "quant" "range" "raw"
787 "read" "record" "records" "references" "remote" "rename" "replication"
788 "restart" "rewrite" "root" "row" "rule" "savepoint" "security"
789 "sensitive" "sequence" "serializable" "server" "since" "size" "some"
790 "startup" "statement" "station" "success" "sys_guid" "tables" "test"
791 "timeout" "trace" "transaction" "translation" "trigger"
792 "trigger_info_size" "true" "trunc" "uncommitted" "unicode" "unknown"
793 "unlimited" "unlisted" "user" "utf8" "value" "varying" "volumes"
794 "wait" "windows_code" "workspace" "write" "xml"
795 ) t) "\\b")))
796 (linter-reserved-words (eval-when-compile
797 (concat "\\b"
798 (regexp-opt '(
799 "access" "action" "add" "address" "after" "all" "alter" "always" "and"
800 "any" "append" "as" "asc" "ascic" "async" "at_begin" "at_end" "audit"
801 "aud_obj_name_len" "backup" "base" "before" "between" "blobfile"
802 "blobfiles" "blobpct" "brief" "browse" "by" "case" "cast" "check"
803 "clear" "close" "column" "comment" "commit" "connect" "contains"
804 "correct" "create" "delete" "desc" "disable" "disconnect" "distinct"
805 "drop" "each" "ef" "else" "enable" "end" "event" "except" "exclude"
806 "execute" "exists" "extract" "fetch" "finish" "for" "from" "get"
807 "grant" "group" "having" "identified" "in" "index" "inner" "insert"
808 "instead" "intersect" "into" "is" "isolation" "join" "left" "level"
809 "like" "lock" "mode" "modify" "not" "nowait" "null" "of" "on" "open"
810 "or" "order" "outer" "owner" "press" "prior" "procedure" "public"
811 "purge" "rebuild" "resource" "restrict" "revoke" "right" "role"
812 "rollback" "rownum" "select" "session" "set" "share" "shutdown"
813 "start" "stop" "sync" "synchronize" "synonym" "sysdate" "table" "then"
814 "to" "union" "unique" "unlock" "until" "update" "using" "values"
815 "view" "when" "where" "with" "without"
816 ) t) "\\b")))
817 (linter-types (eval-when-compile
818 (concat "\\b"
819 (regexp-opt '(
820 "bigint" "bitmap" "blob" "boolean" "char" "character" "date"
821 "datetime" "dec" "decimal" "double" "float" "int" "integer" "nchar"
822 "number" "numeric" "real" "smallint" "varbyte" "varchar" "byte"
823 "cursor" "long"
824 ) t) "\\b")))
825 (linter-builtin-functions (eval-when-compile
826 (concat "\\b"
827 (regexp-opt '(
828 "abs" "acos" "asin" "atan" "atan2" "avg" "ceil" "cos" "cosh" "divtime"
829 "exp" "floor" "getbits" "getblob" "getbyte" "getlong" "getraw"
830 "getstr" "gettext" "getword" "hextoraw" "lenblob" "length" "log"
831 "lower" "lpad" "ltrim" "max" "min" "mod" "monthname" "nvl"
832 "octet_length" "power" "rand" "rawtohex" "repeat_string"
833 "right_substr" "round" "rpad" "rtrim" "sign" "sin" "sinh" "soundex"
834 "sqrt" "sum" "tan" "tanh" "timeint_to_days" "to_char" "to_date"
835 "to_gmtime" "to_localtime" "to_number" "trim" "upper" "decode"
836 "substr" "substring" "chr" "dayname" "days" "greatest" "hex" "initcap"
837 "instr" "least" "multime" "replace" "width"
838 ) t) "\\b"))))
839 (setq sql-mode-linter-font-lock-keywords
840 (append sql-mode-ansi-font-lock-keywords
841 (list (cons linter-keywords 'font-lock-function-name-face)
842 (cons linter-reserved-words 'font-lock-keyword-face)
843 ;; XEmacs doesn't have font-lock-builtin-face
844 (if (string-match "XEmacs\\|Lucid" emacs-version)
845 (cons linter-builtin-functions 'font-lock-preprocessor-face)
846 ;; GNU Emacs 19 doesn't have it either
847 (if (string-match "GNU Emacs 19" emacs-version)
848 (cons linter-builtin-functions 'font-lock-function-name-face)
849 ;; Emacs
850 (cons linter-builtin-functions 'font-lock-builtin-face)))
851 (cons linter-types 'font-lock-type-face))))))
852
853 (defvar sql-mode-font-lock-keywords sql-mode-ansi-font-lock-keywords
854 "SQL keywords used by font-lock.
855
856 This variable defaults to `sql-mode-ansi-font-lock-keywords'. This is
857 used for the default `font-lock-defaults' value in `sql-mode'. This
858 can be changed by some entry functions to provide more hilighting.")
859
860 \f
861
862 ;;; Functions to switch highlighting
863
864 (defun sql-highlight-oracle-keywords ()
865 "Highlight Oracle keywords.
866 Basically, this just sets `font-lock-keywords' appropriately."
867 (interactive)
868 (setq font-lock-keywords sql-mode-oracle-font-lock-keywords)
869 (font-lock-fontify-buffer))
870
871 (defun sql-highlight-postgres-keywords ()
872 "Highlight Postgres keywords.
873 Basically, this just sets `font-lock-keywords' appropriately."
874 (interactive)
875 (setq font-lock-keywords sql-mode-postgres-font-lock-keywords)
876 (font-lock-fontify-buffer))
877
878 (defun sql-highlight-linter-keywords ()
879 "Highlight LINTER keywords.
880 Basically, this just sets `font-lock-keywords' appropriately."
881 (interactive)
882 (setq font-lock-keywords sql-mode-linter-font-lock-keywords)
883 (font-lock-fontify-buffer))
884
885 (defun sql-highlight-ansi-keywords ()
886 "Highlight ANSI SQL keywords.
887 Basically, this just sets `font-lock-keywords' appropriately."
888 (interactive)
889 (setq font-lock-keywords sql-mode-ansi-font-lock-keywords)
890 (font-lock-fontify-buffer))
891
892 \f
893
894 ;;; Compatibility functions
895
896 (if (not (fboundp 'comint-line-beginning-position))
897 ;; comint-line-beginning-position is defined in Emacs 21
898 (defun comint-line-beginning-position ()
899 "Returns the buffer position of the beginning of the line, after any prompt.
900 The prompt is assumed to be any text at the beginning of the line matching
901 the regular expression `comint-prompt-regexp', a buffer local variable."
902 (save-excursion (comint-bol nil) (point))))
903
904 \f
905
906 ;;; Small functions
907
908 (defun sql-magic-go (arg)
909 "Insert \"o\" and call `comint-send-input'.
910 `sql-electric-stuff' must be the symbol `go'."
911 (interactive "P")
912 (self-insert-command (prefix-numeric-value arg))
913 (if (and (equal sql-electric-stuff 'go)
914 (save-excursion
915 (comint-bol nil)
916 (looking-at "go\\b")))
917 (comint-send-input)))
918
919 (defun sql-magic-semicolon (arg)
920 "Insert semicolon and call `comint-send-input'.
921 `sql-electric-stuff' must be the symbol `semicolon'."
922 (interactive "P")
923 (self-insert-command (prefix-numeric-value arg))
924 (if (equal sql-electric-stuff 'semicolon)
925 (comint-send-input)))
926
927 (defun sql-accumulate-and-indent ()
928 "Continue SQL statement on the next line."
929 (interactive)
930 (if (fboundp 'comint-accumulate)
931 (comint-accumulate)
932 (newline))
933 (indent-according-to-mode))
934
935 ;;;###autoload
936 (defun sql-help ()
937 "Show short help for the SQL modes.
938
939 Use an entry function to open an interactive SQL buffer. This buffer is
940 usually named `*SQL*'. The name of the major mode is SQLi.
941
942 Use the following commands to start a specific SQL interpreter:
943
944 PostGres: \\[sql-postgres]
945 MySQL: \\[sql-mysql]
946 SQLite: \\[sql-sqlite]
947
948 Other non-free SQL implementations are also supported:
949
950 Solid: \\[sql-solid]
951 Oracle: \\[sql-oracle]
952 Informix: \\[sql-informix]
953 Sybase: \\[sql-sybase]
954 Ingres: \\[sql-ingres]
955 Microsoft: \\[sql-ms]
956 Interbase: \\[sql-interbase]
957 Linter: \\[sql-linter]
958
959 But we urge you to choose a free implementation instead of these.
960
961 Once you have the SQLi buffer, you can enter SQL statements in the
962 buffer. The output generated is appended to the buffer and a new prompt
963 is generated. See the In/Out menu in the SQLi buffer for some functions
964 that help you navigate through the buffer, the input history, etc.
965
966 If you have a really complex SQL statement or if you are writing a
967 procedure, you can do this in a separate buffer. Put the new buffer in
968 `sql-mode' by calling \\[sql-mode]. The name of this buffer can be
969 anything. The name of the major mode is SQL.
970
971 In this SQL buffer (SQL mode), you can send the region or the entire
972 buffer to the interactive SQL buffer (SQLi mode). The results are
973 appended to the SQLi buffer without disturbing your SQL buffer."
974 (interactive)
975 (describe-function 'sql-help))
976
977 (defun sql-read-passwd (prompt &optional default)
978 "Read a password using PROMPT.
979 Optional DEFAULT is password to start with. This function calls
980 `read-passwd' if it is available. If not, function
981 `ange-ftp-read-passwd' is called. This should always be available,
982 even in old versions of Emacs."
983 (if (fboundp 'read-passwd)
984 (read-passwd prompt nil default)
985 (unless (fboundp 'ange-ftp-read-passwd)
986 (autoload 'ange-ftp-read-passwd "ange-ftp"))
987 (ange-ftp-read-passwd prompt default)))
988
989 (defun sql-get-login (&rest what)
990 "Get username, password and database from the user.
991
992 The variables `sql-user', `sql-password', `sql-server', and
993 `sql-database' can be customized. They are used as the default values.
994 Usernames, servers and databases are stored in `sql-user-history',
995 `sql-server-history' and `database-history'. Passwords are not stored
996 in a history.
997
998 Parameter WHAT is a list of the arguments passed to this function.
999 The function asks for the username if WHAT contains symbol `user', for
1000 the password if it contains symbol `password', for the server if it
1001 contains symbol `server', and for the database if it contains symbol
1002 `database'.
1003
1004 In order to ask the user for username, password and database, call the
1005 function like this: (sql-get-login 'user 'password 'database)."
1006 (interactive)
1007 (if (memq 'user what)
1008 (setq sql-user
1009 (read-from-minibuffer "User: " sql-user nil nil
1010 sql-user-history)))
1011 (if (memq 'password what)
1012 (setq sql-password
1013 (sql-read-passwd "Password: " sql-password)))
1014 (if (memq 'server what)
1015 (setq sql-server
1016 (read-from-minibuffer "Server: " sql-server nil nil
1017 sql-server-history)))
1018 (if (memq 'database what)
1019 (setq sql-database
1020 (read-from-minibuffer "Database: " sql-database nil nil
1021 sql-database-history))))
1022
1023 (defun sql-find-sqli-buffer ()
1024 "Return the current default SQLi buffer or nil.
1025 In order to qualify, the SQLi buffer must be alive,
1026 be in `sql-interactive-mode' and have a process."
1027 (let ((default-buffer (default-value 'sql-buffer)))
1028 (if (and (buffer-live-p default-buffer)
1029 (get-buffer-process default-buffer))
1030 default-buffer
1031 (save-excursion
1032 (let ((buflist (buffer-list))
1033 (found))
1034 (while (not (or (null buflist)
1035 found))
1036 (let ((candidate (car buflist)))
1037 (set-buffer candidate)
1038 (if (and (equal major-mode 'sql-interactive-mode)
1039 (get-buffer-process candidate))
1040 (setq found candidate))
1041 (setq buflist (cdr buflist))))
1042 found)))))
1043
1044 (defun sql-set-sqli-buffer-generally ()
1045 "Set SQLi buffer for all SQL buffers that have none.
1046 This function checks all SQL buffers for their SQLi buffer. If their
1047 SQLi buffer is nonexistent or has no process, it is set to the current
1048 default SQLi buffer. The current default SQLi buffer is determined
1049 using `sql-find-sqli-buffer'. If `sql-buffer' is set,
1050 `sql-set-sqli-hook' is run."
1051 (interactive)
1052 (save-excursion
1053 (let ((buflist (buffer-list))
1054 (default-sqli-buffer (sql-find-sqli-buffer)))
1055 (setq-default sql-buffer default-sqli-buffer)
1056 (while (not (null buflist))
1057 (let ((candidate (car buflist)))
1058 (set-buffer candidate)
1059 (if (and (equal major-mode 'sql-mode)
1060 (not (buffer-live-p sql-buffer)))
1061 (progn
1062 (setq sql-buffer default-sqli-buffer)
1063 (run-hooks 'sql-set-sqli-hook))))
1064 (setq buflist (cdr buflist))))))
1065
1066 (defun sql-set-sqli-buffer ()
1067 "Set the SQLi buffer SQL strings are sent to.
1068
1069 Call this function in a SQL buffer in order to set the SQLi buffer SQL
1070 strings are sent to. Calling this function sets `sql-buffer' and runs
1071 `sql-set-sqli-hook'.
1072
1073 If you call it from a SQL buffer, this sets the local copy of
1074 `sql-buffer'.
1075
1076 If you call it from anywhere else, it sets the global copy of
1077 `sql-buffer'."
1078 (interactive)
1079 (let ((default-buffer (sql-find-sqli-buffer)))
1080 (if (null default-buffer)
1081 (error "There is no suitable SQLi buffer"))
1082 (let ((new-buffer
1083 (get-buffer
1084 (read-buffer "New SQLi buffer: " default-buffer t))))
1085 (if (null (get-buffer-process new-buffer))
1086 (error "Buffer %s has no process" (buffer-name new-buffer)))
1087 (if (null (save-excursion
1088 (set-buffer new-buffer)
1089 (equal major-mode 'sql-interactive-mode)))
1090 (error "Buffer %s is no SQLi buffer" (buffer-name new-buffer)))
1091 (if new-buffer
1092 (progn
1093 (setq sql-buffer new-buffer)
1094 (run-hooks 'sql-set-sqli-hook))))))
1095
1096 (defun sql-show-sqli-buffer ()
1097 "Show the name of current SQLi buffer.
1098
1099 This is the buffer SQL strings are sent to. It is stored in the
1100 variable `sql-buffer'. See `sql-help' on how to create such a buffer."
1101 (interactive)
1102 (if (null (buffer-live-p sql-buffer))
1103 (message "%s has no SQLi buffer set." (buffer-name (current-buffer)))
1104 (if (null (get-buffer-process sql-buffer))
1105 (message "Buffer %s has no process." (buffer-name sql-buffer))
1106 (message "Current SQLi buffer is %s." (buffer-name sql-buffer)))))
1107
1108 (defun sql-make-alternate-buffer-name ()
1109 "Return a string that can be used to rename a SQLi buffer.
1110
1111 This is used to set `sql-alternate-buffer-name' within
1112 `sql-interactive-mode'."
1113 (concat (if (string= "" sql-user)
1114 (if (string= "" (user-login-name))
1115 ()
1116 (concat (user-login-name) "/"))
1117 (concat sql-user "/"))
1118 (if (string= "" sql-database)
1119 (if (string= "" sql-server)
1120 (system-name)
1121 sql-server)
1122 sql-database)))
1123
1124 (defun sql-rename-buffer ()
1125 "Renames a SQLi buffer."
1126 (interactive)
1127 (rename-buffer (format "*SQL: %s*" sql-alternate-buffer-name) t))
1128
1129 (defun sql-copy-column ()
1130 "Copy current column to the end of buffer.
1131 Inserts SELECT or commas if appropriate."
1132 (interactive)
1133 (let ((column))
1134 (save-excursion
1135 (setq column (buffer-substring
1136 (progn (forward-char 1) (backward-sexp 1) (point))
1137 (progn (forward-sexp 1) (point))))
1138 (goto-char (point-max))
1139 (let ((bol (comint-line-beginning-position)))
1140 (cond
1141 ;; if empty command line, insert SELECT
1142 ((= bol (point))
1143 (insert "SELECT "))
1144 ;; else if appending to INTO .* (, SELECT or ORDER BY, insert a comma
1145 ((save-excursion
1146 (re-search-backward "\\b\\(\\(into\\s-+\\S-+\\s-+(\\)\\|select\\|order by\\) .+"
1147 bol t))
1148 (insert ", "))
1149 ;; else insert a space
1150 (t
1151 (if (eq (preceding-char) ? )
1152 nil
1153 (insert " ")))))
1154 ;; in any case, insert the column
1155 (insert column)
1156 (message "%s" column))))
1157
1158 ;; On NT, SQL*Plus for Oracle turns on full buffering for stdout if it
1159 ;; is not attached to a character device; therefore placeholder
1160 ;; replacement by SQL*Plus is fully buffered. The workaround lets
1161 ;; Emacs query for the placeholders.
1162
1163 (defvar sql-placeholder-history nil
1164 "History of placeholder values used.")
1165
1166 (defun sql-query-placeholders-and-send (proc string)
1167 "Send to PROC input STRING, maybe replacing placeholders.
1168 Placeholders are words starting with and ampersand like &this.
1169 This function is used for `comint-input-sender' if using `sql-oracle' on NT."
1170 (while (string-match "&\\(\\sw+\\)" string)
1171 (setq string (replace-match
1172 (read-from-minibuffer
1173 (format "Enter value for %s: " (match-string 1 string))
1174 nil nil nil sql-placeholder-history)
1175 t t string)))
1176 (comint-send-string proc string)
1177 (if comint-input-sender-no-newline
1178 (if (not (string-equal string ""))
1179 (process-send-eof))
1180 (comint-send-string proc "\n")))
1181
1182 ;; Using DB2 interactively, newlines must be escaped with " \".
1183 ;; The space before the backslash is relevant.
1184 (defun sql-escape-newlines-and-send (proc string)
1185 "Send to PROC input STRING, escaping newlines if necessary.
1186 Every newline in STRING will be preceded with a space and a backslash."
1187 (let ((result "") (start 0) mb me)
1188 (while (string-match "\n" string start)
1189 (setq mb (match-beginning 0)
1190 me (match-end 0))
1191 (if (and (> mb 1)
1192 (string-equal " \\" (substring string (- mb 2) mb)))
1193 (setq result (concat result (substring string start me)))
1194 (setq result (concat result (substring string start mb) " \\\n")))
1195 (setq start me))
1196 (setq result (concat result (substring string start)))
1197 (comint-send-string proc result)
1198 (if comint-input-sender-no-newline
1199 (if (not (string-equal string ""))
1200 (process-send-eof))
1201 (comint-send-string proc "\n"))))
1202
1203 \f
1204
1205 ;;; Sending the region to the SQLi buffer.
1206
1207 (defun sql-send-region (start end)
1208 "Send a region to the SQL process."
1209 (interactive "r")
1210 (if (buffer-live-p sql-buffer)
1211 (save-excursion
1212 (comint-send-region sql-buffer start end)
1213 (if (string-match "\n$" (buffer-substring start end))
1214 ()
1215 (comint-send-string sql-buffer "\n"))
1216 (message "Sent string to buffer %s." (buffer-name sql-buffer))
1217 (if sql-pop-to-buffer-after-send-region
1218 (pop-to-buffer sql-buffer)
1219 (display-buffer sql-buffer)))
1220 (message "No SQL process started.")))
1221
1222 (defun sql-send-paragraph ()
1223 "Send the current paragraph to the SQL process."
1224 (interactive)
1225 (let ((start (save-excursion
1226 (backward-paragraph)
1227 (point)))
1228 (end (save-excursion
1229 (forward-paragraph)
1230 (point))))
1231 (sql-send-region start end)))
1232
1233 (defun sql-send-buffer ()
1234 "Send the buffer contents to the SQL process."
1235 (interactive)
1236 (sql-send-region (point-min) (point-max)))
1237
1238 (defun sql-toggle-pop-to-buffer-after-send-region (&optional value)
1239 "Toggle `sql-pop-to-buffer-after-send-region'.
1240
1241 If given the optional parameter VALUE, sets
1242 sql-toggle-pop-to-buffer-after-send-region to VALUE."
1243 (interactive "P")
1244 (if value
1245 (setq sql-pop-to-buffer-after-send-region value)
1246 (setq sql-pop-to-buffer-after-send-region
1247 (null sql-pop-to-buffer-after-send-region ))))
1248
1249 \f
1250
1251 ;;; SQL mode -- uses SQL interactive mode
1252
1253 ;;;###autoload
1254 (defun sql-mode ()
1255 "Major mode to edit SQL.
1256
1257 You can send SQL statements to the SQLi buffer using
1258 \\[sql-send-region]. Such a buffer must exist before you can do this.
1259 See `sql-help' on how to create SQLi buffers.
1260
1261 \\{sql-mode-map}
1262 Customization: Entry to this mode runs the `sql-mode-hook'.
1263
1264 When you put a buffer in SQL mode, the buffer stores the last SQLi
1265 buffer created as its destination in the variable `sql-buffer'. This
1266 will be the buffer \\[sql-send-region] sends the region to. If this
1267 SQLi buffer is killed, \\[sql-send-region] is no longer able to
1268 determine where the strings should be sent to. You can set the
1269 value of `sql-buffer' using \\[sql-set-sqli-buffer].
1270
1271 For information on how to create multiple SQLi buffers, see
1272 `sql-interactive-mode'.
1273
1274 Note that SQL doesn't have an escape character unless you specify
1275 one. If you specify backslash as escape character in SQL,
1276 you must tell Emacs. Here's how to do that in your `~/.emacs' file:
1277
1278 \(add-hook 'sql-mode-hook
1279 (lambda ()
1280 (modify-syntax-entry ?\\\\ \".\" sql-mode-syntax-table)))"
1281 (interactive)
1282 (kill-all-local-variables)
1283 (setq major-mode 'sql-mode)
1284 (setq mode-name "SQL")
1285 (use-local-map sql-mode-map)
1286 (if sql-mode-menu
1287 (easy-menu-add sql-mode-menu)); XEmacs
1288 (set-syntax-table sql-mode-syntax-table)
1289 (make-local-variable 'font-lock-defaults)
1290 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1291 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1292 ;; will have just one quote. Therefore syntactic hilighting is
1293 ;; disabled for interactive buffers. `_' and `.' are considered part
1294 ;; of words.
1295 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1296 nil t ((?_ . "w") (?. . "w"))))
1297 (make-local-variable 'comment-start)
1298 (setq comment-start "--")
1299 ;; Make each buffer in sql-mode remember the "current" SQLi buffer.
1300 (make-local-variable 'sql-buffer)
1301 ;; Add imenu support for sql-mode. Note that imenu-generic-expression
1302 ;; is buffer-local, so we don't need a local-variable for it. SQL is
1303 ;; case-insensitive, that's why we have to set imenu-case-fold-search.
1304 ;; imenu-syntax-alist makes sure that `_' is considered part of object
1305 ;; names.
1306 (setq imenu-generic-expression sql-imenu-generic-expression
1307 imenu-case-fold-search t
1308 imenu-syntax-alist '(("_" . "w")))
1309 ;; Make `sql-send-paragraph' work on paragraphs that contain indented
1310 ;; lines.
1311 (make-local-variable 'paragraph-separate)
1312 (make-local-variable 'paragraph-start)
1313 (setq paragraph-separate "[\f]*$"
1314 paragraph-start "[\n\f]")
1315 ;; Abbrevs
1316 (setq local-abbrev-table sql-mode-abbrev-table)
1317 (setq abbrev-all-caps 1)
1318 ;; Run hook
1319 (run-hooks 'sql-mode-hook))
1320
1321 \f
1322
1323 ;;; SQL interactive mode
1324
1325 (put 'sql-interactive-mode 'mode-class 'special)
1326
1327 (defun sql-interactive-mode ()
1328 "Major mode to use a SQL interpreter interactively.
1329
1330 Do not call this function by yourself. The environment must be
1331 initialized by an entry function specific for the SQL interpreter. See
1332 `sql-help' for a list of available entry functions.
1333
1334 \\[comint-send-input] after the end of the process' output sends the
1335 text from the end of process to the end of the current line.
1336 \\[comint-send-input] before end of process output copies the current
1337 line minus the prompt to the end of the buffer and sends it.
1338 \\[comint-copy-old-input] just copies the current line.
1339 Use \\[sql-accumulate-and-indent] to enter multi-line statements.
1340
1341 If you want to make multiple SQL buffers, rename the `*SQL*' buffer
1342 using \\[rename-buffer] or \\[rename-uniquely] and start a new process.
1343 See `sql-help' for a list of available entry functions. The last buffer
1344 created by such an entry function is the current SQLi buffer. SQL
1345 buffers will send strings to the SQLi buffer current at the time of
1346 their creation. See `sql-mode' for details.
1347
1348 Sample session using two connections:
1349
1350 1. Create first SQLi buffer by calling an entry function.
1351 2. Rename buffer \"*SQL*\" to \"*Connection 1*\".
1352 3. Create a SQL buffer \"test1.sql\".
1353 4. Create second SQLi buffer by calling an entry function.
1354 5. Rename buffer \"*SQL*\" to \"*Connection 2*\".
1355 6. Create a SQL buffer \"test2.sql\".
1356
1357 Now \\[sql-send-region] in buffer \"test1.sql\" will send the region to
1358 buffer \"*Connection 1*\", \\[sql-send-region] in buffer \"test2.sql\"
1359 will send the region to buffer \"*Connection 2*\".
1360
1361 If you accidentally suspend your process, use \\[comint-continue-subjob]
1362 to continue it. On some operating systems, this will not work because
1363 the signals are not supported.
1364
1365 \\{sql-interactive-mode-map}
1366 Customization: Entry to this mode runs the hooks on `comint-mode-hook'
1367 and `sql-interactive-mode-hook' (in that order). Before each input, the
1368 hooks on `comint-input-filter-functions' are run. After each SQL
1369 interpreter output, the hooks on `comint-output-filter-functions' are
1370 run.
1371
1372 Variable `sql-input-ring-file-name' controls the initialisation of the
1373 input ring history.
1374
1375 Variables `comint-output-filter-functions', a hook, and
1376 `comint-scroll-to-bottom-on-input' and
1377 `comint-scroll-to-bottom-on-output' control whether input and output
1378 cause the window to scroll to the end of the buffer.
1379
1380 If you want to make SQL buffers limited in length, add the function
1381 `comint-truncate-buffer' to `comint-output-filter-functions'.
1382
1383 Here is an example for your .emacs file. It keeps the SQLi buffer a
1384 certain length.
1385
1386 \(add-hook 'sql-interactive-mode-hook
1387 \(function (lambda ()
1388 \(setq comint-output-filter-functions 'comint-truncate-buffer))))
1389
1390 Here is another example. It will always put point back to the statement
1391 you entered, right above the output it created.
1392
1393 \(setq comint-output-filter-functions
1394 \(function (lambda (STR) (comint-show-output))))"
1395 (comint-mode)
1396 (setq comint-prompt-regexp sql-prompt-regexp)
1397 (setq left-margin sql-prompt-length)
1398 (setq major-mode 'sql-interactive-mode)
1399 (setq mode-name "SQLi")
1400 (use-local-map sql-interactive-mode-map)
1401 (if sql-interactive-mode-menu
1402 (easy-menu-add sql-interactive-mode-menu)); XEmacs
1403 (set-syntax-table sql-mode-syntax-table)
1404 (make-local-variable 'font-lock-defaults)
1405 ;; Note that making KEYWORDS-ONLY nil will cause havoc if you try
1406 ;; SELECT 'x' FROM DUAL with SQL*Plus, because the title of the column
1407 ;; will have just one quote. Therefore syntactic hilighting is
1408 ;; disabled for interactive buffers. `_' and `.' are considered part
1409 ;; of words.
1410 (setq font-lock-defaults '(sql-mode-font-lock-keywords
1411 t t ((?_ . "w") (?. . "w"))))
1412 ;; Enable commenting and uncommenting of the region.
1413 (make-local-variable 'comment-start)
1414 (setq comment-start "--")
1415 ;; Abbreviation table init and case-insensitive. It is not activatet
1416 ;; by default.
1417 (setq local-abbrev-table sql-mode-abbrev-table)
1418 (setq abbrev-all-caps 1)
1419 ;; Exiting the process will call sql-stop.
1420 (set-process-sentinel (get-buffer-process sql-buffer) 'sql-stop)
1421 ;; People wanting a different history file for each
1422 ;; buffer/process/client/whatever can change separator and file-name
1423 ;; on the sql-interactive-mode-hook.
1424 (setq comint-input-ring-separator sql-input-ring-separator
1425 comint-input-ring-file-name sql-input-ring-file-name)
1426 ;; Create a usefull name for renaming this buffer later.
1427 (make-local-variable 'sql-alternate-buffer-name)
1428 (setq sql-alternate-buffer-name (sql-make-alternate-buffer-name))
1429 ;; User stuff.
1430 (run-hooks 'sql-interactive-mode-hook)
1431 ;; Calling the hook before calling comint-read-input-ring allows users
1432 ;; to set comint-input-ring-file-name in sql-interactive-mode-hook.
1433 (comint-read-input-ring t))
1434
1435 (defun sql-stop (process event)
1436 "Called when the SQL process is stopped.
1437
1438 Writes the input history to a history file using
1439 `comint-write-input-ring' and inserts a short message in the SQL buffer.
1440
1441 This function is a sentinel watching the SQL interpreter process.
1442 Sentinels will always get the two parameters PROCESS and EVENT."
1443 (comint-write-input-ring)
1444 (if (and (eq (current-buffer) sql-buffer)
1445 (not buffer-read-only))
1446 (insert (format "\nProcess %s %s\n" process event))
1447 (message "Process %s %s" process event)))
1448
1449 \f
1450
1451 ;;; Entry functions for different SQL interpreters.
1452
1453 ;;;###autoload
1454 (defun sql-oracle ()
1455 "Run sqlplus by Oracle as an inferior process.
1456
1457 If buffer `*SQL*' exists but no process is running, make a new process.
1458 If buffer exists and a process is running, just switch to buffer
1459 `*SQL*'.
1460
1461 Interpreter used comes from variable `sql-oracle-program'. Login uses
1462 the variables `sql-user', `sql-password', and `sql-database' as
1463 defaults, if set. Additional command line parameters can be stored in
1464 the list `sql-oracle-options'.
1465
1466 The buffer is put in sql-interactive-mode, giving commands for sending
1467 input. See `sql-interactive-mode'.
1468
1469 To specify a coding system for converting non-ASCII characters
1470 in the input and output to the process, use \\[universal-coding-system-argument]
1471 before \\[sql-oracle]. You can also specify this with \\[set-buffer-process-coding-system]
1472 in the SQL buffer, after you start the process.
1473 The default comes from `process-coding-system-alist' and
1474 `default-process-coding-system'.
1475
1476 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1477 (interactive)
1478 (if (comint-check-proc "*SQL*")
1479 (pop-to-buffer "*SQL*")
1480 (sql-get-login 'user 'password 'database)
1481 (message "Login...")
1482 ;; Produce user/password@database construct. Password without user
1483 ;; is meaningless; database without user/password is meaningless,
1484 ;; because "@param" will ask sqlplus to interpret the script
1485 ;; "param".
1486 (let ((parameter nil))
1487 (if (not (string= "" sql-user))
1488 (if (not (string= "" sql-password))
1489 (setq parameter (concat sql-user "/" sql-password))
1490 (setq parameter sql-user)))
1491 (if (and parameter (not (string= "" sql-database)))
1492 (setq parameter (concat parameter "@" sql-database)))
1493 (if parameter
1494 (setq parameter (nconc (list parameter) sql-oracle-options))
1495 (setq parameter sql-oracle-options))
1496 (if parameter
1497 (set-buffer (apply 'make-comint "SQL" sql-oracle-program nil
1498 parameter))
1499 (set-buffer (make-comint "SQL" sql-oracle-program nil))))
1500 (setq sql-prompt-regexp "^SQL> ")
1501 (setq sql-prompt-length 5)
1502 (setq sql-buffer (current-buffer))
1503 ;; set sql-mode-font-lock-keywords to something different before
1504 ;; calling sql-interactive-mode.
1505 (setq sql-mode-font-lock-keywords sql-mode-oracle-font-lock-keywords)
1506 (sql-interactive-mode)
1507 ;; If running on NT, make sure we do placeholder replacement
1508 ;; ourselves. This must come after sql-interactive-mode because all
1509 ;; local variables will be killed, there.
1510 (if (eq window-system 'w32)
1511 (setq comint-input-sender 'sql-query-placeholders-and-send))
1512 (message "Login...done")
1513 (pop-to-buffer sql-buffer)))
1514
1515 \f
1516
1517 ;;;###autoload
1518 (defun sql-sybase ()
1519 "Run isql by SyBase as an inferior process.
1520
1521 If buffer `*SQL*' exists but no process is running, make a new process.
1522 If buffer exists and a process is running, just switch to buffer
1523 `*SQL*'.
1524
1525 Interpreter used comes from variable `sql-sybase-program'. Login uses
1526 the variables `sql-server', `sql-user', `sql-password', and
1527 `sql-database' as defaults, if set. Additional command line parameters
1528 can be stored in the list `sql-sybase-options'.
1529
1530 The buffer is put in sql-interactive-mode, giving commands for sending
1531 input. See `sql-interactive-mode'.
1532
1533 To specify a coding system for converting non-ASCII characters
1534 in the input and output to the process, use \\[universal-coding-system-argument]
1535 before \\[sql-sybase]. You can also specify this with \\[set-buffer-process-coding-system]
1536 in the SQL buffer, after you start the process.
1537 The default comes from `process-coding-system-alist' and
1538 `default-process-coding-system'.
1539
1540 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1541 (interactive)
1542 (if (comint-check-proc "*SQL*")
1543 (pop-to-buffer "*SQL*")
1544 (sql-get-login 'server 'user 'password 'database)
1545 (message "Login...")
1546 ;; Put all parameters to the program (if defined) in a list and call
1547 ;; make-comint.
1548 (let ((params sql-sybase-options))
1549 (if (not (string= "" sql-server))
1550 (setq params (append (list "-S" sql-server) params)))
1551 (if (not (string= "" sql-database))
1552 (setq params (append (list "-D" sql-database) params)))
1553 (if (not (string= "" sql-password))
1554 (setq params (append (list "-P" sql-password) params)))
1555 (if (not (string= "" sql-user))
1556 (setq params (append (list "-U" sql-user) params)))
1557 (set-buffer (apply 'make-comint "SQL" sql-sybase-program
1558 nil params)))
1559 (setq sql-prompt-regexp "^SQL> ")
1560 (setq sql-prompt-length 5)
1561 (setq sql-buffer (current-buffer))
1562 (sql-interactive-mode)
1563 (message "Login...done")
1564 (pop-to-buffer sql-buffer)))
1565
1566 \f
1567
1568 ;;;###autoload
1569 (defun sql-informix ()
1570 "Run dbaccess by Informix as an inferior process.
1571
1572 If buffer `*SQL*' exists but no process is running, make a new process.
1573 If buffer exists and a process is running, just switch to buffer
1574 `*SQL*'.
1575
1576 Interpreter used comes from variable `sql-informix-program'. Login uses
1577 the variable `sql-database' as default, if set.
1578
1579 The buffer is put in sql-interactive-mode, giving commands for sending
1580 input. See `sql-interactive-mode'.
1581
1582 To specify a coding system for converting non-ASCII characters
1583 in the input and output to the process, use \\[universal-coding-system-argument]
1584 before \\[sql-informix]. You can also specify this with \\[set-buffer-process-coding-system]
1585 in the SQL buffer, after you start the process.
1586 The default comes from `process-coding-system-alist' and
1587 `default-process-coding-system'.
1588
1589 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1590 (interactive)
1591 (if (comint-check-proc "*SQL*")
1592 (pop-to-buffer "*SQL*")
1593 (sql-get-login 'database)
1594 (message "Login...")
1595 ;; username and password are ignored.
1596 (if (string= "" sql-database)
1597 (set-buffer (make-comint "SQL" sql-informix-program nil))
1598 (set-buffer (make-comint "SQL" sql-informix-program nil sql-database "-")))
1599 (setq sql-prompt-regexp "^SQL> ")
1600 (setq sql-prompt-length 5)
1601 (setq sql-buffer (current-buffer))
1602 (sql-interactive-mode)
1603 (message "Login...done")
1604 (pop-to-buffer sql-buffer)))
1605
1606 \f
1607
1608 ;;;###autoload
1609 (defun sql-sqlite ()
1610 "Run sqlite as an inferior process.
1611
1612 SQLite is free software.
1613
1614 If buffer `*SQL*' exists but no process is running, make a new process.
1615 If buffer exists and a process is running, just switch to buffer
1616 `*SQL*'.
1617
1618 Interpreter used comes from variable `sql-sqlite-program'. Login uses
1619 the variables `sql-user', `sql-password', `sql-database', and
1620 `sql-server' as defaults, if set. Additional command line parameters
1621 can be stored in the list `sql-sqlite-options'.
1622
1623 The buffer is put in sql-interactive-mode, giving commands for sending
1624 input. See `sql-interactive-mode'.
1625
1626 To specify a coding system for converting non-ASCII characters
1627 in the input and output to the process, use \\[universal-coding-system-argument]
1628 before \\[sql-sqlite]. You can also specify this with \\[set-buffer-process-coding-system]
1629 in the SQL buffer, after you start the process.
1630 The default comes from `process-coding-system-alist' and
1631 `default-process-coding-system'.
1632
1633 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1634 (interactive)
1635 (if (comint-check-proc "*SQL*")
1636 (pop-to-buffer "*SQL*")
1637 (sql-get-login 'database)
1638 (message "Login...")
1639 ;; Put all parameters to the program (if defined) in a list and call
1640 ;; make-comint.
1641 (let ((params))
1642 (if (not (string= "" sql-database))
1643 (setq params (append (list sql-database) params)))
1644 (if (not (string= "" sql-server))
1645 (setq params (append (list (concat "--host=" sql-server)) params)))
1646 (if (not (string= "" sql-password))
1647 (setq params (append (list (concat "--password=" sql-password)) params)))
1648 (if (not (string= "" sql-user))
1649 (setq params (append (list (concat "--user=" sql-user)) params)))
1650 (if (not (null sql-sqlite-options))
1651 (setq params (append sql-sqlite-options params)))
1652 (set-buffer (apply 'make-comint "SQL" sql-sqlite-program
1653 nil params)))
1654 (setq sql-prompt-regexp "^sqlite> ")
1655 (setq sql-prompt-length 8)
1656 (setq sql-buffer (current-buffer))
1657 (sql-interactive-mode)
1658 (message "Login...done")
1659 (pop-to-buffer sql-buffer)))
1660
1661 \f
1662
1663 ;;;###autoload
1664 (defun sql-mysql ()
1665 "Run mysql by TcX as an inferior process.
1666
1667 Mysql versions 3.23 and up are free software.
1668
1669 If buffer `*SQL*' exists but no process is running, make a new process.
1670 If buffer exists and a process is running, just switch to buffer
1671 `*SQL*'.
1672
1673 Interpreter used comes from variable `sql-mysql-program'. Login uses
1674 the variables `sql-user', `sql-password', `sql-database', and
1675 `sql-server' as defaults, if set. Additional command line parameters
1676 can be stored in the list `sql-mysql-options'.
1677
1678 The buffer is put in sql-interactive-mode, giving commands for sending
1679 input. See `sql-interactive-mode'.
1680
1681 To specify a coding system for converting non-ASCII characters
1682 in the input and output to the process, use \\[universal-coding-system-argument]
1683 before \\[sql-mysql]. You can also specify this with \\[set-buffer-process-coding-system]
1684 in the SQL buffer, after you start the process.
1685 The default comes from `process-coding-system-alist' and
1686 `default-process-coding-system'.
1687
1688 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1689 (interactive)
1690 (if (comint-check-proc "*SQL*")
1691 (pop-to-buffer "*SQL*")
1692 (sql-get-login 'user 'password 'database 'server)
1693 (message "Login...")
1694 ;; Put all parameters to the program (if defined) in a list and call
1695 ;; make-comint.
1696 (let ((params))
1697 (if (not (string= "" sql-database))
1698 (setq params (append (list sql-database) params)))
1699 (if (not (string= "" sql-server))
1700 (setq params (append (list (concat "--host=" sql-server)) params)))
1701 (if (not (string= "" sql-password))
1702 (setq params (append (list (concat "--password=" sql-password)) params)))
1703 (if (not (string= "" sql-user))
1704 (setq params (append (list (concat "--user=" sql-user)) params)))
1705 (if (not (null sql-mysql-options))
1706 (setq params (append sql-mysql-options params)))
1707 (set-buffer (apply 'make-comint "SQL" sql-mysql-program
1708 nil params)))
1709 (setq sql-prompt-regexp "^mysql>")
1710 (setq sql-prompt-length 6)
1711 (setq sql-buffer (current-buffer))
1712 (sql-interactive-mode)
1713 (message "Login...done")
1714 (pop-to-buffer sql-buffer)))
1715
1716 \f
1717
1718 ;;;###autoload
1719 (defun sql-solid ()
1720 "Run solsql by Solid as an inferior process.
1721
1722 If buffer `*SQL*' exists but no process is running, make a new process.
1723 If buffer exists and a process is running, just switch to buffer
1724 `*SQL*'.
1725
1726 Interpreter used comes from variable `sql-solid-program'. Login uses
1727 the variables `sql-user', `sql-password', and `sql-server' as
1728 defaults, if set.
1729
1730 The buffer is put in sql-interactive-mode, giving commands for sending
1731 input. See `sql-interactive-mode'.
1732
1733 To specify a coding system for converting non-ASCII characters
1734 in the input and output to the process, use \\[universal-coding-system-argument]
1735 before \\[sql-solid]. You can also specify this with \\[set-buffer-process-coding-system]
1736 in the SQL buffer, after you start the process.
1737 The default comes from `process-coding-system-alist' and
1738 `default-process-coding-system'.
1739
1740 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1741 (interactive)
1742 (if (comint-check-proc "*SQL*")
1743 (pop-to-buffer "*SQL*")
1744 (sql-get-login 'user 'password 'server)
1745 (message "Login...")
1746 ;; Put all parameters to the program (if defined) in a list and call
1747 ;; make-comint.
1748 (let ((params))
1749 ;; It only makes sense if both username and password are there.
1750 (if (not (or (string= "" sql-user)
1751 (string= "" sql-password)))
1752 (setq params (append (list sql-user sql-password) params)))
1753 (if (not (string= "" sql-server))
1754 (setq params (append (list sql-server) params)))
1755 (set-buffer (apply 'make-comint "SQL" sql-solid-program
1756 nil params)))
1757 (setq sql-prompt-regexp "^")
1758 (setq sql-prompt-length 0)
1759 (setq sql-buffer (current-buffer))
1760 (sql-interactive-mode)
1761 (message "Login...done")
1762 (pop-to-buffer sql-buffer)))
1763
1764 \f
1765
1766 ;;;###autoload
1767 (defun sql-ingres ()
1768 "Run sql by Ingres as an inferior process.
1769
1770 If buffer `*SQL*' exists but no process is running, make a new process.
1771 If buffer exists and a process is running, just switch to buffer
1772 `*SQL*'.
1773
1774 Interpreter used comes from variable `sql-ingres-program'. Login uses
1775 the variable `sql-database' as default, if set.
1776
1777 The buffer is put in sql-interactive-mode, giving commands for sending
1778 input. See `sql-interactive-mode'.
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-ingres]. 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 (sql-get-login 'database)
1792 (message "Login...")
1793 ;; username and password are ignored.
1794 (if (string= "" sql-database)
1795 (set-buffer (make-comint "SQL" sql-ingres-program nil))
1796 (set-buffer (make-comint "SQL" sql-ingres-program nil sql-database)))
1797 (setq sql-prompt-regexp "^\* ")
1798 (setq sql-prompt-length 2)
1799 (setq sql-buffer (current-buffer))
1800 (sql-interactive-mode)
1801 (message "Login...done")
1802 (pop-to-buffer sql-buffer)))
1803
1804 \f
1805
1806 ;;;###autoload
1807 (defun sql-ms ()
1808 "Run isql by Microsoft as an inferior process.
1809
1810 If buffer `*SQL*' exists but no process is running, make a new process.
1811 If buffer exists and a process is running, just switch to buffer
1812 `*SQL*'.
1813
1814 Interpreter used comes from variable `sql-ms-program'. Login uses the
1815 variables `sql-user', `sql-password', `sql-database', and `sql-server'
1816 as defaults, if set. Additional command line parameters can be stored
1817 in the list `sql-ms-options'.
1818
1819 The buffer is put in sql-interactive-mode, giving commands for sending
1820 input. See `sql-interactive-mode'.
1821
1822 To specify a coding system for converting non-ASCII characters
1823 in the input and output to the process, use \\[universal-coding-system-argument]
1824 before \\[sql-ms]. You can also specify this with \\[set-buffer-process-coding-system]
1825 in the SQL buffer, after you start the process.
1826 The default comes from `process-coding-system-alist' and
1827 `default-process-coding-system'.
1828
1829 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1830 (interactive)
1831 (if (comint-check-proc "*SQL*")
1832 (pop-to-buffer "*SQL*")
1833 (sql-get-login 'user 'password 'database 'server)
1834 (message "Login...")
1835 ;; Put all parameters to the program (if defined) in a list and call
1836 ;; make-comint.
1837 (let ((params sql-ms-options))
1838 (if (not (string= "" sql-server))
1839 (setq params (append (list "-S" sql-server) params)))
1840 (if (not (string= "" sql-database))
1841 (setq params (append (list "-d" sql-database) params)))
1842 (if (not (string= "" sql-user))
1843 (setq params (append (list "-U" sql-user) params)))
1844 (if (not (string= "" sql-password))
1845 (setq params (append (list "-P" sql-password) params))
1846 ;; If -P is passed to ISQL as the last argument without a password,
1847 ;; it's considered null.
1848 (setq params (append params (list "-P"))))
1849 (set-buffer (apply 'make-comint "SQL" sql-ms-program
1850 nil params)))
1851 (setq sql-prompt-regexp "^[0-9]*>")
1852 (setq sql-prompt-length 5)
1853 (setq sql-buffer (current-buffer))
1854 (sql-interactive-mode)
1855 (message "Login...done")
1856 (pop-to-buffer sql-buffer)))
1857
1858 \f
1859
1860 ;;;###autoload
1861 (defun sql-postgres ()
1862 "Run psql by Postgres as an inferior process.
1863
1864 If buffer `*SQL*' exists but no process is running, make a new process.
1865 If buffer exists and a process is running, just switch to buffer
1866 `*SQL*'.
1867
1868 Interpreter used comes from variable `sql-postgres-program'. Login uses
1869 the variables `sql-database' and `sql-server' as default, if set.
1870 Additional command line parameters can be stored in the list
1871 `sql-postgres-options'.
1872
1873 The buffer is put in sql-interactive-mode, giving commands for sending
1874 input. See `sql-interactive-mode'.
1875
1876 To specify a coding system for converting non-ASCII characters
1877 in the input and output to the process, use \\[universal-coding-system-argument]
1878 before \\[sql-postgres]. You can also specify this with \\[set-buffer-process-coding-system]
1879 in the SQL buffer, after you start the process.
1880 The default comes from `process-coding-system-alist' and
1881 `default-process-coding-system'. If your output lines end with ^M,
1882 your might try undecided-dos as a coding system. If this doesn't help,
1883 Try to set `comint-output-filter-functions' like this:
1884
1885 \(setq comint-output-filter-functions (append comint-output-filter-functions
1886 '(comint-strip-ctrl-m)))
1887
1888 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1889 (interactive)
1890 (if (comint-check-proc "*SQL*")
1891 (pop-to-buffer "*SQL*")
1892 (sql-get-login 'database 'server)
1893 (message "Login...")
1894 ;; username and password are ignored. Mark Stosberg suggest to add
1895 ;; the database at the end. Jason Beegan suggest using --pset and
1896 ;; pager=off instead of \\o|cat. The later was the solution by
1897 ;; Gregor Zych. Jason's suggestion is the default value for
1898 ;; sql-postgres-options.
1899 (let ((params sql-postgres-options))
1900 (if (not (string= "" sql-database))
1901 (setq params (append params (list sql-database))))
1902 (if (not (string= "" sql-server))
1903 (setq params (append (list "-h" sql-server) params)))
1904 (set-buffer (apply 'make-comint "SQL" sql-postgres-program
1905 nil params)))
1906 (setq sql-prompt-regexp "^.*> *")
1907 (setq sql-prompt-length 5)
1908 ;; This is a lousy hack to prevent psql from truncating it's output
1909 ;; and giving stupid warnings. If s.o. knows a way to prevent psql
1910 ;; from acting this way, then I would be very thankful to
1911 ;; incorporate this (Gregor Zych <zych@pool.informatik.rwth-aachen.de>)
1912 ;; (comint-send-string "*SQL*" "\\o \| cat\n")
1913 (setq sql-mode-font-lock-keywords sql-mode-postgres-font-lock-keywords)
1914 (setq sql-buffer (current-buffer))
1915 (sql-interactive-mode)
1916 (message "Login...done")
1917 (pop-to-buffer sql-buffer)))
1918
1919 \f
1920
1921 ;;;###autoload
1922 (defun sql-interbase ()
1923 "Run isql by Interbase as an inferior process.
1924
1925 If buffer `*SQL*' exists but no process is running, make a new process.
1926 If buffer exists and a process is running, just switch to buffer
1927 `*SQL*'.
1928
1929 Interpreter used comes from variable `sql-interbase-program'. Login
1930 uses the variables `sql-user', `sql-password', and `sql-database' as
1931 defaults, if set.
1932
1933 The buffer is put in sql-interactive-mode, giving commands for sending
1934 input. See `sql-interactive-mode'.
1935
1936 To specify a coding system for converting non-ASCII characters
1937 in the input and output to the process, use \\[universal-coding-system-argument]
1938 before \\[sql-interbase]. You can also specify this with \\[set-buffer-process-coding-system]
1939 in the SQL buffer, after you start the process.
1940 The default comes from `process-coding-system-alist' and
1941 `default-process-coding-system'.
1942
1943 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1944 (interactive)
1945 (if (comint-check-proc "*SQL*")
1946 (pop-to-buffer "*SQL*")
1947 (sql-get-login 'user 'password 'database)
1948 (message "Login...")
1949 ;; Put all parameters to the program (if defined) in a list and call
1950 ;; make-comint.
1951 (let ((params sql-interbase-options))
1952 (if (not (string= "" sql-user))
1953 (setq params (append (list "-u" sql-user) params)))
1954 (if (not (string= "" sql-password))
1955 (setq params (append (list "-p" sql-password) params)))
1956 (if (not (string= "" sql-database))
1957 (setq params (cons sql-database params))); add to the front!
1958 (set-buffer (apply 'make-comint "SQL" sql-interbase-program
1959 nil params)))
1960 (setq sql-prompt-regexp "^SQL> ")
1961 (setq sql-prompt-length 5)
1962 (setq sql-buffer (current-buffer))
1963 (sql-interactive-mode)
1964 (message "Login...done")
1965 (pop-to-buffer sql-buffer)))
1966
1967 \f
1968
1969 ;;;###autoload
1970 (defun sql-db2 ()
1971 "Run db2 by IBM as an inferior process.
1972
1973 If buffer `*SQL*' exists but no process is running, make a new process.
1974 If buffer exists and a process is running, just switch to buffer
1975 `*SQL*'.
1976
1977 Interpreter used comes from variable `sql-db2-program'. There is not
1978 automatic login.
1979
1980 The buffer is put in sql-interactive-mode, giving commands for sending
1981 input. See `sql-interactive-mode'.
1982
1983 If you use \\[sql-accumulate-and-indent] to send multiline commands to
1984 db2, newlines will be escaped if necessary. If you don't want that, set
1985 `comint-input-sender' back to `comint-simple-send' by writing an after
1986 advice. See the elisp manual for more information.
1987
1988 To specify a coding system for converting non-ASCII characters
1989 in the input and output to the process, use \\[universal-coding-system-argument]
1990 before \\[sql-db2]. You can also specify this with \\[set-buffer-process-coding-system]
1991 in the SQL buffer, after you start the process.
1992 The default comes from `process-coding-system-alist' and
1993 `default-process-coding-system'.
1994
1995 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
1996 (interactive)
1997 (if (comint-check-proc "*SQL*")
1998 (pop-to-buffer "*SQL*")
1999 (message "Login...")
2000 ;; Put all parameters to the program (if defined) in a list and call
2001 ;; make-comint.
2002 (set-buffer (apply 'make-comint "SQL" sql-db2-program
2003 nil sql-db2-options))
2004 (setq sql-prompt-regexp "^db2 => ")
2005 (setq sql-prompt-length 7)
2006 (setq sql-buffer (current-buffer))
2007 (sql-interactive-mode)
2008 ;; Escape newlines. This must come after sql-interactive-mode
2009 ;; because all local variables will be killed, there.
2010 (setq comint-input-sender 'sql-escape-newlines-and-send)
2011 (message "Login...done")
2012 (pop-to-buffer sql-buffer)))
2013
2014 ;;;###autoload
2015 (defun sql-linter ()
2016 "Run inl by RELEX as an inferior process.
2017
2018 If buffer `*SQL*' exists but no process is running, make a new process.
2019 If buffer exists and a process is running, just switch to buffer
2020 `*SQL*'.
2021
2022 Interpreter used comes from variable `sql-linter-program' - usually `inl'.
2023 Login uses the variables `sql-user', `sql-password', `sql-database' and
2024 `sql-server' as defaults, if set. Additional command line parameters
2025 can be stored in the list `sql-linter-options'. Run inl -h to get help on
2026 parameters.
2027
2028 `sql-database' is used to set the LINTER_MBX environment variable for
2029 local connections, `sql-server' refers to the server name from the
2030 `nodetab' file for the network connection (dbc_tcp or friends must run
2031 for this to work). If `sql-password' is an empty string, inl will use
2032 an empty password.
2033
2034 The buffer is put in sql-interactive-mode, giving commands for sending
2035 input. See `sql-interactive-mode'.
2036
2037 To use LINTER font locking by default, put this line into your .emacs :
2038 (setq sql-mode-font-lock-keywords sql-mode-linter-font-lock-keywords)
2039
2040 \(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
2041 (interactive)
2042 (if (comint-check-proc "*SQL*")
2043 (pop-to-buffer "*SQL*")
2044 (sql-get-login 'user 'password 'database 'server)
2045 (message "Login...")
2046 ;; Put all parameters to the program (if defined) in a list and call
2047 ;; make-comint.
2048 (let ((params sql-linter-options) (login nil) (old-mbx (getenv "LINTER_MBX")))
2049 (if (not (string= "" sql-user))
2050 (setq login (concat sql-user "/" sql-password)))
2051 (setq params (append (list "-u" login) params))
2052 (if (not (string= "" sql-server))
2053 (setq params (append (list "-n" sql-server) params)))
2054 (if (string= "" sql-database)
2055 (setenv "LINTER_MBX" nil)
2056 (setenv "LINTER_MBX" sql-database))
2057 (set-buffer (apply 'make-comint "SQL" sql-linter-program nil
2058 params))
2059 (setenv "LINTER_MBX" old-mbx)
2060 )
2061 (setq sql-prompt-regexp "^SQL>")
2062 (setq sql-prompt-length 4)
2063 (setq sql-buffer (current-buffer))
2064 (sql-interactive-mode)
2065 (message "Login...done")
2066 (pop-to-buffer sql-buffer)))
2067
2068 \f
2069
2070 (provide 'sql)
2071
2072 ;;; sql.el ends here