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

fix: correctly calculate page number in table pagination 🐛 #109

Merged
merged 1 commit into from
Sep 17, 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
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
Loading