ob-sqlite.el 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ;;; ob-sqlite.el --- org-babel functions for sqlite database interaction
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating sqlite source code.
  24. ;;; Code:
  25. (require 'ob)
  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. ;;; ob-sqlite.el ends here