diff options
Diffstat (limited to 'lisp/org/ob-sql.el')
| -rw-r--r-- | lisp/org/ob-sql.el | 59 |
1 files changed, 54 insertions, 5 deletions
diff --git a/lisp/org/ob-sql.el b/lisp/org/ob-sql.el index 7c3ee120d77..9250825d4e5 100644 --- a/lisp/org/ob-sql.el +++ b/lisp/org/ob-sql.el | |||
| @@ -43,15 +43,25 @@ | |||
| 43 | ;; - colnames (default, nil, means "yes") | 43 | ;; - colnames (default, nil, means "yes") |
| 44 | ;; - result-params | 44 | ;; - result-params |
| 45 | ;; - out-file | 45 | ;; - out-file |
| 46 | ;; | ||
| 46 | ;; The following are used but not really implemented for SQL: | 47 | ;; The following are used but not really implemented for SQL: |
| 47 | ;; - colname-names | 48 | ;; - colname-names |
| 48 | ;; - rownames | 49 | ;; - rownames |
| 49 | ;; - rowname-names | 50 | ;; - rowname-names |
| 50 | ;; | 51 | ;; |
| 52 | ;; Engines supported: | ||
| 53 | ;; - mysql | ||
| 54 | ;; - dbi | ||
| 55 | ;; - mssql | ||
| 56 | ;; - sqsh | ||
| 57 | ;; - postgresql | ||
| 58 | ;; - oracle | ||
| 59 | ;; - vertica | ||
| 60 | ;; | ||
| 51 | ;; TODO: | 61 | ;; TODO: |
| 52 | ;; | 62 | ;; |
| 53 | ;; - support for sessions | 63 | ;; - support for sessions |
| 54 | ;; - support for more engines (currently only supports mysql) | 64 | ;; - support for more engines |
| 55 | ;; - what's a reasonable way to drop table data into SQL? | 65 | ;; - what's a reasonable way to drop table data into SQL? |
| 56 | ;; | 66 | ;; |
| 57 | 67 | ||
| @@ -116,6 +126,28 @@ SQL Server on Windows and Linux platform." | |||
| 116 | (when database (format "-d \"%s\"" database)))) | 126 | (when database (format "-d \"%s\"" database)))) |
| 117 | " ")) | 127 | " ")) |
| 118 | 128 | ||
| 129 | (defun org-babel-sql-dbstring-sqsh (host user password database) | ||
| 130 | "Make sqsh commmand line args for database connection. | ||
| 131 | \"sqsh\" is one method to access Sybase or MS SQL via Linux platform" | ||
| 132 | (mapconcat #'identity | ||
| 133 | (delq nil | ||
| 134 | (list (when host (format "-S \"%s\"" host)) | ||
| 135 | (when user (format "-U \"%s\"" user)) | ||
| 136 | (when password (format "-P \"%s\"" password)) | ||
| 137 | (when database (format "-D \"%s\"" database)))) | ||
| 138 | " ")) | ||
| 139 | |||
| 140 | (defun org-babel-sql-dbstring-vertica (host port user password database) | ||
| 141 | "Make Vertica command line args for database connection. Pass nil to omit that arg." | ||
| 142 | (mapconcat #'identity | ||
| 143 | (delq nil | ||
| 144 | (list (when host (format "-h %s" host)) | ||
| 145 | (when port (format "-p %d" port)) | ||
| 146 | (when user (format "-U %s" user)) | ||
| 147 | (when password (format "-w %s" (shell-quote-argument password) )) | ||
| 148 | (when database (format "-d %s" database)))) | ||
| 149 | " ")) | ||
| 150 | |||
| 119 | (defun org-babel-sql-convert-standard-filename (file) | 151 | (defun org-babel-sql-convert-standard-filename (file) |
| 120 | "Convert FILE to OS standard file name. | 152 | "Convert FILE to OS standard file name. |
| 121 | If in Cygwin environment, uses Cygwin specific function to | 153 | If in Cygwin environment, uses Cygwin specific function to |
| @@ -179,6 +211,20 @@ footer=off -F \"\t\" %s -f %s -o %s %s" | |||
| 179 | (org-babel-process-file-name in-file) | 211 | (org-babel-process-file-name in-file) |
| 180 | (org-babel-process-file-name out-file) | 212 | (org-babel-process-file-name out-file) |
| 181 | (or cmdline ""))) | 213 | (or cmdline ""))) |
| 214 | (`sqsh (format "sqsh %s %s -i %s -o %s -m csv" | ||
| 215 | (or cmdline "") | ||
| 216 | (org-babel-sql-dbstring-sqsh | ||
| 217 | dbhost dbuser dbpassword database) | ||
| 218 | (org-babel-sql-convert-standard-filename | ||
| 219 | (org-babel-process-file-name in-file)) | ||
| 220 | (org-babel-sql-convert-standard-filename | ||
| 221 | (org-babel-process-file-name out-file)))) | ||
| 222 | (`vertica (format "vsql %s -f %s -o %s %s" | ||
| 223 | (org-babel-sql-dbstring-vertica | ||
| 224 | dbhost dbport dbuser dbpassword database) | ||
| 225 | (org-babel-process-file-name in-file) | ||
| 226 | (org-babel-process-file-name out-file) | ||
| 227 | (or cmdline ""))) | ||
| 182 | (`oracle (format | 228 | (`oracle (format |
| 183 | "sqlplus -s %s < %s > %s" | 229 | "sqlplus -s %s < %s > %s" |
| 184 | (org-babel-sql-dbstring-oracle | 230 | (org-babel-sql-dbstring-oracle |
| @@ -203,18 +249,21 @@ SET MARKUP HTML OFF SPOOL OFF | |||
| 203 | SET COLSEP '|' | 249 | SET COLSEP '|' |
| 204 | 250 | ||
| 205 | ") | 251 | ") |
| 206 | (`mssql "SET NOCOUNT ON | 252 | ((or `mssql `sqsh) "SET NOCOUNT ON |
| 207 | 253 | ||
| 208 | ") | 254 | ") |
| 255 | (`vertica "\\a\n") | ||
| 209 | (_ "")) | 256 | (_ "")) |
| 210 | (org-babel-expand-body:sql body params))) | 257 | (org-babel-expand-body:sql body params) |
| 258 | ;; "sqsh" requires "go" inserted at EOF. | ||
| 259 | (if (string= engine "sqsh") "\ngo" ""))) | ||
| 211 | (org-babel-eval command "") | 260 | (org-babel-eval command "") |
| 212 | (org-babel-result-cond result-params | 261 | (org-babel-result-cond result-params |
| 213 | (with-temp-buffer | 262 | (with-temp-buffer |
| 214 | (progn (insert-file-contents-literally out-file) (buffer-string))) | 263 | (progn (insert-file-contents-literally out-file) (buffer-string))) |
| 215 | (with-temp-buffer | 264 | (with-temp-buffer |
| 216 | (cond | 265 | (cond |
| 217 | ((memq (intern engine) '(dbi mysql postgresql)) | 266 | ((memq (intern engine) '(dbi mysql postgresql sqsh vertica)) |
| 218 | ;; Add header row delimiter after column-names header in first line | 267 | ;; Add header row delimiter after column-names header in first line |
| 219 | (cond | 268 | (cond |
| 220 | (colnames-p | 269 | (colnames-p |
| @@ -239,7 +288,7 @@ SET COLSEP '|' | |||
| 239 | (goto-char (point-max)) | 288 | (goto-char (point-max)) |
| 240 | (forward-char -1)) | 289 | (forward-char -1)) |
| 241 | (write-file out-file)))) | 290 | (write-file out-file)))) |
| 242 | (org-table-import out-file '(16)) | 291 | (org-table-import out-file (if (string= engine "sqsh") '(4) '(16))) |
| 243 | (org-babel-reassemble-table | 292 | (org-babel-reassemble-table |
| 244 | (mapcar (lambda (x) | 293 | (mapcar (lambda (x) |
| 245 | (if (string= (car x) header-delim) | 294 | (if (string= (car x) header-delim) |