summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven RĂ©mot <steven.remot@gmail.com>2014-09-20 15:02:36 +0200
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2014-09-20 23:06:36 +0200
commite5e9fc974d3e1bb0e50b843f65d4f6689043574e (patch)
treea0e8de22cc64c193c664536ed6f2e95ef74d575a
parent20539b8d1d459f866892742028885565816e8d00 (diff)
downloadorg-mode-e5e9fc974d3e1bb0e50b843f65d4f6689043574e.tar.gz
ob-sql.el: Enhance postgresql support
* lisp/ob-sql.el (org-babel-sql-dbstring-postgresql): New function (org-babel-execute:sql): Use new function. 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.
-rw-r--r--lisp/ob-sql.el12
1 files changed, 11 insertions, 1 deletions
diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
index 7b85df8..292d5dd 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 org-babel-sql-dbstring-postgresql (host user database)
+ "Make PostgreSQL command line args for database connection.
+Pass nil to omit that arg."
+ (combine-and-quote-strings
+ (delq 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")
+ (org-babel-sql-dbstring-postgresql dbhost dbuser database)
(org-babel-process-file-name in-file)
(org-babel-process-file-name out-file)
(or cmdline "")))