ob-sqlite.el 5.1 KB

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