emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Steven Rémot" <steven.remot@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Add support for :dbhost, :dbuser and :database parameters for poastgresql in ob-sql.el
Date: Fri, 08 Aug 2014 22:11:49 +0200	[thread overview]
Message-ID: <53E52F05.3040106@gmail.com> (raw)

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


             reply	other threads:[~2014-08-08 20:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-08 20:11 Steven Rémot [this message]
2014-08-09  0:38 ` [PATCH] Add support for :dbhost, :dbuser and :database parameters for poastgresql in ob-sql.el 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

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=53E52F05.3040106@gmail.com \
    --to=steven.remot@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).