-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Proof of concept for query_tuple!
, similar to query!
but returns a tuple instead of an anonymous record
#3780
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly why I don't think we should support this. The context is immediately and entirely lost. What if there's 50+ lines of code between the query and where a value is used? It won't be obvious at all what
account.0
oraccount.1
is supposed to be.If the idea was to immediately destructure the tuple into separate bindings, like an augmented
query_scalar!()
, I could maybe understand that:However, even that loses the context that they're all connected to a single (logical if not physical) database record--unless you prefix the names like I did here, but even then,
account_id
is the same number of characters asaccount.id
.I don't see the value here. Honestly, I think this would make for less maintainable code than the status quo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand where you're coming from, but I do think this could have its place. It sounds like a general argument against tuples, not so much an argument about tuples specifically in sqlx.
Don't get me wrong, I agree that tuples are not as precise as structs with named fields. That's why we don't just use tuples everywhere. But tuples do have their place; there is a reason that tuples are in the language, and I think it could be reasonably used within sqlx as well. Certainly not always, but sometimes, much the same way as tuples are used generally in Rust 🙂.
I think the example you give with destructuring is a very nice usage for instance. I mean, I could definitely imagine situations where this...
... would be preferable in comparison to this:
I mean, when you look at it in that light, there really is not much of a difference, but the tuple version is sometimes preferable I would say, just the same way as tuples are sometimes preferable in other situations with Rust types.
Another way to think of it: Think of
query_tuple!
as a kind of compromise betweenquery_scalar!
andquery!
. Tbh, if we hadquery_tuple!
, maybequery_scalar!
wouldn't even be necessary as you could just fetch a tuple of a single element and it would be effectively equivalent.I'd also like to point out that with your line of reasoning, you could argue that
FromRow
should not be implemented for tuples. But it is, and it leads to a kind of weird asymmetry in the API, where I can usequery_as
(the function) to get a tuple, but there is no way to use the checked macros to get a tuple.With your reasoning, it would at least be consistent if
FromRow
was not implemented for tuples and you had to use a named type. But that is not the case and I do feel that is the right choice, because sometimes tuples is a more convenient option. In that vein, I think the right choice would be to allow the same usage for the macro case, hence providingquery_tuple
:)I hope that makes sense and I hope you can see where I'm coming from :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abonander it's been a week so hope you don't mind the ping 🙂. Did you have a chance to read my comment above? Do you have any thoughts on the asymmetry in the API between
query_as
(the function) andquery!
(the macro) with regards to tuples?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pinging me doesn't get me to respond faster. I already get notifications for discussions across the whole repository. I have a full-time job that isn't SQLx and it's kept me very busy lately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally fair, take your time 🙏