-
Notifications
You must be signed in to change notification settings - Fork 913
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
Implement Named Parameters #534
Comments
Looks like PostgreSQL doesn't have any special support for named parameters in prepared statements, so is the idea that this would be implemented entirely in the driver?
Not 100% sure if it's worthwhile at the driver level, since it can also be done at a higher level: http://jmoiron.github.io/sqlx/#namedParams (But if so, that implementation could be worth looking at) PostgreSQL does have a named parameter notation when calling functions: Not sure if it provides any benefit over passing $1 over to PostgreSQL if the SQL sent to the driver looks the same either way. |
I don't understand what you're asking here.
The parameter symbols/markers For example, the following query multiplies a number by three: err := db.QueryRow(`SELECT $1 + $1 + $1`, x).Scan(&y) |
When speaking to the prefix, I was referencing the google document for Specifically, there was a section (appears to me gone/edited) that I quoted from the above document in the linked ticket which appeared to suggest the driver pick a prefix and only accept that. I was initially thinking about just supporting the Example: err := db.QueryRow(`SELECT :Num + :Num + :Num`, sql.Named("Num", x)).Scan(&y) The above should result in the same query in your example, just the driver would dedupe the references and mark them all as the same ordinal argument. Thanks for your time. |
@itsjamie That deduping makes sense to me, and how I would expect it to work. I'm just an onlooker though, not the project lead. |
I haven't planned anything around it, was waiting for someone to give the OK on implementation before starting any work. |
It seems to me that the only safe way to implement this is with a parser that can handle all forms of quoting. Without it, any prefix we pick will break an existing app out there. We could skimp on the parser by making the user specify a prefix. In that case, using a literal containing their prefix is their fault. No configured prefix could mean no named arguments. At the same time, configuring Re: de-duping. I see why I was confused. In your example the driver receives only one NamedValue. From my perspective there's nothing to reduce/consolidate. Are there any guidelines for how a driver should behave if two NamedValue arrive with the same Name? Perhaps |
Is this really something that the driver should do? I think sqlx handles this quite well. To me it doesn't feel like this should be handled at the driver level as it's not a postgres feature.. |
@GeertJohan - Not everyone uses the sqlx package and I don't think they should be required to in order to have access to Go stdlib features. I agree, it isn't a Postgres feature, but it is a feature of database/sql as of Go 1.8. A quick excerpt from the 1.8 release notes: "The other features require driver support in database/sql/driver. Driver authors should review the new interfaces. Users of existing driver should review the driver documentation to see what it supports and any system specific documentation on each feature." To me, at least, this clearly states that it is the responsibility of the driver to implement these features. |
I agree that a database/sql/driver is the only thing that can implement these database/sql features. My interpretation of the 1.8 release notes is:
|
I agree with your interpretation @cbandy. I submitted this issue because I feel it is likely the next generation of query builders for Also, when writing composable queries, such as using |
any progress on this? 😊 🚀 |
I will try to work on it. Working on a Golang backend with huge SQL query, such feature can help us a lot to have cleaner SQL code. |
@jeromedoucet there is an issue in sqlx about named query that can help you I'm not sure it should be implemented in a driver, but if it will be i will use it ! |
Hi @jeromedoucet Let me know if I can help. |
what: Add named parameters support for "conn". It works basically like ordinals parameters, but with an alphabetical label prefixed with ":" Example: Select $1, $2, $1; may become Select :firstName, :lastName, :firstName Why: SQL queries may be difficult to maintain, especially when the complexity is growing. Ordinal parameters are great, but may not be meaningfull for the one who is trying to understand the query. The goal here is not to have something full featured and complex : pq is "just" a driver, but having a thin layer, simple to maintain, that will fit 90% of the needs. Limitations: Its simplicity means that there is many limitations : First of all, using mixed types of parameters is not allowed. Man can not use ordinal parameters and named parameters is the same query. Refs lib#534
Add explicit cast for some postgres version. Refs lib#534
I too want this, but I also understand the concerns and hesitations around bringing it in. I have a workaround for now, and few ideas and would love feedback on: Currently I use this workaround:
and use it like:
It's a bit crude and naive, but it works for basic use cases. To land support in
I'm excited to hear any feedback! -b |
The addition of named parameters support in Go 1.8 is documented in #531, tracking this individual change here.
Notes from the document:
I would like to implement this in lib/pq, but figured some discussion would be good. For example, should
lib/pq
require a specific prefix? I would imagine no because they will be reduced down to$1
and$2
ordinal operators.I would still want to implement named parameters though so that any shared names in a query can be automatically reduced to the same ordinal parameter.
Thoughts?
The text was updated successfully, but these errors were encountered: