ob-sqlite.el 5.2 KB

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