Mercurial > emacs
annotate lisp/org/ob-sql.el @ 111668:ccb8652ef52b
The default value of `backup-by-copying-when-mismatch' is now t.
author | Michael Albinus <michael.albinus@gmx.de> |
---|---|
date | Sun, 21 Nov 2010 20:46:48 +0100 |
parents | 5cb272c831e8 |
children | a7740098b594 |
rev | line source |
---|---|
109462
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
1 ;;; ob-sql.el --- org-babel functions for sql evaluation |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
2 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
4 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
5 ;; Author: Eric Schulte |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
6 ;; Keywords: literate programming, reproducible research |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
7 ;; Homepage: http://orgmode.org |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
8 ;; Version: 7.3 |
109462
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
9 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
10 ;; This file is part of GNU Emacs. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
11 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
13 ;; it under the terms of the GNU General Public License as published by |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
15 ;; (at your option) any later version. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
16 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
17 ;; GNU Emacs is distributed in the hope that it will be useful, |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
20 ;; GNU General Public License for more details. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
21 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
22 ;; You should have received a copy of the GNU General Public License |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
24 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
25 ;;; Commentary: |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
26 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
27 ;; Org-Babel support for evaluating sql source code. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
28 ;; |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
29 ;; SQL is somewhat unique in that there are many different engines for |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
30 ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
31 ;; file will have to be implemented engine by engine. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
32 ;; |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
33 ;; Also SQL evaluation generally takes place inside of a database. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
34 ;; |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
35 ;; For now lets just allow a generic ':cmdline' header argument. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
36 ;; |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
37 ;; TODO: |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
38 ;; |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
39 ;; - support for sessions |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
40 ;; - add more useful header arguments (user, passwd, database, etc...) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
41 ;; - support for more engines (currently only supports mysql) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
42 ;; - what's a reasonable way to drop table data into SQL? |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
43 ;; |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
44 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
45 ;;; Code: |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
46 (require 'ob) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
47 (eval-when-compile (require 'cl)) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
48 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
49 (declare-function org-table-import "org-table" (file arg)) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
50 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
51 (defvar org-babel-default-header-args:sql '()) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
52 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
53 (defun org-babel-execute:sql (body params) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
54 "Execute a block of Sql code with Babel. |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
55 This function is called by `org-babel-execute-src-block'." |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
56 (let* ((result-params (cdr (assoc :result-params params))) |
109462
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
57 (cmdline (cdr (assoc :cmdline params))) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
58 (engine (cdr (assoc :engine params))) |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
59 (in-file (org-babel-temp-file "sql-in-")) |
109462
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
60 (out-file (or (cdr (assoc :out-file params)) |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
61 (org-babel-temp-file "sql-out-"))) |
109462
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
62 (command (case (intern engine) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
63 ('mysql (format "mysql %s -e \"source %s\" > %s" |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
64 (or cmdline "") |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
65 (org-babel-process-file-name in-file) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
66 (org-babel-process-file-name out-file))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
67 ('postgresql (format "psql -A -P footer=off -F \"\t\" -f %s -o %s %s" |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
68 (org-babel-process-file-name in-file) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
69 (org-babel-process-file-name out-file) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
70 (or cmdline ""))) |
109462
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
71 (t (error "no support for the %s sql engine" engine))))) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
72 (with-temp-file in-file |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
73 (insert (org-babel-expand-body:generic body params))) |
109462
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
74 (message command) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
75 (shell-command command) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
76 (with-temp-buffer |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
77 (org-table-import out-file nil) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
78 (org-babel-reassemble-table |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
79 (org-table-to-lisp) |
111506
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
80 (org-babel-pick-name (cdr (assoc :colname-names params)) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
81 (cdr (assoc :colnames params))) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
82 (org-babel-pick-name (cdr (assoc :rowname-names params)) |
5cb272c831e8
Install org-mode version 7.3
Carsten Dominik <carsten.dominik@gmail.com>
parents:
109462
diff
changeset
|
83 (cdr (assoc :rownames params))))))) |
109462
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
84 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
85 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
86 (defun org-babel-prep-session:sql (session params) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
87 "Raise an error because Sql sessions aren't implemented." |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
88 (error "sql sessions not yet implemented")) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
89 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
90 (provide 'ob-sql) |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
91 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
92 ;; arch-tag: a43ff944-6de1-4566-a83c-626814e3dad2 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
93 |
a150e8a14679
Install version 7.01 of Org-mode
Carsten Dominik <carsten.dominik@gmail.com>
parents:
diff
changeset
|
94 ;;; ob-sql.el ends here |