ob-sqlite.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: 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 'ob)
  22. (require 'ob-sql)
  23. (declare-function org-table-convert-region "org-table"
  24. (beg0 end0 &optional separator))
  25. (declare-function orgtbl-to-csv "org-table" (table params))
  26. (declare-function org-table-to-lisp "org-table" (&optional txt))
  27. (defvar org-babel-default-header-args:sqlite '())
  28. (defvar org-babel-header-args:sqlite
  29. '((db . :any)
  30. (header . :any)
  31. (echo . :any)
  32. (bail . :any)
  33. (csv . :any)
  34. (column . :any)
  35. (html . :any)
  36. (line . :any)
  37. (list . :any)
  38. (separator . :any)
  39. (nullvalue . :any))
  40. "Sqlite specific header args.")
  41. (defun org-babel-expand-body:sqlite (body params)
  42. "Expand BODY according to the values of PARAMS."
  43. (org-babel-sql-expand-vars
  44. body (org-babel--get-vars params) t))
  45. (defvar org-babel-sqlite3-command "sqlite3")
  46. (defun org-babel-execute:sqlite (body params)
  47. "Execute a block of Sqlite code with Babel.
  48. This function is called by `org-babel-execute-src-block'."
  49. (let ((result-params (split-string (or (cdr (assq :results params)) "")))
  50. (db (cdr (assq :db params)))
  51. (separator (cdr (assq :separator params)))
  52. (nullvalue (cdr (assq :nullvalue params)))
  53. (headers-p (equal "yes" (cdr (assq :colnames params))))
  54. (others (delq nil (mapcar
  55. (lambda (arg) (car (assq arg params)))
  56. (list :header :echo :bail :column
  57. :csv :html :line :list)))))
  58. (unless db (error "ob-sqlite: can't evaluate without a database"))
  59. (with-temp-buffer
  60. (insert
  61. (org-babel-eval
  62. (org-fill-template
  63. "%cmd %header %separator %nullvalue %others %csv %db "
  64. (list
  65. (cons "cmd" org-babel-sqlite3-command)
  66. (cons "header" (if headers-p "-header" "-noheader"))
  67. (cons "separator"
  68. (if separator (format "-separator %s" separator) ""))
  69. (cons "nullvalue"
  70. (if nullvalue (format "-nullvalue %s" nullvalue) ""))
  71. (cons "others"
  72. (mapconcat
  73. (lambda (arg) (format "-%s" (substring (symbol-name arg) 1)))
  74. others " "))
  75. ;; for easy table parsing, default header type should be -csv
  76. (cons "csv" (if (or (member :csv others) (member :column others)
  77. (member :line others) (member :list others)
  78. (member :html others) separator)
  79. ""
  80. "-csv"))
  81. (cons "db " db)))
  82. ;; body of the code block
  83. (org-babel-expand-body:sqlite body params)))
  84. (org-babel-result-cond result-params
  85. (buffer-string)
  86. (if (equal (point-min) (point-max))
  87. ""
  88. (org-table-convert-region (point-min) (point-max)
  89. (if (or (member :csv others)
  90. (member :column others)
  91. (member :line others)
  92. (member :list others)
  93. (member :html others) separator)
  94. nil
  95. '(4)))
  96. (org-babel-sqlite-table-or-scalar
  97. (org-babel-sqlite-offset-colnames
  98. (org-table-to-lisp) headers-p)))))))
  99. (defun org-babel-sqlite-expand-vars (body vars)
  100. "Expand the variables held in VARS in BODY."
  101. (declare (obsolete "use `org-babel-sql-expand-vars' instead." "9.5"))
  102. (org-babel-sql-expand-vars body vars t))
  103. (defun org-babel-sqlite-table-or-scalar (result)
  104. "If RESULT looks like a trivial table, then unwrap it."
  105. (if (and (equal 1 (length result))
  106. (equal 1 (length (car result))))
  107. (org-babel-read (caar result) t)
  108. (mapcar (lambda (row)
  109. (if (eq 'hline row)
  110. 'hline
  111. (mapcar #'org-babel-sqlite--read-cell row)))
  112. result)))
  113. (defun org-babel-sqlite-offset-colnames (table headers-p)
  114. "If HEADERS-P is non-nil then offset the first row as column names."
  115. (if headers-p
  116. (cons (car table) (cons 'hline (cdr table)))
  117. table))
  118. (defun org-babel-prep-session:sqlite (_session _params)
  119. "Raise an error because support for SQLite sessions isn't implemented.
  120. Prepare SESSION according to the header arguments specified in PARAMS."
  121. (error "SQLite sessions not yet implemented"))
  122. (defun org-babel-sqlite--read-cell (cell)
  123. "Process CELL to remove unnecessary characters."
  124. (org-babel-read cell t))
  125. (provide 'ob-sqlite)
  126. ;;; ob-sqlite.el ends here