emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: stardiviner <numbchild@gmail.com>
To: Org Mode <emacs-orgmode@gnu.org>
Cc: Bastien Guerry <bzg@bzg.fr>, Nicolas Goaziou <mail@nicolasgoaziou.fr>
Subject: Re: [PATCH] ob-sql.el support auto set sql-product in editing sql-mode src block buffer
Date: Wed, 05 Feb 2020 23:46:06 +0800	[thread overview]
Message-ID: <8736bp596p.fsf@gmail.com> (raw)
In-Reply-To: <87ftfqb9ke.fsf@gmail.com>


About this patch, patch, what do you think? Nicolas and Bastien.

stardiviner <numbchild@gmail.com> writes:

> I tried to write an alist of all database names. and write an function to used
> to match name to possible names. But I found this solution is a little kind of
> complicated.
>
> Another simple solution is just add an duplicate code of "postgresql" for alias
> "postgres". Because I found only one exception "postgresql" is "postgres" in
> ~sql-mode~ products alist. Other database names are matched with ~sql-mode~.
>
> So I decided to take the simpler solution. I attached patch in the email attachment.
>
> # ==============================================================================
>
> BTW, I'm still not good at Elisp, can't write out a good solution for translate
> and matching for database names.
>
> Here is my temporary databases alist:
>
> #+begin_src diff
> modified   lisp/ob-sql.el
> @@ -87,6 +87,52 @@ (defconst org-babel-header-args:sql
>      (database	       . :any))
>    "SQL-specific header arguments.")
>  
> +(defcustom org-babel-sql-engines-alist nil
> +  "Alist of engine names for :engine header argument.
> +
> +It is an alist data structure: (<engine_identity> . (list of alias names)).
> +Merged with sql-mode's `sql-product-alist'."
> +  :type '(alist :key-type symbol :value-type list)
> +  :group 'org-babel)
> +
> +;;; initialize `org-babel-sql-engines-alist'
> +(dolist (product (mapcar 'car sql-product-alist))
> +  (pcase product
> +    (`ansi
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'ansi (list 'ansi))))
> +    (`sqlite
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'sqlite (list 'sqlite))))
> +    (`mysql
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'mysql (list 'mysql))))
> +    (`mariadb
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'mariadb (list 'mariadb 'maria))))
> +    (`postgresql
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'postgresql (list 'postgresql 'postgres))))
> +    (`oracle
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'oracle (list 'oracle))))
> +    (`mssql
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'mssql (list 'mssql 'ms))))
> +    (`dbi
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'dbi (list 'dbi))))
> +    (`monetdb
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'monetdb (list 'monetdb))))
> +    (`sqsh
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'sqsh (list 'sqsh))))
> +    (`vertica
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'vertica (list 'vertica))))
> +    (`db2
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'db2 (list 'db2))))
> +    (`infomix
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'infomix (list 'infomix))))
> +    (`ingres
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'ingres (list 'ingres))))
> +    (`interbase
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'interbase (list 'interbase))))
> +    (`solid
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'solid (list 'solid))))
> +    (`sybase
> +     (add-to-list 'org-babel-sql-engines-alist (cons 'sybase (list 'sybase))))))
> +
>  (defun org-babel-expand-body:sql (body params)
>    "Expand BODY according to the values of PARAMS."
>    (org-babel-sql-expand-vars
>
> #+end_src
>
> I don't know how to write a translation function for alist to be used in function ~org-babel-execute:sql~.
>
> #+begin_src emacs-lisp :eval no
> (defun org-babel-execute:sql (body params)
>   "Execute a block of Sql code with Babel.
> This function is called by `org-babel-execute-src-block'."
>   (let* ((result-params (cdr (assq :result-params params)))
>          (cmdline (cdr (assq :cmdline params)))
>          (dbhost (org-babel-find-db-connection-param params :dbhost))
>          (dbport (org-babel-find-db-connection-param params :dbport))
>          (dbuser (org-babel-find-db-connection-param params :dbuser))
>          (dbpassword (org-babel-find-db-connection-param params :dbpassword))
>          (database (org-babel-find-db-connection-param params :database))
>          (engine (cdr (assq :engine params)))
>          (colnames-p (not (equal "no" (cdr (assq :colnames params)))))
>          (in-file (org-babel-temp-file "sql-in-"))
>          (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 "")))
> 		    (`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)))
>                     (_ (error "No support for the %s SQL engine" engine)))))
>        ......
> #+end_src


-- 
[ stardiviner ]
       I try to make every word tell the meaning what I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3
      

  reply	other threads:[~2020-02-05 15:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-30  6:21 [QUESTION] A decision about matching ob-sql.el's :engine header argument match sql-mode product name stardiviner
2020-01-30  7:29 ` Tim Cross
2020-01-30 12:32   ` stardiviner
2020-01-31 10:53     ` Bastien
2020-02-04 16:29       ` [PATCH] ob-sql.el support auto set sql-product in editing sql-mode src block buffer stardiviner
2020-02-05 15:46         ` stardiviner [this message]
2020-02-12  7:11         ` stardiviner
2020-02-12  8:06           ` Bastien
2020-02-12  8:41             ` [SOLVED] " stardiviner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8736bp596p.fsf@gmail.com \
    --to=numbchild@gmail.com \
    --cc=bzg@bzg.fr \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).