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