ob-sqlite.el 3.0 KB

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