From 10bcb0fa4a5176657604a8f17c828ff5a60eddaf Mon Sep 17 00:00:00 2001 From: Phil Estival Date: Tue, 26 Nov 2024 11:34:48 +0100 Subject: [PATCH 01/10] ob-sql: untabify --- lisp/ob-sql.el | 288 ++++++++++++++++++++++++------------------------- 1 file changed, 144 insertions(+), 144 deletions(-) diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el index d7bcaa097..24870b354 100644 --- a/lisp/ob-sql.el +++ b/lisp/ob-sql.el @@ -86,20 +86,20 @@ (defvar org-babel-default-header-args:sql '()) (defconst org-babel-header-args:sql - '((engine . :any) - (out-file . :any) - (dbhost . :any) - (dbport . :any) - (dbuser . :any) - (dbpassword . :any) - (dbinstance . :any) - (database . :any)) + '((engine . :any) + (out-file . :any) + (dbhost . :any) + (dbport . :any) + (dbuser . :any) + (dbpassword . :any) + (dbinstance . :any) + (database . :any)) "SQL-specific header arguments.") (defun org-babel-expand-body:sql (body params) "Expand BODY according to the values of PARAMS." (let ((prologue (cdr (assq :prologue params))) - (epilogue (cdr (assq :epilogue params)))) + (epilogue (cdr (assq :epilogue params)))) (mapconcat 'identity (list prologue @@ -120,11 +120,11 @@ corresponding :engine source block header argument." (mapconcat #'identity (delq nil - (list (when host (concat "-h" (shell-quote-argument host))) - (when port (format "-P%d" port)) - (when user (concat "-u" (shell-quote-argument user))) - (when password (concat "-p" (shell-quote-argument password))) - (when database (concat "-D" (shell-quote-argument database))))) + (list (when host (concat "-h" (shell-quote-argument host))) + (when port (format "-P%d" port)) + (when user (concat "-u" (shell-quote-argument user))) + (when password (concat "-p" (shell-quote-argument password))) + (when database (concat "-D" (shell-quote-argument database))))) " ")) (defun org-babel-sql-dbstring-postgresql (host port user database) @@ -133,10 +133,10 @@ Pass nil to omit that arg." (mapconcat #'identity (delq nil - (list (when host (concat "-h" (shell-quote-argument host))) - (when port (format "-p%d" port)) - (when user (concat "-U" (shell-quote-argument user))) - (when database (concat "-d" (shell-quote-argument database))))) + (list (when host (concat "-h" (shell-quote-argument host))) + (when port (format "-p%d" port)) + (when user (concat "-U" (shell-quote-argument user))) + (when database (concat "-d" (shell-quote-argument database))))) " ")) (defun org-babel-sql-dbstring-oracle (host port user password database) @@ -158,22 +158,22 @@ using its alias." (when database (setq database (shell-quote-argument database))) (when host (setq host (shell-quote-argument host))) (cond ((and user password database host port) - (format "%s/%s@%s:%d/%s" user password host port database)) - ((and user password database) - (format "%s/%s@%s" user password database)) - (t (user-error "Missing information to connect to database")))) + (format "%s/%s@%s:%d/%s" user password host port database)) + ((and user password database) + (format "%s/%s@%s" user password database)) + (t (user-error "Missing information to connect to database")))) (defun org-babel-sql-dbstring-mssql (host user password database) "Make sqlcmd command line args for database connection. `sqlcmd' is the preferred command line tool to access Microsoft SQL Server on Windows and Linux platform." (mapconcat #'identity - (delq nil - (list (when host (format "-S \"%s\"" (shell-quote-argument host))) - (when user (format "-U \"%s\"" (shell-quote-argument user))) - (when password (format "-P \"%s\"" (shell-quote-argument password))) - (when database (format "-d \"%s\"" (shell-quote-argument database))))) - " ")) + (delq nil + (list (when host (format "-S \"%s\"" (shell-quote-argument host))) + (when user (format "-U \"%s\"" (shell-quote-argument user))) + (when password (format "-P \"%s\"" (shell-quote-argument password))) + (when database (format "-d \"%s\"" (shell-quote-argument database))))) + " ")) (defun org-babel-sql-dbstring-sqsh (host user password database) "Make sqsh command line args for database connection. @@ -190,13 +190,13 @@ SQL Server on Windows and Linux platform." "Make Vertica command line args for database connection. Pass nil to omit that arg." (mapconcat #'identity - (delq nil - (list (when host (format "-h %s" (shell-quote-argument host))) - (when port (format "-p %d" port)) - (when user (format "-U %s" (shell-quote-argument user))) - (when password (format "-w %s" (shell-quote-argument password) )) - (when database (format "-d %s" (shell-quote-argument database))))) - " ")) + (delq nil + (list (when host (format "-h %s" (shell-quote-argument host))) + (when port (format "-p %d" port)) + (when user (format "-U %s" (shell-quote-argument user))) + (when password (format "-w %s" (shell-quote-argument password) )) + (when database (format "-d %s" (shell-quote-argument database))))) + " ")) (defun org-babel-sql-dbstring-saphana (host port instance user password database) "Make SAP HANA command line args for database connection. @@ -220,9 +220,9 @@ If in Cygwin environment, uses Cygwin specific function to convert the file name. In a Windows-NT environment, do nothing. Otherwise, use Emacs's standard conversion function." (cond ((fboundp 'cygwin-convert-file-name-to-windows) - (format "%S" (cygwin-convert-file-name-to-windows file))) - ((string= "windows-nt" system-type) file) - (t (format "%S" (convert-standard-filename file))))) + (format "%S" (cygwin-convert-file-name-to-windows file))) + ((string= "windows-nt" system-type) file) + (t (format "%S" (convert-standard-filename file))))) (defun org-babel-find-db-connection-param (params name) "Return database connection parameter NAME. @@ -260,80 +260,80 @@ This function is called by `org-babel-execute-src-block'." (in-file (org-babel-temp-file "sql-in-")) (out-file (or (cdr (assq :out-file params)) (org-babel-temp-file "sql-out-"))) - (header-delim "") + (header-delim "") (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))) + (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)))) + (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 postgres) + (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 - "%s%s --set=\"ON_ERROR_STOP=1\" %s -A -P \ + "%s%s --set=\"ON_ERROR_STOP=1\" %s -A -P \ footer=off -F \"\t\" %s -f %s -o %s %s" - (if dbpassword - (format "PGPASSWORD=%s " + (if dbpassword + (format "PGPASSWORD=%s " (shell-quote-argument dbpassword)) - "") + "") (or (bound-and-true-p sql-postgres-program) "psql") - (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 ""))) + (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))) - (saphana (format "hdbsql %s -I %s -o %s %s" - (org-babel-sql-dbstring-saphana - dbhost dbport dbinstance dbuser dbpassword database) - (org-babel-process-file-name in-file) - (org-babel-process-file-name out-file) - (or cmdline ""))) + "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))) + (saphana (format "hdbsql %s -I %s -o %s %s" + (org-babel-sql-dbstring-saphana + dbhost dbport dbinstance dbuser dbpassword database) + (org-babel-process-file-name in-file) + (org-babel-process-file-name out-file) + (or cmdline ""))) (t (user-error "No support for the %s SQL engine" engine))))) (with-temp-file in-file (insert (pcase (intern engine) - (`dbi "/format partbox\n") + (`dbi "/format partbox\n") (`oracle "SET PAGESIZE 50000 SET NEWPAGE 0 SET TAB OFF @@ -348,56 +348,56 @@ SET MARKUP HTML OFF SPOOL OFF SET COLSEP '|' ") - ((or `mssql `sqsh) "SET NOCOUNT ON + ((or `mssql `sqsh) "SET NOCOUNT ON ") - (`vertica "\\a\n") - (_ "")) + (`vertica "\\a\n") + (_ "")) (org-babel-expand-body:sql body params) ;; "sqsh" requires "go" inserted at EOF. (if (string= engine "sqsh") "\ngo" ""))) (org-babel-eval command "") (org-babel-result-cond result-params (with-temp-buffer - (progn (insert-file-contents-literally out-file) (buffer-string))) + (progn (insert-file-contents-literally out-file) (buffer-string))) (with-temp-buffer - (cond - ((memq (intern engine) '(dbi mysql postgresql postgres saphana sqsh vertica)) - ;; Add header row delimiter after column-names header in first line - (cond - (colnames-p - (with-temp-buffer - (insert-file-contents out-file) - (goto-char (point-min)) - (forward-line 1) - (insert "-\n") - (setq header-delim "-") - (write-file out-file))))) - (t - ;; Need to figure out the delimiter for the header row - (with-temp-buffer - (insert-file-contents out-file) - (goto-char (point-min)) - (when (re-search-forward "^\\(-+\\)[^-]" nil t) - (setq header-delim (match-string-no-properties 1))) - (goto-char (point-max)) - (forward-char -1) - (while (looking-at "\n") - (delete-char 1) - (goto-char (point-max)) - (forward-char -1)) - (write-file out-file)))) - (org-table-import out-file (if (string= engine "sqsh") '(4) '(16))) - (org-babel-reassemble-table - (mapcar (lambda (x) - (if (string= (car x) header-delim) - 'hline - x)) - (org-table-to-lisp)) - (org-babel-pick-name (cdr (assq :colname-names params)) - (cdr (assq :colnames params))) - (org-babel-pick-name (cdr (assq :rowname-names params)) - (cdr (assq :rownames params)))))))) + (cond + ((memq (intern engine) '(dbi mysql postgresql postgres saphana sqsh vertica)) + ;; Add header row delimiter after column-names header in first line + (cond + (colnames-p + (with-temp-buffer + (insert-file-contents out-file) + (goto-char (point-min)) + (forward-line 1) + (insert "-\n") + (setq header-delim "-") + (write-file out-file))))) + (t + ;; Need to figure out the delimiter for the header row + (with-temp-buffer + (insert-file-contents out-file) + (goto-char (point-min)) + (when (re-search-forward "^\\(-+\\)[^-]" nil t) + (setq header-delim (match-string-no-properties 1))) + (goto-char (point-max)) + (forward-char -1) + (while (looking-at "\n") + (delete-char 1) + (goto-char (point-max)) + (forward-char -1)) + (write-file out-file)))) + (org-table-import out-file (if (string= engine "sqsh") '(4) '(16))) + (org-babel-reassemble-table + (mapcar (lambda (x) + (if (string= (car x) header-delim) + 'hline + x)) + (org-table-to-lisp)) + (org-babel-pick-name (cdr (assq :colname-names params)) + (cdr (assq :colnames params))) + (org-babel-pick-name (cdr (assq :rowname-names params)) + (cdr (assq :rownames params)))))))) (defun org-babel-sql-expand-vars (body vars &optional sqlite) "Expand the variables held in VARS in BODY. @@ -409,9 +409,9 @@ argument mechanism." (mapc (lambda (pair) (setq body - (replace-regexp-in-string - (format "$%s" (car pair)) - (let ((val (cdr pair))) + (replace-regexp-in-string + (format "$%s" (car pair)) + (let ((val (cdr pair))) (if (listp val) (let ((data-file (org-babel-temp-file "sql-data-"))) (with-temp-file data-file @@ -419,11 +419,11 @@ argument mechanism." val (if sqlite nil '(:fmt (lambda (el) (if (stringp el) - el - (format "%S" el)))))))) + el + (format "%S" el)))))))) data-file) (if (stringp val) val (format "%S" val)))) - body t t))) + body t t))) vars) body) -- 2.39.5