ob-sqlite.el 5.0 KB

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