summaryrefslogtreecommitdiff
path: root/lisp/org/ob-sql.el
diff options
context:
space:
mode:
authorBastien Guerry <bzg@gnu.org>2020-12-13 13:44:15 +0100
committerBastien Guerry <bzg@gnu.org>2020-12-13 13:44:15 +0100
commitf22856a5c54d99867cd24c08a14bbda23d5c6229 (patch)
treeb6bd688963531eccb8b9d18195df0edfc34ba59d /lisp/org/ob-sql.el
parent6aa9fe3e1b4052b2acde86404a90e35893ebfa00 (diff)
downloademacs-f22856a5c54d99867cd24c08a14bbda23d5c6229.tar.gz
emacs-f22856a5c54d99867cd24c08a14bbda23d5c6229.tar.bz2
emacs-f22856a5c54d99867cd24c08a14bbda23d5c6229.zip
Update to Org 9.4.1
Diffstat (limited to 'lisp/org/ob-sql.el')
-rw-r--r--lisp/org/ob-sql.el118
1 files changed, 62 insertions, 56 deletions
diff --git a/lisp/org/ob-sql.el b/lisp/org/ob-sql.el
index 59cf19568ed..7c359b988d3 100644
--- a/lisp/org/ob-sql.el
+++ b/lisp/org/ob-sql.el
@@ -55,7 +55,7 @@
;; - dbi
;; - mssql
;; - sqsh
-;; - postgresql
+;; - postgresql (postgres)
;; - oracle
;; - vertica
;;
@@ -73,6 +73,7 @@
(declare-function orgtbl-to-csv "org-table" (table params))
(declare-function org-table-to-lisp "org-table" (&optional txt))
(declare-function cygwin-convert-file-name-to-windows "cygw32.c" (file &optional absolute-p))
+(declare-function sql-set-product "sql" (product))
(defvar sql-connection-alist)
(defvar org-babel-default-header-args:sql '())
@@ -92,6 +93,13 @@
(org-babel-sql-expand-vars
body (org-babel--get-vars params)))
+(defun org-babel-edit-prep:sql (info)
+ "Set `sql-product' in Org edit buffer.
+Set `sql-product' in Org edit buffer according to the
+corresponding :engine source block header argument."
+ (let ((product (cdr (assq :engine (nth 2 info)))))
+ (sql-set-product product)))
+
(defun org-babel-sql-dbstring-mysql (host port user password database)
"Make MySQL cmd line args for database connection. Pass nil to omit that arg."
(combine-and-quote-strings
@@ -211,64 +219,64 @@ This function is called by `org-babel-execute-src-block'."
(out-file (or (cdr (assq :out-file params))
(org-babel-temp-file "sql-out-")))
(header-delim "")
- (command (pcase (intern engine)
- (`dbi (format "dbish --batch %s < %s | sed '%s' > %s"
- (or cmdline "")
- (org-babel-process-file-name in-file)
- "/^+/d;s/^|//;s/(NULL)/ /g;$d"
- (org-babel-process-file-name out-file)))
- (`monetdb (format "mclient -f tab %s < %s > %s"
- (or cmdline "")
- (org-babel-process-file-name in-file)
- (org-babel-process-file-name out-file)))
- (`mssql (format "sqlcmd %s -s \"\t\" %s -i %s -o %s"
- (or cmdline "")
- (org-babel-sql-dbstring-mssql
- dbhost dbuser dbpassword database)
- (org-babel-sql-convert-standard-filename
- (org-babel-process-file-name in-file))
- (org-babel-sql-convert-standard-filename
- (org-babel-process-file-name out-file))))
- (`mysql (format "mysql %s %s %s < %s > %s"
- (org-babel-sql-dbstring-mysql
- dbhost dbport dbuser dbpassword database)
- (if colnames-p "" "-N")
- (or cmdline "")
- (org-babel-process-file-name in-file)
- (org-babel-process-file-name out-file)))
- (`postgresql (format
- "%spsql --set=\"ON_ERROR_STOP=1\" %s -A -P \
-footer=off -F \"\t\" %s -f %s -o %s %s"
- (if dbpassword
- (format "PGPASSWORD=%s " dbpassword)
- "")
- (if colnames-p "" "-t")
- (org-babel-sql-dbstring-postgresql
- dbhost dbport dbuser database)
- (org-babel-process-file-name in-file)
- (org-babel-process-file-name out-file)
- (or cmdline "")))
- (`sqsh (format "sqsh %s %s -i %s -o %s -m csv"
+ (command (cl-case (intern engine)
+ (dbi (format "dbish --batch %s < %s | sed '%s' > %s"
+ (or cmdline "")
+ (org-babel-process-file-name in-file)
+ "/^+/d;s/^|//;s/(NULL)/ /g;$d"
+ (org-babel-process-file-name out-file)))
+ (monetdb (format "mclient -f tab %s < %s > %s"
+ (or cmdline "")
+ (org-babel-process-file-name in-file)
+ (org-babel-process-file-name out-file)))
+ (mssql (format "sqlcmd %s -s \"\t\" %s -i %s -o %s"
(or cmdline "")
- (org-babel-sql-dbstring-sqsh
+ (org-babel-sql-dbstring-mssql
dbhost dbuser dbpassword database)
(org-babel-sql-convert-standard-filename
(org-babel-process-file-name in-file))
(org-babel-sql-convert-standard-filename
(org-babel-process-file-name out-file))))
- (`vertica (format "vsql %s -f %s -o %s %s"
- (org-babel-sql-dbstring-vertica
- dbhost dbport dbuser dbpassword database)
- (org-babel-process-file-name in-file)
- (org-babel-process-file-name out-file)
- (or cmdline "")))
- (`oracle (format
- "sqlplus -s %s < %s > %s"
- (org-babel-sql-dbstring-oracle
- dbhost dbport dbuser dbpassword database)
- (org-babel-process-file-name in-file)
- (org-babel-process-file-name out-file)))
- (_ (error "No support for the %s SQL engine" engine)))))
+ (mysql (format "mysql %s %s %s < %s > %s"
+ (org-babel-sql-dbstring-mysql
+ dbhost dbport dbuser dbpassword database)
+ (if colnames-p "" "-N")
+ (or cmdline "")
+ (org-babel-process-file-name in-file)
+ (org-babel-process-file-name out-file)))
+ ((postgresql postgres) (format
+ "%spsql --set=\"ON_ERROR_STOP=1\" %s -A -P \
+footer=off -F \"\t\" %s -f %s -o %s %s"
+ (if dbpassword
+ (format "PGPASSWORD=%s " dbpassword)
+ "")
+ (if colnames-p "" "-t")
+ (org-babel-sql-dbstring-postgresql
+ dbhost dbport dbuser database)
+ (org-babel-process-file-name in-file)
+ (org-babel-process-file-name out-file)
+ (or cmdline "")))
+ (sqsh (format "sqsh %s %s -i %s -o %s -m csv"
+ (or cmdline "")
+ (org-babel-sql-dbstring-sqsh
+ dbhost dbuser dbpassword database)
+ (org-babel-sql-convert-standard-filename
+ (org-babel-process-file-name in-file))
+ (org-babel-sql-convert-standard-filename
+ (org-babel-process-file-name out-file))))
+ (vertica (format "vsql %s -f %s -o %s %s"
+ (org-babel-sql-dbstring-vertica
+ dbhost dbport dbuser dbpassword database)
+ (org-babel-process-file-name in-file)
+ (org-babel-process-file-name out-file)
+ (or cmdline "")))
+ (oracle (format
+ "sqlplus -s %s < %s > %s"
+ (org-babel-sql-dbstring-oracle
+ dbhost dbport dbuser dbpassword database)
+ (org-babel-process-file-name in-file)
+ (org-babel-process-file-name out-file)))
+ (t (user-error "No support for the %s SQL engine" engine)))))
(with-temp-file in-file
(insert
(pcase (intern engine)
@@ -301,7 +309,7 @@ SET COLSEP '|'
(progn (insert-file-contents-literally out-file) (buffer-string)))
(with-temp-buffer
(cond
- ((memq (intern engine) '(dbi mysql postgresql sqsh vertica))
+ ((memq (intern engine) '(dbi mysql postgresql postgres sqsh vertica))
;; Add header row delimiter after column-names header in first line
(cond
(colnames-p
@@ -365,6 +373,4 @@ SET COLSEP '|'
(provide 'ob-sql)
-
-
;;; ob-sql.el ends here