ob-sql.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. ;;; ob-sql.el --- Babel Functions for SQL -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2015 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 (org-babel--get-vars params)))
  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-sql-dbstring-oracle (host port user password database)
  87. "Make Oracle command line args for database connection."
  88. (format "%s/%s@%s:%s/%s" user password host port database))
  89. (defun org-babel-execute:sql (body params)
  90. "Execute a block of Sql code with Babel.
  91. This function is called by `org-babel-execute-src-block'."
  92. (let* ((result-params (cdr (assoc :result-params params)))
  93. (cmdline (cdr (assoc :cmdline params)))
  94. (dbhost (cdr (assoc :dbhost params)))
  95. (dbport (cdr (assq :dbport params)))
  96. (dbuser (cdr (assoc :dbuser params)))
  97. (dbpassword (cdr (assoc :dbpassword params)))
  98. (database (cdr (assoc :database params)))
  99. (engine (cdr (assoc :engine params)))
  100. (colnames-p (not (equal "no" (cdr (assoc :colnames params)))))
  101. (in-file (org-babel-temp-file "sql-in-"))
  102. (out-file (or (cdr (assoc :out-file params))
  103. (org-babel-temp-file "sql-out-")))
  104. (header-delim "")
  105. (command (pcase (intern engine)
  106. (`dbi (format "dbish --batch %s < %s | sed '%s' > %s"
  107. (or cmdline "")
  108. (org-babel-process-file-name in-file)
  109. "/^+/d;s/^|//;s/(NULL)/ /g;$d"
  110. (org-babel-process-file-name out-file)))
  111. (`monetdb (format "mclient -f tab %s < %s > %s"
  112. (or cmdline "")
  113. (org-babel-process-file-name in-file)
  114. (org-babel-process-file-name out-file)))
  115. (`msosql (format "osql %s -s \"\t\" -i %s -o %s"
  116. (or cmdline "")
  117. (org-babel-process-file-name in-file)
  118. (org-babel-process-file-name out-file)))
  119. (`mysql (format "mysql %s %s %s < %s > %s"
  120. (org-babel-sql-dbstring-mysql
  121. dbhost dbport dbuser dbpassword database)
  122. (if colnames-p "" "-N")
  123. (or cmdline "")
  124. (org-babel-process-file-name in-file)
  125. (org-babel-process-file-name out-file)))
  126. (`postgresql (format
  127. "psql --set=\"ON_ERROR_STOP=1\" %s -A -P \
  128. footer=off -F \"\t\" %s -f %s -o %s %s"
  129. (if colnames-p "" "-t")
  130. (org-babel-sql-dbstring-postgresql
  131. dbhost dbuser database)
  132. (org-babel-process-file-name in-file)
  133. (org-babel-process-file-name out-file)
  134. (or cmdline "")))
  135. (`oracle (format
  136. "sqlplus -s %s < %s > %s"
  137. (org-babel-sql-dbstring-oracle
  138. dbhost dbport dbuser dbpassword database)
  139. (org-babel-process-file-name in-file)
  140. (org-babel-process-file-name out-file)))
  141. (_ (error "No support for the %s SQL engine" engine)))))
  142. (with-temp-file in-file
  143. (insert
  144. (pcase (intern engine)
  145. (`dbi "/format partbox\n")
  146. (`oracle "SET PAGESIZE 50000
  147. SET NEWPAGE 0
  148. SET TAB OFF
  149. SET SPACE 0
  150. SET LINESIZE 9999
  151. SET ECHO OFF
  152. SET FEEDBACK OFF
  153. SET VERIFY OFF
  154. SET HEADING ON
  155. SET MARKUP HTML OFF SPOOL OFF
  156. SET COLSEP '|'
  157. ")
  158. (_ ""))
  159. (org-babel-expand-body:sql body params)))
  160. (org-babel-eval command "")
  161. (org-babel-result-cond result-params
  162. (with-temp-buffer
  163. (progn (insert-file-contents-literally out-file) (buffer-string)))
  164. (with-temp-buffer
  165. (cond
  166. ((memq (intern engine) '(dbi myslq postgresql))
  167. ;; Add header row delimiter after column-names header in first line
  168. (cond
  169. (colnames-p
  170. (with-temp-buffer
  171. (insert-file-contents out-file)
  172. (goto-char (point-min))
  173. (forward-line 1)
  174. (insert "-\n")
  175. (setq header-delim "-")
  176. (write-file out-file)))))
  177. (t
  178. ;; Need to figure out the delimiter for the header row
  179. (with-temp-buffer
  180. (insert-file-contents out-file)
  181. (goto-char (point-min))
  182. (when (re-search-forward "^\\(-+\\)[^-]" nil t)
  183. (setq header-delim (match-string-no-properties 1)))
  184. (goto-char (point-max))
  185. (forward-char -1)
  186. (while (looking-at "\n")
  187. (delete-char 1)
  188. (goto-char (point-max))
  189. (forward-char -1))
  190. (write-file out-file))))
  191. (org-table-import out-file '(16))
  192. (org-babel-reassemble-table
  193. (mapcar (lambda (x)
  194. (if (string= (car x) header-delim)
  195. 'hline
  196. x))
  197. (org-table-to-lisp))
  198. (org-babel-pick-name (cdr (assoc :colname-names params))
  199. (cdr (assoc :colnames params)))
  200. (org-babel-pick-name (cdr (assoc :rowname-names params))
  201. (cdr (assoc :rownames params))))))))
  202. (defun org-babel-sql-expand-vars (body vars)
  203. "Expand the variables held in VARS in BODY."
  204. (mapc
  205. (lambda (pair)
  206. (setq body
  207. (replace-regexp-in-string
  208. (format "$%s" (car pair))
  209. (let ((val (cdr pair)))
  210. (if (listp val)
  211. (let ((data-file (org-babel-temp-file "sql-data-")))
  212. (with-temp-file data-file
  213. (insert (orgtbl-to-csv
  214. val '(:fmt (lambda (el) (if (stringp el)
  215. el
  216. (format "%S" el)))))))
  217. data-file)
  218. (if (stringp val) val (format "%S" val))))
  219. body)))
  220. vars)
  221. body)
  222. (defun org-babel-prep-session:sql (_session _params)
  223. "Raise an error because Sql sessions aren't implemented."
  224. (error "SQL sessions not yet implemented"))
  225. (provide 'ob-sql)
  226. ;;; ob-sql.el ends here