Release 7.01e
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-sql.el
blobc14ef761adc296506891cb142cd564174376cebf
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.01e
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))
51 (defvar org-babel-default-header-args:sql '())
53 (defun org-babel-expand-body:sql (body params &optional processed-params)
54 "Expand BODY according to PARAMS, return the expanded body." body)
56 (defun org-babel-execute:sql (body params)
57 "Execute a block of Sql code with Babel.
58 This function is called by `org-babel-execute-src-block'."
59 (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
60 (processed-params (org-babel-process-params params))
61 (cmdline (cdr (assoc :cmdline params)))
62 (engine (cdr (assoc :engine params)))
63 (in-file (make-temp-file "org-babel-sql-in"))
64 (out-file (or (cdr (assoc :out-file params))
65 (make-temp-file "org-babel-sql-out")))
66 (command (case (intern engine)
67 ('mysql (format "mysql %s -e \"source %s\" > %s"
68 (or cmdline "") in-file out-file))
69 (t (error "no support for the %s sql engine" engine)))))
70 (with-temp-file in-file
71 (insert (org-babel-expand-body:sql body params)))
72 (message command)
73 (shell-command command)
74 (with-temp-buffer
75 (org-table-import out-file nil)
76 (org-babel-reassemble-table
77 (org-table-to-lisp)
78 (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
79 (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
82 (defun org-babel-prep-session:sql (session params)
83 "Raise an error because Sql sessions aren't implemented."
84 (error "sql sessions not yet implemented"))
86 (provide 'ob-sql)
88 ;; arch-tag: a43ff944-6de1-4566-a83c-626814e3dad2
90 ;;; ob-sql.el ends here