From 0063075f65bdd1dcb9c7ad5d723fcf7ca6cabaa1 Mon Sep 17 00:00:00 2001 From: MaDhAt2r Date: Mon, 5 Dec 2016 22:45:25 +0100 Subject: [PATCH] ob-sql: Add sqsh engine * lisp/ob-sql.el (org-babel-sql-dbstring-sqsh): New function. (org-babel-execute:sql): Add `sqsh' engine. --- etc/ORG-NEWS | 17 ++++++++++++++++- lisp/ob-sql.el | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 684d404f0..78f28cbe5 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 ec94c3510..6e0216858 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) -- 2.11.4.GIT