ob-sql.el 5.7 KB

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