emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-sql: Respect database param when using dbconnection
@ 2020-05-28 15:17 Daniel Kraus
  2020-06-01  2:16 ` Kyle Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Kraus @ 2020-05-28 15:17 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 288 bytes --]

Hi,

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,
  Daniel


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: [PATCH] ob-sql: Respect database param when using dbconnection --]
[-- Type: text/x-patch, Size: 969 bytes --]

From a8dccff104d7426e2f353b1005e0bdcc51de6e99 Mon Sep 17 00:00:00 2001
From: Daniel Kraus <daniel@kraus.my>
Date: Tue, 26 May 2020 16:07:34 +0200
Subject: [PATCH] ob-sql: Respect database param when using dbconnection

---
 lisp/ob-sql.el | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

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)
-- 
2.26.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ob-sql: Respect database param when using dbconnection
  2020-05-28 15:17 [PATCH] ob-sql: Respect database param when using dbconnection Daniel Kraus
@ 2020-06-01  2:16 ` Kyle Meyer
  2020-06-01  8:20   ` Stefano Rodighiero
  0 siblings, 1 reply; 3+ messages in thread
From: Kyle Meyer @ 2020-06-01  2:16 UTC (permalink / raw)
  To: Daniel Kraus; +Cc: stefano.rodighiero, emacs-orgmode

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.




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ob-sql: Respect database param when using dbconnection
  2020-06-01  2:16 ` Kyle Meyer
@ 2020-06-01  8:20   ` Stefano Rodighiero
  0 siblings, 0 replies; 3+ messages in thread
From: Stefano Rodighiero @ 2020-06-01  8:20 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: Daniel Kraus, emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1276 bytes --]

On Mon, Jun 1, 2020 at 4:16 AM Kyle Meyer <kyle@kyleam.com> wrote:

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.
>

Thank you @Daniel


> […]
> 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.]
>

I think it makes sense.

(I personally handle cases like those described by Daniel differently,
keeping distinct sql-connection-alist entries for each DB
param combination I might need, but I can imagine why someone
would want to "override" the database or the host params.
For port, user and password I have more difficulties imagining a
case where combinations of those params would need override,
but I think @Kyle's generic solution is better)

s.


-- 
www.stefanorodighiero.net

[-- Attachment #2: Type: text/html, Size: 2095 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-06-01  8:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-28 15:17 [PATCH] ob-sql: Respect database param when using dbconnection Daniel Kraus
2020-06-01  2:16 ` Kyle Meyer
2020-06-01  8:20   ` Stefano Rodighiero

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).