ob-sql.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; ob-sql.el --- org-babel functions for sql evaluation
  2. ;; Copyright (C) 2009-2011 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. ;;
  20. ;; SQL is somewhat unique in that there are many different engines for
  21. ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this
  22. ;; file will have to be implemented engine by engine.
  23. ;;
  24. ;; Also SQL evaluation generally takes place inside of a database.
  25. ;;
  26. ;; For now lets just allow a generic ':cmdline' header argument.
  27. ;;
  28. ;; TODO:
  29. ;;
  30. ;; - support for sessions
  31. ;; - add more useful header arguments (user, passwd, database, etc...)
  32. ;; - support for more engines (currently only supports mysql)
  33. ;; - what's a reasonable way to drop table data into SQL?
  34. ;;
  35. ;;; Code:
  36. (require 'ob)
  37. (eval-when-compile (require 'cl))
  38. (declare-function org-table-import "org-table" (file arg))
  39. (declare-function orgtbl-to-csv "org-table" (TABLE PARAMS))
  40. (defvar org-babel-default-header-args:sql '())
  41. (defun org-babel-expand-body:sql (body params)
  42. "Expand BODY according to the values of PARAMS."
  43. (org-babel-sql-expand-vars
  44. body (mapcar #'cdr (org-babel-get-header params :var))))
  45. (defun org-babel-execute:sql (body params)
  46. "Execute a block of Sql code with Babel.
  47. This function is called by `org-babel-execute-src-block'."
  48. (let* ((result-params (cdr (assoc :result-params params)))
  49. (cmdline (cdr (assoc :cmdline params)))
  50. (engine (cdr (assoc :engine params)))
  51. (in-file (org-babel-temp-file "sql-in-"))
  52. (out-file (or (cdr (assoc :out-file params))
  53. (org-babel-temp-file "sql-out-")))
  54. (header-delim "")
  55. (command (case (intern engine)
  56. ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
  57. (or cmdline "")
  58. (org-babel-process-file-name in-file)
  59. (org-babel-process-file-name out-file)))
  60. ('mysql (format "mysql %s < %s > %s"
  61. (or cmdline "")
  62. (org-babel-process-file-name in-file)
  63. (org-babel-process-file-name out-file)))
  64. ('postgresql (format
  65. "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
  66. (org-babel-process-file-name in-file)
  67. (org-babel-process-file-name out-file)
  68. (or cmdline "")))
  69. (t (error "no support for the %s sql engine" engine)))))
  70. (with-temp-file in-file
  71. (insert (org-babel-expand-body:sql body params)))
  72. (message command)
  73. (shell-command command)
  74. (if (or (member "scalar" result-params)
  75. (member "verbatim" result-params)
  76. (member "html" result-params)
  77. (member "code" result-params)
  78. (equal (point-min) (point-max)))
  79. (with-temp-buffer
  80. (progn (insert-file-contents-literally out-file) (buffer-string)))
  81. (with-temp-buffer
  82. ;; need to figure out what the delimiter is for the header row
  83. (with-temp-buffer
  84. (insert-file-contents out-file)
  85. (goto-char (point-min))
  86. (when (re-search-forward "^\\(-+\\)[^-]" nil t)
  87. (setq header-delim (match-string-no-properties 1)))
  88. (goto-char (point-max))
  89. (forward-char -1)
  90. (while (looking-at "\n")
  91. (delete-char 1)
  92. (goto-char (point-max))
  93. (forward-char -1))
  94. (write-file out-file))
  95. (org-table-import out-file '(16))
  96. (org-babel-reassemble-table
  97. (mapcar (lambda (x)
  98. (if (string= (car x) header-delim)
  99. 'hline
  100. x))
  101. (org-table-to-lisp))
  102. (org-babel-pick-name (cdr (assoc :colname-names params))
  103. (cdr (assoc :colnames params)))
  104. (org-babel-pick-name (cdr (assoc :rowname-names params))
  105. (cdr (assoc :rownames params))))))))
  106. (defun org-babel-sql-expand-vars (body vars)
  107. "Expand the variables held in VARS in BODY."
  108. (mapc
  109. (lambda (pair)
  110. (setq body
  111. (replace-regexp-in-string
  112. (format "\$%s" (car pair))
  113. ((lambda (val)
  114. (if (listp val)
  115. ((lambda (data-file)
  116. (with-temp-file data-file
  117. (insert (orgtbl-to-csv
  118. val '(:fmt (lambda (el) (if (stringp el)
  119. el
  120. (format "%S" el)))))))
  121. data-file)
  122. (org-babel-temp-file "sql-data-"))
  123. (if (stringp val) val (format "%S" val))))
  124. (cdr pair))
  125. body)))
  126. vars)
  127. body)
  128. (defun org-babel-prep-session:sql (session params)
  129. "Raise an error because Sql sessions aren't implemented."
  130. (error "sql sessions not yet implemented"))
  131. (provide 'ob-sql)
  132. ;;; ob-sql.el ends here