ob-sql.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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-args:sql
  43. '((engine . :any)
  44. (out-file . :any)))
  45. (defun org-babel-expand-body:sql (body params)
  46. "Expand BODY according to the values of PARAMS."
  47. (org-babel-sql-expand-vars
  48. body (mapcar #'cdr (org-babel-get-header params :var))))
  49. (defun org-babel-execute:sql (body params)
  50. "Execute a block of Sql code with Babel.
  51. This function is called by `org-babel-execute-src-block'."
  52. (let* ((result-params (cdr (assoc :result-params params)))
  53. (cmdline (cdr (assoc :cmdline params)))
  54. (engine (cdr (assoc :engine params)))
  55. (in-file (org-babel-temp-file "sql-in-"))
  56. (out-file (or (cdr (assoc :out-file params))
  57. (org-babel-temp-file "sql-out-")))
  58. (header-delim "")
  59. (command (case (intern engine)
  60. ('dbi (format "dbish --batch '%s' < %s | sed '%s' > %s"
  61. (or cmdline "")
  62. (org-babel-process-file-name in-file)
  63. "/^+/d;s/^\|//;$d"
  64. (org-babel-process-file-name out-file)))
  65. ('monetdb (format "mclient -f tab %s < %s > %s"
  66. (or cmdline "")
  67. (org-babel-process-file-name in-file)
  68. (org-babel-process-file-name out-file)))
  69. ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
  70. (or cmdline "")
  71. (org-babel-process-file-name in-file)
  72. (org-babel-process-file-name out-file)))
  73. ('mysql (format "mysql %s < %s > %s"
  74. (or cmdline "")
  75. (org-babel-process-file-name in-file)
  76. (org-babel-process-file-name out-file)))
  77. ('postgresql (format
  78. "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
  79. (org-babel-process-file-name in-file)
  80. (org-babel-process-file-name out-file)
  81. (or cmdline "")))
  82. (t (error "No support for the %s SQL engine" engine)))))
  83. (with-temp-file in-file
  84. (insert
  85. (case (intern engine)
  86. ('dbi "/format partbox\n")
  87. (t ""))
  88. (org-babel-expand-body:sql body params)))
  89. (message command)
  90. (shell-command command)
  91. (if (or (member "scalar" result-params)
  92. (member "verbatim" result-params)
  93. (member "html" result-params)
  94. (member "code" result-params)
  95. (equal (point-min) (point-max)))
  96. (with-temp-buffer
  97. (progn (insert-file-contents-literally out-file) (buffer-string)))
  98. (with-temp-buffer
  99. ;; need to figure out what the delimiter is for the header row
  100. (with-temp-buffer
  101. (insert-file-contents out-file)
  102. (goto-char (point-min))
  103. (when (re-search-forward "^\\(-+\\)[^-]" nil t)
  104. (setq header-delim (match-string-no-properties 1)))
  105. (goto-char (point-max))
  106. (forward-char -1)
  107. (while (looking-at "\n")
  108. (delete-char 1)
  109. (goto-char (point-max))
  110. (forward-char -1))
  111. (write-file out-file))
  112. (org-table-import out-file '(16))
  113. (org-babel-reassemble-table
  114. (mapcar (lambda (x)
  115. (if (string= (car x) header-delim)
  116. 'hline
  117. x))
  118. (org-table-to-lisp))
  119. (org-babel-pick-name (cdr (assoc :colname-names params))
  120. (cdr (assoc :colnames params)))
  121. (org-babel-pick-name (cdr (assoc :rowname-names params))
  122. (cdr (assoc :rownames params))))))))
  123. (defun org-babel-sql-expand-vars (body vars)
  124. "Expand the variables held in VARS in BODY."
  125. (mapc
  126. (lambda (pair)
  127. (setq body
  128. (replace-regexp-in-string
  129. (format "\$%s" (car pair))
  130. ((lambda (val)
  131. (if (listp val)
  132. ((lambda (data-file)
  133. (with-temp-file data-file
  134. (insert (orgtbl-to-csv
  135. val '(:fmt (lambda (el) (if (stringp el)
  136. el
  137. (format "%S" el)))))))
  138. data-file)
  139. (org-babel-temp-file "sql-data-"))
  140. (if (stringp val) val (format "%S" val))))
  141. (cdr pair))
  142. body)))
  143. vars)
  144. body)
  145. (defun org-babel-prep-session:sql (session params)
  146. "Raise an error because Sql sessions aren't implemented."
  147. (error "SQL sessions not yet implemented"))
  148. (provide 'ob-sql)
  149. ;;; ob-sql.el ends here