tags:

views:

149

answers:

1

I can connect manually by doing:

M-x sql-postgres

and type the 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)
+4  A: 

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"))
Kilian Foth
Thanks. I grabbed some of the code and am trying to use it non-interactively.
Eddy Pronk
Thanks. I a similar problem with Sybase. I'll try your solution - I have my irc password in my .emacs, which - along with our db servers - is behind a firewall anyway.
Matt Curtis