ob-sqlite.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction
  2. ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://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 <http://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 (mapcar #'cdr (org-babel-get-header params :var))))
  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 (assoc :results params)) "")))
  49. (db (cdr (assoc :db params)))
  50. (separator (cdr (assoc :separator params)))
  51. (nullvalue (cdr (assoc :nullvalue params)))
  52. (headers-p (equal "yes" (cdr (assoc :colnames params))))
  53. (others (delq nil (mapcar
  54. (lambda (arg) (car (assoc arg params)))
  55. (list :header :echo :bail :column
  56. :csv :html :line :list))))
  57. exit-code)
  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. ;; FIXME: Redundancy with org-babel-sql-expand-vars!
  102. (mapc
  103. (lambda (pair)
  104. (setq body
  105. (replace-regexp-in-string
  106. (format "$%s" (car pair))
  107. (let ((val (cdr pair)))
  108. (if (listp val)
  109. (let ((data-file (org-babel-temp-file "sqlite-data-")))
  110. (with-temp-file data-file
  111. (insert (orgtbl-to-csv
  112. val '(:fmt (lambda (el) (if (stringp el)
  113. el
  114. (format "%S" el)))))))
  115. data-file)
  116. (if (stringp val) val (format "%S" val))))
  117. body)))
  118. vars)
  119. body)
  120. (defun org-babel-sqlite-table-or-scalar (result)
  121. "If RESULT looks like a trivial table, then unwrap it."
  122. (if (and (equal 1 (length result))
  123. (equal 1 (length (car result))))
  124. (org-babel-read (caar result))
  125. (mapcar (lambda (row)
  126. (if (equal 'hline row)
  127. 'hline
  128. (mapcar #'org-babel-string-read row))) result)))
  129. (defun org-babel-sqlite-offset-colnames (table headers-p)
  130. "If HEADERS-P is non-nil then offset the first row as column names."
  131. (if headers-p
  132. (cons (car table) (cons 'hline (cdr table)))
  133. table))
  134. (defun org-babel-prep-session:sqlite (session params)
  135. "Raise an error because support for SQLite sessions isn't implemented.
  136. Prepare SESSION according to the header arguments specified in PARAMS."
  137. (error "SQLite sessions not yet implemented"))
  138. (provide 'ob-sqlite)
  139. ;;; ob-sqlite.el ends here