1 ;;; ob-sql.el --- Babel Functions for SQL -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Org-Babel support for evaluating sql source code.
27 ;; (see also ob-sqlite.el)
29 ;; SQL is somewhat unique in that there are many different engines for
30 ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this
31 ;; file will have to be implemented engine by engine.
33 ;; Also SQL evaluation generally takes place inside of a database.
43 ;; - colnames (default, nil, means "yes")
46 ;; The following are used but not really implemented for SQL:
53 ;; - support for sessions
54 ;; - support for more engines (currently only supports mysql)
55 ;; - what's a reasonable way to drop table data into SQL?
60 (eval-when-compile (require 'cl
))
62 (declare-function org-table-import
"org-table" (file arg
))
63 (declare-function orgtbl-to-csv
"org-table" (table params
))
64 (declare-function org-table-to-lisp
"org-table" (&optional txt
))
66 (defvar org-babel-default-header-args
:sql
'())
68 (defconst org-babel-header-args
:sql
76 "SQL-specific header arguments.")
78 (defun org-babel-expand-body:sql
(body params
)
79 "Expand BODY according to the values of PARAMS."
80 (org-babel-sql-expand-vars
81 body
(org-babel--get-vars params
)))
83 (defun org-babel-sql-dbstring-mysql (host port user password database
)
84 "Make MySQL cmd line args for database connection. Pass nil to omit that arg."
85 (combine-and-quote-strings
87 (list (when host
(concat "-h" host
))
88 (when port
(format "-P%d" port
))
89 (when user
(concat "-u" user
))
90 (when password
(concat "-p" password
))
91 (when database
(concat "-D" database
))))))
93 (defun org-babel-sql-dbstring-postgresql (host user database
)
94 "Make PostgreSQL command line args for database connection.
95 Pass nil to omit that arg."
96 (combine-and-quote-strings
98 (list (when host
(concat "-h" host
))
99 (when user
(concat "-U" user
))
100 (when database
(concat "-d" database
))))))
102 (defun org-babel-sql-dbstring-oracle (host port user password database
)
103 "Make Oracle command line args for database connection."
104 (format "%s/%s@%s:%s/%s" user password host port database
))
106 (defun org-babel-execute:sql
(body params
)
107 "Execute a block of Sql code with Babel.
108 This function is called by `org-babel-execute-src-block'."
109 (let* ((result-params (cdr (assoc :result-params params
)))
110 (cmdline (cdr (assoc :cmdline params
)))
111 (dbhost (cdr (assoc :dbhost params
)))
112 (dbport (cdr (assq :dbport params
)))
113 (dbuser (cdr (assoc :dbuser params
)))
114 (dbpassword (cdr (assoc :dbpassword params
)))
115 (database (cdr (assoc :database params
)))
116 (engine (cdr (assoc :engine params
)))
117 (colnames-p (not (equal "no" (cdr (assoc :colnames params
)))))
118 (in-file (org-babel-temp-file "sql-in-"))
119 (out-file (or (cdr (assoc :out-file params
))
120 (org-babel-temp-file "sql-out-")))
122 (command (pcase (intern engine
)
123 (`dbi
(format "dbish --batch %s < %s | sed '%s' > %s"
125 (org-babel-process-file-name in-file
)
126 "/^+/d;s/^|//;s/(NULL)/ /g;$d"
127 (org-babel-process-file-name out-file
)))
128 (`monetdb
(format "mclient -f tab %s < %s > %s"
130 (org-babel-process-file-name in-file
)
131 (org-babel-process-file-name out-file
)))
132 (`msosql
(format "osql %s -s \"\t\" -i %s -o %s"
134 (org-babel-process-file-name in-file
)
135 (org-babel-process-file-name out-file
)))
136 (`mysql
(format "mysql %s %s %s < %s > %s"
137 (org-babel-sql-dbstring-mysql
138 dbhost dbport dbuser dbpassword database
)
139 (if colnames-p
"" "-N")
141 (org-babel-process-file-name in-file
)
142 (org-babel-process-file-name out-file
)))
144 "psql --set=\"ON_ERROR_STOP=1\" %s -A -P \
145 footer=off -F \"\t\" %s -f %s -o %s %s"
146 (if colnames-p
"" "-t")
147 (org-babel-sql-dbstring-postgresql
148 dbhost dbuser database
)
149 (org-babel-process-file-name in-file
)
150 (org-babel-process-file-name out-file
)
153 "sqlplus -s %s < %s > %s"
154 (org-babel-sql-dbstring-oracle
155 dbhost dbport dbuser dbpassword database
)
156 (org-babel-process-file-name in-file
)
157 (org-babel-process-file-name out-file
)))
158 (_ (error "No support for the %s SQL engine" engine
)))))
159 (with-temp-file in-file
161 (pcase (intern engine
)
162 (`dbi
"/format partbox\n")
163 (`oracle
"SET PAGESIZE 50000
172 SET MARKUP HTML OFF SPOOL OFF
177 (org-babel-expand-body:sql body params
)))
178 (org-babel-eval command
"")
179 (org-babel-result-cond result-params
181 (progn (insert-file-contents-literally out-file
) (buffer-string)))
184 ((memq (intern engine
) '(dbi mysql postgresql
))
185 ;; Add header row delimiter after column-names header in first line
189 (insert-file-contents out-file
)
190 (goto-char (point-min))
193 (setq header-delim
"-")
194 (write-file out-file
)))))
196 ;; Need to figure out the delimiter for the header row
198 (insert-file-contents out-file
)
199 (goto-char (point-min))
200 (when (re-search-forward "^\\(-+\\)[^-]" nil t
)
201 (setq header-delim
(match-string-no-properties 1)))
202 (goto-char (point-max))
204 (while (looking-at "\n")
206 (goto-char (point-max))
208 (write-file out-file
))))
209 (org-table-import out-file
'(16))
210 (org-babel-reassemble-table
212 (if (string= (car x
) header-delim
)
216 (org-babel-pick-name (cdr (assoc :colname-names params
))
217 (cdr (assoc :colnames params
)))
218 (org-babel-pick-name (cdr (assoc :rowname-names params
))
219 (cdr (assoc :rownames params
))))))))
221 (defun org-babel-sql-expand-vars (body vars
)
222 "Expand the variables held in VARS in BODY."
226 (replace-regexp-in-string
227 (format "$%s" (car pair
))
228 (let ((val (cdr pair
)))
230 (let ((data-file (org-babel-temp-file "sql-data-")))
231 (with-temp-file data-file
232 (insert (orgtbl-to-csv
233 val
'(:fmt
(lambda (el) (if (stringp el
)
235 (format "%S" el
)))))))
237 (if (stringp val
) val
(format "%S" val
))))
242 (defun org-babel-prep-session:sql
(_session _params
)
243 "Raise an error because Sql sessions aren't implemented."
244 (error "SQL sessions not yet implemented"))
250 ;;; ob-sql.el ends here