org.el: tiny docstring fix.
[org-mode.git] / lisp / ob-sql.el
blobc52614815272abf911d1feaafdc57a52a3016eff
1 ;;; ob-sql.el --- org-babel functions for sql evaluation
3 ;; Copyright (C) 2009-2011 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.
28 ;; SQL is somewhat unique in that there are many different engines for
29 ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this
30 ;; file will have to be implemented engine by engine.
32 ;; Also SQL evaluation generally takes place inside of a database.
34 ;; For now lets just allow a generic ':cmdline' header argument.
36 ;; TODO:
38 ;; - support for sessions
39 ;; - add more useful header arguments (user, passwd, database, etc...)
40 ;; - support for more engines (currently only supports mysql)
41 ;; - what's a reasonable way to drop table data into SQL?
42 ;;
44 ;;; Code:
45 (require 'ob)
46 (eval-when-compile (require 'cl))
48 (declare-function org-table-import "org-table" (file arg))
49 (declare-function orgtbl-to-csv "org-table" (TABLE PARAMS))
51 (defvar org-babel-default-header-args:sql '())
53 (defun org-babel-expand-body:sql (body params)
54 "Expand BODY according to the values of PARAMS."
55 (org-babel-sql-expand-vars
56 body (mapcar #'cdr (org-babel-get-header params :var))))
58 (defun org-babel-execute:sql (body params)
59 "Execute a block of Sql code with Babel.
60 This function is called by `org-babel-execute-src-block'."
61 (let* ((result-params (cdr (assoc :result-params params)))
62 (cmdline (cdr (assoc :cmdline params)))
63 (engine (cdr (assoc :engine params)))
64 (in-file (org-babel-temp-file "sql-in-"))
65 (out-file (or (cdr (assoc :out-file params))
66 (org-babel-temp-file "sql-out-")))
67 (header-delim "")
68 (command (case (intern engine)
69 ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
70 (or cmdline "")
71 (org-babel-process-file-name in-file)
72 (org-babel-process-file-name out-file)))
73 ('mysql (format "mysql %s < %s > %s"
74 (or cmdline "")
75 (org-babel-process-file-name in-file)
76 (org-babel-process-file-name out-file)))
77 ('postgresql (format
78 "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
79 (org-babel-process-file-name in-file)
80 (org-babel-process-file-name out-file)
81 (or cmdline "")))
82 (t (error "no support for the %s sql engine" engine)))))
83 (with-temp-file in-file
84 (insert (org-babel-expand-body:sql body params)))
85 (message command)
86 (shell-command command)
87 (with-temp-buffer
88 ;; need to figure out what the delimiter is for the header row
89 (with-temp-buffer
90 (insert-file-contents out-file)
91 (goto-char (point-min))
92 (when (re-search-forward "^\\(-+\\)[^-]" nil t)
93 (setq header-delim (match-string-no-properties 1)))
94 (goto-char (point-max))
95 (forward-char -1)
96 (while (looking-at "\n")
97 (delete-char 1)
98 (goto-char (point-max))
99 (forward-char -1))
100 (write-file out-file))
101 (org-table-import out-file '(16))
102 (org-babel-reassemble-table
103 (mapcar (lambda (x)
104 (if (string= (car x) header-delim)
105 'hline
107 (org-table-to-lisp))
108 (org-babel-pick-name (cdr (assoc :colname-names params))
109 (cdr (assoc :colnames params)))
110 (org-babel-pick-name (cdr (assoc :rowname-names params))
111 (cdr (assoc :rownames params)))))))
113 (defun org-babel-sql-expand-vars (body vars)
114 "Expand the variables held in VARS in BODY."
115 (mapc
116 (lambda (pair)
117 (setq body
118 (replace-regexp-in-string
119 (format "\$%s" (car pair))
120 ((lambda (val)
121 (if (listp val)
122 ((lambda (data-file)
123 (with-temp-file data-file
124 (insert (orgtbl-to-csv
125 val '(:fmt (lambda (el) (if (stringp el)
127 (format "%S" el)))))))
128 data-file)
129 (org-babel-temp-file "sql-data-"))
130 (if (stringp val) val (format "%S" val))))
131 (cdr pair))
132 body)))
133 vars)
134 body)
136 (defun org-babel-prep-session:sql (session params)
137 "Raise an error because Sql sessions aren't implemented."
138 (error "sql sessions not yet implemented"))
140 (provide 'ob-sql)
144 ;;; ob-sql.el ends here