Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-sql.el
blob6488afe2ca028473aa28d96fac373fa7b30fb893
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))
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 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 user (concat "-U" user))
100 (when database (concat "-d" database))))))
102 (defun org-babel-sql-dbstring-oracle (host port user password database)
103 "Make Oracle command line args for database connection."
104 (format "%s/%s@%s:%s/%s" user password host port database))
106 (defun org-babel-execute:sql (body params)
107 "Execute a block of Sql code with Babel.
108 This function is called by `org-babel-execute-src-block'."
109 (let* ((result-params (cdr (assoc :result-params params)))
110 (cmdline (cdr (assoc :cmdline params)))
111 (dbhost (cdr (assoc :dbhost params)))
112 (dbport (cdr (assq :dbport params)))
113 (dbuser (cdr (assoc :dbuser params)))
114 (dbpassword (cdr (assoc :dbpassword params)))
115 (database (cdr (assoc :database params)))
116 (engine (cdr (assoc :engine params)))
117 (colnames-p (not (equal "no" (cdr (assoc :colnames params)))))
118 (in-file (org-babel-temp-file "sql-in-"))
119 (out-file (or (cdr (assoc :out-file params))
120 (org-babel-temp-file "sql-out-")))
121 (header-delim "")
122 (command (pcase (intern engine)
123 (`dbi (format "dbish --batch %s < %s | sed '%s' > %s"
124 (or cmdline "")
125 (org-babel-process-file-name in-file)
126 "/^+/d;s/^|//;s/(NULL)/ /g;$d"
127 (org-babel-process-file-name out-file)))
128 (`monetdb (format "mclient -f tab %s < %s > %s"
129 (or cmdline "")
130 (org-babel-process-file-name in-file)
131 (org-babel-process-file-name out-file)))
132 (`msosql (format "osql %s -s \"\t\" -i %s -o %s"
133 (or cmdline "")
134 (org-babel-process-file-name in-file)
135 (org-babel-process-file-name out-file)))
136 (`mysql (format "mysql %s %s %s < %s > %s"
137 (org-babel-sql-dbstring-mysql
138 dbhost dbport dbuser dbpassword database)
139 (if colnames-p "" "-N")
140 (or cmdline "")
141 (org-babel-process-file-name in-file)
142 (org-babel-process-file-name out-file)))
143 (`postgresql (format
144 "psql --set=\"ON_ERROR_STOP=1\" %s -A -P \
145 footer=off -F \"\t\" %s -f %s -o %s %s"
146 (if colnames-p "" "-t")
147 (org-babel-sql-dbstring-postgresql
148 dbhost dbuser database)
149 (org-babel-process-file-name in-file)
150 (org-babel-process-file-name out-file)
151 (or cmdline "")))
152 (`oracle (format
153 "sqlplus -s %s < %s > %s"
154 (org-babel-sql-dbstring-oracle
155 dbhost dbport dbuser dbpassword database)
156 (org-babel-process-file-name in-file)
157 (org-babel-process-file-name out-file)))
158 (_ (error "No support for the %s SQL engine" engine)))))
159 (with-temp-file in-file
160 (insert
161 (pcase (intern engine)
162 (`dbi "/format partbox\n")
163 (`oracle "SET PAGESIZE 50000
164 SET NEWPAGE 0
165 SET TAB OFF
166 SET SPACE 0
167 SET LINESIZE 9999
168 SET ECHO OFF
169 SET FEEDBACK OFF
170 SET VERIFY OFF
171 SET HEADING ON
172 SET MARKUP HTML OFF SPOOL OFF
173 SET COLSEP '|'
176 (_ ""))
177 (org-babel-expand-body:sql body params)))
178 (org-babel-eval command "")
179 (org-babel-result-cond result-params
180 (with-temp-buffer
181 (progn (insert-file-contents-literally out-file) (buffer-string)))
182 (with-temp-buffer
183 (cond
184 ((memq (intern engine) '(dbi mysql postgresql))
185 ;; Add header row delimiter after column-names header in first line
186 (cond
187 (colnames-p
188 (with-temp-buffer
189 (insert-file-contents out-file)
190 (goto-char (point-min))
191 (forward-line 1)
192 (insert "-\n")
193 (setq header-delim "-")
194 (write-file out-file)))))
196 ;; Need to figure out the delimiter for the header row
197 (with-temp-buffer
198 (insert-file-contents out-file)
199 (goto-char (point-min))
200 (when (re-search-forward "^\\(-+\\)[^-]" nil t)
201 (setq header-delim (match-string-no-properties 1)))
202 (goto-char (point-max))
203 (forward-char -1)
204 (while (looking-at "\n")
205 (delete-char 1)
206 (goto-char (point-max))
207 (forward-char -1))
208 (write-file out-file))))
209 (org-table-import out-file '(16))
210 (org-babel-reassemble-table
211 (mapcar (lambda (x)
212 (if (string= (car x) header-delim)
213 'hline
215 (org-table-to-lisp))
216 (org-babel-pick-name (cdr (assoc :colname-names params))
217 (cdr (assoc :colnames params)))
218 (org-babel-pick-name (cdr (assoc :rowname-names params))
219 (cdr (assoc :rownames params))))))))
221 (defun org-babel-sql-expand-vars (body vars)
222 "Expand the variables held in VARS in BODY."
223 (mapc
224 (lambda (pair)
225 (setq body
226 (replace-regexp-in-string
227 (format "$%s" (car pair))
228 (let ((val (cdr pair)))
229 (if (listp val)
230 (let ((data-file (org-babel-temp-file "sql-data-")))
231 (with-temp-file data-file
232 (insert (orgtbl-to-csv
233 val '(:fmt (lambda (el) (if (stringp el)
235 (format "%S" el)))))))
236 data-file)
237 (if (stringp val) val (format "%S" val))))
238 body)))
239 vars)
240 body)
242 (defun org-babel-prep-session:sql (_session _params)
243 "Raise an error because Sql sessions aren't implemented."
244 (error "SQL sessions not yet implemented"))
246 (provide 'ob-sql)
250 ;;; ob-sql.el ends here