How do I create an Emacs SQL buffer?
I can connect manually by doing:
M-x sql-postgres
and type t开发者_高级运维he User, Database, Host, Server
I'd like to do something like:
(connect-foo (user "me")(database "bar")(host "earth"))
Solution so far:
I grabbed some code from sql.el and changed it so I can use it non-interactively.
(defun sql-create-buffer (profile)
"derived from sql-product-interactive in sql.el which is"
"covered by GNU General Public License version 3."
(setq sql-user (cdr (assoc 'user profile))
sql-password (cdr (assoc 'password profile))
sql-server (cdr (assoc 'server profile))
sql-database (cdr (assoc 'database profile)))
(setq product 'postgres) ;;(cdr (assoc 'product profile)))
(when (sql-product-feature :sqli-connect product)
(if (comint-check-proc "*SQL*")
(pop-to-buffer "*SQL*")
;; Connect to database.
(message "Login...")
(funcall (sql-product-feature :sqli-connect product))
;; Set SQLi mode.
(setq sql-interactive-product product)
(setq sql-buffer (current-buffer))
(sql-interactive-mode)
;; All done.
(message "Login...done")
(pop-to-buffer sql-buffer))))
(setq bar-profile '(
(product . "postgres")
(user . "me")
(password . "")
(server . "earth")
(database . "bar")))
I don't know what to do with the 'product' parameter, so I left it hard coded for now. It is 'postgres in sql.el.
(sql-create-buffer bar-profile)
It seems that package is not intended to be used non-interactively (probably to discourage writing login info into init files, which is a perfectly sound principle).
However, if you really want to avoid answering the questions, you can simply redefine the query function, like this:
(defun sql-get-login (&rest what)
(setq sql-user "me"
sql-password "secret"
sql-server "localhost"
sql-database "MyDB"))
精彩评论