ob-sqlite.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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
  25. "org-table" (beg0 end0 &optional separator))
  26. (defvar org-babel-default-header-args:sqlite '())
  27. (defvar org-babel-header-arg-names:sqlite
  28. '(db header echo bail csv column html line list separator nullvalue)
  29. "Sqlite specific header args.")
  30. (defun org-babel-expand-body:sqlite
  31. (body params &optional processed-params) body)
  32. (defvar org-babel-sqlite3-command "sqlite3")
  33. (defun org-babel-execute:sqlite (body params)
  34. "Execute a block of Sqlite code with org-babel. This function is
  35. called by `org-babel-execute-src-block'."
  36. (message "executing Sqlite source code block")
  37. (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
  38. (vars (org-babel-ref-variables params))
  39. (db (cdr (assoc :db params)))
  40. (separator (cdr (assoc :separator params)))
  41. (nullvalue (cdr (assoc :nullvalue params)))
  42. (headers-p (equal "yes" (cdr (assoc :colnames params))))
  43. (others (delq nil (mapcar
  44. (lambda (arg) (car (assoc arg params)))
  45. (list :header :echo :bail :column
  46. :csv :html :line :list))))
  47. exit-code)
  48. (message "others:%s" others)
  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 %header %separator %nullvalue %others %csv %db %body"
  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. (cons "body" (format "%S" (org-babel-sqlite-expand-vars
  74. body vars)))))))
  75. (if (or (member "scalar" result-params)
  76. (member "html" result-params)
  77. (member "code" result-params))
  78. (buffer-string)
  79. (org-table-convert-region (point-min) (point-max))
  80. (org-babel-sqlite-table-or-scalar
  81. (org-babel-sqlite-offset-colnames
  82. (org-table-to-lisp) headers-p))))))
  83. (defun org-babel-sqlite-expand-vars (body vars)
  84. "Expand the variables held in VARS in BODY."
  85. (mapc
  86. (lambda (pair)
  87. (setq body (replace-regexp-in-string
  88. (format "\$%s" (car pair))
  89. (format "%S" (cdr pair))
  90. body)))
  91. vars)
  92. body)
  93. (defun org-babel-sqlite-table-or-scalar (result)
  94. "If RESULT looks like a trivial table, then unwrap it."
  95. (if (and (equal 1 (length result))
  96. (equal 1 (length (car result))))
  97. (org-babel-read (caar result))
  98. (mapcar (lambda (row)
  99. (if (equal 'hline row)
  100. 'hline
  101. (mapcar #'org-babel-read row))) result)))
  102. (defun org-babel-sqlite-offset-colnames (table headers-p)
  103. "If HEADERS-P is non-nil then offset the first row as column names."
  104. (if headers-p
  105. (cons (car table) (cons 'hline (cdr table)))
  106. table))
  107. (defun org-babel-prep-session:sqlite (session params)
  108. "Prepare SESSION according to the header arguments specified in PARAMS."
  109. (error "sqlite sessions not yet implemented"))
  110. (provide 'ob-sqlite)
  111. ;; arch-tag: 5c03d7f2-0f72-48b8-bbd1-35aafea248ac
  112. ;;; ob-sqlite.el ends here