How to create an Emacs SQL buffer?

I can connect manually by doing:

Mx sql-postgres

and enter User, Database, Host, Server

I would like to do something like:

(connect-foo (user "I") (database "bar") (host "earth"))

Decision:

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 hardcoded. This is the "postgres" in sql.el.

(sql-create-buffer bar-profile)
+3
source share
1 answer

, (, init, ).

, , , :

(defun sql-get-login (&rest what)
  (setq sql-user     "me"
        sql-password "secret"
        sql-server   "localhost"
        sql-database "MyDB"))
+5

Source: https://habr.com/ru/post/1738446/


All Articles