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