all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: MaDhAt2r <madhat2r@dukefoo.com>
To: emacs-orgmode@gnu.org
Subject: Add support for sqsh sql src blocks
Date: Thu, 24 Nov 2016 08:00:43 -0600	[thread overview]
Message-ID: <87vavdkmes.fsf@ArchNemesis.i-did-not-set--mail-host-address--so-tickle-me> (raw)


ob-sql-el: Add support for `sqsh' as an SQL engine

TINYCHANGE

1 file changed, 41 insertions(+), 10 deletions(-)
ob-sql.el | 51 +++++++++++++++++++++++++++++++++++++++++----------

modified   ob-sql.el
@@ -116,6 +116,17 @@ SQL Server on Windows and Linux platform."
 			 (when database (format "-d \"%s\"" database))))
 	     " "))
 
+(defun org-babel-sql-dbstring-sqsh (host user password database)
+  "Make sqlcmd commmand line args for database connection.
+`sqsh' is one method to access Sybase or MS SQL via Linux platform"
+  (mapconcat #'identity
+             (delq nil
+                   (list (when host (format "-S \"%s\"" host))
+                         (when user (format "-U \"%s\"" user))
+                         (when password (format "-P \"%s\"" password))
+                         (when database (format "-D \"%s\"" database))))
+             " "))
+
 (defun org-babel-sql-convert-standard-filename (file)
   "Convert the file name to OS standard.
 If in Cygwin environment, uses Cygwin specific function to
@@ -141,14 +152,14 @@ This function is called by `org-babel-execute-src-block'."
          (in-file (org-babel-temp-file "sql-in-"))
          (out-file (or (cdr (assq :out-file params))
                        (org-babel-temp-file "sql-out-")))
-	 (header-delim "")
+         (header-delim "")
          (command (pcase (intern engine)
-                    (`dbi (format "dbish --batch %s < %s | sed '%s' > %s"
+        (`dbi (format "dbish --batch %s < %s | sed '%s' > %s"
 				  (or cmdline "")
 				  (org-babel-process-file-name in-file)
 				  "/^+/d;s/^|//;s/(NULL)/ /g;$d"
 				  (org-babel-process-file-name out-file)))
-                    (`monetdb (format "mclient -f tab %s < %s > %s"
+        (`monetdb (format "mclient -f tab %s < %s > %s"
 				      (or cmdline "")
 				      (org-babel-process-file-name in-file)
 				      (org-babel-process-file-name out-file)))
@@ -160,7 +171,15 @@ This function is called by `org-babel-execute-src-block'."
 				     (org-babel-process-file-name in-file))
 				    (org-babel-sql-convert-standard-filename
 				     (org-babel-process-file-name out-file))))
-                    (`mysql (format "mysql %s %s %s < %s > %s"
+        (`sqsh (format "sqsh %s %s -i %s -o %s -m csv"
+                        (or cmdline "")
+                        (org-babel-sql-dbstring-sqsh
+                         dbhost dbuser dbpassword database)
+                        (org-babel-sql-convert-standard-filename
+                         (org-babel-process-file-name in-file))
+                        (org-babel-sql-convert-standard-filename
+                         (org-babel-process-file-name out-file))))
+        (`mysql (format "mysql %s %s %s < %s > %s"
 				    (org-babel-sql-dbstring-mysql
 				     dbhost dbport dbuser dbpassword database)
 				    (if colnames-p "" "-N")
@@ -179,13 +198,13 @@ footer=off -F \"\t\"  %s -f %s -o %s %s"
 				  (org-babel-process-file-name in-file)
 				  (org-babel-process-file-name out-file)
 				  (or cmdline "")))
-                    (`oracle (format
+        (`oracle (format
 			      "sqlplus -s %s < %s > %s"
 			      (org-babel-sql-dbstring-oracle
 			       dbhost dbport dbuser dbpassword database)
 			      (org-babel-process-file-name in-file)
 			      (org-babel-process-file-name out-file)))
-                    (_ (error "No support for the %s SQL engine" engine)))))
+        (_ (error "No support for the %s SQL engine" engine)))))
     (with-temp-file in-file
       (insert
        (pcase (intern engine)
@@ -203,18 +222,23 @@ SET MARKUP HTML OFF SPOOL OFF
 SET COLSEP '|'
 
 ")
-	 (`mssql "SET NOCOUNT ON
+         ((or `mssql `sqsh ) "SET NOCOUNT ON
 
 ")
 	 (_ ""))
-       (org-babel-expand-body:sql body params)))
+       (org-babel-expand-body:sql body params)
+       ;; sqsh requires `go' inserted at EOF
+       (if (equal (intern engine) `sqsh)
+           "\ngo"
+         "")
+       ))
     (org-babel-eval command "")
     (org-babel-result-cond result-params
       (with-temp-buffer
 	(progn (insert-file-contents-literally out-file) (buffer-string)))
       (with-temp-buffer
 	(cond
-	 ((memq (intern engine) '(dbi mysql postgresql))
+	 ((memq (intern engine) '(dbi mysql postgresql sqsh))
 	  ;; Add header row delimiter after column-names header in first line
 	  (cond
 	   (colnames-p
@@ -239,7 +263,14 @@ SET COLSEP '|'
 	      (goto-char (point-max))
 	      (forward-char -1))
 	    (write-file out-file))))
-	(org-table-import out-file '(16))
+  (cond
+	 ((equal (intern engine) 'sqsh)
+    (org-table-import out-file '(4))
+    )
+   (t
+    (org-table-import out-file '(16))
+    )
+   )
 	(org-babel-reassemble-table
 	 (mapcar (lambda (x)
 		   (if (string= (car x) header-delim)

             reply	other threads:[~2016-11-24 14:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-24 14:00 MaDhAt2r [this message]
2016-11-28 22:12 ` Add support for sqsh sql src blocks Nicolas Goaziou
2016-11-30  1:07   ` [Patch v2] " MaDhAt2r
2016-12-03  8:08     ` Nicolas Goaziou
2016-12-03 15:38       ` MaDhAt2r
2016-12-03 21:06         ` Nicolas Goaziou
2016-12-03 21:31           ` MaDhAt2r
2016-12-05 21:47             ` Nicolas Goaziou
2016-12-04 13:01     ` Sebastien Vauban

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vavdkmes.fsf@ArchNemesis.i-did-not-set--mail-host-address--so-tickle-me \
    --to=madhat2r@dukefoo.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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.