Fix file:// uri handling for windows-nt and cygwin
[org-mode/org-tableheadings.git] / lisp / ob-sql.el
blob7e9085dffcb0e3644a229f8b833afb372699a67e
1 ;;; ob-sql.el --- Babel Functions for SQL -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating sql source code.
27 ;; (see also ob-sqlite.el)
29 ;; SQL is somewhat unique in that there are many different engines for
30 ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this
31 ;; file will have to be implemented engine by engine.
33 ;; Also SQL evaluation generally takes place inside of a database.
35 ;; Header args used:
36 ;; - engine
37 ;; - cmdline
38 ;; - dbhost
39 ;; - dbport
40 ;; - dbuser
41 ;; - dbpassword
42 ;; - database
43 ;; - colnames (default, nil, means "yes")
44 ;; - result-params
45 ;; - out-file
46 ;; The following are used but not really implemented for SQL:
47 ;; - colname-names
48 ;; - rownames
49 ;; - rowname-names
51 ;; TODO:
53 ;; - support for sessions
54 ;; - support for more engines (currently only supports mysql)
55 ;; - what's a reasonable way to drop table data into SQL?
58 ;;; Code:
59 (require 'ob)
61 (declare-function org-table-import "org-table" (file arg))
62 (declare-function orgtbl-to-csv "org-table" (table params))
63 (declare-function org-table-to-lisp "org-table" (&optional txt))
64 (declare-function cygwin-convert-file-name-to-windows "cygw32.c" (file &optional absolute-p))
66 (defvar org-babel-default-header-args:sql '())
68 (defconst org-babel-header-args:sql
69 '((engine . :any)
70 (out-file . :any)
71 (dbhost . :any)
72 (dbport . :any)
73 (dbuser . :any)
74 (dbpassword . :any)
75 (database . :any))
76 "SQL-specific header arguments.")
78 (defun org-babel-expand-body:sql (body params)
79 "Expand BODY according to the values of PARAMS."
80 (org-babel-sql-expand-vars
81 body (org-babel--get-vars params)))
83 (defun org-babel-sql-dbstring-mysql (host port user password database)
84 "Make MySQL cmd line args for database connection. Pass nil to omit that arg."
85 (combine-and-quote-strings
86 (delq nil
87 (list (when host (concat "-h" host))
88 (when port (format "-P%d" port))
89 (when user (concat "-u" user))
90 (when password (concat "-p" password))
91 (when database (concat "-D" database))))))
93 (defun org-babel-sql-dbstring-postgresql (host port user database)
94 "Make PostgreSQL command line args for database connection.
95 Pass nil to omit that arg."
96 (combine-and-quote-strings
97 (delq nil
98 (list (when host (concat "-h" host))
99 (when port (format "-p%d" port))
100 (when user (concat "-U" user))
101 (when database (concat "-d" database))))))
103 (defun org-babel-sql-dbstring-oracle (host port user password database)
104 "Make Oracle command line args for database connection."
105 (format "%s/%s@%s:%s/%s" user password host port database))
107 (defun org-babel-sql-dbstring-mssql (host user password database)
108 "Make sqlcmd commmand line args for database connection.
109 `sqlcmd' is the preferred command line tool to access Microsoft
110 SQL Server on Windows and Linux platform."
111 (mapconcat #'identity
112 (delq nil
113 (list (when host (format "-S \"%s\"" host))
114 (when user (format "-U \"%s\"" user))
115 (when password (format "-P \"%s\"" password))
116 (when database (format "-d \"%s\"" database))))
117 " "))
119 (defun org-babel-sql-convert-standard-filename (file)
120 "Convert the file name to OS standard.
121 If in Cygwin environment, uses Cygwin specific function to
122 convert the file name. Otherwise, uses Emacs' standard conversion
123 function."
124 (format "\"%s\""
125 (if (fboundp 'cygwin-convert-file-name-to-windows)
126 (cygwin-convert-file-name-to-windows file)
127 (convert-standard-filename file))))
129 (defun org-babel-execute:sql (body params)
130 "Execute a block of Sql code with Babel.
131 This function is called by `org-babel-execute-src-block'."
132 (let* ((result-params (cdr (assq :result-params params)))
133 (cmdline (cdr (assq :cmdline params)))
134 (dbhost (cdr (assq :dbhost params)))
135 (dbport (cdr (assq :dbport params)))
136 (dbuser (cdr (assq :dbuser params)))
137 (dbpassword (cdr (assq :dbpassword params)))
138 (database (cdr (assq :database params)))
139 (engine (cdr (assq :engine params)))
140 (colnames-p (not (equal "no" (cdr (assq :colnames params)))))
141 (in-file (org-babel-temp-file "sql-in-"))
142 (out-file (or (cdr (assq :out-file params))
143 (org-babel-temp-file "sql-out-")))
144 (header-delim "")
145 (command (pcase (intern engine)
146 (`dbi (format "dbish --batch %s < %s | sed '%s' > %s"
147 (or cmdline "")
148 (org-babel-process-file-name in-file)
149 "/^+/d;s/^|//;s/(NULL)/ /g;$d"
150 (org-babel-process-file-name out-file)))
151 (`monetdb (format "mclient -f tab %s < %s > %s"
152 (or cmdline "")
153 (org-babel-process-file-name in-file)
154 (org-babel-process-file-name out-file)))
155 (`mssql (format "sqlcmd %s -s \"\t\" %s -i %s -o %s"
156 (or cmdline "")
157 (org-babel-sql-dbstring-mssql
158 dbhost dbuser dbpassword database)
159 (org-babel-sql-convert-standard-filename
160 (org-babel-process-file-name in-file))
161 (org-babel-sql-convert-standard-filename
162 (org-babel-process-file-name out-file))))
163 (`mysql (format "mysql %s %s %s < %s > %s"
164 (org-babel-sql-dbstring-mysql
165 dbhost dbport dbuser dbpassword database)
166 (if colnames-p "" "-N")
167 (or cmdline "")
168 (org-babel-process-file-name in-file)
169 (org-babel-process-file-name out-file)))
170 (`postgresql (format
171 "%spsql --set=\"ON_ERROR_STOP=1\" %s -A -P \
172 footer=off -F \"\t\" %s -f %s -o %s %s"
173 (if dbpassword
174 (format "PGPASSWORD=%s " dbpassword)
176 (if colnames-p "" "-t")
177 (org-babel-sql-dbstring-postgresql
178 dbhost dbport dbuser database)
179 (org-babel-process-file-name in-file)
180 (org-babel-process-file-name out-file)
181 (or cmdline "")))
182 (`oracle (format
183 "sqlplus -s %s < %s > %s"
184 (org-babel-sql-dbstring-oracle
185 dbhost dbport dbuser dbpassword database)
186 (org-babel-process-file-name in-file)
187 (org-babel-process-file-name out-file)))
188 (_ (error "No support for the %s SQL engine" engine)))))
189 (with-temp-file in-file
190 (insert
191 (pcase (intern engine)
192 (`dbi "/format partbox\n")
193 (`oracle "SET PAGESIZE 50000
194 SET NEWPAGE 0
195 SET TAB OFF
196 SET SPACE 0
197 SET LINESIZE 9999
198 SET ECHO OFF
199 SET FEEDBACK OFF
200 SET VERIFY OFF
201 SET HEADING ON
202 SET MARKUP HTML OFF SPOOL OFF
203 SET COLSEP '|'
206 (`mssql "SET NOCOUNT ON
209 (_ ""))
210 (org-babel-expand-body:sql body params)))
211 (org-babel-eval command "")
212 (org-babel-result-cond result-params
213 (with-temp-buffer
214 (progn (insert-file-contents-literally out-file) (buffer-string)))
215 (with-temp-buffer
216 (cond
217 ((memq (intern engine) '(dbi mysql postgresql))
218 ;; Add header row delimiter after column-names header in first line
219 (cond
220 (colnames-p
221 (with-temp-buffer
222 (insert-file-contents out-file)
223 (goto-char (point-min))
224 (forward-line 1)
225 (insert "-\n")
226 (setq header-delim "-")
227 (write-file out-file)))))
229 ;; Need to figure out the delimiter for the header row
230 (with-temp-buffer
231 (insert-file-contents out-file)
232 (goto-char (point-min))
233 (when (re-search-forward "^\\(-+\\)[^-]" nil t)
234 (setq header-delim (match-string-no-properties 1)))
235 (goto-char (point-max))
236 (forward-char -1)
237 (while (looking-at "\n")
238 (delete-char 1)
239 (goto-char (point-max))
240 (forward-char -1))
241 (write-file out-file))))
242 (org-table-import out-file '(16))
243 (org-babel-reassemble-table
244 (mapcar (lambda (x)
245 (if (string= (car x) header-delim)
246 'hline
248 (org-table-to-lisp))
249 (org-babel-pick-name (cdr (assq :colname-names params))
250 (cdr (assq :colnames params)))
251 (org-babel-pick-name (cdr (assq :rowname-names params))
252 (cdr (assq :rownames params))))))))
254 (defun org-babel-sql-expand-vars (body vars)
255 "Expand the variables held in VARS in BODY."
256 (mapc
257 (lambda (pair)
258 (setq body
259 (replace-regexp-in-string
260 (format "$%s" (car pair))
261 (let ((val (cdr pair)))
262 (if (listp val)
263 (let ((data-file (org-babel-temp-file "sql-data-")))
264 (with-temp-file data-file
265 (insert (orgtbl-to-csv
266 val '(:fmt (lambda (el) (if (stringp el)
268 (format "%S" el)))))))
269 data-file)
270 (if (stringp val) val (format "%S" val))))
271 body)))
272 vars)
273 body)
275 (defun org-babel-prep-session:sql (_session _params)
276 "Raise an error because Sql sessions aren't implemented."
277 (error "SQL sessions not yet implemented"))
279 (provide 'ob-sql)
283 ;;; ob-sql.el ends here