emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: Daniel Kraus <daniel@kraus.my>
Cc: stefano.rodighiero@gmail.com, emacs-orgmode@gnu.org
Subject: Re: [PATCH] ob-sql: Respect database param when using dbconnection
Date: Mon, 01 Jun 2020 02:16:06 +0000	[thread overview]
Message-ID: <87v9kbttsp.fsf@kyleam.com> (raw)
In-Reply-To: <87a71s9jio.fsf@kraus.my>

Daniel Kraus writes:

> I use ob-sql with the :dbconnection param so I don't have my username and password in my org file.
> But often I don't want to use the default database from the dbconnection alist but
> rather specify it explicitly with :database.
> Attached is a patch that fixes this.

Thanks for the patch.

> Subject: [PATCH] ob-sql: Respect database param when using dbconnection
>
> ---
>  lisp/ob-sql.el | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)


Would you mind formatting your commit message as described at
<https://orgmode.org/worg/org-contribute.html>?  It'd also be good to
have an ORG-NEWS entry for this, I think.

> diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
> index 7c359b988..d7a8bf0a0 100644
> --- a/lisp/ob-sql.el
> +++ b/lisp/ob-sql.el
> @@ -191,7 +191,8 @@ then look for the parameter into the corresponding connection
>  defined in `sql-connection-alist`, otherwise look into PARAMS.
>  Look `sql-connection-alist` (part of SQL mode) for how to define
>  database connections."
> -  (if (assq :dbconnection params)
> +  (if (and (assq :dbconnection params)
> +	   (not (and (assq :database params) (eq name :database))))
>        (let* ((dbconnection (cdr (assq :dbconnection params)))
>               (name-mapping '((:dbhost . sql-server)
>                               (:dbport . sql-port)

From what I can gather from your description, this looks reasonable.
I'm not an ob-sql user, so perhaps I missing something, but would it
make sense for any connection parameter to take precedence if explicitly
given in the source block header (i.e. something like the patch below)?
[+cc Stefano, who added the :dbconneciton feature.]


diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
index 7c359b988..e7186938f 100644
--- a/lisp/ob-sql.el
+++ b/lisp/ob-sql.el
@@ -191,17 +191,17 @@ (defun org-babel-find-db-connection-param (params name)
 defined in `sql-connection-alist`, otherwise look into PARAMS.
 Look `sql-connection-alist` (part of SQL mode) for how to define
 database connections."
-  (if (assq :dbconnection params)
-      (let* ((dbconnection (cdr (assq :dbconnection params)))
-             (name-mapping '((:dbhost . sql-server)
-                             (:dbport . sql-port)
-                             (:dbuser . sql-user)
-                             (:dbpassword . sql-password)
-                             (:database . sql-database)))
-             (mapped-name (cdr (assq name name-mapping))))
-        (cadr (assq mapped-name
-                    (cdr (assoc dbconnection sql-connection-alist)))))
-    (cdr (assq name params))))
+  (or (cdr (assq name params))
+      (and (assq :dbconnection params)
+	   (let* ((dbconnection (cdr (assq :dbconnection params)))
+		  (name-mapping '((:dbhost . sql-server)
+				  (:dbport . sql-port)
+				  (:dbuser . sql-user)
+				  (:dbpassword . sql-password)
+				  (:database . sql-database)))
+		  (mapped-name (cdr (assq name name-mapping))))
+	     (cadr (assq mapped-name
+			 (cdr (assoc dbconnection sql-connection-alist))))))))
 
 (defun org-babel-execute:sql (body params)
   "Execute a block of Sql code with Babel.




  reply	other threads:[~2020-06-01  2:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-28 15:17 Daniel Kraus
2020-06-01  2:16 ` Kyle Meyer [this message]
2020-06-01  8:20   ` Stefano Rodighiero

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v9kbttsp.fsf@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=daniel@kraus.my \
    --cc=emacs-orgmode@gnu.org \
    --cc=stefano.rodighiero@gmail.com \
    --subject='Re: [PATCH] ob-sql: Respect database param when using dbconnection' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).