ob-sqlite.el (5080B)
1 ;;; ob-sqlite.el --- Babel Functions for SQLite Databases -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2010-2023 Free Software Foundation, Inc. 4 5 ;; Author: Eric Schulte 6 ;; Maintainer: Nick Savage <nick@nicksavage.ca> 7 ;; Keywords: literate programming, reproducible research 8 ;; URL: https://orgmode.org 9 10 ;; This file is part of GNU Emacs. 11 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. 16 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. 21 22 ;; You should have received a copy of the GNU General Public License 23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 24 25 ;;; Commentary: 26 27 ;; Org-Babel support for evaluating sqlite source code. 28 29 ;;; Code: 30 31 (require 'org-macs) 32 (org-assert-version) 33 34 (require 'ob) 35 (require 'ob-sql) 36 37 (declare-function org-table-convert-region "org-table" 38 (beg0 end0 &optional separator)) 39 (declare-function orgtbl-to-csv "org-table" (table params)) 40 (declare-function org-table-to-lisp "org-table" (&optional txt)) 41 42 (defvar org-babel-default-header-args:sqlite '()) 43 44 (defvar org-babel-header-args:sqlite 45 '((db . :any) 46 (header . :any) 47 (echo . :any) 48 (bail . :any) 49 (csv . :any) 50 (column . :any) 51 (html . :any) 52 (line . :any) 53 (list . :any) 54 (separator . :any) 55 (nullvalue . :any)) 56 "Sqlite specific header args.") 57 58 (defun org-babel-expand-body:sqlite (body params) 59 "Expand BODY according to the values of PARAMS." 60 (org-babel-sql-expand-vars 61 body (org-babel--get-vars params) t)) 62 63 (defvar org-babel-sqlite3-command "sqlite3") 64 65 (defun org-babel-execute:sqlite (body params) 66 "Execute a block of Sqlite code with Babel. 67 This function is called by `org-babel-execute-src-block'." 68 (let ((result-params (split-string (or (cdr (assq :results params)) ""))) 69 (db (cdr (assq :db params))) 70 (separator (cdr (assq :separator params))) 71 (nullvalue (cdr (assq :nullvalue params))) 72 (headers-p (equal "yes" (cdr (assq :colnames params)))) 73 (others (delq nil (mapcar 74 (lambda (arg) (car (assq arg params))) 75 (list :header :echo :bail :column 76 :csv :html :line :list))))) 77 (unless db (error "ob-sqlite: can't evaluate without a database")) 78 (with-temp-buffer 79 (insert 80 (org-babel-eval 81 (org-fill-template 82 "%cmd %header %separator %nullvalue %others %csv %db " 83 (list 84 (cons "cmd" org-babel-sqlite3-command) 85 (cons "header" (if headers-p "-header" "-noheader")) 86 (cons "separator" 87 (if separator (format "-separator %s" separator) "")) 88 (cons "nullvalue" 89 (if nullvalue (format "-nullvalue %s" nullvalue) "")) 90 (cons "others" 91 (mapconcat 92 (lambda (arg) (format "-%s" (substring (symbol-name arg) 1))) 93 others " ")) 94 ;; for easy table parsing, default header type should be -csv 95 (cons "csv" (if (or (member :csv others) (member :column others) 96 (member :line others) (member :list others) 97 (member :html others) separator) 98 "" 99 "-csv")) 100 (cons "db " db))) 101 ;; body of the code block 102 (org-babel-expand-body:sqlite body params))) 103 (org-babel-result-cond result-params 104 (buffer-string) 105 (if (equal (point-min) (point-max)) 106 "" 107 (org-table-convert-region (point-min) (point-max) 108 (if (or (member :csv others) 109 (member :column others) 110 (member :line others) 111 (member :list others) 112 (member :html others) separator) 113 nil 114 '(4))) 115 (org-babel-sqlite-table-or-scalar 116 (org-babel-sqlite-offset-colnames 117 (org-table-to-lisp) headers-p))))))) 118 119 (defun org-babel-sqlite-expand-vars (body vars) 120 "Expand the variables held in VARS in BODY." 121 (declare (obsolete "use `org-babel-sql-expand-vars' instead." "9.5")) 122 (org-babel-sql-expand-vars body vars t)) 123 124 (defun org-babel-sqlite-table-or-scalar (result) 125 "If RESULT looks like a trivial table, then unwrap it." 126 (if (and (equal 1 (length result)) 127 (equal 1 (length (car result)))) 128 (org-babel-read (caar result) t) 129 (mapcar (lambda (row) 130 (if (eq 'hline row) 131 'hline 132 (mapcar #'org-babel-sqlite--read-cell row))) 133 result))) 134 135 (defun org-babel-sqlite-offset-colnames (table headers-p) 136 "If HEADERS-P is non-nil then offset the first row as column names." 137 (if headers-p 138 (cons (car table) (cons 'hline (cdr table))) 139 table)) 140 141 (defun org-babel-prep-session:sqlite (_session _params) 142 "Raise an error because support for SQLite sessions isn't implemented. 143 Prepare SESSION according to the header arguments specified in PARAMS." 144 (error "SQLite sessions not yet implemented")) 145 146 (defun org-babel-sqlite--read-cell (cell) 147 "Process CELL to remove unnecessary characters." 148 (org-babel-read cell t)) 149 150 (provide 'ob-sqlite) 151 152 ;;; ob-sqlite.el ends here