ob-sqlite.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction
  2. ;; Copyright (C) 2010-2013 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. (require 'ob-eval)
  22. (require 'ob-ref)
  23. (declare-function org-fill-template "org" (template alist))
  24. (declare-function org-table-convert-region "org-table"
  25. (beg0 end0 &optional separator))
  26. (declare-function orgtbl-to-csv "org-table" (table params))
  27. (declare-function org-table-to-lisp "org-table" (&optional txt))
  28. (defvar org-babel-default-header-args:sqlite '())
  29. (defvar org-babel-header-args:sqlite
  30. '((db . :any)
  31. (header . :any)
  32. (echo . :any)
  33. (bail . :any)
  34. (csv . :any)
  35. (column . :any)
  36. (html . :any)
  37. (line . :any)
  38. (list . :any)
  39. (separator . :any)
  40. (nullvalue . :any))
  41. "Sqlite specific header args.")
  42. (defun org-babel-expand-body:sqlite (body params)
  43. "Expand BODY according to the values of PARAMS."
  44. (org-babel-sqlite-expand-vars
  45. body (mapcar #'cdr (org-babel-get-header params :var))))
  46. (defvar org-babel-sqlite3-command "sqlite3")
  47. (defun org-babel-execute:sqlite (body params)
  48. "Execute a block of Sqlite code with Babel.
  49. This function is called by `org-babel-execute-src-block'."
  50. (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
  51. (db (cdr (assoc :db params)))
  52. (separator (cdr (assoc :separator params)))
  53. (nullvalue (cdr (assoc :nullvalue params)))
  54. (headers-p (equal "yes" (cdr (assoc :colnames params))))
  55. (others (delq nil (mapcar
  56. (lambda (arg) (car (assoc arg params)))
  57. (list :header :echo :bail :column
  58. :csv :html :line :list))))
  59. exit-code)
  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. (if (or (member "scalar" result-params)
  87. (member "verbatim" result-params)
  88. (member "html" result-params)
  89. (member "code" result-params)
  90. (equal (point-min) (point-max)))
  91. (buffer-string)
  92. (org-table-convert-region (point-min) (point-max)
  93. (if (or (member :csv others)
  94. (member :column others)
  95. (member :line others)
  96. (member :list others)
  97. (member :html others) separator)
  98. nil
  99. '(4)))
  100. (org-babel-sqlite-table-or-scalar
  101. (org-babel-sqlite-offset-colnames
  102. (org-table-to-lisp) headers-p))))))
  103. (defun org-babel-sqlite-expand-vars (body vars)
  104. "Expand the variables held in VARS in BODY."
  105. (mapc
  106. (lambda (pair)
  107. (setq body
  108. (replace-regexp-in-string
  109. (format "\$%s" (car pair))
  110. ((lambda (val)
  111. (if (listp val)
  112. ((lambda (data-file)
  113. (with-temp-file data-file
  114. (insert (orgtbl-to-csv
  115. val '(:fmt (lambda (el) (if (stringp el)
  116. el
  117. (format "%S" el)))))))
  118. data-file)
  119. (org-babel-temp-file "sqlite-data-"))
  120. (if (stringp val) val (format "%S" val))))
  121. (cdr pair))
  122. body)))
  123. vars)
  124. body)
  125. (defun org-babel-sqlite-table-or-scalar (result)
  126. "If RESULT looks like a trivial table, then unwrap it."
  127. (if (and (equal 1 (length result))
  128. (equal 1 (length (car result))))
  129. (org-babel-read (caar result))
  130. (mapcar (lambda (row)
  131. (if (equal 'hline row)
  132. 'hline
  133. (mapcar #'org-babel-read row))) result)))
  134. (defun org-babel-sqlite-offset-colnames (table headers-p)
  135. "If HEADERS-P is non-nil then offset the first row as column names."
  136. (if headers-p
  137. (cons (car table) (cons 'hline (cdr table)))
  138. table))
  139. (defun org-babel-prep-session:sqlite (session params)
  140. "Raise an error because support for SQLite sessions isn't implemented.
  141. Prepare SESSION according to the header arguments specified in PARAMS."
  142. (error "SQLite sessions not yet implemented"))
  143. (provide 'ob-sqlite)
  144. ;;; ob-sqlite.el ends here