summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaDhAt2r <madhat2r@dukefoo.com>2016-12-05 22:45:25 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2016-12-05 22:45:25 +0100
commit0063075f65bdd1dcb9c7ad5d723fcf7ca6cabaa1 (patch)
tree7de2e06e8dfac4f714f97b0dbaa5fe5bae8faf25
parentb25dd07bc0b098224172e8a7cf48080dc2f899f6 (diff)
downloadorg-mode-0063075f65bdd1dcb9c7ad5d723fcf7ca6cabaa1.tar.gz
ob-sql: Add sqsh engine
* lisp/ob-sql.el (org-babel-sql-dbstring-sqsh): New function. (org-babel-execute:sql): Add `sqsh' engine.
-rw-r--r--etc/ORG-NEWS17
-rw-r--r--lisp/ob-sql.el41
2 files changed, 52 insertions, 6 deletions
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 684d404..78f28cb 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -33,7 +33,6 @@ into
Creation of a new setting to specify the Cider timeout. By setting
the =org-babel-clojure-sync-nrepl-timeout= setting option. The value
is in seconds and if set to =nil= then no timeout will occur.
-
**** Clojure: new header ~:show-process~
A new block code header has been created for Org Babel that enables
@@ -67,6 +66,22 @@ is specified then all the output from the window will appears in the
results section. If =value= is specified, then only the last returned
value of the code will be displayed in the results section.
+**** SQL: new engine added ~sqsh~
+
+A new engine was added to support ~sqsh~ command line utility for use
+against Microsoft SQL Server or Sybase SQL server.
+
+More information on ~sqsh~ can be found here: [[https://sourceforge.net/projects/sqsh/][sourceforge/sqsh]]
+
+To use ~sqsh~ in an *sql* =SRC_BLK= set the =:engine= like this:
+
+#+begin_example
+,#+BEGIN_SRC sql :engine sqsh :dbhost my_host :dbuser master :dbpassword pass :database support
+Select * From Users
+Where clue > 0
+,#+END_SRC
+#+end_example
+
*** Horizontal rules are no longer ignored in LaTeX table math mode
** Removed options
diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
index ec94c35..6e02168 100644
--- a/lisp/ob-sql.el
+++ b/lisp/ob-sql.el
@@ -43,15 +43,24 @@
;; - colnames (default, nil, means "yes")
;; - result-params
;; - out-file
+;;
;; The following are used but not really implemented for SQL:
;; - colname-names
;; - rownames
;; - rowname-names
;;
+;; Engines supported:
+;; - mysql
+;; - dbi
+;; - mssql
+;; - sqsh
+;; - postgresql
+;; - oracle
+;;
;; TODO:
;;
;; - support for sessions
-;; - support for more engines (currently only supports mysql)
+;; - support for more engines
;; - what's a reasonable way to drop table data into SQL?
;;
@@ -116,6 +125,18 @@ SQL Server on Windows and Linux platform."
(when database (format "-d \"%s\"" database))))
" "))
+(defun org-babel-sql-dbstring-sqsh (host user password database)
+ "Make sqsh 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
@@ -179,6 +200,14 @@ 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 "")))
+ (`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))))
(`oracle (format
"sqlplus -s %s < %s > %s"
(org-babel-sql-dbstring-oracle
@@ -203,18 +232,20 @@ 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 (string= 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 +270,7 @@ SET COLSEP '|'
(goto-char (point-max))
(forward-char -1))
(write-file out-file))))
- (org-table-import out-file '(16))
+ (org-table-import out-file (if (string= engine "sqsh") '(4) '(16)))
(org-babel-reassemble-table
(mapcar (lambda (x)
(if (string= (car x) header-delim)