use std::collections::HashMap;
use crate::encode::{IntoGrpcValue, Params};
use crate::GrpcValue;
#[derive(Debug, Clone, Default)]
pub struct FilterClause {
frags: Vec<String>,
params: HashMap<String, GrpcValue>,
}
impl FilterClause {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.frags.is_empty()
}
pub fn sql(&self) -> String {
self.frags.join(" AND ")
}
pub fn where_sql(&self) -> String {
if self.frags.is_empty() {
String::new()
} else {
format!("WHERE {}", self.sql())
}
}
pub fn into_params(self) -> Params {
Params(self.params)
}
pub fn filter(mut self, fragment: impl Into<String>) -> Self {
self.frags.push(fragment.into());
self
}
pub fn filter_when(self, cond: bool, fragment: impl Into<String>) -> Self {
if cond {
self.filter(fragment)
} else {
self
}
}
pub fn apply_if<T>(self, option: Option<T>, f: impl FnOnce(Self, T) -> Self) -> Self {
match option {
Some(v) => f(self, v),
None => self,
}
}
pub fn bind(mut self, name: &str, value: impl IntoGrpcValue) -> Self {
self.params.insert(name.to_string(), value.into_value());
self
}
pub fn eq(mut self, col: &str, value: impl IntoGrpcValue) -> Self {
let c = col.to_string();
let name = self.param_name(&c);
self.frags.push(format!("{c} = :{name}"));
self.params.insert(name, value.into_value());
self
}
pub fn ne(self, col: &str, value: impl IntoGrpcValue) -> Self {
self.tagged(col, "ne", "<>", value)
}
pub fn lt(self, col: &str, value: impl IntoGrpcValue) -> Self {
self.tagged(col, "lt", "<", value)
}
pub fn lte(self, col: &str, value: impl IntoGrpcValue) -> Self {
self.tagged(col, "lte", "<=", value)
}
pub fn gt(self, col: &str, value: impl IntoGrpcValue) -> Self {
self.tagged(col, "gt", ">", value)
}
pub fn gte(self, col: &str, value: impl IntoGrpcValue) -> Self {
self.tagged(col, "gte", ">=", value)
}
pub fn between(mut self, col: &str, from: impl IntoGrpcValue, to: impl IntoGrpcValue) -> Self {
let c = col.to_string();
let name = self.param_name(&format!("{c}__from"));
let to_name = self.param_name(&format!("{c}__to"));
self.frags
.push(format!("{c} BETWEEN :{name} AND :{to_name}"));
self.params.insert(name, from.into_value());
self.params.insert(to_name, to.into_value());
self
}
pub fn is_null(mut self, col: &str) -> Self {
self.frags.push(format!("{col} IS NULL"));
self
}
pub fn is_not_null(mut self, col: &str) -> Self {
self.frags.push(format!("{col} IS NOT NULL"));
self
}
pub fn in_list<I>(mut self, col: &str, values: I) -> Self
where
I: IntoIterator,
I::Item: IntoGrpcValue,
{
let c = col.to_string();
let name = self.param_name(&format!("{c}__in"));
self.frags.push(format!("{c} IN :{name}"));
self.params.insert(
name,
crate::encode::list_v(values.into_iter().map(IntoGrpcValue::into_value)),
);
self
}
pub fn not_in<I>(mut self, col: &str, values: I) -> Self
where
I: IntoIterator,
I::Item: IntoGrpcValue,
{
let c = col.to_string();
let name = self.param_name(&format!("{c}__nin"));
self.frags.push(format!("{c} NOT IN :{name}"));
self.params.insert(
name,
crate::encode::list_v(values.into_iter().map(IntoGrpcValue::into_value)),
);
self
}
pub fn contains(self, col: &str, value: impl IntoGrpcValue) -> Self {
self.tagged(col, "has", "CONTAINS", value)
}
pub fn contains_all<I>(mut self, col: &str, values: I) -> Self
where
I: IntoIterator,
I::Item: IntoGrpcValue,
{
let c = col.to_string();
let name = self.param_name(&format!("{c}__all"));
self.frags.push(format!("{c} CONTAINSALL :{name}"));
self.params.insert(
name,
crate::encode::list_v(values.into_iter().map(IntoGrpcValue::into_value)),
);
self
}
pub fn contains_any<I>(mut self, col: &str, values: I) -> Self
where
I: IntoIterator,
I::Item: IntoGrpcValue,
{
let c = col.to_string();
let name = self.param_name(&format!("{c}__any"));
self.frags.push(format!("{c} CONTAINSANY :{name}"));
self.params.insert(
name,
crate::encode::list_v(values.into_iter().map(IntoGrpcValue::into_value)),
);
self
}
pub fn not_contains_any<I>(self, col: &str, values: I) -> Self
where
I: IntoIterator,
I::Item: IntoGrpcValue,
{
let mut this = self.contains_any(col, values);
let last = this
.frags
.last_mut()
.expect("contains_any pushed a fragment");
*last = format!("NOT ({last})");
this
}
pub fn like(self, col: &str, pattern: impl IntoGrpcValue) -> Self {
self.tagged(col, "like", "LIKE", pattern)
}
pub fn ilike(self, col: &str, pattern: impl IntoGrpcValue) -> Self {
self.tagged(col, "ilike", "ILIKE", pattern)
}
fn tagged(mut self, col: &str, tag: &str, op: &str, value: impl IntoGrpcValue) -> Self {
let c = col.to_string();
let name = self.param_name(&format!("{c}__{tag}"));
self.frags.push(format!("{c} {op} :{name}"));
self.params.insert(name, value.into_value());
self
}
fn param_name(&mut self, base: &str) -> String {
if !self.params.contains_key(base) {
return base.to_string();
}
for n in 2.. {
let candidate = format!("{base}_{n}");
if !self.params.contains_key(&candidate) {
return candidate;
}
}
unreachable!("u32 sequence exhausts memory first")
}
}