ob-python: allow user choice between python.el and python-mode.el for
[org-mode.git] / lisp / ob-sqlite.el
blob5571019ef4eb9662894563bad1a0cc2754e601f7
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: 0.01
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
35 "org-table" (beg0 end0 &optional separator))
37 (defvar org-babel-default-header-args:sqlite '())
39 (defvar org-babel-header-arg-names:sqlite
40 '(db header echo bail csv column html line list separator nullvalue)
41 "Sqlite specific header args.")
43 (defun org-babel-expand-body:sqlite
44 (body params &optional processed-params) body)
46 (defvar org-babel-sqlite3-command "sqlite3")
48 (defun org-babel-execute:sqlite (body params)
49 "Execute a block of Sqlite code with org-babel. This function is
50 called by `org-babel-execute-src-block'."
51 (message "executing Sqlite source code block")
52 (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
53 (vars (org-babel-ref-variables params))
54 (db (cdr (assoc :db params)))
55 (separator (cdr (assoc :separator params)))
56 (nullvalue (cdr (assoc :nullvalue params)))
57 (headers-p (equal "yes" (cdr (assoc :colnames params))))
58 (others (delq nil (mapcar
59 (lambda (arg) (car (assoc arg params)))
60 (list :header :echo :bail :column
61 :csv :html :line :list))))
62 exit-code)
63 (message "others:%s" others)
64 (unless db (error "ob-sqlite: can't evaluate without a database."))
65 (with-temp-buffer
66 (insert
67 (shell-command-to-string
68 (org-fill-template
69 "%cmd %header %separator %nullvalue %others %csv %db %body"
70 (list
71 (cons "cmd" org-babel-sqlite3-command)
72 (cons "header" (if headers-p "-header" "-noheader"))
73 (cons "separator"
74 (if separator (format "-separator %s" separator) ""))
75 (cons "nullvalue"
76 (if nullvalue (format "-nullvalue %s" nullvalue) ""))
77 (cons "others"
78 (mapconcat
79 (lambda (arg) (format "-%s" (substring (symbol-name arg) 1)))
80 others " "))
81 ;; for easy table parsing, default header type should be -csv
82 (cons "csv" (if (or (member :csv others) (member :column others)
83 (member :line others) (member :list others)
84 (member :html others) separator)
86 "-csv"))
87 (cons "db " db)
88 (cons "body" (format "%S" (org-babel-sqlite-expand-vars
89 body vars)))))))
90 (if (or (member "scalar" result-params)
91 (member "html" result-params)
92 (member "code" result-params))
93 (buffer-string)
94 (org-table-convert-region (point-min) (point-max))
95 (org-babel-sqlite-table-or-scalar
96 (org-babel-sqlite-offset-colnames
97 (org-table-to-lisp) headers-p))))))
99 (defun org-babel-sqlite-expand-vars (body vars)
100 "Expand the variables held in VARS in BODY."
101 (mapc
102 (lambda (pair)
103 (setq body (replace-regexp-in-string
104 (format "\$%s" (car pair))
105 (format "%S" (cdr pair))
106 body)))
107 vars)
108 body)
110 (defun org-babel-sqlite-table-or-scalar (result)
111 "If RESULT looks like a trivial table, then unwrap it."
112 (if (and (equal 1 (length result))
113 (equal 1 (length (car result))))
114 (org-babel-read (caar result))
115 (mapcar (lambda (row)
116 (if (equal 'hline row)
117 'hline
118 (mapcar #'org-babel-read row))) result)))
120 (defun org-babel-sqlite-offset-colnames (table headers-p)
121 "If HEADERS-P is non-nil then offset the first row as column names."
122 (if headers-p
123 (cons (car table) (cons 'hline (cdr table)))
124 table))
126 (defun org-babel-prep-session:sqlite (session params)
127 "Prepare SESSION according to the header arguments specified in PARAMS."
128 (error "sqlite sessions not yet implemented"))
130 (provide 'ob-sqlite)
132 ;; arch-tag: 5c03d7f2-0f72-48b8-bbd1-35aafea248ac
134 ;;; ob-sqlite.el ends here