babel: remove all language file reference to define language variables
[org-mode.git] / lisp / babel / langs / ob-sql.el
blobf48f1a577fdd43373fef0e0036d215140b9c9c44
1 ;;; ob-sql.el --- org-babel functions for sql evaluation
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Org-Babel support for evaluating sql source code.
31 ;; SQL is somewhat unique in that there are many different engines for
32 ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this
33 ;; file will have to be implemented engine by engine.
35 ;; Also SQL evaluation generally takes place inside of a database.
37 ;; For now lets just allow a generic ':cmdline' header argument.
39 ;; TODO:
41 ;; - support for sessions
42 ;; - add more useful header arguments (user, passwd, database, etc...)
43 ;; - support for more engines (currently only supports mysql)
44 ;; - what's a reasonable way to drop table data into SQL?
45 ;;
47 ;;; Code:
48 (require 'ob)
50 (defun org-babel-expand-body:sql (body params &optional processed-params)
51 "Expand BODY according to PARAMS, return the expanded body." body)
53 (defun org-babel-execute:sql (body params)
54 "Execute a block of Sql code with org-babel. This function is
55 called by `org-babel-execute-src-block'."
56 (message "executing Sql source code block")
57 (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
58 (cmdline (cdr (assoc :cmdline params)))
59 (engine (cdr (assoc :engine params)))
60 (in-file (make-temp-file "org-babel-sql-in"))
61 (out-file (or (cdr (assoc :out-file params))
62 (make-temp-file "org-babel-sql-out")))
63 (command (case (intern engine)
64 ('mysql (format "mysql %s -e \"source %s\" > %s"
65 (or cmdline "") in-file out-file))
66 (t (error "no support for the %s sql engine")))))
67 (with-temp-file in-file
68 (insert (org-babel-expand-body:sql body params)))
69 (message command)
70 (shell-command command)
71 (with-temp-buffer
72 (org-table-import out-file nil)
73 (org-babel-reassemble-table
74 (org-table-to-lisp)
75 (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
76 (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
79 (defun org-babel-prep-session:sql (session params)
80 "Prepare SESSION according to the header arguments specified in PARAMS."
81 (error "sql sessions not yet implemented"))
83 (provide 'ob-sql)
84 ;;; ob-sql.el ends here