ob-sql.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
  60. (or cmdline "")
  61. (org-babel-process-file-name in-file)
  62. (org-babel-process-file-name out-file)))
  63. ('mysql (format "mysql %s < %s > %s"
  64. (or cmdline "")
  65. (org-babel-process-file-name in-file)
  66. (org-babel-process-file-name out-file)))
  67. ('postgresql (format
  68. "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
  69. (org-babel-process-file-name in-file)
  70. (org-babel-process-file-name out-file)
  71. (or cmdline "")))
  72. (t (error "no support for the %s sql engine" engine)))))
  73. (with-temp-file in-file
  74. (insert (org-babel-expand-body:sql body params)))
  75. (message command)
  76. (shell-command command)
  77. (if (or (member "scalar" result-params)
  78. (member "verbatim" result-params)
  79. (member "html" result-params)
  80. (member "code" result-params)
  81. (equal (point-min) (point-max)))
  82. (with-temp-buffer
  83. (progn (insert-file-contents-literally out-file) (buffer-string)))
  84. (with-temp-buffer
  85. ;; need to figure out what the delimiter is for the header row
  86. (with-temp-buffer
  87. (insert-file-contents out-file)
  88. (goto-char (point-min))
  89. (when (re-search-forward "^\\(-+\\)[^-]" nil t)
  90. (setq header-delim (match-string-no-properties 1)))
  91. (goto-char (point-max))
  92. (forward-char -1)
  93. (while (looking-at "\n")
  94. (delete-char 1)
  95. (goto-char (point-max))
  96. (forward-char -1))
  97. (write-file out-file))
  98. (org-table-import out-file '(16))
  99. (org-babel-reassemble-table
  100. (mapcar (lambda (x)
  101. (if (string= (car x) header-delim)
  102. 'hline
  103. x))
  104. (org-table-to-lisp))
  105. (org-babel-pick-name (cdr (assoc :colname-names params))
  106. (cdr (assoc :colnames params)))
  107. (org-babel-pick-name (cdr (assoc :rowname-names params))
  108. (cdr (assoc :rownames params))))))))
  109. (defun org-babel-sql-expand-vars (body vars)
  110. "Expand the variables held in VARS in BODY."
  111. (mapc
  112. (lambda (pair)
  113. (setq body
  114. (replace-regexp-in-string
  115. (format "\$%s" (car pair))
  116. ((lambda (val)
  117. (if (listp val)
  118. ((lambda (data-file)
  119. (with-temp-file data-file
  120. (insert (orgtbl-to-csv
  121. val '(:fmt (lambda (el) (if (stringp el)
  122. el
  123. (format "%S" el)))))))
  124. data-file)
  125. (org-babel-temp-file "sql-data-"))
  126. (if (stringp val) val (format "%S" val))))
  127. (cdr pair))
  128. body)))
  129. vars)
  130. body)
  131. (defun org-babel-prep-session:sql (session params)
  132. "Raise an error because Sql sessions aren't implemented."
  133. (error "sql sessions not yet implemented"))
  134. (provide 'ob-sql)
  135. ;;; ob-sql.el ends here