-
-
Notifications
You must be signed in to change notification settings - Fork 519
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
Add method for retrieiving column names #2148
Conversation
src/executor/query.rs
Outdated
@@ -115,6 +115,27 @@ impl QueryResult { | |||
{ | |||
Ok(T::try_get_many_by_index(self)?) | |||
} | |||
|
|||
/// Gets all column names in the order they were written. | |||
#[cfg(feature = "sqlx-dep")] |
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 think if we move this line to directly above use sqlx::Column
, this can still compile?
sqlx-dep is a 'transitive dependency'. You have to specify a specific backend, e.g. |
Whatever suits your need is good enough for now! Although I'd rename the method to |
Alright, I've made the requested changes and added a test! |
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.
Nice work, thanks!
🎉 Released In 1.0.0-rc.4 🎉Thank you everyone for the contribution! |
🎉 Released In 1.0.0 🎉Thank you everyone for the contribution! |
tl;dr: Add a method for retrieving column names from query results so that applications can use them alongside retrieved rows
Background
For creating some database tooling, I'd like to be able to retrieve database column names that come with the query results. This would be very dynamic because the queries are either generated at runtime from the user, or from introspection. Ideally I'd be able to get to a point where I have something like this:
so that I can display headers in a table and the rows underneath them. Not so different from the output that you'd see in
psql
.Aside
I've considered using sqlx directly, but I can't map the inner types to rust types easily Export
PgType
launchbadge/sqlx#1369, and that seems to be covered in sea-orm.My current implementation is really basic. I'm not sure if it would be better to return an
impl Iterator
for the column names, or return actual column structs with their metadata instead.I wasn't too sure how to write and execute tests for this. I had initially wrote one in the
query.rs
file like:And tried to run it with
cargo t --test query_tests --features sqlx-dep
, but I get a bunch of compile errors sayinguse of undeclared crate or module
sqlx`. Maybe there's a better way?New Features
Breaking Changes