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