-
Notifications
You must be signed in to change notification settings - Fork 650
Refactor paginating queries to a trait #751
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use diesel::prelude::*; | ||
use diesel::query_builder::*; | ||
use diesel::types::BigInt; | ||
use diesel::pg::Pg; | ||
|
||
pub struct Paginated<T> { | ||
query: T, | ||
limit: i64, | ||
offset: i64, | ||
} | ||
|
||
pub trait Paginate: AsQuery + Sized { | ||
fn paginate(self, limit: i64, offset: i64) -> Paginated<Self::Query> { | ||
Paginated { | ||
query: self.as_query(), | ||
limit, | ||
offset, | ||
} | ||
} | ||
} | ||
|
||
impl<T: AsQuery> Paginate for T {} | ||
|
||
impl<T: Query> Query for Paginated<T> { | ||
type SqlType = (T::SqlType, BigInt); | ||
} | ||
|
||
impl<T> QueryFragment<Pg> for Paginated<T> | ||
where | ||
T: QueryFragment<Pg>, | ||
{ | ||
fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> { | ||
out.push_sql("SELECT *, COUNT(*) OVER () FROM ("); | ||
self.query.walk_ast(out.reborrow())?; | ||
out.push_sql(") t LIMIT "); | ||
out.push_bind_param::<BigInt, _>(&self.limit)?; | ||
out.push_sql(" OFFSET "); | ||
out.push_bind_param::<BigInt, _>(&self.offset)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl_query_id!(Paginated<T>); |
This file contains hidden or 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
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.
It's a little unfortunate that selecting the total number of results is now gone from this location, but we have to still remember to include the
i64
type in theload
type here. Not sure what to do differently though, maybe if we made apaginated_load
that only took the types this code knows about and inserts thei64
?Not a huge deal, this PR is still a huge improvement for factoring out duplication and raw SQL everywhere, just a thought for a possible next step and/or if pagination becomes a more general diesel thing.
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 doubt I'd add it to Diesel, but this could definitely be extracted to a crate. I have considered adding a method that lets you specify the type you're going to deserialize to outside of
load
. (The primary motivation is thatVersion::belonging_to(&crate).load::<Version>
bugs me) If that were present, we could definitely have this be.paginate::<Keyword>
and have the paginate method call that with(T, i64)
. I haven't explored how much work it'd be to add that method though