Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't pass a single array as query param #100

Closed
straight-shoota opened this issue Mar 28, 2019 · 1 comment · Fixed by #110
Closed

Can't pass a single array as query param #100

straight-shoota opened this issue Mar 28, 2019 · 1 comment · Fixed by #110

Comments

@straight-shoota
Copy link
Member

The following query fails (using pg driver):

DB.open(ENV["DATABASE_URL"]) do |db|
  db.exec("SELECT $1::int[]", [1, 2])
end

Error message:

bind message supplies 2 parameters, but prepared statement "" requires 1

The call is essentially equivalent to db.build("SELECT $1::int[]").exec([1, 2]), so QueryMethods#exec builds a statement and forwards args to Statement#exec.

The reason for this fails is that Statement#exec(*args) is shadowed by #exec(args : Array) which can be used to pass a dynamic array of param bindings instead of varargs.
When the only query param is an array, this is interpreted as an array of params, instead of an array as single param.

A workaround is to wrap the array in another array: db.exec("SELECT $1::int[]", [[1, 2]]) which will effectively call #exec(args : Array) with a params array including the intended array as single value.

While the workaround is relatively simple, this behaviour is very unexpected. Every other type works fine as a param provided as vararg. Array should just work as well.

A solution would be to make args : Array a required named argument: #exec(*, args : Array). Thus, when calling exec([1, 2]), it passes the array [1, 2] as single param. When calling exec(args: [1, 2]), it passes two params, 1 and 2. The delegating methods need to get support for this named argument as well.

(The prepared statement being empty in the error message is another issue).

@bcardiff
Copy link
Member

I like the proposal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants