Merge branch 'maint'
[org-mode.git] / lisp / ob-sql.el
blob7b85df8a153c7168a098976d07392a0517841317
1 ;;; ob-sql.el --- org-babel functions for sql evaluation
3 ;; Copyright (C) 2009-2014 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 ;; - dbuser
40 ;; - dbpassword
41 ;; - database
42 ;; - colnames (default, nil, means "yes")
43 ;; - result-params
44 ;; - out-file
45 ;; The following are used but not really implemented for SQL:
46 ;; - colname-names
47 ;; - rownames
48 ;; - rowname-names
50 ;; TODO:
52 ;; - support for sessions
53 ;; - support for more engines (currently only supports mysql)
54 ;; - what's a reasonable way to drop table data into SQL?
57 ;;; Code:
58 (require 'ob)
59 (eval-when-compile (require 'cl))
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))
65 (defvar org-babel-default-header-args:sql '())
67 (defconst org-babel-header-args:sql
68 '((engine . :any)
69 (out-file . :any)
70 (dbhost . :any)
71 (dbuser . :any)
72 (dbpassword . :any)
73 (database . :any))
74 "SQL-specific header arguments.")
76 (defun org-babel-expand-body:sql (body params)
77 "Expand BODY according to the values of PARAMS."
78 (org-babel-sql-expand-vars
79 body (mapcar #'cdr (org-babel-get-header params :var))))
81 (defun dbstring-mysql (host user password database)
82 "Make MySQL cmd line args for database connection. Pass nil to omit that arg."
83 (combine-and-quote-strings
84 (remq nil
85 (list (when host (concat "-h" host))
86 (when user (concat "-u" user))
87 (when password (concat "-p" password))
88 (when database (concat "-D" database))))))
90 (defun org-babel-execute:sql (body params)
91 "Execute a block of Sql code with Babel.
92 This function is called by `org-babel-execute-src-block'."
93 (let* ((result-params (cdr (assoc :result-params params)))
94 (cmdline (cdr (assoc :cmdline params)))
95 (dbhost (cdr (assoc :dbhost params)))
96 (dbuser (cdr (assoc :dbuser params)))
97 (dbpassword (cdr (assoc :dbpassword params)))
98 (database (cdr (assoc :database params)))
99 (engine (cdr (assoc :engine params)))
100 (colnames-p (not (equal "no" (cdr (assoc :colnames params)))))
101 (in-file (org-babel-temp-file "sql-in-"))
102 (out-file (or (cdr (assoc :out-file params))
103 (org-babel-temp-file "sql-out-")))
104 (header-delim "")
105 (command (case (intern engine)
106 ('dbi (format "dbish --batch %s < %s | sed '%s' > %s"
107 (or cmdline "")
108 (org-babel-process-file-name in-file)
109 "/^+/d;s/^\|//;s/(NULL)/ /g;$d"
110 (org-babel-process-file-name out-file)))
111 ('monetdb (format "mclient -f tab %s < %s > %s"
112 (or cmdline "")
113 (org-babel-process-file-name in-file)
114 (org-babel-process-file-name out-file)))
115 ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
116 (or cmdline "")
117 (org-babel-process-file-name in-file)
118 (org-babel-process-file-name out-file)))
119 ('mysql (format "mysql %s %s %s < %s > %s"
120 (dbstring-mysql dbhost dbuser dbpassword database)
121 (if colnames-p "" "-N")
122 (or cmdline "")
123 (org-babel-process-file-name in-file)
124 (org-babel-process-file-name out-file)))
125 ('postgresql (format
126 "psql --set=\"ON_ERROR_STOP=1\" %s -A -P footer=off -F \"\t\" -f %s -o %s %s"
127 (if colnames-p "" "-t")
128 (org-babel-process-file-name in-file)
129 (org-babel-process-file-name out-file)
130 (or cmdline "")))
131 (t (error "No support for the %s SQL engine" engine)))))
132 (with-temp-file in-file
133 (insert
134 (case (intern engine)
135 ('dbi "/format partbox\n")
136 (t ""))
137 (org-babel-expand-body:sql body params)))
138 (message command)
139 (org-babel-eval command "")
140 (org-babel-result-cond result-params
141 (with-temp-buffer
142 (progn (insert-file-contents-literally out-file) (buffer-string)))
143 (with-temp-buffer
144 (cond
145 ((or (eq (intern engine) 'mysql)
146 (eq (intern engine) 'dbi)
147 (eq (intern engine) 'postgresql))
148 ;; Add header row delimiter after column-names header in first line
149 (cond
150 (colnames-p
151 (with-temp-buffer
152 (insert-file-contents out-file)
153 (goto-char (point-min))
154 (forward-line 1)
155 (insert "-\n")
156 (setq header-delim "-")
157 (write-file out-file)))))
159 ;; Need to figure out the delimiter for the header row
160 (with-temp-buffer
161 (insert-file-contents out-file)
162 (goto-char (point-min))
163 (when (re-search-forward "^\\(-+\\)[^-]" nil t)
164 (setq header-delim (match-string-no-properties 1)))
165 (goto-char (point-max))
166 (forward-char -1)
167 (while (looking-at "\n")
168 (delete-char 1)
169 (goto-char (point-max))
170 (forward-char -1))
171 (write-file out-file))))
172 (org-table-import out-file '(16))
173 (org-babel-reassemble-table
174 (mapcar (lambda (x)
175 (if (string= (car x) header-delim)
176 'hline
178 (org-table-to-lisp))
179 (org-babel-pick-name (cdr (assoc :colname-names params))
180 (cdr (assoc :colnames params)))
181 (org-babel-pick-name (cdr (assoc :rowname-names params))
182 (cdr (assoc :rownames params))))))))
184 (defun org-babel-sql-expand-vars (body vars)
185 "Expand the variables held in VARS in BODY."
186 (mapc
187 (lambda (pair)
188 (setq body
189 (replace-regexp-in-string
190 (format "\$%s" (car pair)) ;FIXME: "\$" == "$"!
191 (let ((val (cdr pair)))
192 (if (listp val)
193 (let ((data-file (org-babel-temp-file "sql-data-")))
194 (with-temp-file data-file
195 (insert (orgtbl-to-csv
196 val '(:fmt (lambda (el) (if (stringp el)
198 (format "%S" el)))))))
199 data-file)
200 (if (stringp val) val (format "%S" val))))
201 body)))
202 vars)
203 body)
205 (defun org-babel-prep-session:sql (session params)
206 "Raise an error because Sql sessions aren't implemented."
207 (error "SQL sessions not yet implemented"))
209 (provide 'ob-sql)
213 ;;; ob-sql.el ends here