1 ;;; ob-sqlite.el --- Babel Functions for SQLite Databases -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2018 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-fill-template
"org" (template alist
))
32 (declare-function org-table-convert-region
"org-table"
33 (beg0 end0
&optional separator
))
34 (declare-function orgtbl-to-csv
"org-table" (table params
))
35 (declare-function org-table-to-lisp
"org-table" (&optional txt
))
37 (defvar org-babel-default-header-args
:sqlite
'())
39 (defvar org-babel-header-args
:sqlite
51 "Sqlite specific header args.")
53 (defun org-babel-expand-body:sqlite
(body params
)
54 "Expand BODY according to the values of PARAMS."
55 (org-babel-sqlite-expand-vars
56 body
(org-babel--get-vars params
)))
58 (defvar org-babel-sqlite3-command
"sqlite3")
60 (defun org-babel-execute:sqlite
(body params
)
61 "Execute a block of Sqlite code with Babel.
62 This function is called by `org-babel-execute-src-block'."
63 (let ((result-params (split-string (or (cdr (assq :results params
)) "")))
64 (db (cdr (assq :db params
)))
65 (separator (cdr (assq :separator params
)))
66 (nullvalue (cdr (assq :nullvalue params
)))
67 (headers-p (equal "yes" (cdr (assq :colnames params
))))
68 (others (delq nil
(mapcar
69 (lambda (arg) (car (assq arg params
)))
70 (list :header
:echo
:bail
:column
71 :csv
:html
:line
:list
)))))
72 (unless db
(error "ob-sqlite: can't evaluate without a database"))
77 "%cmd %header %separator %nullvalue %others %csv %db "
79 (cons "cmd" org-babel-sqlite3-command
)
80 (cons "header" (if headers-p
"-header" "-noheader"))
82 (if separator
(format "-separator %s" separator
) ""))
84 (if nullvalue
(format "-nullvalue %s" nullvalue
) ""))
87 (lambda (arg) (format "-%s" (substring (symbol-name arg
) 1)))
89 ;; for easy table parsing, default header type should be -csv
90 (cons "csv" (if (or (member :csv others
) (member :column others
)
91 (member :line others
) (member :list others
)
92 (member :html others
) separator
)
96 ;; body of the code block
97 (org-babel-expand-body:sqlite body params
)))
98 (org-babel-result-cond result-params
100 (if (equal (point-min) (point-max))
102 (org-table-convert-region (point-min) (point-max)
103 (if (or (member :csv others
)
104 (member :column others
)
105 (member :line others
)
106 (member :list others
)
107 (member :html others
) separator
)
110 (org-babel-sqlite-table-or-scalar
111 (org-babel-sqlite-offset-colnames
112 (org-table-to-lisp) headers-p
)))))))
114 (defun org-babel-sqlite-expand-vars (body vars
)
115 "Expand the variables held in VARS in BODY."
116 ;; FIXME: Redundancy with org-babel-sql-expand-vars!
120 (replace-regexp-in-string
121 (format "$%s" (car pair
))
122 (let ((val (cdr pair
)))
124 (let ((data-file (org-babel-temp-file "sqlite-data-")))
125 (with-temp-file data-file
126 (insert (orgtbl-to-csv val nil
)))
128 (if (stringp val
) val
(format "%S" val
))))
133 (defun org-babel-sqlite-table-or-scalar (result)
134 "If RESULT looks like a trivial table, then unwrap it."
135 (if (and (equal 1 (length result
))
136 (equal 1 (length (car result
))))
137 (org-babel-read (caar result
))
138 (mapcar (lambda (row)
141 (mapcar #'org-babel-string-read row
))) result
)))
143 (defun org-babel-sqlite-offset-colnames (table headers-p
)
144 "If HEADERS-P is non-nil then offset the first row as column names."
146 (cons (car table
) (cons 'hline
(cdr table
)))
149 (defun org-babel-prep-session:sqlite
(_session _params
)
150 "Raise an error because support for SQLite sessions isn't implemented.
151 Prepare SESSION according to the header arguments specified in PARAMS."
152 (error "SQLite sessions not yet implemented"))
158 ;;; ob-sqlite.el ends here