org-babel-sql.el 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; org-babel-sql.el --- org-babel functions for sql evaluation
  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 sql source code.
  24. ;;
  25. ;; SQL is somewhat unique in that there are many different engines for
  26. ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this
  27. ;; file will have to be implemented engine by engine.
  28. ;;
  29. ;; Also SQL evaluation generally takes place inside of a database.
  30. ;;
  31. ;; For now lets just allow a generic ':cmdline' header argument.
  32. ;;
  33. ;; TODO:
  34. ;;
  35. ;; - support for sessions
  36. ;; - add more useful header arguments (user, passwd, database, etc...)
  37. ;; - support for more engines (currently only supports mysql)
  38. ;; - what's a reasonable way to drop table data into SQL?
  39. ;;
  40. ;;; Code:
  41. (require 'org-babel)
  42. (org-babel-add-interpreter "sql")
  43. (add-to-list 'org-babel-tangle-langs '("sql" "sql"))
  44. (defun org-babel-execute:sql (body params)
  45. "Execute a block of Sql code with org-babel. This function is
  46. called by `org-babel-execute-src-block' via multiple-value-bind."
  47. (message "executing Sql source code block")
  48. (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
  49. (cmdline (cdr (assoc :cmdline params)))
  50. (engine (cdr (assoc :engine params)))
  51. (in-file (make-temp-file "org-babel-sql-in"))
  52. (out-file (or (cdr (assoc :out-file params))
  53. (make-temp-file "org-babel-sql-out")))
  54. (command (case (intern engine)
  55. ('mysql (format "mysql %s -e \"source %s\" > %s"
  56. (or cmdline "") in-file out-file))
  57. (t (error "no support for the %s sql engine")))))
  58. (with-temp-file in-file (insert body))
  59. (message command)
  60. (shell-command command)
  61. (with-temp-buffer
  62. (org-table-import out-file nil)
  63. (org-table-to-lisp))))
  64. (defun org-babel-prep-session:sql (session params)
  65. "Prepare SESSION according to the header arguments specified in PARAMS."
  66. (error "sql sessions not yet implemented"))
  67. (provide 'org-babel-sql)
  68. ;;; org-babel-sql.el ends here