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