org-export: Fix :input-file property
[org-mode.git] / lisp / ob-sql.el
blob20a136a80fb3410d56fcdfe30293dd21e40ba331
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))
51 (declare-function org-table-to-lisp "org-table" (&optional txt))
53 (defvar org-babel-default-header-args:sql '())
55 (defvar org-babel-header-args:sql
56 '((engine . :any)
57 (out-file . :any)))
59 (defun org-babel-expand-body:sql (body params)
60 "Expand BODY according to the values of PARAMS."
61 (org-babel-sql-expand-vars
62 body (mapcar #'cdr (org-babel-get-header params :var))))
64 (defun org-babel-execute:sql (body params)
65 "Execute a block of Sql code with Babel.
66 This function is called by `org-babel-execute-src-block'."
67 (let* ((result-params (cdr (assoc :result-params params)))
68 (cmdline (cdr (assoc :cmdline params)))
69 (engine (cdr (assoc :engine params)))
70 (in-file (org-babel-temp-file "sql-in-"))
71 (out-file (or (cdr (assoc :out-file params))
72 (org-babel-temp-file "sql-out-")))
73 (header-delim "")
74 (command (case (intern engine)
75 ('dbi (format "dbish --batch '%s' < %s | sed '%s' > %s"
76 (or cmdline "")
77 (org-babel-process-file-name in-file)
78 "/^+/d;s/^\|//;$d"
79 (org-babel-process-file-name out-file)))
80 ('monetdb (format "mclient -f tab %s < %s > %s"
81 (or cmdline "")
82 (org-babel-process-file-name in-file)
83 (org-babel-process-file-name out-file)))
84 ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
85 (or cmdline "")
86 (org-babel-process-file-name in-file)
87 (org-babel-process-file-name out-file)))
88 ('mysql (format "mysql %s < %s > %s"
89 (or cmdline "")
90 (org-babel-process-file-name in-file)
91 (org-babel-process-file-name out-file)))
92 ('postgresql (format
93 "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
94 (org-babel-process-file-name in-file)
95 (org-babel-process-file-name out-file)
96 (or cmdline "")))
97 (t (error "No support for the %s SQL engine" engine)))))
98 (with-temp-file in-file
99 (insert
100 (case (intern engine)
101 ('dbi "/format partbox\n")
102 (t ""))
103 (org-babel-expand-body:sql body params)))
104 (message command)
105 (shell-command command)
106 (if (or (member "scalar" result-params)
107 (member "verbatim" result-params)
108 (member "html" result-params)
109 (member "code" result-params)
110 (equal (point-min) (point-max)))
111 (with-temp-buffer
112 (progn (insert-file-contents-literally out-file) (buffer-string)))
113 (with-temp-buffer
114 ;; need to figure out what the delimiter is for the header row
115 (with-temp-buffer
116 (insert-file-contents out-file)
117 (goto-char (point-min))
118 (when (re-search-forward "^\\(-+\\)[^-]" nil t)
119 (setq header-delim (match-string-no-properties 1)))
120 (goto-char (point-max))
121 (forward-char -1)
122 (while (looking-at "\n")
123 (delete-char 1)
124 (goto-char (point-max))
125 (forward-char -1))
126 (write-file out-file))
127 (org-table-import out-file '(16))
128 (org-babel-reassemble-table
129 (mapcar (lambda (x)
130 (if (string= (car x) header-delim)
131 'hline
133 (org-table-to-lisp))
134 (org-babel-pick-name (cdr (assoc :colname-names params))
135 (cdr (assoc :colnames params)))
136 (org-babel-pick-name (cdr (assoc :rowname-names params))
137 (cdr (assoc :rownames params))))))))
139 (defun org-babel-sql-expand-vars (body vars)
140 "Expand the variables held in VARS in BODY."
141 (mapc
142 (lambda (pair)
143 (setq body
144 (replace-regexp-in-string
145 (format "\$%s" (car pair))
146 ((lambda (val)
147 (if (listp val)
148 ((lambda (data-file)
149 (with-temp-file data-file
150 (insert (orgtbl-to-csv
151 val '(:fmt (lambda (el) (if (stringp el)
153 (format "%S" el)))))))
154 data-file)
155 (org-babel-temp-file "sql-data-"))
156 (if (stringp val) val (format "%S" val))))
157 (cdr pair))
158 body)))
159 vars)
160 body)
162 (defun org-babel-prep-session:sql (session params)
163 "Raise an error because Sql sessions aren't implemented."
164 (error "SQL sessions not yet implemented"))
166 (provide 'ob-sql)
170 ;;; ob-sql.el ends here