From mboxrd@z Thu Jan 1 00:00:00 1970 From: stardiviner Subject: Re: [PATCH] ob-sql.el support auto set sql-product in editing sql-mode src block buffer Date: Wed, 12 Feb 2020 15:11:26 +0800 Message-ID: <875zgce0v5.fsf@gmail.com> References: <87tv4d5us5.fsf@gmail.com> <878slppfke.fsf@gmail.com> <877e195dmk.fsf@gmail.com> <87pnezc2xo.fsf@gnu.org> <87ftfqb9ke.fsf@gmail.com> Reply-To: numbchild@gmail.com Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:53090) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j1mBN-0002K8-PM for emacs-orgmode@gnu.org; Wed, 12 Feb 2020 02:11:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j1mBM-00036Q-45 for emacs-orgmode@gnu.org; Wed, 12 Feb 2020 02:11:37 -0500 In-reply-to: <87ftfqb9ke.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane-mx.org@gnu.org Sender: "Emacs-orgmode" To: Bastien Cc: emacs-orgmode@gnu.org =2D----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 Hmmm.. Just a gentle ping ... This might is been missed .... Let me resume = this thread. stardiviner 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 ki= nd 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-mod= e~. > > So I decided to take the simpler solution. I attached patch in the email = attachment. > > # =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D > > BTW, I'm still not good at Elisp, can't write out a good solution for tra= nslate > 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.") >=20=20 > +(defcustom org-babel-sql-engines-alist nil > + "Alist of engine names for :engine header argument. > + > +It is an alist data structure: ( . (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 'sqli= te)))) > + (`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 'mar= iadb '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 'orac= le)))) > + (`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 'mon= etdb)))) > + (`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 'ver= tica)))) > + (`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 'inf= omix)))) > + (`ingres > + (add-to-list 'org-babel-sql-engines-alist (cons 'ingres (list 'ingr= es)))) > + (`interbase > + (add-to-list 'org-babel-sql-engines-alist (cons 'interbase (list 'i= nterbase)))) > + (`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 'syba= se)))))) > + > (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 :dbpasswo= rd)) > (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=3D\"ON_ERROR_STOP=3D1\" %s -A -P \ > footer=3Doff -F \"\t\" %s -f %s -o %s %s" > (if dbpassword > (format "PGPASSWORD=3D%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=3D\"ON_ERROR_STOP=3D1\" %s -A -P \ > footer=3Doff -F \"\t\" %s -f %s -o %s %s" > (if dbpassword > (format "PGPASSWORD=3D%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 =2D --=20 [ 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 =20=20=20=20=20=20 =2D----BEGIN PGP SIGNATURE----- iQFIBAEBCAAyFiEE8J9lDX1nSBmJJZFAG13xyVromsMFAl5DpR8UHG51bWJjaGls ZEBnbWFpbC5jb20ACgkQG13xyVromsMpdwgAnoi3snJMb0j6J54yoFqsly7jrg5L z2DYZGZtg9lHloogAn+qbkso314PkVgrmDYDtHqYgVw67N6rbkP/Sj8NRGZMSa5J RBOXOHx/svurGmz8ICHVAvnDumFBl23ApoHCVY8NJWE7ofpXTkNi33NbShXKsrZb wCiuZFYuKESQ4rwgIasBVn/y1f/chTASMNV2rFVQLW3rL+lIfehw8aRVOTa5azJw Nbnv7nJb4nmQQt/czhy0Bxn2XiOF4CC53iPQW/6zpr428zFPGAw+YlxoQG8qB77q ldmnMIW4L7E9ExCge2EjXSRfpChgkOUzZUXva3DS0TMbtyQ76ptzfm97vA=3D=3D =3DHARo =2D----END PGP SIGNATURE-----