org-texi: Fix typo
[org-mode/org-tableheadings.git] / lisp / ob-sql.el
blob493b3dc1828095bc8a11a438e162215c52bb92de
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 org-babel-sql-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 (delq 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-sql-dbstring-postgresql (host user database)
91 "Make PostgreSQL command line args for database connection.
92 Pass nil to omit that arg."
93 (combine-and-quote-strings
94 (delq nil
95 (list (when host (concat "-h" host))
96 (when user (concat "-U" user))
97 (when database (concat "-d" database))))))
99 (defun org-babel-execute:sql (body params)
100 "Execute a block of Sql code with Babel.
101 This function is called by `org-babel-execute-src-block'."
102 (let* ((result-params (cdr (assoc :result-params params)))
103 (cmdline (cdr (assoc :cmdline params)))
104 (dbhost (cdr (assoc :dbhost params)))
105 (dbuser (cdr (assoc :dbuser params)))
106 (dbpassword (cdr (assoc :dbpassword params)))
107 (database (cdr (assoc :database params)))
108 (engine (cdr (assoc :engine params)))
109 (colnames-p (not (equal "no" (cdr (assoc :colnames params)))))
110 (in-file (org-babel-temp-file "sql-in-"))
111 (out-file (or (cdr (assoc :out-file params))
112 (org-babel-temp-file "sql-out-")))
113 (header-delim "")
114 (command (case (intern engine)
115 ('dbi (format "dbish --batch %s < %s | sed '%s' > %s"
116 (or cmdline "")
117 (org-babel-process-file-name in-file)
118 "/^+/d;s/^\|//;s/(NULL)/ /g;$d"
119 (org-babel-process-file-name out-file)))
120 ('monetdb (format "mclient -f tab %s < %s > %s"
121 (or cmdline "")
122 (org-babel-process-file-name in-file)
123 (org-babel-process-file-name out-file)))
124 ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
125 (or cmdline "")
126 (org-babel-process-file-name in-file)
127 (org-babel-process-file-name out-file)))
128 ('mysql (format "mysql %s %s %s < %s > %s"
129 (org-babel-sql-dbstring-mysql dbhost dbuser dbpassword database)
130 (if colnames-p "" "-N")
131 (or cmdline "")
132 (org-babel-process-file-name in-file)
133 (org-babel-process-file-name out-file)))
134 ('postgresql (format
135 "psql --set=\"ON_ERROR_STOP=1\" %s -A -P footer=off -F \"\t\" %s -f %s -o %s %s"
136 (if colnames-p "" "-t")
137 (org-babel-sql-dbstring-postgresql dbhost dbuser database)
138 (org-babel-process-file-name in-file)
139 (org-babel-process-file-name out-file)
140 (or cmdline "")))
141 (t (error "No support for the %s SQL engine" engine)))))
142 (with-temp-file in-file
143 (insert
144 (case (intern engine)
145 ('dbi "/format partbox\n")
146 (t ""))
147 (org-babel-expand-body:sql body params)))
148 (message command)
149 (org-babel-eval command "")
150 (org-babel-result-cond result-params
151 (with-temp-buffer
152 (progn (insert-file-contents-literally out-file) (buffer-string)))
153 (with-temp-buffer
154 (cond
155 ((or (eq (intern engine) 'mysql)
156 (eq (intern engine) 'dbi)
157 (eq (intern engine) 'postgresql))
158 ;; Add header row delimiter after column-names header in first line
159 (cond
160 (colnames-p
161 (with-temp-buffer
162 (insert-file-contents out-file)
163 (goto-char (point-min))
164 (forward-line 1)
165 (insert "-\n")
166 (setq header-delim "-")
167 (write-file out-file)))))
169 ;; Need to figure out the delimiter for the header row
170 (with-temp-buffer
171 (insert-file-contents out-file)
172 (goto-char (point-min))
173 (when (re-search-forward "^\\(-+\\)[^-]" nil t)
174 (setq header-delim (match-string-no-properties 1)))
175 (goto-char (point-max))
176 (forward-char -1)
177 (while (looking-at "\n")
178 (delete-char 1)
179 (goto-char (point-max))
180 (forward-char -1))
181 (write-file out-file))))
182 (org-table-import out-file '(16))
183 (org-babel-reassemble-table
184 (mapcar (lambda (x)
185 (if (string= (car x) header-delim)
186 'hline
188 (org-table-to-lisp))
189 (org-babel-pick-name (cdr (assoc :colname-names params))
190 (cdr (assoc :colnames params)))
191 (org-babel-pick-name (cdr (assoc :rowname-names params))
192 (cdr (assoc :rownames params))))))))
194 (defun org-babel-sql-expand-vars (body vars)
195 "Expand the variables held in VARS in BODY."
196 (mapc
197 (lambda (pair)
198 (setq body
199 (replace-regexp-in-string
200 (format "\$%s" (car pair)) ;FIXME: "\$" == "$"!
201 (let ((val (cdr pair)))
202 (if (listp val)
203 (let ((data-file (org-babel-temp-file "sql-data-")))
204 (with-temp-file data-file
205 (insert (orgtbl-to-csv
206 val '(:fmt (lambda (el) (if (stringp el)
208 (format "%S" el)))))))
209 data-file)
210 (if (stringp val) val (format "%S" val))))
211 body)))
212 vars)
213 body)
215 (defun org-babel-prep-session:sql (session params)
216 "Raise an error because Sql sessions aren't implemented."
217 (error "SQL sessions not yet implemented"))
219 (provide 'ob-sql)
223 ;;; ob-sql.el ends here