use crate::database::SqlValue;
use crate::models::query_builder::{WhereCondition, WhereConnector};
use std::fmt::Debug;
#[derive(Clone, Debug, Default)]
pub struct ModelFilter {
conditions: Vec<WhereCondition>,
}
impl ModelFilter {
pub fn new() -> Self {
Self {
conditions: Vec::new(),
}
}
pub fn where_eq<V: Into<SqlValue>>(mut self, field: &str, value: V) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "=".to_string(),
value: value.into(),
connector: WhereConnector::And,
});
self
}
pub fn where_ne<V: Into<SqlValue>>(mut self, field: &str, value: V) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "!=".to_string(),
value: value.into(),
connector: WhereConnector::And,
});
self
}
pub fn where_gt<V: Into<SqlValue>>(mut self, field: &str, value: V) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: ">".to_string(),
value: value.into(),
connector: WhereConnector::And,
});
self
}
pub fn where_gte<V: Into<SqlValue>>(mut self, field: &str, value: V) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: ">=".to_string(),
value: value.into(),
connector: WhereConnector::And,
});
self
}
pub fn where_lt<V: Into<SqlValue>>(mut self, field: &str, value: V) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "<".to_string(),
value: value.into(),
connector: WhereConnector::And,
});
self
}
pub fn where_lte<V: Into<SqlValue>>(mut self, field: &str, value: V) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "<=".to_string(),
value: value.into(),
connector: WhereConnector::And,
});
self
}
pub fn where_like(mut self, field: &str, pattern: &str) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "LIKE".to_string(),
value: SqlValue::String(pattern.to_string()),
connector: WhereConnector::And,
});
self
}
pub fn where_not_like(mut self, field: &str, pattern: &str) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "NOT LIKE".to_string(),
value: SqlValue::String(pattern.to_string()),
connector: WhereConnector::And,
});
self
}
pub fn where_in<V: Into<SqlValue>>(mut self, field: &str, values: Vec<V>) -> Self {
let values_vec: Vec<SqlValue> = values.into_iter().map(|v| v.into()).collect();
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "IN".to_string(),
value: SqlValue::Array(values_vec),
connector: WhereConnector::And,
});
self
}
pub fn where_not_in<V: Into<SqlValue>>(mut self, field: &str, values: Vec<V>) -> Self {
let values_vec: Vec<SqlValue> = values.into_iter().map(|v| v.into()).collect();
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "NOT IN".to_string(),
value: SqlValue::Array(values_vec),
connector: WhereConnector::And,
});
self
}
pub fn where_null(mut self, field: &str) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "IS NULL".to_string(),
value: SqlValue::Null,
connector: WhereConnector::And,
});
self
}
pub fn where_not_null(mut self, field: &str) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "IS NOT NULL".to_string(),
value: SqlValue::Null,
connector: WhereConnector::And,
});
self
}
pub fn where_between<V: Into<SqlValue>>(mut self, field: &str, start: V, end: V) -> Self {
self.conditions.push(WhereCondition {
column: field.to_string(),
operator: "BETWEEN".to_string(),
value: SqlValue::Array(vec![start.into(), end.into()]),
connector: WhereConnector::And,
});
self
}
pub fn and(mut self, other: ModelFilter) -> Self {
self.conditions.extend(other.conditions);
self
}
pub(crate) fn get_conditions(&self) -> &[WhereCondition] {
&self.conditions
}
pub fn is_empty(&self) -> bool {
self.conditions.is_empty()
}
pub fn len(&self) -> usize {
self.conditions.len()
}
}