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