Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction |
2 | ||
c7e9ed79 | 3 | ;; Copyright (C) 2010-2012 Free Software Foundation, Inc. |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
86fbb8ca CD |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;; Org-Babel support for evaluating sqlite source code. | |
27 | ||
28 | ;;; Code: | |
29 | (require 'ob) | |
acedf35c | 30 | (require 'ob-eval) |
86fbb8ca CD |
31 | (require 'ob-ref) |
32 | ||
33 | (declare-function org-fill-template "org" (template alist)) | |
34 | (declare-function org-table-convert-region "org-table" | |
35 | (beg0 end0 &optional separator)) | |
36 | (declare-function orgtbl-to-csv "org-table" (TABLE PARAMS)) | |
37 | ||
38 | (defvar org-babel-default-header-args:sqlite '()) | |
39 | ||
40 | (defvar org-babel-header-arg-names:sqlite | |
41 | '(db header echo bail csv column html line list separator nullvalue) | |
42 | "Sqlite specific header args.") | |
43 | ||
afe98dfa CD |
44 | (defun org-babel-expand-body:sqlite (body params) |
45 | "Expand BODY according to the values of PARAMS." | |
86fbb8ca | 46 | (org-babel-sqlite-expand-vars |
afe98dfa | 47 | body (mapcar #'cdr (org-babel-get-header params :var)))) |
86fbb8ca CD |
48 | |
49 | (defvar org-babel-sqlite3-command "sqlite3") | |
50 | ||
51 | (defun org-babel-execute:sqlite (body params) | |
52 | "Execute a block of Sqlite code with Babel. | |
53 | This function is called by `org-babel-execute-src-block'." | |
54 | (let ((result-params (split-string (or (cdr (assoc :results params)) ""))) | |
86fbb8ca CD |
55 | (db (cdr (assoc :db params))) |
56 | (separator (cdr (assoc :separator params))) | |
57 | (nullvalue (cdr (assoc :nullvalue params))) | |
58 | (headers-p (equal "yes" (cdr (assoc :colnames params)))) | |
59 | (others (delq nil (mapcar | |
60 | (lambda (arg) (car (assoc arg params))) | |
61 | (list :header :echo :bail :column | |
62 | :csv :html :line :list)))) | |
63 | exit-code) | |
64 | (unless db (error "ob-sqlite: can't evaluate without a database.")) | |
65 | (with-temp-buffer | |
66 | (insert | |
acedf35c | 67 | (org-babel-eval |
86fbb8ca | 68 | (org-fill-template |
acedf35c | 69 | "%cmd %header %separator %nullvalue %others %csv %db " |
86fbb8ca | 70 | (list |
86fbb8ca CD |
71 | (cons "cmd" org-babel-sqlite3-command) |
72 | (cons "header" (if headers-p "-header" "-noheader")) | |
73 | (cons "separator" | |
74 | (if separator (format "-separator %s" separator) "")) | |
75 | (cons "nullvalue" | |
76 | (if nullvalue (format "-nullvalue %s" nullvalue) "")) | |
77 | (cons "others" | |
78 | (mapconcat | |
79 | (lambda (arg) (format "-%s" (substring (symbol-name arg) 1))) | |
80 | others " ")) | |
81 | ;; for easy table parsing, default header type should be -csv | |
82 | (cons "csv" (if (or (member :csv others) (member :column others) | |
83 | (member :line others) (member :list others) | |
84 | (member :html others) separator) | |
85 | "" | |
86 | "-csv")) | |
acedf35c CD |
87 | (cons "db " db))) |
88 | ;; body of the code block | |
89 | (org-babel-expand-body:sqlite body params))) | |
86fbb8ca | 90 | (if (or (member "scalar" result-params) |
3ab2c837 | 91 | (member "verbatim" result-params) |
86fbb8ca CD |
92 | (member "html" result-params) |
93 | (member "code" result-params) | |
94 | (equal (point-min) (point-max))) | |
95 | (buffer-string) | |
153ae947 BG |
96 | (org-table-convert-region (point-min) (point-max) |
97 | (if (or (member :csv others) | |
98 | (member :column others) | |
99 | (member :line others) | |
100 | (member :list others) | |
101 | (member :html others) separator) | |
102 | nil | |
103 | '(4))) | |
86fbb8ca CD |
104 | (org-babel-sqlite-table-or-scalar |
105 | (org-babel-sqlite-offset-colnames | |
106 | (org-table-to-lisp) headers-p)))))) | |
107 | ||
108 | (defun org-babel-sqlite-expand-vars (body vars) | |
109 | "Expand the variables held in VARS in BODY." | |
110 | (mapc | |
111 | (lambda (pair) | |
112 | (setq body | |
113 | (replace-regexp-in-string | |
114 | (format "\$%s" (car pair)) | |
115 | ((lambda (val) | |
116 | (if (listp val) | |
117 | ((lambda (data-file) | |
118 | (with-temp-file data-file | |
119 | (insert (orgtbl-to-csv | |
120 | val '(:fmt (lambda (el) (if (stringp el) | |
121 | el | |
122 | (format "%S" el))))))) | |
123 | data-file) | |
afe98dfa CD |
124 | (org-babel-temp-file "sqlite-data-")) |
125 | (if (stringp val) val (format "%S" val)))) | |
86fbb8ca CD |
126 | (cdr pair)) |
127 | body))) | |
128 | vars) | |
129 | body) | |
130 | ||
131 | (defun org-babel-sqlite-table-or-scalar (result) | |
132 | "If RESULT looks like a trivial table, then unwrap it." | |
133 | (if (and (equal 1 (length result)) | |
134 | (equal 1 (length (car result)))) | |
135 | (org-babel-read (caar result)) | |
136 | (mapcar (lambda (row) | |
137 | (if (equal 'hline row) | |
138 | 'hline | |
139 | (mapcar #'org-babel-read row))) result))) | |
140 | ||
141 | (defun org-babel-sqlite-offset-colnames (table headers-p) | |
142 | "If HEADERS-P is non-nil then offset the first row as column names." | |
143 | (if headers-p | |
144 | (cons (car table) (cons 'hline (cdr table))) | |
145 | table)) | |
146 | ||
147 | (defun org-babel-prep-session:sqlite (session params) | |
148 | "Raise an error because support for sqlite sessions isn't implemented. | |
149 | Prepare SESSION according to the header arguments specified in PARAMS." | |
150 | (error "sqlite sessions not yet implemented")) | |
151 | ||
152 | (provide 'ob-sqlite) | |
153 | ||
5b409b39 | 154 | |
86fbb8ca CD |
155 | |
156 | ;;; ob-sqlite.el ends here |