ob-sql.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ;;; ob-sql.el --- org-babel functions for sql evaluation
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating sql source code.
  19. ;; (see also ob-sqlite.el)
  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. (declare-function orgtbl-to-csv "org-table" (TABLE PARAMS))
  41. (defvar org-babel-default-header-args:sql '())
  42. (defvar org-babel-header-arg-names:sql
  43. '(engine out-file))
  44. (defun org-babel-expand-body:sql (body params)
  45. "Expand BODY according to the values of PARAMS."
  46. (org-babel-sql-expand-vars
  47. body (mapcar #'cdr (org-babel-get-header params :var))))
  48. (defun org-babel-execute:sql (body params)
  49. "Execute a block of Sql code with Babel.
  50. This function is called by `org-babel-execute-src-block'."
  51. (let* ((result-params (cdr (assoc :result-params params)))
  52. (cmdline (cdr (assoc :cmdline params)))
  53. (engine (cdr (assoc :engine params)))
  54. (in-file (org-babel-temp-file "sql-in-"))
  55. (out-file (or (cdr (assoc :out-file params))
  56. (org-babel-temp-file "sql-out-")))
  57. (header-delim "")
  58. (command (case (intern engine)
  59. ('monetdb (format "mclient -f tab %s < %s > %s"
  60. (or cmdline "")
  61. (org-babel-process-file-name in-file)
  62. (org-babel-process-file-name out-file)))
  63. ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
  64. (or cmdline "")
  65. (org-babel-process-file-name in-file)
  66. (org-babel-process-file-name out-file)))
  67. ('mysql (format "mysql %s < %s > %s"
  68. (or cmdline "")
  69. (org-babel-process-file-name in-file)
  70. (org-babel-process-file-name out-file)))
  71. ('postgresql (format
  72. "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
  73. (org-babel-process-file-name in-file)
  74. (org-babel-process-file-name out-file)
  75. (or cmdline "")))
  76. (t (error "no support for the %s sql engine" engine)))))
  77. (with-temp-file in-file
  78. (insert (org-babel-expand-body:sql body params)))
  79. (message command)
  80. (shell-command command)
  81. (if (or (member "scalar" result-params)
  82. (member "verbatim" result-params)
  83. (member "html" result-params)
  84. (member "code" result-params)
  85. (equal (point-min) (point-max)))
  86. (with-temp-buffer
  87. (progn (insert-file-contents-literally out-file) (buffer-string)))
  88. (with-temp-buffer
  89. ;; need to figure out what the delimiter is for the header row
  90. (with-temp-buffer
  91. (insert-file-contents out-file)
  92. (goto-char (point-min))
  93. (when (re-search-forward "^\\(-+\\)[^-]" nil t)
  94. (setq header-delim (match-string-no-properties 1)))
  95. (goto-char (point-max))
  96. (forward-char -1)
  97. (while (looking-at "\n")
  98. (delete-char 1)
  99. (goto-char (point-max))
  100. (forward-char -1))
  101. (write-file out-file))
  102. (org-table-import out-file '(16))
  103. (org-babel-reassemble-table
  104. (mapcar (lambda (x)
  105. (if (string= (car x) header-delim)
  106. 'hline
  107. x))
  108. (org-table-to-lisp))
  109. (org-babel-pick-name (cdr (assoc :colname-names params))
  110. (cdr (assoc :colnames params)))
  111. (org-babel-pick-name (cdr (assoc :rowname-names params))
  112. (cdr (assoc :rownames params))))))))
  113. (defun org-babel-sql-expand-vars (body vars)
  114. "Expand the variables held in VARS in BODY."
  115. (mapc
  116. (lambda (pair)
  117. (setq body
  118. (replace-regexp-in-string
  119. (format "\$%s" (car pair))
  120. ((lambda (val)
  121. (if (listp val)
  122. ((lambda (data-file)
  123. (with-temp-file data-file
  124. (insert (orgtbl-to-csv
  125. val '(:fmt (lambda (el) (if (stringp el)
  126. el
  127. (format "%S" el)))))))
  128. data-file)
  129. (org-babel-temp-file "sql-data-"))
  130. (if (stringp val) val (format "%S" val))))
  131. (cdr pair))
  132. body)))
  133. vars)
  134. body)
  135. (defun org-babel-prep-session:sql (session params)
  136. "Raise an error because Sql sessions aren't implemented."
  137. (error "sql sessions not yet implemented"))
  138. (provide 'ob-sql)
  139. ;;; ob-sql.el ends here