Skip to content

Commit

Permalink
Move count to PaginatorTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshieraHuang committed Nov 10, 2021
1 parent 4b11a10 commit 0518199
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
40 changes: 14 additions & 26 deletions src/executor/paginator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,31 @@ where
}
}

#[async_trait::async_trait]
/// Used to enforce constraints on any type that wants to paginate results
pub trait PaginatorTrait<'db, C>
where
C: ConnectionTrait<'db>,
{
/// Select operation
type Selector: SelectorTrait + 'db;
type Selector: SelectorTrait + Send + Sync + 'db;

/// Paginate the result of a select operation.
fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, Self::Selector>;

/// Perform a count on the paginated results
async fn count(self, db: &'db C) -> Result<usize, DbErr>
where
Self: Send + Sized
{
self.paginate(db, 1).num_items().await
}
}

impl<'db, C, S> PaginatorTrait<'db, C> for Selector<S>
where
C: ConnectionTrait<'db>,
S: SelectorTrait + 'db,
S: SelectorTrait + Send + Sync + 'db,
{
type Selector = S;

Expand All @@ -189,7 +198,7 @@ impl<'db, C, M, E> PaginatorTrait<'db, C> for Select<E>
where
C: ConnectionTrait<'db>,
E: EntityTrait<Model = M>,
M: FromQueryResult + Sized + 'db,
M: FromQueryResult + Sized + Send + Sync + 'db,
{
type Selector = SelectModel<M>;

Expand All @@ -203,8 +212,8 @@ where
C: ConnectionTrait<'db>,
E: EntityTrait<Model = M>,
F: EntityTrait<Model = N>,
M: FromQueryResult + Sized + 'db,
N: FromQueryResult + Sized + 'db,
M: FromQueryResult + Sized + Send + Sync + 'db,
N: FromQueryResult + Sized + Send + Sync + 'db,
{
type Selector = SelectTwoModel<M, N>;

Expand All @@ -213,27 +222,6 @@ where
}
}

/// Used to enforce constraints on any type that wants to count results using pagination.
#[async_trait::async_trait]
pub trait CountTrait<'db, C>: PaginatorTrait<'db, C>
where
C: ConnectionTrait<'db>,
{
/// Perform a count on the paginated results
async fn count(self, db: &'db C) -> Result<usize, DbErr>;
}

#[async_trait::async_trait]
impl<'db, C, P, S> CountTrait<'db, C> for P
where
C: ConnectionTrait<'db>,
P: PaginatorTrait<'db, C, Selector = S> + Send,
S: SelectorTrait + Send + Sync + 'db
{
async fn count(self, db:&'db C) -> Result<usize, DbErr> {
self.paginate(db, 1).num_items().await
}
}
#[cfg(test)]
#[cfg(feature = "mock")]
mod tests {
Expand Down
2 changes: 1 addition & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod common;

pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn, CountTrait};
pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn, PaginatorTrait};

// cargo test --features sqlx-sqlite,runtime-async-std-native-tls --test basic
#[sea_orm_macros::test]
Expand Down
2 changes: 1 addition & 1 deletion tests/crud/updates.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use super::*;
use rust_decimal_macros::dec;
use sea_orm::{DbErr, CountTrait};
use sea_orm::{DbErr, PaginatorTrait};
use uuid::Uuid;

pub async fn test_update_cake(db: &DbConn) {
Expand Down

0 comments on commit 0518199

Please sign in to comment.