Skip to content
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

Revert "Merge pull request #7941 from eth3lbert/count-subq" #8050

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions src/controllers/helpers/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,6 @@ pub(crate) trait Paginate: Sized {
options,
}
}

fn pages_pagination_with_count_query<C>(
self,
options: PaginationOptions,
count_query: C,
) -> PaginatedQueryWithCountSubq<Self, C> {
PaginatedQueryWithCountSubq {
query: self,
count_query,
options,
}
}
}

impl<T> Paginate for T {}
Expand Down Expand Up @@ -315,61 +303,6 @@ pub(crate) fn decode_seek<D: for<'a> Deserialize<'a>>(seek: &str) -> anyhow::Res
Ok(decoded)
}

#[derive(Debug)]
pub(crate) struct PaginatedQueryWithCountSubq<T, C> {
query: T,
count_query: C,
options: PaginationOptions,
}

impl<T, C> QueryId for PaginatedQueryWithCountSubq<T, C> {
const HAS_STATIC_QUERY_ID: bool = false;
type QueryId = ();
}

impl<
T: Query,
C: Query + QueryDsl + diesel::query_dsl::methods::SelectDsl<diesel::dsl::CountStar>,
> Query for PaginatedQueryWithCountSubq<T, C>
{
type SqlType = (T::SqlType, BigInt);
}

impl<T, C, DB> RunQueryDsl<DB> for PaginatedQueryWithCountSubq<T, C> {}

impl<T, C> QueryFragment<Pg> for PaginatedQueryWithCountSubq<T, C>
where
T: QueryFragment<Pg>,
C: QueryFragment<Pg>,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
out.push_sql("SELECT *, (");
self.count_query.walk_ast(out.reborrow())?;
out.push_sql(") FROM (");
self.query.walk_ast(out.reborrow())?;
out.push_sql(") t LIMIT ");
out.push_bind_param::<BigInt, _>(&self.options.per_page)?;
if let Some(offset) = self.options.offset() {
out.push_sql(format!(" OFFSET {offset}").as_str());
}
Ok(())
}
}

impl<T, C> PaginatedQueryWithCountSubq<T, C> {
pub(crate) fn load<'a, U>(self, conn: &mut PgConnection) -> QueryResult<Paginated<U>>
where
Self: LoadQuery<'a, PgConnection, WithCount<U>>,
{
let options = self.options.clone();
let records_and_total = self.internal_load(conn)?.collect::<QueryResult<_>>()?;
Ok(Paginated {
records_and_total,
options,
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading