ob-sqlite.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction
  2. ;; Copyright (C) 2010-2012 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. (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-sqlite-expand-vars
  44. body (mapcar #'cdr (org-babel-get-header params :var))))
  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 (assoc :results params)) "")))
  50. (db (cdr (assoc :db params)))
  51. (separator (cdr (assoc :separator params)))
  52. (nullvalue (cdr (assoc :nullvalue params)))
  53. (headers-p (equal "yes" (cdr (assoc :colnames params))))
  54. (others (delq nil (mapcar
  55. (lambda (arg) (car (assoc arg params)))
  56. (list :header :echo :bail :column
  57. :csv :html :line :list))))
  58. exit-code)
  59. (unless db (error "ob-sqlite: can't evaluate without a database."))
  60. (with-temp-buffer
  61. (insert
  62. (org-babel-eval
  63. (org-fill-template
  64. "%cmd %header %separator %nullvalue %others %csv %db "
  65. (list
  66. (cons "cmd" org-babel-sqlite3-command)
  67. (cons "header" (if headers-p "-header" "-noheader"))
  68. (cons "separator"
  69. (if separator (format "-separator %s" separator) ""))
  70. (cons "nullvalue"
  71. (if nullvalue (format "-nullvalue %s" nullvalue) ""))
  72. (cons "others"
  73. (mapconcat
  74. (lambda (arg) (format "-%s" (substring (symbol-name arg) 1)))
  75. others " "))
  76. ;; for easy table parsing, default header type should be -csv
  77. (cons "csv" (if (or (member :csv others) (member :column others)
  78. (member :line others) (member :list others)
  79. (member :html others) separator)
  80. ""
  81. "-csv"))
  82. (cons "db " db)))
  83. ;; body of the code block
  84. (org-babel-expand-body:sqlite body params)))
  85. (if (or (member "scalar" result-params)
  86. (member "verbatim" result-params)
  87. (member "html" result-params)
  88. (member "code" result-params)
  89. (equal (point-min) (point-max)))
  90. (buffer-string)
  91. (org-table-convert-region (point-min) (point-max)
  92. (if (or (member :csv others)
  93. (member :column others)
  94. (member :line others)
  95. (member :list others)
  96. (member :html others) separator)
  97. nil
  98. '(4)))
  99. (org-babel-sqlite-table-or-scalar
  100. (org-babel-sqlite-offset-colnames
  101. (org-table-to-lisp) headers-p))))))
  102. (defun org-babel-sqlite-expand-vars (body vars)
  103. "Expand the variables held in VARS in BODY."
  104. (mapc
  105. (lambda (pair)
  106. (setq body
  107. (replace-regexp-in-string
  108. (format "\$%s" (car pair))
  109. ((lambda (val)
  110. (if (listp val)
  111. ((lambda (data-file)
  112. (with-temp-file data-file
  113. (insert (orgtbl-to-csv
  114. val '(:fmt (lambda (el) (if (stringp el)
  115. el
  116. (format "%S" el)))))))
  117. data-file)
  118. (org-babel-temp-file "sqlite-data-"))
  119. (if (stringp val) val (format "%S" val))))
  120. (cdr pair))
  121. body)))
  122. vars)
  123. body)
  124. (defun org-babel-sqlite-table-or-scalar (result)
  125. "If RESULT looks like a trivial table, then unwrap it."
  126. (if (and (equal 1 (length result))
  127. (equal 1 (length (car result))))
  128. (org-babel-read (caar result))
  129. (mapcar (lambda (row)
  130. (if (equal 'hline row)
  131. 'hline
  132. (mapcar #'org-babel-read row))) result)))
  133. (defun org-babel-sqlite-offset-colnames (table headers-p)
  134. "If HEADERS-P is non-nil then offset the first row as column names."
  135. (if headers-p
  136. (cons (car table) (cons 'hline (cdr table)))
  137. table))
  138. (defun org-babel-prep-session:sqlite (session params)
  139. "Raise an error because support for sqlite sessions isn't implemented.
  140. Prepare SESSION according to the header arguments specified in PARAMS."
  141. (error "sqlite sessions not yet implemented"))
  142. (provide 'ob-sqlite)
  143. ;;; ob-sqlite.el ends here