1 ;;; ob-sqlite.el --- Babel Functions for SQLite Databases -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2019 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://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 <https://www.gnu.org/licenses/>.
26 ;; Org-Babel support for evaluating sqlite source code.
31 (declare-function org-table-convert-region
"org-table"
32 (beg0 end0
&optional separator
))
33 (declare-function orgtbl-to-csv
"org-table" (table params
))
34 (declare-function org-table-to-lisp
"org-table" (&optional txt
))
36 (defvar org-babel-default-header-args
:sqlite
'())
38 (defvar org-babel-header-args
:sqlite
50 "Sqlite specific header args.")
52 (defun org-babel-expand-body:sqlite
(body params
)
53 "Expand BODY according to the values of PARAMS."
54 (org-babel-sqlite-expand-vars
55 body
(org-babel--get-vars params
)))
57 (defvar org-babel-sqlite3-command
"sqlite3")
59 (defun org-babel-execute:sqlite
(body params
)
60 "Execute a block of Sqlite code with Babel.
61 This function is called by `org-babel-execute-src-block'."
62 (let ((result-params (split-string (or (cdr (assq :results params
)) "")))
63 (db (cdr (assq :db params
)))
64 (separator (cdr (assq :separator params
)))
65 (nullvalue (cdr (assq :nullvalue params
)))
66 (headers-p (equal "yes" (cdr (assq :colnames params
))))
67 (others (delq nil
(mapcar
68 (lambda (arg) (car (assq arg params
)))
69 (list :header
:echo
:bail
:column
70 :csv
:html
:line
:list
)))))
71 (unless db
(error "ob-sqlite: can't evaluate without a database"))
76 "%cmd %header %separator %nullvalue %others %csv %db "
78 (cons "cmd" org-babel-sqlite3-command
)
79 (cons "header" (if headers-p
"-header" "-noheader"))
81 (if separator
(format "-separator %s" separator
) ""))
83 (if nullvalue
(format "-nullvalue %s" nullvalue
) ""))
86 (lambda (arg) (format "-%s" (substring (symbol-name arg
) 1)))
88 ;; for easy table parsing, default header type should be -csv
89 (cons "csv" (if (or (member :csv others
) (member :column others
)
90 (member :line others
) (member :list others
)
91 (member :html others
) separator
)
95 ;; body of the code block
96 (org-babel-expand-body:sqlite body params
)))
97 (org-babel-result-cond result-params
99 (if (equal (point-min) (point-max))
101 (org-table-convert-region (point-min) (point-max)
102 (if (or (member :csv others
)
103 (member :column others
)
104 (member :line others
)
105 (member :list others
)
106 (member :html others
) separator
)
109 (org-babel-sqlite-table-or-scalar
110 (org-babel-sqlite-offset-colnames
111 (org-table-to-lisp) headers-p
)))))))
113 (defun org-babel-sqlite-expand-vars (body vars
)
114 "Expand the variables held in VARS in BODY."
115 ;; FIXME: Redundancy with org-babel-sql-expand-vars!
119 (replace-regexp-in-string
120 (format "$%s" (car pair
))
121 (let ((val (cdr pair
)))
123 (let ((data-file (org-babel-temp-file "sqlite-data-")))
124 (with-temp-file data-file
125 (insert (orgtbl-to-csv val nil
)))
127 (if (stringp val
) val
(format "%S" val
))))
132 (defun org-babel-sqlite-table-or-scalar (result)
133 "If RESULT looks like a trivial table, then unwrap it."
134 (if (and (equal 1 (length result
))
135 (equal 1 (length (car result
))))
136 (org-babel-read (caar result
))
137 (mapcar (lambda (row)
140 (mapcar #'org-babel-string-read row
))) result
)))
142 (defun org-babel-sqlite-offset-colnames (table headers-p
)
143 "If HEADERS-P is non-nil then offset the first row as column names."
145 (cons (car table
) (cons 'hline
(cdr table
)))
148 (defun org-babel-prep-session:sqlite
(_session _params
)
149 "Raise an error because support for SQLite sessions isn't implemented.
150 Prepare SESSION according to the header arguments specified in PARAMS."
151 (error "SQLite sessions not yet implemented"))
157 ;;; ob-sqlite.el ends here