I added a port switch for postgresql in ob-sql.el

If you have sql loaded and do something like the follow:

#+name: example
#+header: :engine postgresql
#+header: :dbhost localhost
#+header: :dbport 6543
#+header: :dbuser orgmode
#+header: :database ob-sql
#+BEGIN_SRC sql
select * from everything
#+END_SRC

Note: default port for postrgres is 5432 not 6543

You will be either presented with an error or in my case a failed
password prompt since I had multiple db servers running. The problem was
that org-babel-sql-dbstring-postgresql did not accept a port argument.

I have attached a simple patch to fix this and have verified it works
for me. I would have loved to add some tests, but I ran into some
problems:

1) I could not figure out how to get just one test file to run
2) I was not able get make test to load the sql language in babel


Is there a recommended/standard way to resolve these problems with tests?

>From 4ca513afe3725fbfbefd7ad9bf54637adfd56ea2 Mon Sep 17 00:00:00 2001
From: Justin Kirby <jus...@redsorbet.com>
Date: Wed, 18 May 2016 23:14:48 -0400
Subject: [PATCH] add dbport parameter to postgresql dbstring

If postgesql is running on a different port or using a ssh tunnel to
remote db, the ability to specify a different port is needed.
---
 lisp/ob-sql.el | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
index 6488afe..5f96aa3 100644
--- a/lisp/ob-sql.el
+++ b/lisp/ob-sql.el
@@ -90,12 +90,13 @@
 	       (when password (concat "-p" password))
 	       (when database (concat "-D" database))))))
 
-(defun org-babel-sql-dbstring-postgresql (host user database)
+(defun org-babel-sql-dbstring-postgresql (host port 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 port (format "-p%d" port))
 	       (when user (concat "-U" user))
 	       (when database (concat "-d" database))))))
 
@@ -145,7 +146,7 @@ This function is called by `org-babel-execute-src-block'."
 footer=off -F \"\t\"  %s -f %s -o %s %s"
 				  (if colnames-p "" "-t")
 				  (org-babel-sql-dbstring-postgresql
-				   dbhost dbuser database)
+				   dbhost dbport dbuser database)
 				  (org-babel-process-file-name in-file)
 				  (org-babel-process-file-name out-file)
 				  (or cmdline "")))
-- 
2.6.3

Reply via email to