Fix Org ChangeLog entries and remove arch-tag.
[bpt/emacs.git] / lisp / org / ob-sqlite.el
1 ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction
2
3 ;; Copyright (C) 2010 Free Software Foundation
4
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.7
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Org-Babel support for evaluating sqlite source code.
28
29 ;;; Code:
30 (require 'ob)
31 (require 'ob-eval)
32 (require 'ob-ref)
33
34 (declare-function org-fill-template "org" (template alist))
35 (declare-function org-table-convert-region "org-table"
36 (beg0 end0 &optional separator))
37 (declare-function orgtbl-to-csv "org-table" (TABLE PARAMS))
38
39 (defvar org-babel-default-header-args:sqlite '())
40
41 (defvar org-babel-header-arg-names:sqlite
42 '(db header echo bail csv column html line list separator nullvalue)
43 "Sqlite specific header args.")
44
45 (defun org-babel-expand-body:sqlite (body params)
46 "Expand BODY according to the values of PARAMS."
47 (org-babel-sqlite-expand-vars
48 body (mapcar #'cdr (org-babel-get-header params :var))))
49
50 (defvar org-babel-sqlite3-command "sqlite3")
51
52 (defun org-babel-execute:sqlite (body params)
53 "Execute a block of Sqlite code with Babel.
54 This function is called by `org-babel-execute-src-block'."
55 (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
56 (db (cdr (assoc :db params)))
57 (separator (cdr (assoc :separator params)))
58 (nullvalue (cdr (assoc :nullvalue params)))
59 (headers-p (equal "yes" (cdr (assoc :colnames params))))
60 (others (delq nil (mapcar
61 (lambda (arg) (car (assoc arg params)))
62 (list :header :echo :bail :column
63 :csv :html :line :list))))
64 exit-code)
65 (unless db (error "ob-sqlite: can't evaluate without a database."))
66 (with-temp-buffer
67 (insert
68 (org-babel-eval
69 (org-fill-template
70 "%cmd %header %separator %nullvalue %others %csv %db "
71 (list
72 (cons "cmd" org-babel-sqlite3-command)
73 (cons "header" (if headers-p "-header" "-noheader"))
74 (cons "separator"
75 (if separator (format "-separator %s" separator) ""))
76 (cons "nullvalue"
77 (if nullvalue (format "-nullvalue %s" nullvalue) ""))
78 (cons "others"
79 (mapconcat
80 (lambda (arg) (format "-%s" (substring (symbol-name arg) 1)))
81 others " "))
82 ;; for easy table parsing, default header type should be -csv
83 (cons "csv" (if (or (member :csv others) (member :column others)
84 (member :line others) (member :list others)
85 (member :html others) separator)
86 ""
87 "-csv"))
88 (cons "db " db)))
89 ;; body of the code block
90 (org-babel-expand-body:sqlite body params)))
91 (if (or (member "scalar" result-params)
92 (member "verbatim" result-params)
93 (member "html" result-params)
94 (member "code" result-params)
95 (equal (point-min) (point-max)))
96 (buffer-string)
97 (org-table-convert-region (point-min) (point-max))
98 (org-babel-sqlite-table-or-scalar
99 (org-babel-sqlite-offset-colnames
100 (org-table-to-lisp) headers-p))))))
101
102 (defun org-babel-sqlite-expand-vars (body vars)
103 "Expand the variables held in VARS in BODY."
104 (mapc
105 (lambda (pair)
106 (setq body
107 (replace-regexp-in-string
108 (format "\$%s" (car pair))
109 ((lambda (val)
110 (if (listp val)
111 ((lambda (data-file)
112 (with-temp-file data-file
113 (insert (orgtbl-to-csv
114 val '(:fmt (lambda (el) (if (stringp el)
115 el
116 (format "%S" el)))))))
117 data-file)
118 (org-babel-temp-file "sqlite-data-"))
119 (if (stringp val) val (format "%S" val))))
120 (cdr pair))
121 body)))
122 vars)
123 body)
124
125 (defun org-babel-sqlite-table-or-scalar (result)
126 "If RESULT looks like a trivial table, then unwrap it."
127 (if (and (equal 1 (length result))
128 (equal 1 (length (car result))))
129 (org-babel-read (caar result))
130 (mapcar (lambda (row)
131 (if (equal 'hline row)
132 'hline
133 (mapcar #'org-babel-read row))) result)))
134
135 (defun org-babel-sqlite-offset-colnames (table headers-p)
136 "If HEADERS-P is non-nil then offset the first row as column names."
137 (if headers-p
138 (cons (car table) (cons 'hline (cdr table)))
139 table))
140
141 (defun org-babel-prep-session:sqlite (session params)
142 "Raise an error because support for sqlite sessions isn't implemented.
143 Prepare SESSION according to the header arguments specified in PARAMS."
144 (error "sqlite sessions not yet implemented"))
145
146 (provide 'ob-sqlite)
147
148
149
150 ;;; ob-sqlite.el ends here