ob-sqlite.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction
  2. ;; Copyright (C) 2010 Free Software Foundation
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.5
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating sqlite source code.
  20. ;;; Code:
  21. (require 'ob)
  22. (require 'ob-eval)
  23. (require 'ob-ref)
  24. (declare-function org-fill-template "org" (template alist))
  25. (declare-function org-table-convert-region "org-table"
  26. (beg0 end0 &optional separator))
  27. (declare-function orgtbl-to-csv "org-table" (TABLE PARAMS))
  28. (defvar org-babel-default-header-args:sqlite '())
  29. (defvar org-babel-header-arg-names:sqlite
  30. '(db header echo bail csv column html line list separator nullvalue)
  31. "Sqlite specific header args.")
  32. (defun org-babel-expand-body:sqlite (body params)
  33. "Expand BODY according to the values of PARAMS."
  34. (org-babel-sqlite-expand-vars
  35. body (mapcar #'cdr (org-babel-get-header params :var))))
  36. (defvar org-babel-sqlite3-command "sqlite3")
  37. (defun org-babel-execute:sqlite (body params)
  38. "Execute a block of Sqlite code with Babel.
  39. This function is called by `org-babel-execute-src-block'."
  40. (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
  41. (db (cdr (assoc :db params)))
  42. (separator (cdr (assoc :separator params)))
  43. (nullvalue (cdr (assoc :nullvalue params)))
  44. (headers-p (equal "yes" (cdr (assoc :colnames params))))
  45. (others (delq nil (mapcar
  46. (lambda (arg) (car (assoc arg params)))
  47. (list :header :echo :bail :column
  48. :csv :html :line :list))))
  49. exit-code)
  50. (unless db (error "ob-sqlite: can't evaluate without a database."))
  51. (with-temp-buffer
  52. (insert
  53. (org-babel-eval
  54. (org-fill-template
  55. "%cmd %header %separator %nullvalue %others %csv %db "
  56. (list
  57. (cons "cmd" org-babel-sqlite3-command)
  58. (cons "header" (if headers-p "-header" "-noheader"))
  59. (cons "separator"
  60. (if separator (format "-separator %s" separator) ""))
  61. (cons "nullvalue"
  62. (if nullvalue (format "-nullvalue %s" nullvalue) ""))
  63. (cons "others"
  64. (mapconcat
  65. (lambda (arg) (format "-%s" (substring (symbol-name arg) 1)))
  66. others " "))
  67. ;; for easy table parsing, default header type should be -csv
  68. (cons "csv" (if (or (member :csv others) (member :column others)
  69. (member :line others) (member :list others)
  70. (member :html others) separator)
  71. ""
  72. "-csv"))
  73. (cons "db " db)))
  74. ;; body of the code block
  75. (org-babel-expand-body:sqlite body params)))
  76. (if (or (member "scalar" 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. (org-babel-sqlite-table-or-scalar
  83. (org-babel-sqlite-offset-colnames
  84. (org-table-to-lisp) headers-p))))))
  85. (defun org-babel-sqlite-expand-vars (body vars)
  86. "Expand the variables held in VARS in BODY."
  87. (mapc
  88. (lambda (pair)
  89. (setq body
  90. (replace-regexp-in-string
  91. (format "\$%s" (car pair))
  92. ((lambda (val)
  93. (if (listp val)
  94. ((lambda (data-file)
  95. (with-temp-file data-file
  96. (insert (orgtbl-to-csv
  97. val '(:fmt (lambda (el) (if (stringp el)
  98. el
  99. (format "%S" el)))))))
  100. data-file)
  101. (org-babel-temp-file "sqlite-data-"))
  102. (if (stringp val) val (format "%S" val))))
  103. (cdr pair))
  104. body)))
  105. vars)
  106. body)
  107. (defun org-babel-sqlite-table-or-scalar (result)
  108. "If RESULT looks like a trivial table, then unwrap it."
  109. (if (and (equal 1 (length result))
  110. (equal 1 (length (car result))))
  111. (org-babel-read (caar result))
  112. (mapcar (lambda (row)
  113. (if (equal 'hline row)
  114. 'hline
  115. (mapcar #'org-babel-read row))) result)))
  116. (defun org-babel-sqlite-offset-colnames (table headers-p)
  117. "If HEADERS-P is non-nil then offset the first row as column names."
  118. (if headers-p
  119. (cons (car table) (cons 'hline (cdr table)))
  120. table))
  121. (defun org-babel-prep-session:sqlite (session params)
  122. "Raise an error because support for sqlite sessions isn't implemented.
  123. Prepare SESSION according to the header arguments specified in PARAMS."
  124. (error "sqlite sessions not yet implemented"))
  125. (provide 'ob-sqlite)
  126. ;; arch-tag: 5c03d7f2-0f72-48b8-bbd1-35aafea248ac
  127. ;;; ob-sqlite.el ends here