sql.el --- specialized comint.el for SQL interpreters
This page is home of SQL mode for Emacs and XEmacs. SQL mode is part of both Emacs and XEmacs. You can get the latest versions here.
The interactive SQL mode is based on ComintMode. The following interpreters are supported:
Type M-x sql-help RET to read the online help. The latest version is available from Emacs’s source control:
The current maintainer of sql.el is MichaelMauger.
Starting with version 21.4-a, sql.el is included in the regular sources of Emacs. The CVS version linked above depends on functions that haven’t made it out of CVS, so don’t use the latest version if you’re not running a bleeding edge Emacs from CVS.
If those with pre-22.1 versions of Emacs can identify changes needed to run on older releases, send them to MichaelMauger for consideration. However, the emphasis is on improving future versions of Emacs.
If you don’t like window splittings related to the SQL buffer, try the following, per Force Same Window.
(add-to-list 'same-window-buffer-names "*SQL*")
If you only work on one database, you might prefer not to confirm the default user, password, database, etc. settings. In that case, load sql.el, and disable sql-get-login in your ~/.emacs:
(require 'sql) (defalias 'sql-get-login 'ignore)
To interact with the interpreter from a window already in SQL mode, execute the following:
M-x sql-set-sqli-buffer RET *SQL* RET
Want to save you history between sessions? Consider adding this hook to your .emacs. It saves the history in a separate file for each SQL “product”. You’ll need to run `
M-x make-directory RET ~/.emacs.d/sql/ RET’
(defun my-sql-save-history-hook () (let ((lval 'sql-input-ring-file-name) (rval 'sql-product)) (if (symbol-value rval) (let ((filename (concat "~/.emacs.d/sql/" (symbol-name (symbol-value rval)) "-history.sql"))) (set (make-local-variable lval) filename)) (error (format "SQL history will not be saved because %s is nil" (symbol-name rval)))))) (add-hook 'sql-interactive-mode-hook 'my-sql-save-history-hook)
When using sql-send-region to execute a query in a SQLi buffer, the table formatting is off because the column names are printed on the same row as the the prompt. By adding a newline before the comint output we can make sure everything lines up nice. This will add a preceding newline to every comint output, even queries run at the prompt - though the extra line isn’t too noticeable.
(defvar sql-last-prompt-pos 1 "position of last prompt when added recording started") (make-variable-buffer-local 'sql-last-prompt-pos) (put 'sql-last-prompt-pos 'permanent-local t) (defun sql-add-newline-first (output) "Add newline to beginning of OUTPUT for `comint-preoutput-filter-functions' This fixes up the display of queries sent to the inferior buffer programatically." (let ((begin-of-prompt (or (and comint-last-prompt-overlay ;; sometimes this overlay is not on prompt (save-excursion (goto-char (overlay-start comint-last-prompt-overlay)) (looking-at-p comint-prompt-regexp) (point))) 1))) (if (> begin-of-prompt sql-last-prompt-pos) (progn (setq sql-last-prompt-pos begin-of-prompt) (concat "\n" output)) output))) (defun sqli-add-hooks () "Add hooks to `sql-interactive-mode-hook'." (add-hook 'comint-preoutput-filter-functions 'sql-add-newline-first)) (add-hook 'sql-interactive-mode-hook 'sqli-add-hooks)
If you are using GNU Emacs 19.34, you will need two additional things:
Once you completed installation, type M-x load-library RET sql RET to load sql.el. Then, type M-x sql-help RET to read the online help.
There is a useful tutorial style article on using sql-mode (targeted at DB2 but more broadly applicable) by RoyMathew at http://www.ibm.com/developerworks/data/library/techarticle/0206mathew/0206mathew.html Also check out the Oracle FAQ at http://www.orafaq.com/
If you’re trying to use MS SQL Server in sql-mode, and the Microsoft command-line query processors osql and isql are not available to you, try jisql, a free, Java-based “workalike” provided by the clever (and generous) folks at Xigole Systems, Inc. http://www.xigole.com/software/jisql.jsp. Apache license.
Another option for users lacking access to osql/isql is sqsh, another Java-based command-line SQL client. The primary advantage to sqsh is that it is in the Debian/Ubuntu repositories (I can’t speak for Fedora, etc.) making it easier to install. When using sqsh to connect to SQL Server, use M-x sql-sybase, not sql-ms. Otherwise, Emacs won’t pass the right command-line parameters and it won’t connect.