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.6
  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 "verbatim" result-params)
  78. (member "html" result-params)
  79. (member "code" result-params)
  80. (equal (point-min) (point-max)))
  81. (buffer-string)
  82. (org-table-convert-region (point-min) (point-max))
  83. (org-babel-sqlite-table-or-scalar
  84. (org-babel-sqlite-offset-colnames
  85. (org-table-to-lisp) headers-p))))))
  86. (defun org-babel-sqlite-expand-vars (body vars)
  87. "Expand the variables held in VARS in BODY."
  88. (mapc
  89. (lambda (pair)
  90. (setq body
  91. (replace-regexp-in-string
  92. (format "\$%s" (car pair))
  93. ((lambda (val)
  94. (if (listp val)
  95. ((lambda (data-file)
  96. (with-temp-file data-file
  97. (insert (orgtbl-to-csv
  98. val '(:fmt (lambda (el) (if (stringp el)
  99. el
  100. (format "%S" el)))))))
  101. data-file)
  102. (org-babel-temp-file "sqlite-data-"))
  103. (if (stringp val) val (format "%S" val))))
  104. (cdr pair))
  105. body)))
  106. vars)
  107. body)
  108. (defun org-babel-sqlite-table-or-scalar (result)
  109. "If RESULT looks like a trivial table, then unwrap it."
  110. (if (and (equal 1 (length result))
  111. (equal 1 (length (car result))))
  112. (org-babel-read (caar result))
  113. (mapcar (lambda (row)
  114. (if (equal 'hline row)
  115. 'hline
  116. (mapcar #'org-babel-read row))) result)))
  117. (defun org-babel-sqlite-offset-colnames (table headers-p)
  118. "If HEADERS-P is non-nil then offset the first row as column names."
  119. (if headers-p
  120. (cons (car table) (cons 'hline (cdr table)))
  121. table))
  122. (defun org-babel-prep-session:sqlite (session params)
  123. "Raise an error because support for sqlite sessions isn't implemented.
  124. Prepare SESSION according to the header arguments specified in PARAMS."
  125. (error "sqlite sessions not yet implemented"))
  126. (provide 'ob-sqlite)
  127. ;; arch-tag: 5c03d7f2-0f72-48b8-bbd1-35aafea248ac
  128. ;;; ob-sqlite.el ends here