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