Skip to content

Commit

Permalink
helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsleyh committed Dec 6, 2024
1 parent 40c479f commit 45fa540
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl FilteringOptions {
}
}

fn to_filter_builder(&self) -> Result<FilterBuilder> {
pub fn to_filter_builder(&self) -> Result<FilterBuilder> {
let mut builder = FilterBuilder::new().case_insensitive(self.case_insensitive);

// If there are multiple expressions, wrap them in a group with AND operator
Expand All @@ -348,6 +348,17 @@ impl FilteringOptions {

Ok(builder)
}

pub fn try_from_expressions(
expressions: Vec<Result<FilterExpression, eyre::Error>>,
) -> Result<Option<Self>> {
let expressions: Result<Vec<_>, _> = expressions.into_iter().collect();
match expressions {
Ok(exprs) if !exprs.is_empty() => Ok(Some(Self::new(exprs))),
Ok(_) => Ok(None),
Err(e) => Err(e),
}
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -405,6 +416,14 @@ impl PgFilters {

Ok(sql)
}

pub fn count_sql(&self, schema: &str, table: &str) -> Result<String> {
let mut sql = format!("SELECT COUNT(*) FROM {}.{}", schema, table);
if let Some(filters) = &self.filters {
sql.push_str(&filters.build()?);
}
Ok(sql)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 45fa540

Please sign in to comment.