ob-sqlite.el 5.1 KB

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