add "contains" symbol

feat/vaults
Tomáš Mládek 2021-02-17 21:40:51 +01:00
parent ac02450b8b
commit 1bbcc00d65
1 changed files with 31 additions and 14 deletions

View File

@ -261,15 +261,10 @@ where
{
Exact(T),
In(Vec<T>),
Contains(String),
Any,
}
// #[derive(Debug)]
// pub enum StringOrLikeString {
// String(String),
// Like(String)
// }
#[derive(Debug)]
pub struct EntryQuery {
pub entity: QueryComponent<Address>,
@ -407,6 +402,22 @@ impl Query {
))
}
}
"contains" => {
let (mut cons_vec, _) = cons.clone().into_vec();
match cons_vec.len() {
2 => {
let value = cons_vec.remove(1);
if let lexpr::Value::String(str) = value {
Ok(QueryComponent::Contains(str.into_string()))
} else {
Err(anyhow!("Malformed expression: 'Contains' argument must be a string."))
}
}
_ => Err(anyhow!(
"Malformed expression: 'Contains' requires a single argument."
)),
}
}
_ => Err(anyhow!(format!(
"Malformed expression: Unknown symbol {}",
symbol
@ -477,6 +488,9 @@ fn query_to_sqlite(query: &Query) -> Result<Box<Predicate>> {
q_targets.into_iter().map(|t| t.encode()).collect();
subqueries.push(Box::new(data::target.eq_any(targets?)))
}
QueryComponent::Contains(_) => {
return Err(anyhow!("Addresses cannot be queried alike."))
}
QueryComponent::Any => {}
};
@ -487,6 +501,9 @@ fn query_to_sqlite(query: &Query) -> Result<Box<Predicate>> {
QueryComponent::In(q_keys) => {
subqueries.push(Box::new(data::key.eq_any(q_keys.clone())))
}
QueryComponent::Contains(q_key) => {
subqueries.push(Box::new(data::key.like(format!("%{}%", q_key))))
}
QueryComponent::Any => {}
};
@ -499,6 +516,9 @@ fn query_to_sqlite(query: &Query) -> Result<Box<Predicate>> {
q_values.into_iter().map(|v| v.to_str()).collect();
subqueries.push(Box::new(data::value.eq_any(values?)))
}
QueryComponent::Contains(q_value) => {
subqueries.push(Box::new(data::value.like(format!("%{}%", q_value))))
}
QueryComponent::Any => {}
};
@ -561,14 +581,14 @@ pub fn query_entries<C: Connection<Backend = Sqlite>>(
let targets: Result<Vec<_>, _> = q_targets.into_iter().map(|t| t.encode()).collect();
query.filter(target.eq_any(targets?))
}
// QueryComponent::ILike(_) => return Err(anyhow!("Cannot query Address alike.")),
QueryComponent::Contains(_) => return Err(anyhow!("Cannot query Address alike.")),
QueryComponent::Any => query,
};
query = match entry_query.attribute {
QueryComponent::Exact(q_key) => query.filter(key.eq(q_key)),
QueryComponent::In(q_keys) => query.filter(key.eq_any(q_keys)),
// QueryComponent::ILike(q_key) => query.filter(key.like(format!("%{}%", q_key))),
QueryComponent::Contains(q_key) => query.filter(key.like(format!("%{}%", q_key))),
QueryComponent::Any => query,
};
@ -578,12 +598,9 @@ pub fn query_entries<C: Connection<Backend = Sqlite>>(
let values: Result<Vec<_>, _> = q_values.into_iter().map(|v| v.to_str()).collect();
query.filter(value.eq_any(values?))
}
// QueryComponent::ILike(EntryValue::Value(Value::String(q_value_string))) => {
// query.filter(value.like(format!("%{}%", q_value_string)))
// }
// QueryComponent::ILike(_) => {
// return Err(anyhow!("Only string Values can be queried alike."));
// }
QueryComponent::Contains(q_value_string) => {
query.filter(value.like(format!("%{}%", q_value_string)))
}
QueryComponent::Any => query,
};