emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-sql.el: Support sqlcmd and cygwin environment
@ 2016-06-08 10:24 Xi Shen
  2016-06-10 22:06 ` Nicolas Goaziou
  0 siblings, 1 reply; 17+ messages in thread
From: Xi Shen @ 2016-06-08 10:24 UTC (permalink / raw)
  To: Emacs-orgmode@gnu.org


[-- Attachment #1.1: Type: text/plain, Size: 327 bytes --]

Hi,

I would like to apply this path to add sqlcmd support, and allow org-mode
to execute and capture sqlcmd output in cygwin environment.

I added a "platform-convert-file-name" function to convert a *nix path to
Windows path. Should I put this function in ob-sql.el, or somewhere else?


Thanks,
David

-- 

Thanks,
David S.

[-- Attachment #1.2: Type: text/html, Size: 580 bytes --]

[-- Attachment #2: 0001-ob-sql.el-Support-sqlcmd-and-cygwin-environment.patch --]
[-- Type: application/octet-stream, Size: 3204 bytes --]

From 96daeb8d22e33e3484fefb27914bd434efd32142 Mon Sep 17 00:00:00 2001
From: Xi Shen <davidshen84@gmail.com>
Date: Wed, 8 Jun 2016 13:49:54 +0800
Subject: [PATCH] ob-sql.el: Support sqlcmd and cygwin environment

* lisp/ob-sql.el (org-babel-sql-dbstring-mssql): Format Microsoft
  sqlcmd command line args.

* lisp/ob-sql.el (platform-convert-file-name): Convert a *nix path to
  Windows long path in Cygwin environment, or do nothing.

* lisp/ob-sql.el (org-babel-execute:sql): Add `mssql` command support.
  For both `msosql` and `mssql` the `input` and `output` path will be
  mapped by `platform-convert-file-name`

The `osql` command line tool was last updated in 2004,
https://technet.microsoft.com/en-us/library/aa214012(v=sql.80).aspx,
and could not output the query result in a way that morden
`org-table.el` expects.  The `sqlcmd` is the preferred command line
tool to connect the Microsoft SQL Server and it also has a Linux
version,
https://msdn.microsoft.com/en-us/library/hh568447(v=sql.110).aspx.

TINYCHANGE
---
 lisp/ob-sql.el | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
index 6488afe..b115faa 100644
--- a/lisp/ob-sql.el
+++ b/lisp/ob-sql.el
@@ -103,6 +103,24 @@ Pass nil to omit that arg."
   "Make Oracle command line args for database connection."
   (format "%s/%s@%s:%s/%s" user password host port database))
 
+(defun org-babel-sql-dbstring-mssql (host user password database)
+  "Make Microsoft sqlcmd commmand line args for database
+connection."
+  (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 platform-convert-file-name (file)
+  "In Cygwin environment convert the file path into Windows long
+path and quote it."
+  (if (fboundp 'cygwin-convert-file-name-to-windows)
+      (format "\"%s\"" (cygwin-convert-file-name-to-windows file))
+    file))
+
 (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'."
@@ -131,8 +149,14 @@ This function is called by `org-babel-execute-src-block'."
 				      (org-babel-process-file-name out-file)))
                     (`msosql (format "osql %s -s \"\t\" -i %s -o %s"
 				     (or cmdline "")
-				     (org-babel-process-file-name in-file)
-				     (org-babel-process-file-name out-file)))
+				     (platform-convert-file-name (org-babel-process-file-name in-file))
+				     (platform-convert-file-name (org-babel-process-file-name out-file))))
+		    (`mssql (format "sqlcmd %s -s \"\t\" %s -i %s -o %s"
+				     (or cmdline "")
+				     (org-babel-sql-dbstring-mssql
+				      dbhost dbuser dbpassword database)
+				     (platform-convert-file-name (org-babel-process-file-name in-file))
+				     (platform-convert-file-name (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)
-- 
2.8.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2016-07-04  8:12 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-08 10:24 [PATCH] ob-sql.el: Support sqlcmd and cygwin environment Xi Shen
2016-06-10 22:06 ` Nicolas Goaziou
2016-06-11  2:17   ` Xi Shen
2016-06-11  8:40     ` Nicolas Goaziou
2016-06-12  2:12       ` Xi Shen
2016-06-12 10:37         ` Xi Shen
2016-06-13  5:36           ` Xi Shen
2016-06-13  6:30             ` Xi Shen
2016-06-14 11:52               ` Nicolas Goaziou
2016-06-14 13:02                 ` Xi Shen
2016-06-15  4:01                   ` Xi Shen
2016-06-15 16:49                     ` Nicolas Goaziou
2016-06-16  6:04                       ` Xi Shen
2016-06-16  8:56                         ` tumashu
2016-06-16 22:29                         ` Nicolas Goaziou
2016-06-20 12:34                           ` Xi Shen
2016-07-04  8:11                             ` Xi Shen

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).