Release 7.3
[org-mode/org-jambu.git] / lisp / ob-sqlite.el
blobc744e4334ecd62a4783df492f11b772ff86b8cda
1 ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction
3 ;; Copyright (C) 2010 Free Software Foundation
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.3
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating sqlite source code.
29 ;;; Code:
30 (require 'ob)
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-arg-names:sqlite
41 '(db header echo bail csv column html line list separator nullvalue)
42 "Sqlite specific header args.")
44 (defun org-babel-expand-body:sqlite (body params)
45 "Expand BODY according to the values of PARAMS."
46 (org-babel-sqlite-expand-vars
47 body (mapcar #'cdr (org-babel-get-header params :var))))
49 (defvar org-babel-sqlite3-command "sqlite3")
51 (defun org-babel-execute:sqlite (body params)
52 "Execute a block of Sqlite code with Babel.
53 This function is called by `org-babel-execute-src-block'."
54 (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
55 (vars (org-babel-get-header params :var))
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 (shell-command-to-string
69 (org-fill-template
70 "%cmd -init %body %header %separator %nullvalue %others %csv %db "
71 (list
72 (cons "body" ((lambda (sql-file)
73 (with-temp-file sql-file
74 (insert (org-babel-expand-body:sqlite body params)))
75 sql-file)
76 (org-babel-temp-file "sqlite-sql-")))
77 (cons "cmd" org-babel-sqlite3-command)
78 (cons "header" (if headers-p "-header" "-noheader"))
79 (cons "separator"
80 (if separator (format "-separator %s" separator) ""))
81 (cons "nullvalue"
82 (if nullvalue (format "-nullvalue %s" nullvalue) ""))
83 (cons "others"
84 (mapconcat
85 (lambda (arg) (format "-%s" (substring (symbol-name arg) 1)))
86 others " "))
87 ;; for easy table parsing, default header type should be -csv
88 (cons "csv" (if (or (member :csv others) (member :column others)
89 (member :line others) (member :list others)
90 (member :html others) separator)
92 "-csv"))
93 (cons "db " db)))))
94 (if (or (member "scalar" result-params)
95 (member "html" result-params)
96 (member "code" result-params)
97 (equal (point-min) (point-max)))
98 (buffer-string)
99 (org-table-convert-region (point-min) (point-max))
100 (org-babel-sqlite-table-or-scalar
101 (org-babel-sqlite-offset-colnames
102 (org-table-to-lisp) headers-p))))))
104 (defun org-babel-sqlite-expand-vars (body vars)
105 "Expand the variables held in VARS in BODY."
106 (mapc
107 (lambda (pair)
108 (setq body
109 (replace-regexp-in-string
110 (format "\$%s" (car pair))
111 ((lambda (val)
112 (if (listp val)
113 ((lambda (data-file)
114 (with-temp-file data-file
115 (insert (orgtbl-to-csv
116 val '(:fmt (lambda (el) (if (stringp el)
118 (format "%S" el)))))))
119 data-file)
120 (org-babel-temp-file "sqlite-data-"))
121 (if (stringp val) val (format "%S" val))))
122 (cdr pair))
123 body)))
124 vars)
125 body)
127 (defun org-babel-sqlite-table-or-scalar (result)
128 "If RESULT looks like a trivial table, then unwrap it."
129 (if (and (equal 1 (length result))
130 (equal 1 (length (car result))))
131 (org-babel-read (caar result))
132 (mapcar (lambda (row)
133 (if (equal 'hline row)
134 'hline
135 (mapcar #'org-babel-read row))) result)))
137 (defun org-babel-sqlite-offset-colnames (table headers-p)
138 "If HEADERS-P is non-nil then offset the first row as column names."
139 (if headers-p
140 (cons (car table) (cons 'hline (cdr table)))
141 table))
143 (defun org-babel-prep-session:sqlite (session params)
144 "Raise an error because support for sqlite sessions isn't implemented.
145 Prepare SESSION according to the header arguments specified in PARAMS."
146 (error "sqlite sessions not yet implemented"))
148 (provide 'ob-sqlite)
150 ;; arch-tag: 5c03d7f2-0f72-48b8-bbd1-35aafea248ac
152 ;;; ob-sqlite.el ends here