In ob-sql postgres would ignore dbport header argument. Attached is a
patch that corrects this.

Let me know if there are any modifications needed.

thnx,
Justin

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 6e003d2..f6a83e8 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -519,6 +519,8 @@ to force opening it in either Emacs or with system application.
 Allows J source blocks be indicated by letter j.  Previously the
 indication letter was solely J.
 
+*** Add port argument to ~org-babel-sql-dbstring-postgresql~
+    Can now use dbport argument in #+header for postgresql engine in org-ob-sql
 * Version 8.3
 
 ** Incompatible changes
diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
index 7801c5f..f8824c8 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))))))
 
@@ -171,7 +172,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 "")))
diff --git a/testing/lisp/test-ob-sql.el b/testing/lisp/test-ob-sql.el
new file mode 100644
index 0000000..0a7fe7e
--- /dev/null
+++ b/testing/lisp/test-ob-sql.el
@@ -0,0 +1,26 @@
+;;; test-ob-sql.el --- tests for ob-sql.el
+
+;; This file is not part of GNU Emacs.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+(unless (featurep 'ob-sql)
+  (signal 'missing-test-dependency "Support for SQL code blocks"))
+
+(ert-deftest test-ob-sql/postgresql-dbstring-with-port ()
+    (should (string-match "-hlocalhost -p4242 -Uorg -dbabel"
+		  (org-babel-sql-dbstring-postgresql "localhost" 4242 "org" "babel")))
+    (should (string-match "-hlocalhost -Uorg -dbabel"
+		  (org-babel-sql-dbstring-postgresql "localhost" nil "org" "babel"))))

Reply via email to