* ob-vbnet.el: Org-babel functions for VB.Net evaluation
[org-mode/org-tableheadings.git] / lisp / ob-sql.el
blobfdf73a0612fba09fab49436626be51df49363004
1 ;;; ob-sql.el --- Babel Functions for SQL -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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)
60 (eval-when-compile (require 'cl))
62 (declare-function org-table-import "org-table" (file arg))
63 (declare-function orgtbl-to-csv "org-table" (table params))
64 (declare-function org-table-to-lisp "org-table" (&optional txt))
65 (declare-function cygwin-convert-file-name-to-windows "cygw32.c" (file &optional absolute-p))
67 (defvar org-babel-default-header-args:sql '())
69 (defconst org-babel-header-args:sql
70 '((engine . :any)
71 (out-file . :any)
72 (dbhost . :any)
73 (dbport . :any)
74 (dbuser . :any)
75 (dbpassword . :any)
76 (database . :any))
77 "SQL-specific header arguments.")
79 (defun org-babel-expand-body:sql (body params)
80 "Expand BODY according to the values of PARAMS."
81 (org-babel-sql-expand-vars
82 body (org-babel--get-vars params)))
84 (defun org-babel-sql-dbstring-mysql (host port user password database)
85 "Make MySQL cmd line args for database connection. Pass nil to omit that arg."
86 (combine-and-quote-strings
87 (delq nil
88 (list (when host (concat "-h" host))
89 (when port (format "-P%d" port))
90 (when user (concat "-u" user))
91 (when password (concat "-p" password))
92 (when database (concat "-D" database))))))
94 (defun org-babel-sql-dbstring-postgresql (host user database)
95 "Make PostgreSQL command line args for database connection.
96 Pass nil to omit that arg."
97 (combine-and-quote-strings
98 (delq nil
99 (list (when host (concat "-h" host))
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 (assoc :result-params params)))
133 (cmdline (cdr (assoc :cmdline params)))
134 (dbhost (cdr (assoc :dbhost params)))
135 (dbport (cdr (assq :dbport params)))
136 (dbuser (cdr (assoc :dbuser params)))
137 (dbpassword (cdr (assoc :dbpassword params)))
138 (database (cdr (assoc :database params)))
139 (engine (cdr (assoc :engine params)))
140 (colnames-p (not (equal "no" (cdr (assoc :colnames params)))))
141 (in-file (org-babel-temp-file "sql-in-"))
142 (out-file (or (cdr (assoc :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 "psql --set=\"ON_ERROR_STOP=1\" %s -A -P \
172 footer=off -F \"\t\" %s -f %s -o %s %s"
173 (if colnames-p "" "-t")
174 (org-babel-sql-dbstring-postgresql
175 dbhost dbuser database)
176 (org-babel-process-file-name in-file)
177 (org-babel-process-file-name out-file)
178 (or cmdline "")))
179 (`oracle (format
180 "sqlplus -s %s < %s > %s"
181 (org-babel-sql-dbstring-oracle
182 dbhost dbport dbuser dbpassword database)
183 (org-babel-process-file-name in-file)
184 (org-babel-process-file-name out-file)))
185 (_ (error "No support for the %s SQL engine" engine)))))
186 (with-temp-file in-file
187 (insert
188 (pcase (intern engine)
189 (`dbi "/format partbox\n")
190 (`oracle "SET PAGESIZE 50000
191 SET NEWPAGE 0
192 SET TAB OFF
193 SET SPACE 0
194 SET LINESIZE 9999
195 SET ECHO OFF
196 SET FEEDBACK OFF
197 SET VERIFY OFF
198 SET HEADING ON
199 SET MARKUP HTML OFF SPOOL OFF
200 SET COLSEP '|'
203 (`mssql "SET NOCOUNT ON
206 (_ ""))
207 (org-babel-expand-body:sql body params)))
208 (org-babel-eval command "")
209 (org-babel-result-cond result-params
210 (with-temp-buffer
211 (progn (insert-file-contents-literally out-file) (buffer-string)))
212 (with-temp-buffer
213 (cond
214 ((memq (intern engine) '(dbi mysql postgresql))
215 ;; Add header row delimiter after column-names header in first line
216 (cond
217 (colnames-p
218 (with-temp-buffer
219 (insert-file-contents out-file)
220 (goto-char (point-min))
221 (forward-line 1)
222 (insert "-\n")
223 (setq header-delim "-")
224 (write-file out-file)))))
226 ;; Need to figure out the delimiter for the header row
227 (with-temp-buffer
228 (insert-file-contents out-file)
229 (goto-char (point-min))
230 (when (re-search-forward "^\\(-+\\)[^-]" nil t)
231 (setq header-delim (match-string-no-properties 1)))
232 (goto-char (point-max))
233 (forward-char -1)
234 (while (looking-at "\n")
235 (delete-char 1)
236 (goto-char (point-max))
237 (forward-char -1))
238 (write-file out-file))))
239 (org-table-import out-file '(16))
240 (org-babel-reassemble-table
241 (mapcar (lambda (x)
242 (if (string= (car x) header-delim)
243 'hline
245 (org-table-to-lisp))
246 (org-babel-pick-name (cdr (assoc :colname-names params))
247 (cdr (assoc :colnames params)))
248 (org-babel-pick-name (cdr (assoc :rowname-names params))
249 (cdr (assoc :rownames params))))))))
251 (defun org-babel-sql-expand-vars (body vars)
252 "Expand the variables held in VARS in BODY."
253 (mapc
254 (lambda (pair)
255 (setq body
256 (replace-regexp-in-string
257 (format "$%s" (car pair))
258 (let ((val (cdr pair)))
259 (if (listp val)
260 (let ((data-file (org-babel-temp-file "sql-data-")))
261 (with-temp-file data-file
262 (insert (orgtbl-to-csv
263 val '(:fmt (lambda (el) (if (stringp el)
265 (format "%S" el)))))))
266 data-file)
267 (if (stringp val) val (format "%S" val))))
268 body)))
269 vars)
270 body)
272 (defun org-babel-prep-session:sql (_session _params)
273 "Raise an error because Sql sessions aren't implemented."
274 (error "SQL sessions not yet implemented"))
276 (provide 'ob-sql)
280 ;;; ob-sql.el ends here