Skip to content

Commit

Permalink
Refactor paginator.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbyers committed Mar 31, 2024
1 parent 5cd7192 commit 5fa410f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod jobs;
pub mod sm_scrape;
pub mod subscriptions;
mod utils;
31 changes: 3 additions & 28 deletions src/models/sm_scrape.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use chrono::DateTime;
use libsql::{Connection, Error};

use crate::models::utils::create_paginator;

#[derive(Debug)]
pub struct SMScrapeRow {
pub id: String,
Expand Down Expand Up @@ -64,34 +66,7 @@ pub fn select_with_pagination(
limit: u32,
offset: u32,
) -> String {
let mut result = String::from("SELECT ");
result.push_str(columns);

result.push_str(" FROM sm_scrapes");

if !q.is_empty() {
result.push_str(format!(" WHERE {}", q).as_str());
}

if !sort_by.is_empty() {
result.push_str(format!(" ORDER BY {}", sort_by).as_str());
if !sort_direction.is_empty() {
result.push_str(format!(" {}", sort_direction).as_str());
} else {
result.push_str(" ASC");
}
}

if limit > 0 {
result.push_str(format!(" LIMIT {}", limit).as_str());
}

if offset > 0 {
result.push_str(format!(" OFFSET {}", offset.to_string().as_str()).as_str());
}

result.push(';');
result
create_paginator("sm_scrapes")(columns, q, sort_by, sort_direction, limit, offset)
}

fn select_page(filter_non_null_embeddings: bool) -> String {
Expand Down
34 changes: 34 additions & 0 deletions src/models/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pub fn create_paginator(table_name: &str) -> impl Fn(&str, &str, &str, &str, u32, u32) -> String {
let table_name = table_name.to_string(); // Clone table_name
let paginate = move |columns: &str, q: &str, sort_by: &str, sort_direction: &str, limit: u32, offset: u32| {
let mut result = String::from("SELECT ");
result.push_str(columns);

result.push_str(format!(" FROM {}", table_name).as_str());

if !q.is_empty() {
result.push_str(format!(" WHERE {}", q).as_str());
}

if !sort_by.is_empty() {
result.push_str(format!(" ORDER BY {}", sort_by).as_str());
if !sort_direction.is_empty() {
result.push_str(format!(" {}", sort_direction).as_str());
} else {
result.push_str(" ASC");
}
}

if limit > 0 {
result.push_str(format!(" LIMIT {}", limit).as_str());
}

if offset > 0 {
result.push_str(format!(" OFFSET {}", offset.to_string().as_str()).as_str());
}

result.push(';');
result
};
paginate
}

0 comments on commit 5fa410f

Please sign in to comment.