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

Allow a prepared Statement to be called like a function #220

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

quildtide
Copy link
Contributor

I'm aware that this maybe doesn't go entirely hand-in-hand with the intent of this library, but here's my pitch:

I've noticed that, in my own workflow, I often construct a Statement ahead of time and then call execute on it several times later.

In a lot of ways, the way I treat the Statement is how I might treat a function, except I can't call it directly.

I personally think it would be Julia-idiomatic for a Statement to be a functor that can be called, e.g.:

stmt = LibPQ.prepare("... \$1;")
data = [["hello"], ["world"]]
map(stmt, data)

I have also added tests to make sure that the current Statement tests also yield the same results using the functor call, and I have added some degree of documentation.

@quildtide
Copy link
Contributor Author

quildtide commented May 25, 2021

A second commit has been made after I realized that it's even more idiomatic to allow the following:

stmt = LibPQ.prepare("... \$1 ... \$2;")
stmt("hello", "world")

The previous commit required users to do

stmt = LibPQ.prepare("... \$1 ... \$2;")
stmt(["hello", "world"])

The second syntax is still allowed and viable in this 2nd commit, but the first syntax also works now.

stmt(; kwargs...) works when the statement takes 0 parameters. Otherwise, if there is exactly one parameter that is Union{AbstractVector, Tuple}, it will be fed as the parameters argument to execute. In any other case, all parameters given to stmt(parameters...; kwargs...) will be interpreted as a parameter tuple to be fed to the parameters argument to execute.

There is one minor API flaw here if stmt actually expects exactly 1 vector or something as an argument; in that case, the user will have to wrap this vector in an additional vector or tuple in order to get the desired result (otherwise, Postgres will error).

@c42f
Copy link
Contributor

c42f commented Jun 18, 2021

I can't speak for the owners of this repo, but making prepared statements callable seems like a reasonable idea to me.

There is one minor API flaw here if stmt actually expects exactly 1 vector or something as an argument; in that case, the user will have to wrap this vector in an additional vector or tuple in order to get the desired result (otherwise, Postgres will error).

This seems problematic. How about just removing the vector form, as the same effect can be had with splatting:

stmt = LibPQ.prepare("... \$1 ... \$2;")
stmt("hello", "world")

args = ["hello", "world"]
stmt(args...)

@quildtide
Copy link
Contributor Author

There is one minor API flaw here if stmt actually expects exactly 1 vector or something as an argument; in that case, the user will have to wrap this vector in an additional vector or tuple in order to get the desired result (otherwise, Postgres will error).

This seems problematic. How about just removing the vector form, as the same effect can be had with splatting:

stmt = LibPQ.prepare("... \$1 ... \$2;")
stmt("hello", "world")

args = ["hello", "world"]
stmt(args...)

Good point. Will adjust the pull request at some point in the near future along these lines.

@quildtide quildtide marked this pull request as draft July 7, 2021 17:42
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 this pull request may close these issues.

2 participants