Skip to content

Commit

Permalink
fix: div ceil to get page count 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Sep 16, 2024
1 parent 2fc9dad commit 8380a32
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/core/src-tauri/src/commands/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub async fn get_paginated_rows(
state: State<'_, Mutex<SharedState>>,
table_name: String,
page_index: u16,
page_size: i32,
page_size: u32,
) -> Result<PaginatedRows> {
let state = state.lock().await;
let pool = &state.pool;
Expand Down
8 changes: 5 additions & 3 deletions crates/handlers/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,28 @@ pub trait TableHandler {
#[async_trait]
/// The logic for this trait is almost identical between all drivers, so default implementation is created.
pub trait RowHandler {
#[allow(clippy::incompatible_msrv)]
async fn get_paginated_rows(
&self,
pool: &AnyPool,
table_name: String,
page_index: u16,
page_size: i32,
page_size: u32,
) -> Result<PaginatedRows> {
let query_str = format!(
"SELECT * FROM {} limit {} offset {};",
table_name,
page_size,
page_index as i32 * page_size
page_index as u32 * page_size
);

let rows = sqlx::query(&query_str).fetch_all(pool).await?;

let query_str = format!("SELECT COUNT(*) from {}", table_name);

let page_count_result = sqlx::query(&query_str).fetch_one(pool).await?;
let page_count = page_count_result.try_get::<i64, usize>(0).unwrap() as i32 / page_size;
let page_count =
(page_count_result.try_get::<i64, usize>(0).unwrap() as u32).div_ceil(page_size);

let paginated_rows = PaginatedRows::new(decode::decode_raw_rows(rows)?, page_count);

Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ pub type ConnectionsFileSchema = HashMap<String, ConnConfig>;
#[serde(rename_all = "camelCase")]
pub struct PaginatedRows {
data: Vec<JsonMap<String, JsonValue>>,
page_count: i32,
page_count: u32,
}

impl PaginatedRows {
pub fn new(data: Vec<JsonMap<String, JsonValue>>, page_count: i32) -> Self {
pub fn new(data: Vec<JsonMap<String, JsonValue>>, page_count: u32) -> Self {
PaginatedRows { data, page_count }
}
}
Expand Down

0 comments on commit 8380a32

Please sign in to comment.