ob-sqlite.el 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. (defvar org-babel-default-header-args:sqlite '())
  23. (defun org-babel-expand-body:sqlite
  24. (body params &optional processed-params) body)
  25. (defvar org-babel-sqlite3-command "sqlite3")
  26. (defun org-babel-execute:sqlite (body params)
  27. "Execute a block of Sqlite code with org-babel. This function is
  28. called by `org-babel-execute-src-block'."
  29. (message "executing Sqlite source code block")
  30. (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
  31. (vars (org-babel-ref-variables params))
  32. (headers-p (equal "yes" (cdr (assoc :colnames params)))))
  33. (with-temp-buffer
  34. (insert
  35. (shell-command-to-string
  36. (format "%s %s -csv %s %S"
  37. org-babel-sqlite3-command
  38. (if headers-p "-header" "")
  39. (cdr (assoc :db params))
  40. (org-babel-sqlite-expand-vars body vars))))
  41. (if (or (member "scalar" result-params)
  42. (member "code" result-params))
  43. (buffer-string)
  44. (org-table-convert-region (point-min) (point-max))
  45. (org-babel-sqlite-table-or-scalar
  46. (org-babel-sqlite-offset-colnames
  47. (org-table-to-lisp) headers-p))))))
  48. (defun org-babel-sqlite-expand-vars (body vars)
  49. "Expand the variables held in VARS in BODY."
  50. (mapc
  51. (lambda (pair)
  52. (setq body (replace-regexp-in-string
  53. (format "\$%s" (car pair))
  54. (format "%S" (cdr pair))
  55. body)))
  56. vars)
  57. body)
  58. (defun org-babel-sqlite-table-or-scalar (result)
  59. "If RESULT looks like a trivial table, then unwrap it."
  60. (if (and (equal 1 (length result))
  61. (equal 1 (length (car result))))
  62. (caar result)
  63. result))
  64. (defun org-babel-sqlite-offset-colnames (table headers-p)
  65. "If HEADERS-P is non-nil then offset the first row as column names."
  66. (if headers-p
  67. (cons (car table) (cons 'hline (cdr table)))
  68. table))
  69. (defun org-babel-prep-session:sqlite (session params)
  70. "Prepare SESSION according to the header arguments specified in PARAMS."
  71. (error "sqlite sessions not yet implemented"))
  72. (provide 'ob-sqlite)
  73. ;; arch-tag: 5c03d7f2-0f72-48b8-bbd1-35aafea248ac
  74. ;;; ob-sqlite.el ends here