diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 0ef7dd1..a163b2e 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -336,7 +336,7 @@ impl FilteringOptions { } } - fn to_filter_builder(&self) -> Result { + pub fn to_filter_builder(&self) -> Result { let mut builder = FilterBuilder::new().case_insensitive(self.case_insensitive); // If there are multiple expressions, wrap them in a group with AND operator @@ -348,6 +348,17 @@ impl FilteringOptions { Ok(builder) } + + pub fn try_from_expressions( + expressions: Vec>, + ) -> Result> { + let expressions: Result, _> = 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)] @@ -405,6 +416,14 @@ impl PgFilters { Ok(sql) } + + pub fn count_sql(&self, schema: &str, table: &str) -> Result { + let mut sql = format!("SELECT COUNT(*) FROM {}.{}", schema, table); + if let Some(filters) = &self.filters { + sql.push_str(&filters.build()?); + } + Ok(sql) + } } #[cfg(test)]