-
Notifications
You must be signed in to change notification settings - Fork 25
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
fix: support repeated named parameters #237
fix: support repeated named parameters #237
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
68a9f06
to
3feca7c
Compare
This fixes `got %v argument values, but found %v parameters in the sql string` error when using repeated named arguments. The "workaround" is to specify as many parameters as observed. The last one will win due to `prepareSpannerStmt` using map keyed by name. Due to this change the `Bad` examples will start to return an error. Example: ```golang // Bad: @name will be "value2" s.db.QueryRow( "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)", sql.Named("name", "value"), sql.Named("name", "value2"), ) // Bad: @name will be "value2" s.db.QueryRow( "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)", "value", "value2", ) // Good: works, ideal usage s.db.QueryRow( "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)", sql.Named("name", "value"), ) // Good: not ideal, inferred positional arguments s.db.QueryRow( "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)", "value", ) ``` Signed-off-by: Kamil Trzciński <[email protected]>
3feca7c
to
90cff3d
Compare
@ayufan Thanks for the contribution. While I agree that the proposed behavior would probably have been the best from the start, changing it in the way that this pull request does is a breaking change for anyone who currently uses any of the workarounds that are marked as 'bad' in the description, as it goes from working (albeit not in a great way), into returning an error. Let me see if there is a combination that retains the current behavior, but also fixes it for the more logical use case (especially the one that uses named parameters). |
Named parameters that occurred multiple times in the SQL string had to be added to the list of arguments multiple times, even if these were given as named arguments. This fix allows queries that use named parameters to accept only one occurence of the named parameter. This also clarifies the use of named parameters and positional parameters, and that mixing named parameters with positional arguments is not a good idea. Based on the issue reported in #237
@ayufan We've created an alternative solution in #240 that retains the current behavior, but also adds support for re-using named parameters. It also updates the documentation to clarify what the recommended usage is of named vs positional parameters. We are therefore going to close this pull request, but I would still like to thank you for pointing out this issue in the driver, and for the proposed fix. |
Named parameters that occurred multiple times in the SQL string had to be added to the list of arguments multiple times, even if these were given as named arguments. This fix allows queries that use named parameters to accept only one occurence of the named parameter. This also clarifies the use of named parameters and positional parameters, and that mixing named parameters with positional arguments is not a good idea. Based on the issue reported in #237
Thank you @olavloite for swift fix. I'm happy with a "balanced" solution for this. |
This fixes
got %v argument values, but found %v parameters in the sql string
error when using repeated named arguments.The "workaround" is to specify as many parameters as observed. The last one will win due to
prepareSpannerStmt
using map keyed by name. The fix will make theBad
examples to return an error, whileGood
will start to work.Example:
Disclaimer
GitLab is working on a new project Topology Service that plans to use Cloud Spanner. This has been an identified bug and part of effort to upstream fixes. Proof of concept is not public, below are public materials available: