ob-sql.el 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ;;; ob-sql.el --- org-babel functions for sql evaluation
  2. ;; Copyright (C) 2009-2014 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 org-babel-sql-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. (delq 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-sql-dbstring-postgresql (host user database)
  76. "Make PostgreSQL command line args for database connection.
  77. Pass nil to omit that arg."
  78. (combine-and-quote-strings
  79. (delq nil
  80. (list (when host (concat "-h" host))
  81. (when user (concat "-U" user))
  82. (when database (concat "-d" database))))))
  83. (defun org-babel-execute:sql (body params)
  84. "Execute a block of Sql code with Babel.
  85. This function is called by `org-babel-execute-src-block'."
  86. (let* ((result-params (cdr (assoc :result-params params)))
  87. (cmdline (cdr (assoc :cmdline params)))
  88. (dbhost (cdr (assoc :dbhost params)))
  89. (dbuser (cdr (assoc :dbuser params)))
  90. (dbpassword (cdr (assoc :dbpassword params)))
  91. (database (cdr (assoc :database params)))
  92. (engine (cdr (assoc :engine params)))
  93. (colnames-p (not (equal "no" (cdr (assoc :colnames params)))))
  94. (in-file (org-babel-temp-file "sql-in-"))
  95. (out-file (or (cdr (assoc :out-file params))
  96. (org-babel-temp-file "sql-out-")))
  97. (header-delim "")
  98. (command (case (intern engine)
  99. ('dbi (format "dbish --batch %s < %s | sed '%s' > %s"
  100. (or cmdline "")
  101. (org-babel-process-file-name in-file)
  102. "/^+/d;s/^\|//;s/(NULL)/ /g;$d"
  103. (org-babel-process-file-name out-file)))
  104. ('monetdb (format "mclient -f tab %s < %s > %s"
  105. (or cmdline "")
  106. (org-babel-process-file-name in-file)
  107. (org-babel-process-file-name out-file)))
  108. ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
  109. (or cmdline "")
  110. (org-babel-process-file-name in-file)
  111. (org-babel-process-file-name out-file)))
  112. ('mysql (format "mysql %s %s %s < %s > %s"
  113. (org-babel-sql-dbstring-mysql dbhost dbuser dbpassword database)
  114. (if colnames-p "" "-N")
  115. (or cmdline "")
  116. (org-babel-process-file-name in-file)
  117. (org-babel-process-file-name out-file)))
  118. ('postgresql (format
  119. "psql --set=\"ON_ERROR_STOP=1\" %s -A -P footer=off -F \"\t\" %s -f %s -o %s %s"
  120. (if colnames-p "" "-t")
  121. (org-babel-sql-dbstring-postgresql dbhost dbuser database)
  122. (org-babel-process-file-name in-file)
  123. (org-babel-process-file-name out-file)
  124. (or cmdline "")))
  125. (t (error "No support for the %s SQL engine" engine)))))
  126. (with-temp-file in-file
  127. (insert
  128. (case (intern engine)
  129. ('dbi "/format partbox\n")
  130. (t ""))
  131. (org-babel-expand-body:sql body params)))
  132. (message command)
  133. (org-babel-eval command "")
  134. (org-babel-result-cond result-params
  135. (with-temp-buffer
  136. (progn (insert-file-contents-literally out-file) (buffer-string)))
  137. (with-temp-buffer
  138. (cond
  139. ((or (eq (intern engine) 'mysql)
  140. (eq (intern engine) 'dbi)
  141. (eq (intern engine) 'postgresql))
  142. ;; Add header row delimiter after column-names header in first line
  143. (cond
  144. (colnames-p
  145. (with-temp-buffer
  146. (insert-file-contents out-file)
  147. (goto-char (point-min))
  148. (forward-line 1)
  149. (insert "-\n")
  150. (setq header-delim "-")
  151. (write-file out-file)))))
  152. (t
  153. ;; Need to figure out the delimiter for the header row
  154. (with-temp-buffer
  155. (insert-file-contents out-file)
  156. (goto-char (point-min))
  157. (when (re-search-forward "^\\(-+\\)[^-]" nil t)
  158. (setq header-delim (match-string-no-properties 1)))
  159. (goto-char (point-max))
  160. (forward-char -1)
  161. (while (looking-at "\n")
  162. (delete-char 1)
  163. (goto-char (point-max))
  164. (forward-char -1))
  165. (write-file out-file))))
  166. (org-table-import out-file '(16))
  167. (org-babel-reassemble-table
  168. (mapcar (lambda (x)
  169. (if (string= (car x) header-delim)
  170. 'hline
  171. x))
  172. (org-table-to-lisp))
  173. (org-babel-pick-name (cdr (assoc :colname-names params))
  174. (cdr (assoc :colnames params)))
  175. (org-babel-pick-name (cdr (assoc :rowname-names params))
  176. (cdr (assoc :rownames params))))))))
  177. (defun org-babel-sql-expand-vars (body vars)
  178. "Expand the variables held in VARS in BODY."
  179. (mapc
  180. (lambda (pair)
  181. (setq body
  182. (replace-regexp-in-string
  183. (format "\$%s" (car pair)) ;FIXME: "\$" == "$"!
  184. (let ((val (cdr pair)))
  185. (if (listp val)
  186. (let ((data-file (org-babel-temp-file "sql-data-")))
  187. (with-temp-file data-file
  188. (insert (orgtbl-to-csv
  189. val '(:fmt (lambda (el) (if (stringp el)
  190. el
  191. (format "%S" el)))))))
  192. data-file)
  193. (if (stringp val) val (format "%S" val))))
  194. body)))
  195. vars)
  196. body)
  197. (defun org-babel-prep-session:sql (session params)
  198. "Raise an error because Sql sessions aren't implemented."
  199. (error "SQL sessions not yet implemented"))
  200. (provide 'ob-sql)
  201. ;;; ob-sql.el ends here