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