ob-sql: Add possibility to set dbport
[org-mode/org-tableheadings.git] / lisp / ob-sql.el
blobc29b175d9af2e165895761b4d5d9eee625c0077e
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/>.
24 ;;; Commentary:
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.
35 ;; Header args used:
36 ;; - engine
37 ;; - cmdline
38 ;; - dbhost
39 ;; - dbport
40 ;; - dbuser
41 ;; - dbpassword
42 ;; - database
43 ;; - colnames (default, nil, means "yes")
44 ;; - result-params
45 ;; - out-file
46 ;; The following are used but not really implemented for SQL:
47 ;; - colname-names
48 ;; - rownames
49 ;; - rowname-names
51 ;; TODO:
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?
58 ;;; Code:
59 (require 'ob)
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
69 '((engine . :any)
70 (out-file . :any)
71 (dbhost . :any)
72 (dbport . :any)
73 (dbuser . :any)
74 (dbpassword . :any)
75 (database . :any))
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
86 (delq nil
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
97 (delq nil
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-")))
117 (header-delim "")
118 (command (case (intern engine)
119 ('dbi (format "dbish --batch %s < %s | sed '%s' > %s"
120 (or cmdline "")
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"
125 (or cmdline "")
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"
129 (or cmdline "")
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")
136 (or cmdline "")
137 (org-babel-process-file-name in-file)
138 (org-babel-process-file-name out-file)))
139 ('postgresql (format
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)
145 (or cmdline "")))
146 (t (error "No support for the %s SQL engine" engine)))))
147 (with-temp-file in-file
148 (insert
149 (case (intern engine)
150 ('dbi "/format partbox\n")
151 (t ""))
152 (org-babel-expand-body:sql body params)))
153 (message command)
154 (org-babel-eval command "")
155 (org-babel-result-cond result-params
156 (with-temp-buffer
157 (progn (insert-file-contents-literally out-file) (buffer-string)))
158 (with-temp-buffer
159 (cond
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
164 (cond
165 (colnames-p
166 (with-temp-buffer
167 (insert-file-contents out-file)
168 (goto-char (point-min))
169 (forward-line 1)
170 (insert "-\n")
171 (setq header-delim "-")
172 (write-file out-file)))))
174 ;; Need to figure out the delimiter for the header row
175 (with-temp-buffer
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))
181 (forward-char -1)
182 (while (looking-at "\n")
183 (delete-char 1)
184 (goto-char (point-max))
185 (forward-char -1))
186 (write-file out-file))))
187 (org-table-import out-file '(16))
188 (org-babel-reassemble-table
189 (mapcar (lambda (x)
190 (if (string= (car x) header-delim)
191 'hline
193 (org-table-to-lisp))
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."
201 (mapc
202 (lambda (pair)
203 (setq body
204 (replace-regexp-in-string
205 (format "\$%s" (car pair)) ;FIXME: "\$" == "$"!
206 (let ((val (cdr pair)))
207 (if (listp val)
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)))))))
214 data-file)
215 (if (stringp val) val (format "%S" val))))
216 body)))
217 vars)
218 body)
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"))
224 (provide 'ob-sql)
228 ;;; ob-sql.el ends here