Allow exec_params to accept a Hash

100 views
Skip to first unread message

ama...@tradegecko.com

unread,
Feb 23, 2016, 5:36:59 PM2/23/16
to ruby-pg
As SQL queries get complicated it becomes really hard to track which parameter is which $<num> and it would be nice to be able to reference a param with `$name` or something similar. Otherwise one often reverts to plain old % string formatting which is convenient but does not go through the desired escape/encoding functions by default. The python postgres adapter allows both tuples and dicts to be passed in params http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries. If this seems reasonable I'd willing to come up with a pull request for this.

Alex

Aaron Patterson

unread,
Feb 24, 2016, 2:43:55 PM2/24/16
to rub...@googlegroups.com

Lars Kanis

unread,
Feb 24, 2016, 3:05:34 PM2/24/16
to rub...@googlegroups.com
Hi Alex,

I understand your issue and can confirm that it's not particular convenient, if the number of parameters grows. However the exposed interface is not the interface of any client library, but it's the interface of the PostgreSQL server. And the only way of the server to accept query parameters is per $<num> and a list of values.

The situation on Python is somewhat different, since psycopg implements the generic python DB-API-2.0. That API requires these two forms of parameter binding.

Although Ruby used a similar approach in the past, it has been obsoleted by higher level APIs since years. That is most importantly ActiveRecord and Sequel. Both libraries allow parameter binding in more convenient ways.

You're free to implement your own wrapper to the exec_params etc. calls, but I don't see a good reason to integrate this in ruby-pg. It would cost some performance for the current usage and will probably not be useful for the higher level abstractions. And it would require to add some kind of escape rules for the query string to still allow plain texts like $name in the query string.

--
Regards,
Lars

Reply all
Reply to author
Forward
0 new messages