ob-sql.el 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ;;; ob-sql.el --- org-babel functions for sql evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  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 sql source code.
  20. ;;
  21. ;; SQL is somewhat unique in that there are many different engines for
  22. ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this
  23. ;; file will have to be implemented engine by engine.
  24. ;;
  25. ;; Also SQL evaluation generally takes place inside of a database.
  26. ;;
  27. ;; For now lets just allow a generic ':cmdline' header argument.
  28. ;;
  29. ;; TODO:
  30. ;;
  31. ;; - support for sessions
  32. ;; - add more useful header arguments (user, passwd, database, etc...)
  33. ;; - support for more engines (currently only supports mysql)
  34. ;; - what's a reasonable way to drop table data into SQL?
  35. ;;
  36. ;;; Code:
  37. (require 'ob)
  38. (eval-when-compile (require 'cl))
  39. (declare-function org-table-import "org-table" (file arg))
  40. (defvar org-babel-default-header-args:sql '())
  41. (defun org-babel-execute:sql (body params)
  42. "Execute a block of Sql code with Babel.
  43. This function is called by `org-babel-execute-src-block'."
  44. (let* ((result-params (cdr (assoc :result-params params)))
  45. (cmdline (cdr (assoc :cmdline params)))
  46. (engine (cdr (assoc :engine params)))
  47. (in-file (org-babel-temp-file "sql-in-"))
  48. (out-file (or (cdr (assoc :out-file params))
  49. (org-babel-temp-file "sql-out-")))
  50. (command (case (intern engine)
  51. ('mysql (format "mysql %s -e \"source %s\" > %s"
  52. (or cmdline "")
  53. (org-babel-process-file-name in-file)
  54. (org-babel-process-file-name out-file)))
  55. ('postgresql (format "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
  56. (org-babel-process-file-name in-file)
  57. (org-babel-process-file-name out-file)
  58. (or cmdline "")))
  59. (t (error "no support for the %s sql engine" engine)))))
  60. (with-temp-file in-file
  61. (insert (org-babel-expand-body:generic body params)))
  62. (message command)
  63. (shell-command command)
  64. (with-temp-buffer
  65. (org-table-import out-file nil)
  66. (org-babel-reassemble-table
  67. (org-table-to-lisp)
  68. (org-babel-pick-name (cdr (assoc :colname-names params))
  69. (cdr (assoc :colnames params)))
  70. (org-babel-pick-name (cdr (assoc :rowname-names params))
  71. (cdr (assoc :rownames params)))))))
  72. (defun org-babel-prep-session:sql (session params)
  73. "Raise an error because Sql sessions aren't implemented."
  74. (error "sql sessions not yet implemented"))
  75. (provide 'ob-sql)
  76. ;; arch-tag: a43ff944-6de1-4566-a83c-626814e3dad2
  77. ;;; ob-sql.el ends here