Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsleyh committed Dec 21, 2024
1 parent 66eed0d commit 0c457ed
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 99 deletions.
63 changes: 34 additions & 29 deletions src/lib/filtering.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;
use crate::ColumnDef;
use eyre::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::fmt::Debug;
use crate::ColumnDef;

#[derive(Debug, Clone, PartialEq)]
pub enum LogicalOperator {
Expand Down Expand Up @@ -421,7 +421,7 @@ impl FilterCondition {
operator.as_sql(),
formatted_values
))
},
}

// Numeric types
FilterCondition::SmallIntValue {
Expand Down Expand Up @@ -685,7 +685,10 @@ impl FilterBuilder {
return Ok(Self::new());
}

fn create_condition(filter: &JsonFilter, column_defs: &HashMap<&str, ColumnDef>) -> FilterCondition {
fn create_condition(
filter: &JsonFilter,
column_defs: &HashMap<&str, ColumnDef>,
) -> FilterCondition {
match column_defs.get(filter.n.as_str()) {
Some(ColumnDef::Uuid(_)) => {
FilterCondition::uuid(&filter.n, parse_operator(&filter.f), Some(&filter.v))
Expand Down Expand Up @@ -717,23 +720,30 @@ impl FilterBuilder {
}
Some(ColumnDef::Boolean(_)) => {
if let Ok(bool_val) = filter.v.parse::<bool>() {
FilterCondition::boolean(&filter.n, parse_operator(&filter.f), Some(bool_val))
FilterCondition::boolean(
&filter.n,
parse_operator(&filter.f),
Some(bool_val),
)
} else {
FilterCondition::text(&filter.n, parse_operator(&filter.f), Some(&filter.v))
}
}
Some(ColumnDef::Timestamp(_)) => {
FilterCondition::timestamp(&filter.n, parse_operator(&filter.f), Some(&filter.v))
}
Some(ColumnDef::Timestamp(_)) => FilterCondition::timestamp(
&filter.n,
parse_operator(&filter.f),
Some(&filter.v),
),
Some(ColumnDef::Text(_)) | Some(ColumnDef::Varchar(_)) => {
FilterCondition::text(&filter.n, parse_operator(&filter.f), Some(&filter.v))
}
_ => FilterCondition::text(&filter.n, parse_operator(&filter.f), Some(&filter.v))
_ => FilterCondition::text(&filter.n, parse_operator(&filter.f), Some(&filter.v)),
}
}

// Always start with the first filter as the base condition
let first_condition = FilterExpression::Condition(create_condition(&filters[0], column_defs));
let first_condition =
FilterExpression::Condition(create_condition(&filters[0], column_defs));

// If only one filter, just return it
if filters.len() == 1 {
Expand Down Expand Up @@ -812,7 +822,6 @@ impl FilterBuilder {
}
}


fn parse_operator(op: &str) -> FilterOperator {
match op {
"LIKE" => FilterOperator::Like,
Expand Down Expand Up @@ -845,7 +854,10 @@ mod tests {
fn setup_test_columns() -> HashMap<&'static str, ColumnDef> {
let mut columns = HashMap::new();
columns.insert("id", ColumnDef::Uuid("id"));
columns.insert("property_full_address", ColumnDef::Text("property_full_address"));
columns.insert(
"property_full_address",
ColumnDef::Text("property_full_address"),
);
columns.insert("client_name", ColumnDef::Text("client_name"));
columns.insert("name", ColumnDef::Text("name"));
columns.insert("email", ColumnDef::Text("email"));
Expand Down Expand Up @@ -1019,7 +1031,8 @@ mod tests {
);

// Test case insensitive
let sql_insensitive = FilterBuilder::from_json_filters(&filters, true, &columns)?.build()?;
let sql_insensitive =
FilterBuilder::from_json_filters(&filters, true, &columns)?.build()?;
assert_eq!(
sql_insensitive,
" WHERE (LOWER(name) LIKE LOWER('%John%') OR LOWER(email) LIKE LOWER('%gmail.com'))"
Expand Down Expand Up @@ -1078,21 +1091,16 @@ mod tests {
#[test]
fn test_uuid_conditions() -> Result<()> {
let columns = setup_test_columns();
let filters = vec![
JsonFilter {
n: "id".to_string(),
f: "=".to_string(),
v: "123e4567-e89b-12d3-a456-426614174000".to_string(),
c: None,
},
];
let filters = vec![JsonFilter {
n: "id".to_string(),
f: "=".to_string(),
v: "123e4567-e89b-12d3-a456-426614174000".to_string(),
c: None,
}];

// Case sensitivity should be ignored for UUID
let sql = FilterBuilder::from_json_filters(&filters, true, &columns)?.build()?;
assert_eq!(
sql,
" WHERE id = '123e4567-e89b-12d3-a456-426614174000'"
);
assert_eq!(sql, " WHERE id = '123e4567-e89b-12d3-a456-426614174000'");
Ok(())
}

Expand Down Expand Up @@ -1154,10 +1162,7 @@ mod tests {
}];

let sql = FilterBuilder::from_json_filters(&filters, true, &columns)?.build()?;
assert_eq!(
sql,
" WHERE created_at > '2024-01-01 00:00:00'"
);
assert_eq!(sql, " WHERE created_at > '2024-01-01 00:00:00'");
Ok(())
}

Expand Down
66 changes: 28 additions & 38 deletions src/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use eyre::Result;
use std::collections::HashMap;

pub mod filtering;
pub mod pagination;
Expand Down Expand Up @@ -323,29 +323,40 @@ pub struct FilteringOptions {
}

impl FilteringOptions {
pub fn new(expressions: Vec<FilterExpression>, column_defs: HashMap<&'static str, ColumnDef>) -> Self {
pub fn new(
expressions: Vec<FilterExpression>,
column_defs: HashMap<&'static str, ColumnDef>,
) -> Self {
Self {
expressions,
case_insensitive: true,
column_defs,
}
}

pub fn case_sensitive(expressions: Vec<FilterExpression>, column_defs: HashMap<&'static str, ColumnDef>) -> Self {
pub fn case_sensitive(
expressions: Vec<FilterExpression>,
column_defs: HashMap<&'static str, ColumnDef>,
) -> Self {
Self {
expressions,
case_insensitive: false,
column_defs,
}
}

pub fn from_json_filters(filters: &[JsonFilter], column_defs: HashMap<&'static str, ColumnDef>) -> Result<Option<Self>> {
pub fn from_json_filters(
filters: &[JsonFilter],
column_defs: HashMap<&'static str, ColumnDef>,
) -> Result<Option<Self>> {
if filters.is_empty() {
return Ok(None);
}

let filter_builder = FilterBuilder::from_json_filters(filters, true, &column_defs)?;
Ok(filter_builder.root.map(|root| Self::new(vec![root], column_defs)))
Ok(filter_builder
.root
.map(|root| Self::new(vec![root], column_defs)))
}

pub fn to_filter_builder(&self) -> Result<FilterBuilder> {
Expand Down Expand Up @@ -449,7 +460,10 @@ mod tests {
fn setup_test_columns() -> HashMap<&'static str, ColumnDef> {
let mut columns = HashMap::new();
columns.insert("id", ColumnDef::Uuid("id"));
columns.insert("property_full_address", ColumnDef::Text("property_full_address"));
columns.insert(
"property_full_address",
ColumnDef::Text("property_full_address"),
);
columns.insert("client_name", ColumnDef::Text("client_name"));
columns.insert("name", ColumnDef::Text("name"));
columns.insert("email", ColumnDef::Text("email"));
Expand Down Expand Up @@ -483,10 +497,7 @@ mod tests {
];

let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?;
let sql = filtering_options
.unwrap()
.to_filter_builder()?
.build()?;
let sql = filtering_options.unwrap().to_filter_builder()?.build()?;
assert_eq!(
sql,
" WHERE (LOWER(property_full_address) LIKE LOWER('%James%') OR LOWER(client_name) LIKE LOWER('%James%'))"
Expand Down Expand Up @@ -519,10 +530,7 @@ mod tests {
];

let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?;
let sql = filtering_options
.unwrap()
.to_filter_builder()?
.build()?;
let sql = filtering_options.unwrap().to_filter_builder()?.build()?;
assert_eq!(
sql,
" WHERE (LOWER(field1) = LOWER('value1') OR LOWER(field2) = LOWER('value2') OR LOWER(field3) = LOWER('value3'))"
Expand Down Expand Up @@ -555,10 +563,7 @@ mod tests {
];

let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?;
let sql = filtering_options
.unwrap()
.to_filter_builder()?
.build()?;
let sql = filtering_options.unwrap().to_filter_builder()?.build()?;
assert_eq!(
sql,
" WHERE (LOWER(name) LIKE LOWER('%John%') AND (age > 18 OR LOWER(city) LIKE LOWER('%York%')))"
Expand Down Expand Up @@ -603,10 +608,7 @@ mod tests {
];

let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?;
let sql = filtering_options
.unwrap()
.to_filter_builder()?
.build()?;
let sql = filtering_options.unwrap().to_filter_builder()?.build()?;
assert_eq!(
sql,
" WHERE (LOWER(status) = LOWER('active') AND (age > 21 OR LOWER(city) = LOWER('New York') OR LOWER(city) = LOWER('London') OR LOWER(department) = LOWER('Sales')))"
Expand Down Expand Up @@ -634,10 +636,7 @@ mod tests {
}];

let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?;
let sql = filtering_options
.unwrap()
.to_filter_builder()?
.build()?;
let sql = filtering_options.unwrap().to_filter_builder()?.build()?;
assert_eq!(sql, " WHERE LOWER(name) LIKE LOWER('%John%')");
Ok(())
}
Expand All @@ -661,10 +660,7 @@ mod tests {
];

let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?;
let sql = filtering_options
.unwrap()
.to_filter_builder()?
.build()?;
let sql = filtering_options.unwrap().to_filter_builder()?.build()?;
assert_eq!(sql, " WHERE (age > 25 OR salary < 50000)");
Ok(())
}
Expand Down Expand Up @@ -701,10 +697,7 @@ mod tests {

// Test case insensitive
let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?;
let sql = filtering_options
.unwrap()
.to_filter_builder()?
.build()?;
let sql = filtering_options.unwrap().to_filter_builder()?.build()?;
assert_eq!(
sql,
" WHERE (LOWER(name) LIKE LOWER('%John%') OR LOWER(email) LIKE LOWER('%gmail.com'))"
Expand All @@ -724,10 +717,7 @@ mod tests {
}];

let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?;
let sql = filtering_options
.unwrap()
.to_filter_builder()?
.build()?;
let sql = filtering_options.unwrap().to_filter_builder()?.build()?;
assert_eq!(sql, format!(" WHERE id = '{}'", uuid));
Ok(())
}
Expand Down
Loading

0 comments on commit 0c457ed

Please sign in to comment.