lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / ob-sqlite.el
blob7522c8361fa719e23f198fed7f6f321863a336a8
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/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating sqlite source code.
28 ;;; Code:
29 (require 'ob)
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
39 '((db . :any)
40 (header . :any)
41 (echo . :any)
42 (bail . :any)
43 (csv . :any)
44 (column . :any)
45 (html . :any)
46 (line . :any)
47 (list . :any)
48 (separator . :any)
49 (nullvalue . :any))
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"))
72 (with-temp-buffer
73 (insert
74 (org-babel-eval
75 (org-fill-template
76 "%cmd %header %separator %nullvalue %others %csv %db "
77 (list
78 (cons "cmd" org-babel-sqlite3-command)
79 (cons "header" (if headers-p "-header" "-noheader"))
80 (cons "separator"
81 (if separator (format "-separator %s" separator) ""))
82 (cons "nullvalue"
83 (if nullvalue (format "-nullvalue %s" nullvalue) ""))
84 (cons "others"
85 (mapconcat
86 (lambda (arg) (format "-%s" (substring (symbol-name arg) 1)))
87 others " "))
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)
93 "-csv"))
94 (cons "db " db)))
95 ;; body of the code block
96 (org-babel-expand-body:sqlite body params)))
97 (org-babel-result-cond result-params
98 (buffer-string)
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)
108 '(4)))
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!
116 (mapc
117 (lambda (pair)
118 (setq body
119 (replace-regexp-in-string
120 (format "$%s" (car pair))
121 (let ((val (cdr pair)))
122 (if (listp val)
123 (let ((data-file (org-babel-temp-file "sqlite-data-")))
124 (with-temp-file data-file
125 (insert (orgtbl-to-csv val nil)))
126 data-file)
127 (if (stringp val) val (format "%S" val))))
128 body)))
129 vars)
130 body)
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)
138 (if (eq 'hline row)
139 'hline
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."
144 (if headers-p
145 (cons (car table) (cons 'hline (cdr table)))
146 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"))
153 (provide 'ob-sqlite)
157 ;;; ob-sqlite.el ends here