Allow a prepared Statement to be called like a function
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.
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).
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...)
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.