Skip to main content

PageInfo

Struct PageInfo 

Source
pub struct PageInfo {
    pub total: i64,
    pub page_size: i64,
    pub page_count: i64,
}
Expand description

Pagination metadata container

Fields§

§total: i64

Total number of records

§page_size: i64

Records per page

§page_count: i64

Calculated page count

Implementations§

Source§

impl PageInfo

Source

pub fn new(total: i64, page_size: i64) -> PageInfo

Constructs new PageInfo with automatic page count calculation

§Arguments
  • total - Total records in dataset
  • page_size - Desired records per page
Examples found in repository?
examples/example_adapter.rs (line 164)
122async fn test_adapter_query(url: &str) -> Result<(), Error> {
123    let data = vec![
124        User {
125            id: 1,
126            name: "admin".to_string(),
127        },
128        User {
129            id: 99999,
130            name: "super man".to_string(),
131        },
132    ];
133    //  test count
134    let user_query = UserQuery {
135        user_id: 1,
136        user_name: "admin",
137    };
138    let pool = AnyPool::connect(url).await?;
139
140    let mut db_adatper = user_query.adapter_render();
141
142    let count = db_adatper.count(&pool).await?;
143    assert_eq!(2, count);
144
145    // test pagination
146    let user: Option<User> = user_query
147        .adapter_render()
148        .set_page(1, 1)
149        .fetch_optional_as(&pool)
150        .await?;
151    assert_eq!(data.first(), user.as_ref());
152    // println!("{user:?}");
153
154    let mut conn = pool.acquire().await?;
155    let user: Vec<User> = user_query
156        .adapter_render()
157        .set_page(1, 2)
158        .fetch_all_as(&mut *conn)
159        .await?;
160    assert_eq!(data[1..], user);
161    // println!("{user:?}");
162
163    let page_info = user_query.adapter_render().count_page(1, &pool).await?;
164    assert_eq!(PageInfo::new(2, 1), page_info);
165    // println!("{page_info:?}");
166    //fecth
167    let mut tx = pool.begin().await?;
168
169    let rows = UserQuery {
170        user_id: 1,
171        user_name: "admin",
172    }
173    .adapter_render()
174    .fetch_all(&mut *tx)
175    .await?;
176    assert_eq!(2, rows.len());
177    //println!("{:?}", rows.len());
178    let row = UserQuery {
179        user_id: 1,
180        user_name: "admin",
181    }
182    .adapter_render()
183    .fetch_optional(&mut *tx)
184    .await?;
185    assert!(row.is_some());
186    let row = UserQuery {
187        user_id: 1,
188        user_name: "admin",
189    }
190    .adapter_render()
191    .fetch_one(&mut *tx)
192    .await?;
193    assert_eq!(2, row.columns().len());
194    // fetch_as
195    let users: Vec<User> = user_query.adapter_render().fetch_all_as(&pool).await?;
196    assert_eq!(data, users);
197    //println!("{:?}", users);
198
199    let u: Option<User> = UserQuery {
200        user_id: 1,
201        user_name: "admin",
202    }
203    .adapter_render()
204    .fetch_optional_as(&mut *tx)
205    .await?;
206    assert_eq!(data.first(), u.as_ref());
207    let u: User = UserQuery {
208        user_id: 1,
209        user_name: "admin",
210    }
211    .adapter_render()
212    .fetch_one_as(&mut *tx)
213    .await?;
214    assert_eq!(data.first(), Some(&u));
215
216    // stream
217    let mut query = user_query.adapter_render();
218    {
219        let stream = query.fetch(&mut *tx);
220        pin_mut!(stream);
221        while let Some(row) = stream.try_next().await? {
222            assert_eq!(2, row.columns().len());
223        }
224    }
225
226    tx.rollback().await?;
227
228    Ok(())
229}
More examples
Hide additional examples
examples/example_axum.rs (line 100)
58async fn test_adapter_query(url: &str) -> Result<(), Error> {
59    let data = vec![
60        User {
61            id: 1,
62            name: "admin".to_string(),
63        },
64        User {
65            id: 99999,
66            name: "super man".to_string(),
67        },
68    ];
69    //  test count
70    let user_query = UserQuery {
71        user_id: 1,
72        user_name: "admin",
73    };
74    let pool = AnyPool::connect(url).await?;
75
76    let mut db_adatper = user_query.adapter_render();
77
78    let count = db_adatper.count(&pool).await?;
79    assert_eq!(2, count);
80
81    // test pagination
82    let user: Option<User> = user_query
83        .adapter_render()
84        .set_page(1, 1)
85        .fetch_optional_as(&pool)
86        .await?;
87    assert_eq!(data.first(), user.as_ref());
88    // println!("{user:?}");
89
90    let mut conn = pool.acquire().await?;
91    let user: Vec<User> = user_query
92        .adapter_render()
93        .set_page(1, 2)
94        .fetch_all_as(&mut *conn)
95        .await?;
96    assert_eq!(data[1..], user);
97    // println!("{user:?}");
98
99    let page_info = user_query.adapter_render().count_page(1, &pool).await?;
100    assert_eq!(PageInfo::new(2, 1), page_info);
101    // println!("{page_info:?}");
102    //fecth
103    let mut tx = pool.begin().await?;
104
105    let rows = UserQuery {
106        user_id: 1,
107        user_name: "admin",
108    }
109    .adapter_render()
110    .fetch_all(&mut *tx)
111    .await?;
112    assert_eq!(2, rows.len());
113    //println!("{:?}", rows.len());
114    let row = UserQuery {
115        user_id: 1,
116        user_name: "admin",
117    }
118    .adapter_render()
119    .fetch_optional(&mut *tx)
120    .await?;
121    assert!(row.is_some());
122    let row = UserQuery {
123        user_id: 1,
124        user_name: "admin",
125    }
126    .adapter_render()
127    .fetch_one(&mut *tx)
128    .await?;
129    assert_eq!(2, row.columns().len());
130    // fetch_as
131    let users: Vec<User> = user_query.adapter_render().fetch_all_as(&pool).await?;
132    assert_eq!(data, users);
133    //println!("{:?}", users);
134
135    let u: Option<User> = UserQuery {
136        user_id: 1,
137        user_name: "admin",
138    }
139    .adapter_render()
140    .fetch_optional_as(&mut *tx)
141    .await?;
142    assert_eq!(data.first(), u.as_ref());
143    let u: User = UserQuery {
144        user_id: 1,
145        user_name: "admin",
146    }
147    .adapter_render()
148    .fetch_one_as(&mut *tx)
149    .await?;
150    assert_eq!(data.first(), Some(&u));
151
152    // stream
153    let mut query = user_query.adapter_render();
154    {
155        let stream = query.fetch(&mut *tx);
156        pin_mut!(stream);
157        while let Some(row) = stream.try_next().await? {
158            assert_eq!(2, row.columns().len());
159        }
160    }
161
162    tx.rollback().await?;
163
164    Ok(())
165}

Trait Implementations§

Source§

impl Debug for PageInfo

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for PageInfo

Source§

fn eq(&self, other: &PageInfo) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for PageInfo

Source§

impl StructuralPartialEq for PageInfo

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more