emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Saulius Menkevičius" <saulius.menkevicius@gmail.com>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: emacs-orgmode@gnu.org,
	"Saulius Menkevičius" <saulius.menkevicius@gmail.com>
Subject: Re: [PATCH] lisp/ob-sql.el: add support for :dbport parameter
Date: Tue, 07 Apr 2015 20:36:17 +0300	[thread overview]
Message-ID: <m26197q1ri.fsf@mbp.Dlink> (raw)
In-Reply-To: <87zj6kpilk.fsf@nicolasgoaziou.fr>

[-- Attachment #1: Type: text/plain, Size: 1846 bytes --]


Nicolas Goaziou writes:

> Hello,
>
> Saulius Menkevičius <saulius.menkevicius@gmail.com> writes:
>
>> This (tiny) patch implements ability to set dbport for org-babel sql
>> functionality. I often use ssh port forwarding to connect to remote
>> mysql servers where port is mapped to non-standard one on local
>> machine.
>
> Thank you.
>
>> This is my first patch, below the 15 line threshold.
>>
>> From ca3f85877bdf406deefaf66cbac3483a7e41f134 Mon Sep 17 00:00:00 2001
>> From: =?UTF-8?q?Saulius=20Menkevi=C4=8Dius?= <saulius.menkevicius@gmail.com>
>> Date: Mon, 6 Apr 2015 23:13:06 +0300
>> Subject: [PATCH] ob-sql: Add possibility to set dbport
>>
>>  * lisp/ob-sql.el: will now recognize dbport parameter. Currently it
>>    is supported for mysql engine only.
>
> You need to also specify what functions are modified, e.g.,
>
> * lisp/ob-sql.el (org-babel-sql-dbstring-mysql): Change signature.

Done.

>
>> +(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
>>     (delq nil
>>  	 (list (when host     (concat "-h" host))
>> +	       (when port     (concat "-P" (number-to-string port)))
>
> Isn't PORT a string already?

Apparently it is not a string. I don't know the internal machinery for
#+header parameters in org-mode/babel, but apparently number-to-string
is required, otherwise I get the "progn: Wrong type argument: sequencep,
33060" error. Where 33060 is my port number from the :dbport param.

>
>>           (dbhost (cdr (assoc :dbhost params)))
>> +         (dbport (cdr (assoc :dbport params)))
>
> Nitpick: `assoc' -> `assq'

Ok. I did not know much of elisp to decide on which one to. This was
copy-paste code from previous line, sorry.

Updated patch is attached to this mail.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Patch for lips/ob-sql.el to support database port number --]
[-- Type: text/x-patch, Size: 2694 bytes --]

From e6d7585c4eaba775a370d40135ae8725c04751c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Saulius=20Menkevi=C4=8Dius?= <saulius.menkevicius@gmail.com>
Date: Mon, 6 Apr 2015 23:13:06 +0300
Subject: [PATCH] ob-sql: Add possibility to set dbport

 * lisp/ob-sql.el (org-babel-sql-dbstring-mysql): Change type
   signature to accept server port number too.
   (org-babel-execute:sql) Will now recognize the dbport
   parameter. Passed to org-babel-sql-dbstring-mysql only for now.

TINYCHANGE
---
 lisp/ob-sql.el | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el
index 493b3dc..110a34d 100644
--- a/lisp/ob-sql.el
+++ b/lisp/ob-sql.el
@@ -36,6 +36,7 @@
 ;; - engine
 ;; - cmdline
 ;; - dbhost
+;; - dbport
 ;; - dbuser
 ;; - dbpassword
 ;; - database
@@ -68,6 +69,7 @@
   '((engine	       . :any)
     (out-file	       . :any)
     (dbhost	       . :any)
+    (dbport	       . :any)
     (dbuser	       . :any)
     (dbpassword	       . :any)
     (database	       . :any))
@@ -78,11 +80,12 @@
   (org-babel-sql-expand-vars
    body (mapcar #'cdr (org-babel-get-header params :var))))
 
-(defun org-babel-sql-dbstring-mysql (host user password database)
+(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
    (delq nil
 	 (list (when host     (concat "-h" host))
+	       (when port     (concat "-P" (number-to-string port)))
 	       (when user     (concat "-u" user))
 	       (when password (concat "-p" password))
 	       (when database (concat "-D" database))))))
@@ -102,6 +105,7 @@ This function is called by `org-babel-execute-src-block'."
   (let* ((result-params (cdr (assoc :result-params params)))
          (cmdline (cdr (assoc :cmdline params)))
          (dbhost (cdr (assoc :dbhost params)))
+         (dbport (cdr (assq :dbport params)))
          (dbuser (cdr (assoc :dbuser params)))
          (dbpassword (cdr (assoc :dbpassword params)))
          (database (cdr (assoc :database params)))
@@ -126,7 +130,7 @@ This function is called by `org-babel-execute-src-block'."
                                      (org-babel-process-file-name in-file)
                                      (org-babel-process-file-name out-file)))
                     ('mysql (format "mysql %s %s %s < %s > %s"
-				    (org-babel-sql-dbstring-mysql dbhost dbuser dbpassword database)
+				    (org-babel-sql-dbstring-mysql dbhost dbport dbuser dbpassword database)
 				    (if colnames-p "" "-N")
                                     (or cmdline "")
 				    (org-babel-process-file-name in-file)
-- 
2.3.3


[-- Attachment #3: Type: text/plain, Size: 59 bytes --]



-- 
Saulius Menkevičius (saulius.menkevicius@gmail.com)

  reply	other threads:[~2015-04-07 17:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-06 20:34 [PATCH] lisp/ob-sql.el: add support for :dbport parameter Saulius Menkevičius
2015-04-07  4:49 ` Xavier Maillard
2015-04-07 17:12   ` Saulius Menkevičius
2015-04-07  6:17 ` Nicolas Goaziou
2015-04-07 17:36   ` Saulius Menkevičius [this message]
2015-04-07 20:27     ` Nicolas Goaziou

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=m26197q1ri.fsf@mbp.Dlink \
    --to=saulius.menkevicius@gmail.com \
    --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).