1 |
;;; sql.el --- specialized comint.el for SQL interpreters |
;;; sql.el --- specialized comint.el for SQL interpreters |
2 |
|
|
3 |
;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
;; Author: Alex Schroeder <alex@gnu.org> |
;; Author: Alex Schroeder <alex@gnu.org> |
6 |
;; Maintainer: Alex Schroeder <alex@gnu.org> |
;; Maintainer: Alex Schroeder <alex@gnu.org> |
7 |
;; Version: 1.6.5 |
;; Version: 1.7.0 |
8 |
;; Keywords: comm languages processes |
;; Keywords: comm languages processes |
9 |
;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlMode |
;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlMode |
10 |
|
|
343 |
:type 'file |
:type 'file |
344 |
:group 'SQL) |
:group 'SQL) |
345 |
|
|
346 |
|
(defcustom sql-ms-options '("-w" "300" "-n") |
347 |
|
;; -w is the linesize |
348 |
|
"*List of additional options for `sql-ms-program'." |
349 |
|
:type '(repeat string) |
350 |
|
:version "21.4" |
351 |
|
:group 'SQL) |
352 |
|
|
353 |
;; Customization for Postgres |
;; Customization for Postgres |
354 |
|
|
355 |
(defcustom sql-postgres-program "psql" |
(defcustom sql-postgres-program "psql" |
407 |
:version "20.8" |
:version "20.8" |
408 |
:group 'SQL) |
:group 'SQL) |
409 |
|
|
410 |
|
;; Customization for Linter |
411 |
|
|
412 |
|
(defcustom sql-linter-program "inl" |
413 |
|
"*Command to start inl by RELEX. |
414 |
|
|
415 |
|
Starts `sql-interactive-mode' after doing some setup." |
416 |
|
:type 'file |
417 |
|
:group 'SQL) |
418 |
|
|
419 |
|
(defcustom sql-linter-options nil |
420 |
|
"*List of additional options for `sql-linter-program'." |
421 |
|
:type '(repeat string) |
422 |
|
:version "21.3" |
423 |
|
:group 'SQL) |
424 |
|
|
425 |
|
|
426 |
|
|
427 |
;;; Variables which do not need customization |
;;; Variables which do not need customization |
463 |
|
|
464 |
;; Keymap for sql-interactive-mode. |
;; Keymap for sql-interactive-mode. |
465 |
|
|
466 |
(defvar sql-interactive-mode-map |
(defvar sql-interactive-mode-map |
467 |
(let ((map (make-sparse-keymap))) |
(let ((map (make-sparse-keymap))) |
468 |
(if (functionp 'set-keymap-parent) |
(if (functionp 'set-keymap-parent) |
469 |
(set-keymap-parent map comint-mode-map); Emacs |
(set-keymap-parent map comint-mode-map); Emacs |
513 |
("Highlighting" |
("Highlighting" |
514 |
["ANSI SQL keywords" sql-highlight-ansi-keywords t] |
["ANSI SQL keywords" sql-highlight-ansi-keywords t] |
515 |
["Oracle keywords" sql-highlight-oracle-keywords t] |
["Oracle keywords" sql-highlight-oracle-keywords t] |
516 |
["Postgres keywords" sql-highlight-postgres-keywords t]))) |
["Postgres keywords" sql-highlight-postgres-keywords t] |
517 |
|
["Linter keywords" sql-highlight-linter-keywords t] |
518 |
|
))) |
519 |
|
|
520 |
;; easy menu for sql-interactive-mode. |
;; easy menu for sql-interactive-mode. |
521 |
|
|
737 |
(cons postgres-types 'font-lock-type-face)))))) |
(cons postgres-types 'font-lock-type-face)))))) |
738 |
|
|
739 |
|
|
740 |
|
(defvar sql-mode-linter-font-lock-keywords nil |
741 |
|
"Linter SQL keywords used by font-lock. |
742 |
|
|
743 |
|
This variable is used by `sql-mode' and `sql-interactive-mode'. The |
744 |
|
regular expressions are created during compilation by calling the |
745 |
|
function `regexp-opt'.") |
746 |
|
|
747 |
|
(if sql-mode-linter-font-lock-keywords |
748 |
|
() |
749 |
|
(let ((linter-keywords (eval-when-compile |
750 |
|
(concat "\\b" |
751 |
|
(regexp-opt '( |
752 |
|
"autocommit" "autoinc" "autorowid" "cancel" "cascade" "channel" |
753 |
|
"committed" "count" "countblob" "cross" "current" "data" "database" |
754 |
|
"datafile" "datafiles" "datesplit" "dba" "dbname" "default" "deferred" |
755 |
|
"denied" "description" "device" "difference" "directory" "error" |
756 |
|
"escape" "euc" "exclusive" "external" "extfile" "false" "file" |
757 |
|
"filename" "filesize" "filetime" "filter" "findblob" "first" "foreign" |
758 |
|
"full" "fuzzy" "global" "granted" "ignore" "immediate" "increment" |
759 |
|
"indexes" "indexfile" "indexfiles" "indextime" "initial" "integrity" |
760 |
|
"internal" "key" "last_autoinc" "last_rowid" "limit" "linter" |
761 |
|
"linter_file_device" "linter_file_size" "linter_name_length" "ln" |
762 |
|
"local" "login" "maxisn" "maxrow" "maxrowid" "maxvalue" "message" |
763 |
|
"minvalue" "module" "names" "national" "natural" "new" "new_table" |
764 |
|
"no" "node" "noneuc" "nulliferror" "numbers" "off" "old" "old_table" |
765 |
|
"only" "operation" "optimistic" "option" "page" "partially" "password" |
766 |
|
"phrase" "plan" "precision" "primary" "priority" "privileges" |
767 |
|
"proc_info_size" "proc_par_name_len" "protocol" "quant" "range" "raw" |
768 |
|
"read" "record" "records" "references" "remote" "rename" "replication" |
769 |
|
"restart" "rewrite" "root" "row" "rule" "savepoint" "security" |
770 |
|
"sensitive" "sequence" "serializable" "server" "since" "size" "some" |
771 |
|
"startup" "statement" "station" "success" "sys_guid" "tables" "test" |
772 |
|
"timeout" "trace" "transaction" "translation" "trigger" |
773 |
|
"trigger_info_size" "true" "trunc" "uncommitted" "unicode" "unknown" |
774 |
|
"unlimited" "unlisted" "user" "utf8" "value" "varying" "volumes" |
775 |
|
"wait" "windows_code" "workspace" "write" "xml" |
776 |
|
) t) "\\b"))) |
777 |
|
(linter-reserved-words (eval-when-compile |
778 |
|
(concat "\\b" |
779 |
|
(regexp-opt '( |
780 |
|
"access" "action" "add" "address" "after" "all" "alter" "always" "and" |
781 |
|
"any" "append" "as" "asc" "ascic" "async" "at_begin" "at_end" "audit" |
782 |
|
"aud_obj_name_len" "backup" "base" "before" "between" "blobfile" |
783 |
|
"blobfiles" "blobpct" "brief" "browse" "by" "case" "cast" "check" |
784 |
|
"clear" "close" "column" "comment" "commit" "connect" "contains" |
785 |
|
"correct" "create" "delete" "desc" "disable" "disconnect" "distinct" |
786 |
|
"drop" "each" "ef" "else" "enable" "end" "event" "except" "exclude" |
787 |
|
"execute" "exists" "extract" "fetch" "finish" "for" "from" "get" |
788 |
|
"grant" "group" "having" "identified" "in" "index" "inner" "insert" |
789 |
|
"instead" "intersect" "into" "is" "isolation" "join" "left" "level" |
790 |
|
"like" "lock" "mode" "modify" "not" "nowait" "null" "of" "on" "open" |
791 |
|
"or" "order" "outer" "owner" "press" "prior" "procedure" "public" |
792 |
|
"purge" "rebuild" "resource" "restrict" "revoke" "right" "role" |
793 |
|
"rollback" "rownum" "select" "session" "set" "share" "shutdown" |
794 |
|
"start" "stop" "sync" "synchronize" "synonym" "sysdate" "table" "then" |
795 |
|
"to" "union" "unique" "unlock" "until" "update" "using" "values" |
796 |
|
"view" "when" "where" "with" "without" |
797 |
|
) t) "\\b"))) |
798 |
|
(linter-types (eval-when-compile |
799 |
|
(concat "\\b" |
800 |
|
(regexp-opt '( |
801 |
|
"bigint" "bitmap" "blob" "boolean" "char" "character" "date" |
802 |
|
"datetime" "dec" "decimal" "double" "float" "int" "integer" "nchar" |
803 |
|
"number" "numeric" "real" "smallint" "varbyte" "varchar" "byte" |
804 |
|
"cursor" "long" |
805 |
|
) t) "\\b"))) |
806 |
|
(linter-builtin-functions (eval-when-compile |
807 |
|
(concat "\\b" |
808 |
|
(regexp-opt '( |
809 |
|
"abs" "acos" "asin" "atan" "atan2" "avg" "ceil" "cos" "cosh" "divtime" |
810 |
|
"exp" "floor" "getbits" "getblob" "getbyte" "getlong" "getraw" |
811 |
|
"getstr" "gettext" "getword" "hextoraw" "lenblob" "length" "log" |
812 |
|
"lower" "lpad" "ltrim" "max" "min" "mod" "monthname" "nvl" |
813 |
|
"octet_length" "power" "rand" "rawtohex" "repeat_string" |
814 |
|
"right_substr" "round" "rpad" "rtrim" "sign" "sin" "sinh" "soundex" |
815 |
|
"sqrt" "sum" "tan" "tanh" "timeint_to_days" "to_char" "to_date" |
816 |
|
"to_gmtime" "to_localtime" "to_number" "trim" "upper" "decode" |
817 |
|
"substr" "substring" "chr" "dayname" "days" "greatest" "hex" "initcap" |
818 |
|
"instr" "least" "multime" "replace" "width" |
819 |
|
) t) "\\b")))) |
820 |
|
(setq sql-mode-linter-font-lock-keywords |
821 |
|
(append sql-mode-ansi-font-lock-keywords |
822 |
|
(list (cons linter-keywords 'font-lock-function-name-face) |
823 |
|
(cons linter-reserved-words 'font-lock-keyword-face) |
824 |
|
;; XEmacs doesn't have font-lock-builtin-face |
825 |
|
(if (string-match "XEmacs\\|Lucid" emacs-version) |
826 |
|
(cons linter-builtin-functions 'font-lock-preprocessor-face) |
827 |
|
;; GNU Emacs 19 doesn't have it either |
828 |
|
(if (string-match "GNU Emacs 19" emacs-version) |
829 |
|
(cons linter-builtin-functions 'font-lock-function-name-face) |
830 |
|
;; Emacs |
831 |
|
(cons linter-builtin-functions 'font-lock-builtin-face))) |
832 |
|
(cons linter-types 'font-lock-type-face)))))) |
833 |
|
|
834 |
(defvar sql-mode-font-lock-keywords sql-mode-ansi-font-lock-keywords |
(defvar sql-mode-font-lock-keywords sql-mode-ansi-font-lock-keywords |
835 |
"SQL keywords used by font-lock. |
"SQL keywords used by font-lock. |
836 |
|
|
856 |
(setq font-lock-keywords sql-mode-postgres-font-lock-keywords) |
(setq font-lock-keywords sql-mode-postgres-font-lock-keywords) |
857 |
(font-lock-fontify-buffer)) |
(font-lock-fontify-buffer)) |
858 |
|
|
859 |
|
(defun sql-highlight-linter-keywords () |
860 |
|
"Highlight LINTER keywords. |
861 |
|
Basically, this just sets `font-lock-keywords' appropriately." |
862 |
|
(interactive) |
863 |
|
(setq font-lock-keywords sql-mode-linter-font-lock-keywords) |
864 |
|
(font-lock-fontify-buffer)) |
865 |
|
|
866 |
(defun sql-highlight-ansi-keywords () |
(defun sql-highlight-ansi-keywords () |
867 |
"Highlight ANSI SQL keywords. |
"Highlight ANSI SQL keywords. |
868 |
Basically, this just sets `font-lock-keywords' appropriately." |
Basically, this just sets `font-lock-keywords' appropriately." |
908 |
(defun sql-accumulate-and-indent () |
(defun sql-accumulate-and-indent () |
909 |
"Continue SQL statement on the next line." |
"Continue SQL statement on the next line." |
910 |
(interactive) |
(interactive) |
911 |
(if (fboundp 'comint-accumulate) |
(if (fboundp 'comint-accumulate) |
912 |
(comint-accumulate) |
(comint-accumulate) |
913 |
(newline)) |
(newline)) |
914 |
(indent-according-to-mode)) |
(indent-according-to-mode)) |
934 |
Ingres: \\[sql-ingres] |
Ingres: \\[sql-ingres] |
935 |
Microsoft: \\[sql-ms] |
Microsoft: \\[sql-ms] |
936 |
Interbase: \\[sql-interbase] |
Interbase: \\[sql-interbase] |
937 |
|
Linter: \\[sql-linter] |
938 |
|
|
939 |
But we urge you to choose a free implementation instead of these. |
But we urge you to choose a free implementation instead of these. |
940 |
|
|
1022 |
found))))) |
found))))) |
1023 |
|
|
1024 |
(defun sql-set-sqli-buffer-generally () |
(defun sql-set-sqli-buffer-generally () |
1025 |
"Set SQLi buffer for all SQL buffers that have none. |
"Set SQLi buffer for all SQL buffers that have none. |
1026 |
This function checks all SQL buffers for their SQLi buffer. If their |
This function checks all SQL buffers for their SQLi buffer. If their |
1027 |
SQLi buffer is nonexistent or has no process, it is set to the current |
SQLi buffer is nonexistent or has no process, it is set to the current |
1028 |
default SQLi buffer. The current default SQLi buffer is determined |
default SQLi buffer. The current default SQLi buffer is determined |
1148 |
Placeholders are words starting with and ampersand like &this. |
Placeholders are words starting with and ampersand like &this. |
1149 |
This function is used for `comint-input-sender' if using `sql-oracle' on NT." |
This function is used for `comint-input-sender' if using `sql-oracle' on NT." |
1150 |
(while (string-match "&\\(\\sw+\\)" string) |
(while (string-match "&\\(\\sw+\\)" string) |
1151 |
(setq string (replace-match |
(setq string (replace-match |
1152 |
(read-from-minibuffer |
(read-from-minibuffer |
1153 |
(format "Enter value for %s: " (match-string 1 string)) |
(format "Enter value for %s: " (match-string 1 string)) |
1154 |
nil nil nil sql-placeholder-history) |
nil nil nil sql-placeholder-history) |
1474 |
(setq parameter (nconc (list parameter) sql-oracle-options)) |
(setq parameter (nconc (list parameter) sql-oracle-options)) |
1475 |
(setq parameter sql-oracle-options)) |
(setq parameter sql-oracle-options)) |
1476 |
(if parameter |
(if parameter |
1477 |
(set-buffer (apply 'make-comint "SQL" sql-oracle-program nil |
(set-buffer (apply 'make-comint "SQL" sql-oracle-program nil |
1478 |
parameter)) |
parameter)) |
1479 |
(set-buffer (make-comint "SQL" sql-oracle-program nil)))) |
(set-buffer (make-comint "SQL" sql-oracle-program nil)))) |
1480 |
(setq sql-prompt-regexp "^SQL> ") |
(setq sql-prompt-regexp "^SQL> ") |
1738 |
|
|
1739 |
Interpreter used comes from variable `sql-ms-program'. Login uses the |
Interpreter used comes from variable `sql-ms-program'. Login uses the |
1740 |
variables `sql-user', `sql-password', `sql-database', and `sql-server' |
variables `sql-user', `sql-password', `sql-database', and `sql-server' |
1741 |
as defaults, if set. |
as defaults, if set. Additional command line parameters can be stored |
1742 |
|
in the list `sql-ms-options'. |
1743 |
|
|
1744 |
The buffer is put in sql-interactive-mode, giving commands for sending |
The buffer is put in sql-interactive-mode, giving commands for sending |
1745 |
input. See `sql-interactive-mode'. |
input. See `sql-interactive-mode'. |
1759 |
(message "Login...") |
(message "Login...") |
1760 |
;; Put all parameters to the program (if defined) in a list and call |
;; Put all parameters to the program (if defined) in a list and call |
1761 |
;; make-comint. |
;; make-comint. |
1762 |
(let ((params '("-w 300"))) |
(let ((params sql-ms-options)) |
1763 |
(if (not (string= "" sql-server)) |
(if (not (string= "" sql-server)) |
1764 |
(setq params (append (list "-S" sql-server) params))) |
(setq params (append (list "-S" sql-server) params))) |
1765 |
(if (not (string= "" sql-database)) |
(if (not (string= "" sql-database)) |
1936 |
(message "Login...done") |
(message "Login...done") |
1937 |
(pop-to-buffer sql-buffer))) |
(pop-to-buffer sql-buffer))) |
1938 |
|
|
1939 |
|
;;;###autoload |
1940 |
|
(defun sql-linter () |
1941 |
|
"Run inl by RELEX as an inferior process. |
1942 |
|
|
1943 |
|
If buffer `*SQL*' exists but no process is running, make a new process. |
1944 |
|
If buffer exists and a process is running, just switch to buffer |
1945 |
|
`*SQL*'. |
1946 |
|
|
1947 |
|
Interpreter used comes from variable `sql-linter-program' - usually `inl'. |
1948 |
|
Login uses the variables `sql-user', `sql-password', `sql-database' and |
1949 |
|
`sql-server' as defaults, if set. Additional command line parameters |
1950 |
|
can be stored in the list `sql-linter-options'. Run inl -h to get help on |
1951 |
|
parameters. |
1952 |
|
|
1953 |
|
`sql-database' is used to set the LINTER_MBX environment variable for |
1954 |
|
local connections, `sql-server' refers to the server name from the |
1955 |
|
`nodetab' file for the network connection (dbc_tcp or friends must run |
1956 |
|
for this to work). If `sql-password' is an empty string, inl will use |
1957 |
|
an empty password. |
1958 |
|
|
1959 |
|
The buffer is put in sql-interactive-mode, giving commands for sending |
1960 |
|
input. See `sql-interactive-mode'. |
1961 |
|
|
1962 |
|
To use LINTER font locking by default, put this line into your .emacs : |
1963 |
|
(setq sql-mode-font-lock-keywords sql-mode-linter-font-lock-keywords) |
1964 |
|
|
1965 |
|
\(Type \\[describe-mode] in the SQL buffer for a list of commands.)" |
1966 |
|
(interactive) |
1967 |
|
(if (comint-check-proc "*SQL*") |
1968 |
|
(pop-to-buffer "*SQL*") |
1969 |
|
(sql-get-login 'user 'password 'database 'server) |
1970 |
|
(message "Login...") |
1971 |
|
;; Put all parameters to the program (if defined) in a list and call |
1972 |
|
;; make-comint. |
1973 |
|
(let ((params sql-linter-options) (login nil) (old-mbx (getenv "LINTER_MBX"))) |
1974 |
|
(if (not (string= "" sql-user)) |
1975 |
|
(setq login (concat sql-user "/" sql-password))) |
1976 |
|
(setq params (append (list "-u" login) params)) |
1977 |
|
(if (not (string= "" sql-server)) |
1978 |
|
(setq params (append (list "-n" sql-server) params))) |
1979 |
|
(if (string= "" sql-database) |
1980 |
|
(setenv "LINTER_MBX" nil) |
1981 |
|
(setenv "LINTER_MBX" sql-database)) |
1982 |
|
(set-buffer (apply 'make-comint "SQL" sql-linter-program nil |
1983 |
|
params)) |
1984 |
|
(setenv "LINTER_MBX" old-mbx) |
1985 |
|
) |
1986 |
|
(setq sql-prompt-regexp "^SQL>") |
1987 |
|
(setq sql-prompt-length 4) |
1988 |
|
(setq sql-buffer (current-buffer)) |
1989 |
|
(sql-interactive-mode) |
1990 |
|
(message "Login...done") |
1991 |
|
(pop-to-buffer sql-buffer))) |
1992 |
|
|
1993 |
|
|
1994 |
|
|
1995 |
(provide 'sql) |
(provide 'sql) |
1996 |
|
|
1997 |
;;; sql.el ends here |
;;; sql.el ends here |