org.el: Small docstring clean-up
[org-mode.git] / lisp / ob-sqlite.el
blob2b685da6cbd914ab8fc4e748c7bf5259245ba709
1 ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating sqlite source code.
28 ;;; Code:
29 (require 'ob)
30 (require 'ob-eval)
31 (require 'ob-ref)
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))
38 (defvar org-babel-default-header-args:sqlite '())
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))
52 "Sqlite specific header args.")
54 (defun org-babel-expand-body:sqlite (body params)
55 "Expand BODY according to the values of PARAMS."
56 (org-babel-sqlite-expand-vars
57 body (mapcar #'cdr (org-babel-get-header params :var))))
59 (defvar org-babel-sqlite3-command "sqlite3")
61 (defun org-babel-execute:sqlite (body params)
62 "Execute a block of Sqlite code with Babel.
63 This function is called by `org-babel-execute-src-block'."
64 (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
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)
74 (unless db (error "ob-sqlite: can't evaluate without a database."))
75 (with-temp-buffer
76 (insert
77 (org-babel-eval
78 (org-fill-template
79 "%cmd %header %separator %nullvalue %others %csv %db "
80 (list
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)
96 "-csv"))
97 (cons "db " db)))
98 ;; body of the code block
99 (org-babel-expand-body:sqlite body params)))
100 (if (or (member "scalar" result-params)
101 (member "verbatim" result-params)
102 (member "html" result-params)
103 (member "code" result-params)
104 (equal (point-min) (point-max)))
105 (buffer-string)
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)
113 '(4)))
114 (org-babel-sqlite-table-or-scalar
115 (org-babel-sqlite-offset-colnames
116 (org-table-to-lisp) headers-p))))))
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)
132 (format "%S" el)))))))
133 data-file)
134 (org-babel-temp-file "sqlite-data-"))
135 (if (stringp val) val (format "%S" val))))
136 (cdr pair))
137 body)))
138 vars)
139 body)
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)))
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))
157 (defun org-babel-prep-session:sqlite (session params)
158 "Raise an error because support for SQLite sessions isn't implemented.
159 Prepare SESSION according to the header arguments specified in PARAMS."
160 (error "SQLite sessions not yet implemented"))
162 (provide 'ob-sqlite)
166 ;;; ob-sqlite.el ends here