1 ;;; ob-sql.el --- org-babel functions for sql evaluation
3 ;; Copyright (C) 2009-2014 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
(mapcar #'cdr
(org-babel-get-header params
:var
))))
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-execute:sql
(body params
)
103 "Execute a block of Sql code with Babel.
104 This function is called by `org-babel-execute-src-block'."
105 (let* ((result-params (cdr (assoc :result-params params
)))
106 (cmdline (cdr (assoc :cmdline params
)))
107 (dbhost (cdr (assoc :dbhost params
)))
108 (dbport (cdr (assq :dbport params
)))
109 (dbuser (cdr (assoc :dbuser params
)))
110 (dbpassword (cdr (assoc :dbpassword params
)))
111 (database (cdr (assoc :database params
)))
112 (engine (cdr (assoc :engine params
)))
113 (colnames-p (not (equal "no" (cdr (assoc :colnames params
)))))
114 (in-file (org-babel-temp-file "sql-in-"))
115 (out-file (or (cdr (assoc :out-file params
))
116 (org-babel-temp-file "sql-out-")))
118 (command (case (intern engine
)
119 ('dbi
(format "dbish --batch %s < %s | sed '%s' > %s"
121 (org-babel-process-file-name in-file
)
122 "/^+/d;s/^\|//;s/(NULL)/ /g;$d"
123 (org-babel-process-file-name out-file
)))
124 ('monetdb
(format "mclient -f tab %s < %s > %s"
126 (org-babel-process-file-name in-file
)
127 (org-babel-process-file-name out-file
)))
128 ('msosql
(format "osql %s -s \"\t\" -i %s -o %s"
130 (org-babel-process-file-name in-file
)
131 (org-babel-process-file-name out-file
)))
132 ('mysql
(format "mysql %s %s %s < %s > %s"
133 (org-babel-sql-dbstring-mysql
134 dbhost dbport dbuser dbpassword database
)
135 (if colnames-p
"" "-N")
137 (org-babel-process-file-name in-file
)
138 (org-babel-process-file-name out-file
)))
140 "psql --set=\"ON_ERROR_STOP=1\" %s -A -P footer=off -F \"\t\" %s -f %s -o %s %s"
141 (if colnames-p
"" "-t")
142 (org-babel-sql-dbstring-postgresql dbhost dbuser database
)
143 (org-babel-process-file-name in-file
)
144 (org-babel-process-file-name out-file
)
146 (t (error "No support for the %s SQL engine" engine
)))))
147 (with-temp-file in-file
149 (case (intern engine
)
150 ('dbi
"/format partbox\n")
152 (org-babel-expand-body:sql body params
)))
154 (org-babel-eval command
"")
155 (org-babel-result-cond result-params
157 (progn (insert-file-contents-literally out-file
) (buffer-string)))
160 ((or (eq (intern engine
) 'mysql
)
161 (eq (intern engine
) 'dbi
)
162 (eq (intern engine
) 'postgresql
))
163 ;; Add header row delimiter after column-names header in first line
167 (insert-file-contents out-file
)
168 (goto-char (point-min))
171 (setq header-delim
"-")
172 (write-file out-file
)))))
174 ;; Need to figure out the delimiter for the header row
176 (insert-file-contents out-file
)
177 (goto-char (point-min))
178 (when (re-search-forward "^\\(-+\\)[^-]" nil t
)
179 (setq header-delim
(match-string-no-properties 1)))
180 (goto-char (point-max))
182 (while (looking-at "\n")
184 (goto-char (point-max))
186 (write-file out-file
))))
187 (org-table-import out-file
'(16))
188 (org-babel-reassemble-table
190 (if (string= (car x
) header-delim
)
194 (org-babel-pick-name (cdr (assoc :colname-names params
))
195 (cdr (assoc :colnames params
)))
196 (org-babel-pick-name (cdr (assoc :rowname-names params
))
197 (cdr (assoc :rownames params
))))))))
199 (defun org-babel-sql-expand-vars (body vars
)
200 "Expand the variables held in VARS in BODY."
204 (replace-regexp-in-string
205 (format "\$%s" (car pair
)) ;FIXME: "\$" == "$"!
206 (let ((val (cdr pair
)))
208 (let ((data-file (org-babel-temp-file "sql-data-")))
209 (with-temp-file data-file
210 (insert (orgtbl-to-csv
211 val
'(:fmt
(lambda (el) (if (stringp el
)
213 (format "%S" el
)))))))
215 (if (stringp val
) val
(format "%S" val
))))
220 (defun org-babel-prep-session:sql
(session params
)
221 "Raise an error because Sql sessions aren't implemented."
222 (error "SQL sessions not yet implemented"))
228 ;;; ob-sql.el ends here