ob-sql.el 8.0 KB

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