ob-sqlite.el 3.1 KB

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