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

replaced usize with u64 in PaginatorTrait #789

Merged
merged 2 commits into from
Aug 20, 2022
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
6 changes: 3 additions & 3 deletions examples/actix3_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
use std::env;
use tera::Tera;

const DEFAULT_POSTS_PER_PAGE: usize = 5;
const DEFAULT_POSTS_PER_PAGE: u64 = 5;

#[derive(Debug, Clone)]
struct AppState {
Expand All @@ -22,8 +22,8 @@ struct AppState {
}
#[derive(Debug, Deserialize)]
pub struct Params {
page: Option<usize>,
posts_per_page: Option<usize>,
page: Option<u64>,
posts_per_page: Option<u64>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions examples/actix_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
use std::env;
use tera::Tera;

const DEFAULT_POSTS_PER_PAGE: usize = 5;
const DEFAULT_POSTS_PER_PAGE: u64 = 5;

#[derive(Debug, Clone)]
struct AppState {
Expand All @@ -23,8 +23,8 @@ struct AppState {

#[derive(Debug, Deserialize)]
pub struct Params {
page: Option<usize>,
posts_per_page: Option<usize>,
page: Option<u64>,
posts_per_page: Option<u64>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions examples/axum_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ async fn main() -> anyhow::Result<()> {

#[derive(Deserialize)]
struct Params {
page: Option<usize>,
posts_per_page: Option<usize>,
page: Option<u64>,
posts_per_page: Option<u64>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
Expand Down
10 changes: 5 additions & 5 deletions examples/jsonrpsee_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ use std::net::SocketAddr;
use tokio::signal::ctrl_c;
use tokio::signal::unix::{signal, SignalKind};

const DEFAULT_POSTS_PER_PAGE: usize = 5;
const DEFAULT_POSTS_PER_PAGE: u64 = 5;

#[rpc(server, client)]
pub trait PostRpc {
#[method(name = "Post.List")]
async fn list(
&self,
page: Option<usize>,
posts_per_page: Option<usize>,
page: Option<u64>,
posts_per_page: Option<u64>,
) -> RpcResult<Vec<post::Model>>;

#[method(name = "Post.Insert")]
Expand All @@ -45,8 +45,8 @@ pub struct PpcImpl {
impl PostRpcServer for PpcImpl {
async fn list(
&self,
page: Option<usize>,
posts_per_page: Option<usize>,
page: Option<u64>,
posts_per_page: Option<u64>,
) -> RpcResult<Vec<post::Model>> {
let page = page.unwrap_or(1);
let posts_per_page = posts_per_page.unwrap_or(DEFAULT_POSTS_PER_PAGE);
Expand Down
6 changes: 3 additions & 3 deletions examples/poem_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sea_orm::{entity::*, query::*, DatabaseConnection};
use serde::Deserialize;
use tera::Tera;

const DEFAULT_POSTS_PER_PAGE: usize = 5;
const DEFAULT_POSTS_PER_PAGE: u64 = 5;

#[derive(Debug, Clone)]
struct AppState {
Expand All @@ -22,8 +22,8 @@ struct AppState {

#[derive(Deserialize)]
struct Params {
page: Option<usize>,
posts_per_page: Option<usize>,
page: Option<u64>,
posts_per_page: Option<u64>,
}

#[handler]
Expand Down
6 changes: 3 additions & 3 deletions examples/rocket_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use pool::Db;
pub use entity::post;
pub use entity::post::Entity as Post;

const DEFAULT_POSTS_PER_PAGE: usize = 5;
const DEFAULT_POSTS_PER_PAGE: u64 = 5;

#[get("/new")]
async fn new() -> Template {
Expand Down Expand Up @@ -80,8 +80,8 @@ async fn update(
#[get("/?<page>&<posts_per_page>")]
async fn list(
conn: Connection<'_, Db>,
page: Option<usize>,
posts_per_page: Option<usize>,
page: Option<u64>,
posts_per_page: Option<u64>,
flash: Option<FlashMessage<'_>>,
) -> Template {
let db = conn.into_inner();
Expand Down
48 changes: 24 additions & 24 deletions src/executor/paginator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ where
S: SelectorTrait + 'db,
{
pub(crate) query: SelectStatement,
pub(crate) page: usize,
pub(crate) page_size: usize,
pub(crate) page: u64,
pub(crate) page_size: u64,
pub(crate) db: &'db C,
pub(crate) selector: PhantomData<S>,
}
Expand All @@ -28,9 +28,9 @@ where
#[derive(Clone, Debug)]
pub struct ItemsAndPagesNumber {
/// The total number of items of a paginator
pub number_of_items: usize,
pub number_of_items: u64,
/// The total number of pages of a paginator
pub number_of_pages: usize,
pub number_of_pages: u64,
}

// LINT: warn if paginator is used without an order by clause
Expand All @@ -41,7 +41,7 @@ where
S: SelectorTrait + 'db,
{
/// Fetch a specific page; page index starts from zero
pub async fn fetch_page(&self, page: usize) -> Result<Vec<S::Item>, DbErr> {
pub async fn fetch_page(&self, page: u64) -> Result<Vec<S::Item>, DbErr> {
let query = self
.query
.clone()
Expand All @@ -65,7 +65,7 @@ where
}

/// Get the total number of items
pub async fn num_items(&self) -> Result<usize, DbErr> {
pub async fn num_items(&self) -> Result<u64, DbErr> {
let builder = self.db.get_database_backend();
let stmt = builder.build(
SelectStatement::new()
Expand All @@ -80,14 +80,14 @@ where
None => return Ok(0),
};
let num_items = match builder {
DbBackend::Postgres => result.try_get::<i64>("", "num_items")? as usize,
_ => result.try_get::<i32>("", "num_items")? as usize,
DbBackend::Postgres => result.try_get::<i64>("", "num_items")? as u64,
_ => result.try_get::<i32>("", "num_items")? as u64,
};
Ok(num_items)
}

/// Get the total number of pages
pub async fn num_pages(&self) -> Result<usize, DbErr> {
pub async fn num_pages(&self) -> Result<u64, DbErr> {
let num_items = self.num_items().await?;
let num_pages = self.compute_pages_number(num_items);
Ok(num_pages)
Expand All @@ -105,8 +105,8 @@ where
}

/// Compute the number of pages for the current page
fn compute_pages_number(&self, num_items: usize) -> usize {
(num_items / self.page_size) + (num_items % self.page_size > 0) as usize
fn compute_pages_number(&self, num_items: u64) -> u64 {
(num_items / self.page_size) + (num_items % self.page_size > 0) as u64
}

/// Increment the page counter
Expand All @@ -115,7 +115,7 @@ where
}

/// Get current page number
pub fn cur_page(&self) -> usize {
pub fn cur_page(&self) -> u64 {
self.page
}

Expand Down Expand Up @@ -215,10 +215,10 @@ where
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>;
fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, Self::Selector>;

/// Perform a count on the paginated results
async fn count(self, db: &'db C) -> Result<usize, DbErr>
async fn count(self, db: &'db C) -> Result<u64, DbErr>
where
Self: Send + Sized,
{
Expand All @@ -233,7 +233,7 @@ where
{
type Selector = S;

fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, S> {
fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, S> {
Paginator {
query: self.query,
page: 0,
Expand All @@ -250,7 +250,7 @@ where
S: SelectorTrait + Send + Sync + 'db,
{
type Selector = S;
fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, S> {
fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, S> {
let sql = &self.stmt.sql[6..];
let mut query = SelectStatement::new();
query.expr(if let Some(values) = self.stmt.values {
Expand All @@ -277,7 +277,7 @@ where
{
type Selector = SelectModel<M>;

fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, Self::Selector> {
fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, Self::Selector> {
self.into_model().paginate(db, page_size)
}
}
Expand All @@ -292,7 +292,7 @@ where
{
type Selector = SelectTwoModel<M, N>;

fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, Self::Selector> {
fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, Self::Selector> {
self.into_model().paginate(db, page_size)
}
}
Expand Down Expand Up @@ -492,9 +492,9 @@ mod tests {
async fn num_pages() -> Result<(), DbErr> {
let (db, num_items) = setup_num_items();

let num_items = num_items as usize;
let page_size = 2_usize;
let num_pages = (num_items / page_size) + (num_items % page_size > 0) as usize;
let num_items = num_items as u64;
let page_size = 2_u64;
let num_pages = (num_items / page_size) + (num_items % page_size > 0) as u64;
let paginator = fruit::Entity::find().paginate(&db, page_size);

assert_eq!(paginator.num_pages().await?, num_pages);
Expand Down Expand Up @@ -524,9 +524,9 @@ mod tests {
async fn num_pages_raw() -> Result<(), DbErr> {
let (db, num_items) = setup_num_items();

let num_items = num_items as usize;
let page_size = 2_usize;
let num_pages = (num_items / page_size) + (num_items % page_size > 0) as usize;
let num_items = num_items as u64;
let page_size = 2_u64;
let num_pages = (num_items / page_size) + (num_items % page_size > 0) as u64;
let paginator = fruit::Entity::find()
.from_raw_sql(RAW_STMT.clone())
.paginate(&db, page_size);
Expand Down