ob-sqlite.el 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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: 0.01
  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-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 &optional processed-params)
  32. (org-babel-sqlite-expand-vars
  33. body (or (nth 1 processed-params) (org-babel-ref-variables params))))
  34. (defvar org-babel-sqlite3-command "sqlite3")
  35. (defun org-babel-execute:sqlite (body params)
  36. "Execute a block of Sqlite code with org-babel. This function is
  37. called by `org-babel-execute-src-block'."
  38. (message "executing Sqlite source code block")
  39. (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
  40. (vars (org-babel-ref-variables 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. (shell-command-to-string
  54. (org-fill-template
  55. "%cmd -init %body %header %separator %nullvalue %others %csv %db "
  56. (list
  57. (cons "body" ((lambda (sql-file)
  58. (with-temp-file sql-file
  59. (insert (org-babel-expand-body:sqlite
  60. body nil (list nil vars))))
  61. sql-file)
  62. (make-temp-file "ob-sqlite-sql")))
  63. (cons "cmd" org-babel-sqlite3-command)
  64. (cons "header" (if headers-p "-header" "-noheader"))
  65. (cons "separator"
  66. (if separator (format "-separator %s" separator) ""))
  67. (cons "nullvalue"
  68. (if nullvalue (format "-nullvalue %s" nullvalue) ""))
  69. (cons "others"
  70. (mapconcat
  71. (lambda (arg) (format "-%s" (substring (symbol-name arg) 1)))
  72. others " "))
  73. ;; for easy table parsing, default header type should be -csv
  74. (cons "csv" (if (or (member :csv others) (member :column others)
  75. (member :line others) (member :list others)
  76. (member :html others) separator)
  77. ""
  78. "-csv"))
  79. (cons "db " db)))))
  80. (if (or (member "scalar" result-params)
  81. (member "html" result-params)
  82. (member "code" result-params)
  83. (equal (point-min) (point-max)))
  84. (buffer-string)
  85. (org-table-convert-region (point-min) (point-max))
  86. (org-babel-sqlite-table-or-scalar
  87. (org-babel-sqlite-offset-colnames
  88. (org-table-to-lisp) headers-p))))))
  89. (defun org-babel-sqlite-expand-vars (body vars)
  90. "Expand the variables held in VARS in BODY."
  91. (mapc
  92. (lambda (pair)
  93. (setq body
  94. (replace-regexp-in-string
  95. (format "\$%s" (car pair))
  96. ((lambda (val)
  97. (if (listp val)
  98. ((lambda (data-file)
  99. (with-temp-file data-file
  100. (insert (orgtbl-to-csv
  101. val '(:fmt (lambda (el) (if (stringp el)
  102. el
  103. (format "%S" el)))))))
  104. data-file)
  105. (make-temp-file "ob-sqlite-data"))
  106. (format "%S" val)))
  107. (cdr pair))
  108. body)))
  109. vars)
  110. body)
  111. (defun org-babel-sqlite-table-or-scalar (result)
  112. "If RESULT looks like a trivial table, then unwrap it."
  113. (if (and (equal 1 (length result))
  114. (equal 1 (length (car result))))
  115. (org-babel-read (caar result))
  116. (mapcar (lambda (row)
  117. (if (equal 'hline row)
  118. 'hline
  119. (mapcar #'org-babel-read row))) result)))
  120. (defun org-babel-sqlite-offset-colnames (table headers-p)
  121. "If HEADERS-P is non-nil then offset the first row as column names."
  122. (if headers-p
  123. (cons (car table) (cons 'hline (cdr table)))
  124. table))
  125. (defun org-babel-prep-session:sqlite (session params)
  126. "Prepare SESSION according to the header arguments specified in PARAMS."
  127. (error "sqlite sessions not yet implemented"))
  128. (provide 'ob-sqlite)
  129. ;; arch-tag: 5c03d7f2-0f72-48b8-bbd1-35aafea248ac
  130. ;;; ob-sqlite.el ends here