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/>.
26 ;; Org-Babel support for evaluating sqlite source code.
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
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
))))
74 (unless db
(error "ob-sqlite: can't evaluate without a database."))
79 "%cmd %header %separator %nullvalue %others %csv %db "
81 (cons "cmd" org-babel-sqlite3-command
)
82 (cons "header" (if headers-p
"-header" "-noheader"))
84 (if separator
(format "-separator %s" separator
) ""))
86 (if nullvalue
(format "-nullvalue %s" nullvalue
) ""))
89 (lambda (arg) (format "-%s" (substring (symbol-name arg
) 1)))
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
)
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)))
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
)
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."
123 (replace-regexp-in-string
124 (format "\$%s" (car pair
))
128 (with-temp-file data-file
129 (insert (orgtbl-to-csv
130 val
'(:fmt
(lambda (el) (if (stringp el
)
132 (format "%S" el
)))))))
134 (org-babel-temp-file "sqlite-data-"))
135 (if (stringp val
) val
(format "%S" val
))))
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
)
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."
154 (cons (car table
) (cons 'hline
(cdr 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"))
166 ;;; ob-sqlite.el ends here