ob-sql.el 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ;;; ob-sql.el --- org-babel functions for sql evaluation
  2. ;; Copyright (C) 2009-2013 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. ;; Header args used:
  28. ;; - engine
  29. ;; - cmdline
  30. ;; - dbhost
  31. ;; - dbuser
  32. ;; - dbpassword
  33. ;; - database
  34. ;; - colnames (default, nil, means "yes")
  35. ;; - result-params
  36. ;; - out-file
  37. ;; The following are used but not really implemented for SQL:
  38. ;; - colname-names
  39. ;; - rownames
  40. ;; - rowname-names
  41. ;;
  42. ;; TODO:
  43. ;;
  44. ;; - support for sessions
  45. ;; - support for more engines (currently only supports mysql)
  46. ;; - what's a reasonable way to drop table data into SQL?
  47. ;;
  48. ;;; Code:
  49. (require 'ob)
  50. (eval-when-compile (require 'cl))
  51. (declare-function org-table-import "org-table" (file arg))
  52. (declare-function orgtbl-to-csv "org-table" (table params))
  53. (declare-function org-table-to-lisp "org-table" (&optional txt))
  54. (defvar org-babel-default-header-args:sql '())
  55. (defconst org-babel-header-args:sql
  56. '((engine . :any)
  57. (out-file . :any)
  58. (dbhost . :any)
  59. (dbuser . :any)
  60. (dbpassword . :any)
  61. (database . :any))
  62. "SQL-specific header arguments.")
  63. (defun org-babel-expand-body:sql (body params)
  64. "Expand BODY according to the values of PARAMS."
  65. (org-babel-sql-expand-vars
  66. body (mapcar #'cdr (org-babel-get-header params :var))))
  67. (defun dbstring-mysql (host user password database)
  68. "Make MySQL cmd line args for database connection. Pass nil to omit that arg."
  69. (combine-and-quote-strings
  70. (remq nil
  71. (list (when host (concat "-h" host))
  72. (when user (concat "-u" user))
  73. (when password (concat "-p" password))
  74. (when database (concat "-D" database))))))
  75. (defun org-babel-execute:sql (body params)
  76. "Execute a block of Sql code with Babel.
  77. This function is called by `org-babel-execute-src-block'."
  78. (let* ((result-params (cdr (assoc :result-params params)))
  79. (cmdline (cdr (assoc :cmdline params)))
  80. (dbhost (cdr (assoc :dbhost params)))
  81. (dbuser (cdr (assoc :dbuser params)))
  82. (dbpassword (cdr (assoc :dbpassword params)))
  83. (database (cdr (assoc :database params)))
  84. (engine (cdr (assoc :engine params)))
  85. (colnames-p (not (equal "no" (cdr (assoc :colnames params)))))
  86. (in-file (org-babel-temp-file "sql-in-"))
  87. (out-file (or (cdr (assoc :out-file params))
  88. (org-babel-temp-file "sql-out-")))
  89. (header-delim "")
  90. (command (case (intern engine)
  91. ('dbi (format "dbish --batch %s < %s | sed '%s' > %s"
  92. (or cmdline "")
  93. (org-babel-process-file-name in-file)
  94. "/^+/d;s/^\|//;s/(NULL)/ /g;$d"
  95. (org-babel-process-file-name out-file)))
  96. ('monetdb (format "mclient -f tab %s < %s > %s"
  97. (or cmdline "")
  98. (org-babel-process-file-name in-file)
  99. (org-babel-process-file-name out-file)))
  100. ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
  101. (or cmdline "")
  102. (org-babel-process-file-name in-file)
  103. (org-babel-process-file-name out-file)))
  104. ('mysql (format "mysql %s %s %s < %s > %s"
  105. (dbstring-mysql dbhost dbuser dbpassword database)
  106. (if colnames-p "" "-N")
  107. (or cmdline "")
  108. (org-babel-process-file-name in-file)
  109. (org-babel-process-file-name out-file)))
  110. ('postgresql (format
  111. "psql -A -P footer=off -F \"\t\" -f %s -o %s %s"
  112. (org-babel-process-file-name in-file)
  113. (org-babel-process-file-name out-file)
  114. (or cmdline "")))
  115. (t (error "No support for the %s SQL engine" engine)))))
  116. (with-temp-file in-file
  117. (insert
  118. (case (intern engine)
  119. ('dbi "/format partbox\n")
  120. (t ""))
  121. (org-babel-expand-body:sql body params)))
  122. (message command)
  123. (org-babel-eval command "")
  124. (org-babel-result-cond result-params
  125. (with-temp-buffer
  126. (progn (insert-file-contents-literally out-file) (buffer-string)))
  127. (with-temp-buffer
  128. (cond
  129. ((or (eq (intern engine) 'mysql)
  130. (eq (intern engine) 'dbi)
  131. (eq (intern engine) 'postgresql))
  132. ;; Add header row delimiter after column-names header in first line
  133. (cond
  134. (colnames-p
  135. (with-temp-buffer
  136. (insert-file-contents out-file)
  137. (goto-char (point-min))
  138. (forward-line 1)
  139. (insert "-\n")
  140. (setq header-delim "-")
  141. (write-file out-file)))))
  142. (t
  143. ;; Need to figure out the delimiter for the header row
  144. (with-temp-buffer
  145. (insert-file-contents out-file)
  146. (goto-char (point-min))
  147. (when (re-search-forward "^\\(-+\\)[^-]" nil t)
  148. (setq header-delim (match-string-no-properties 1)))
  149. (goto-char (point-max))
  150. (forward-char -1)
  151. (while (looking-at "\n")
  152. (delete-char 1)
  153. (goto-char (point-max))
  154. (forward-char -1))
  155. (write-file out-file))))
  156. (org-table-import out-file '(16))
  157. (org-babel-reassemble-table
  158. (mapcar (lambda (x)
  159. (if (string= (car x) header-delim)
  160. 'hline
  161. x))
  162. (org-table-to-lisp))
  163. (org-babel-pick-name (cdr (assoc :colname-names params))
  164. (cdr (assoc :colnames params)))
  165. (org-babel-pick-name (cdr (assoc :rowname-names params))
  166. (cdr (assoc :rownames params))))))))
  167. (defun org-babel-sql-expand-vars (body vars)
  168. "Expand the variables held in VARS in BODY."
  169. (mapc
  170. (lambda (pair)
  171. (setq body
  172. (replace-regexp-in-string
  173. (format "\$%s" (car pair))
  174. ((lambda (val)
  175. (if (listp val)
  176. ((lambda (data-file)
  177. (with-temp-file data-file
  178. (insert (orgtbl-to-csv
  179. val '(:fmt (lambda (el) (if (stringp el)
  180. el
  181. (format "%S" el)))))))
  182. data-file)
  183. (org-babel-temp-file "sql-data-"))
  184. (if (stringp val) val (format "%S" val))))
  185. (cdr pair))
  186. body)))
  187. vars)
  188. body)
  189. (defun org-babel-prep-session:sql (session params)
  190. "Raise an error because Sql sessions aren't implemented."
  191. (error "SQL sessions not yet implemented"))
  192. (provide 'ob-sql)
  193. ;;; ob-sql.el ends here