Release 7.4
[org-mode/org-tableheadings.git] / lisp / ob-sql.el
blob5bb123d631bf7e07184b63707b085ecd2e969ddd
1 ;;; ob-sql.el --- org-babel functions for sql evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.4
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating sql source code.
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?
43 ;;
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 (defun org-babel-expand-body:sql (body params)
55 "Expand BODY according to the values of PARAMS."
56 (org-babel-sql-expand-vars
57 body (mapcar #'cdr (org-babel-get-header params :var))))
59 (defun org-babel-execute:sql (body params)
60 "Execute a block of Sql code with Babel.
61 This function is called by `org-babel-execute-src-block'."
62 (let* ((result-params (cdr (assoc :result-params params)))
63 (cmdline (cdr (assoc :cmdline params)))
64 (engine (cdr (assoc :engine params)))
65 (in-file (org-babel-temp-file "sql-in-"))
66 (out-file (or (cdr (assoc :out-file params))
67 (org-babel-temp-file "sql-out-")))
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 -e \"source %s\" > %s"
74 (or cmdline "")
75 (org-babel-process-file-name in-file)
76 (org-babel-process-file-name out-file)))
77 ('postgresql (format "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
78 (org-babel-process-file-name in-file)
79 (org-babel-process-file-name out-file)
80 (or cmdline "")))
81 (t (error "no support for the %s sql engine" engine)))))
82 (with-temp-file in-file
83 (insert (org-babel-expand-body:sql body params)))
84 (message command)
85 (shell-command command)
86 (with-temp-buffer
87 (org-table-import out-file '(16))
88 (org-babel-reassemble-table
89 (org-table-to-lisp)
90 (org-babel-pick-name (cdr (assoc :colname-names params))
91 (cdr (assoc :colnames params)))
92 (org-babel-pick-name (cdr (assoc :rowname-names params))
93 (cdr (assoc :rownames params)))))))
95 (defun org-babel-sql-expand-vars (body vars)
96 "Expand the variables held in VARS in BODY."
97 (mapc
98 (lambda (pair)
99 (setq body
100 (replace-regexp-in-string
101 (format "\$%s" (car pair))
102 ((lambda (val)
103 (if (listp val)
104 ((lambda (data-file)
105 (with-temp-file data-file
106 (insert (orgtbl-to-csv
107 val '(:fmt (lambda (el) (if (stringp el)
109 (format "%S" el)))))))
110 data-file)
111 (org-babel-temp-file "sql-data-"))
112 (if (stringp val) val (format "%S" val))))
113 (cdr pair))
114 body)))
115 vars)
116 body)
118 (defun org-babel-prep-session:sql (session params)
119 "Raise an error because Sql sessions aren't implemented."
120 (error "sql sessions not yet implemented"))
122 (provide 'ob-sql)
124 ;; arch-tag: a43ff944-6de1-4566-a83c-626814e3dad2
126 ;;; ob-sql.el ends here