Merge branch 'maint'
[org-mode.git] / lisp / ob-sql.el
blobad7b1e29a931fa064dd7b4b36854f167e8a6749b
1 ;;; ob-sql.el --- org-babel functions for sql evaluation
3 ;; Copyright (C) 2009-2012 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 ;; For now lets just allow a generic ':cmdline' header argument.
37 ;; TODO:
39 ;; - support for sessions
40 ;; - add more useful header arguments (user, passwd, database, etc...)
41 ;; - support for more engines (currently only supports mysql)
42 ;; - what's a reasonable way to drop table data into SQL?
45 ;;; Code:
46 (require 'ob)
47 (eval-when-compile (require 'cl))
49 (declare-function org-table-import "org-table" (file arg))
50 (declare-function orgtbl-to-csv "org-table" (TABLE PARAMS))
52 (defvar org-babel-default-header-args:sql '())
54 (defvar org-babel-header-args:sql
55 '((engine . :any)
56 (out-file . :any)))
58 (defun org-babel-expand-body:sql (body params)
59 "Expand BODY according to the values of PARAMS."
60 (org-babel-sql-expand-vars
61 body (mapcar #'cdr (org-babel-get-header params :var))))
63 (defun org-babel-execute:sql (body params)
64 "Execute a block of Sql code with Babel.
65 This function is called by `org-babel-execute-src-block'."
66 (let* ((result-params (cdr (assoc :result-params params)))
67 (cmdline (cdr (assoc :cmdline params)))
68 (engine (cdr (assoc :engine params)))
69 (in-file (org-babel-temp-file "sql-in-"))
70 (out-file (or (cdr (assoc :out-file params))
71 (org-babel-temp-file "sql-out-")))
72 (header-delim "")
73 (command (case (intern engine)
74 ('dbi (format "dbish --batch '%s' < %s | sed '%s' > %s"
75 (or cmdline "")
76 (org-babel-process-file-name in-file)
77 "/^+/d;s/^\|//;$d"
78 (org-babel-process-file-name out-file)))
79 ('monetdb (format "mclient -f tab %s < %s > %s"
80 (or cmdline "")
81 (org-babel-process-file-name in-file)
82 (org-babel-process-file-name out-file)))
83 ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
84 (or cmdline "")
85 (org-babel-process-file-name in-file)
86 (org-babel-process-file-name out-file)))
87 ('mysql (format "mysql %s < %s > %s"
88 (or cmdline "")
89 (org-babel-process-file-name in-file)
90 (org-babel-process-file-name out-file)))
91 ('postgresql (format
92 "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
93 (org-babel-process-file-name in-file)
94 (org-babel-process-file-name out-file)
95 (or cmdline "")))
96 (t (error "No support for the %s SQL engine" engine)))))
97 (with-temp-file in-file
98 (insert
99 (case (intern engine)
100 ('dbi "/format partbox\n")
101 (t ""))
102 (org-babel-expand-body:sql body params)))
103 (message command)
104 (shell-command command)
105 (if (or (member "scalar" result-params)
106 (member "verbatim" result-params)
107 (member "html" result-params)
108 (member "code" result-params)
109 (equal (point-min) (point-max)))
110 (with-temp-buffer
111 (progn (insert-file-contents-literally out-file) (buffer-string)))
112 (with-temp-buffer
113 ;; need to figure out what the delimiter is for the header row
114 (with-temp-buffer
115 (insert-file-contents out-file)
116 (goto-char (point-min))
117 (when (re-search-forward "^\\(-+\\)[^-]" nil t)
118 (setq header-delim (match-string-no-properties 1)))
119 (goto-char (point-max))
120 (forward-char -1)
121 (while (looking-at "\n")
122 (delete-char 1)
123 (goto-char (point-max))
124 (forward-char -1))
125 (write-file out-file))
126 (org-table-import out-file '(16))
127 (org-babel-reassemble-table
128 (mapcar (lambda (x)
129 (if (string= (car x) header-delim)
130 'hline
132 (org-table-to-lisp))
133 (org-babel-pick-name (cdr (assoc :colname-names params))
134 (cdr (assoc :colnames params)))
135 (org-babel-pick-name (cdr (assoc :rowname-names params))
136 (cdr (assoc :rownames params))))))))
138 (defun org-babel-sql-expand-vars (body vars)
139 "Expand the variables held in VARS in BODY."
140 (mapc
141 (lambda (pair)
142 (setq body
143 (replace-regexp-in-string
144 (format "\$%s" (car pair))
145 ((lambda (val)
146 (if (listp val)
147 ((lambda (data-file)
148 (with-temp-file data-file
149 (insert (orgtbl-to-csv
150 val '(:fmt (lambda (el) (if (stringp el)
152 (format "%S" el)))))))
153 data-file)
154 (org-babel-temp-file "sql-data-"))
155 (if (stringp val) val (format "%S" val))))
156 (cdr pair))
157 body)))
158 vars)
159 body)
161 (defun org-babel-prep-session:sql (session params)
162 "Raise an error because Sql sessions aren't implemented."
163 (error "SQL sessions not yet implemented"))
165 (provide 'ob-sql)
169 ;;; ob-sql.el ends here