emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Add support for :dbhost, :dbuser and :database parameters for poastgresql in ob-sql.el
@ 2014-08-08 20:11 Steven Rémot
  2014-08-09  0:38 ` Thomas S. Dye
  0 siblings, 1 reply; 9+ messages in thread
From: Steven Rémot @ 2014-08-08 20:11 UTC (permalink / raw)
  To: emacs-orgmode

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

Hi,

I did some changes to support :dbname, :dbhost and :database in SQL code 
blocks when using postgresql engine.

Even if it was possible to specify this information using :cmdline 
parameter, I thought it was a bit cleaner to be able to provide this 
information in a way independent from the command line.

I will gladly accept any remark / comment on this patch.

Regards,
Steven Rémot

[-- Attachment #2: 0001-ob-sql.el-Enhance-postgresql-support.patch --]
[-- Type: text/x-patch, Size: 2070 bytes --]

From 6ad99759a16c8b6f9decfb8ea90c84e7a1c18fdc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Steven=20R=C3=A9mot?= <steven.remot@gmail.com>
Date: Fri, 8 Aug 2014 21:49:44 +0200
Subject: [PATCH] ob-sql.el: Enhance postgresql support

* lisp/ob-sql.el: Add support for :dbhost, :dbuser and :database
  parameters in sql code blocks for postgresql engine.

Before this patch, it was necessary to use :cmdline parameter to
specify host, user and database different the the default ones.  Now,
this can be done using parameters that are independents of the engine
used.

This is not trivial (and not recommended) to pass password as a
command line argument to psql, so :dbpassword is not supported.
---
 lisp/ob-sql.el | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
index 7b85df8..9fe9da2 100644
--- a/lisp/ob-sql.el
+++ b/lisp/ob-sql.el
@@ -87,6 +87,15 @@
 	       (when password (concat "-p" password))
 	       (when database (concat "-D" database))))))
 
+(defun dbstring-postgresql (host user database)
+  "Make PostgreSQL command line ards for database connection.
+Pass nil to omit that arg."
+  (combine-and-quote-strings
+   (remq nil
+	 (list (when host (concat "-h" host))
+	       (when user (concat "-U" user))
+	       (when database (concat "-d" database))))))
+
 (defun org-babel-execute:sql (body params)
   "Execute a block of Sql code with Babel.
 This function is called by `org-babel-execute-src-block'."
@@ -123,8 +132,9 @@ This function is called by `org-babel-execute-src-block'."
 				    (org-babel-process-file-name in-file)
 				    (org-babel-process-file-name out-file)))
 		    ('postgresql (format
-				  "psql --set=\"ON_ERROR_STOP=1\" %s -A -P footer=off -F \"\t\"  -f %s -o %s %s"
+				  "psql --set=\"ON_ERROR_STOP=1\" %s -A -P footer=off -F \"\t\"  %s -f %s -o %s %s"
 				  (if colnames-p "" "-t")
+				  (dbstring-postgresql dbhost dbuser database)
 				  (org-babel-process-file-name in-file)
 				  (org-babel-process-file-name out-file)
 				  (or cmdline "")))
-- 
1.9.1


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

end of thread, other threads:[~2014-09-20 21:08 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-08 20:11 [PATCH] Add support for :dbhost, :dbuser and :database parameters for poastgresql in ob-sql.el Steven Rémot
2014-08-09  0:38 ` Thomas S. Dye
2014-08-09 10:04   ` Steven Rémot
2014-08-09 15:31     ` Thomas S. Dye
2014-08-09 16:02       ` Steven Rémot
2014-09-20 11:55       ` Steven Rémot
2014-09-20 12:16         ` Nicolas Goaziou
2014-09-20 13:31           ` Steven Rémot
2014-09-20 21:09             ` Nicolas Goaziou

Code repositories for project(s) associated with this public 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).