use super::{Row, values_equal};
use crate::ast::{Condition, SimpleOperator, ValuePosition};
use crate::json::Value;
use crate::strings;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Constraint {
pub entries: Vec<(String, Option<Value>)>,
}
impl Constraint {
pub fn new(entries: Vec<(String, Option<Value>)>) -> Self {
Constraint { entries }
}
pub fn keys(&self) -> Vec<&str> {
self.entries.iter().map(|(k, _)| k.as_str()).collect()
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn has(&self, key: &str) -> bool {
self.entries.iter().any(|(k, _)| k == key)
}
pub fn get(&self, key: &str) -> Option<&Value> {
self.entries
.iter()
.find(|(k, _)| k == key)
.and_then(|(_, v)| v.as_ref())
}
pub fn set(&mut self, key: &str, value: Option<Value>) {
match self.entries.iter_mut().find(|(k, _)| k == key) {
Some(e) => e.1 = value,
None => self.entries.push((key.to_owned(), value)),
}
}
pub fn merging(&self, other: &Constraint) -> Constraint {
let mut result = self.clone();
for (k, v) in &other.entries {
result.set(k, v.clone());
}
result
}
pub fn matches(&self, row: &Row) -> bool {
self.entries
.iter()
.all(|(k, v)| values_equal(row.get(k), v.as_ref()))
}
pub fn is_compatible(&self, other: &Constraint) -> bool {
self.entries
.iter()
.all(|(k, v)| !other.has(k) || values_equal(v.as_ref(), other.get(k)))
}
pub fn matches_primary_key(&self, primary_key: &[String]) -> bool {
key_matches_primary_key(&self.keys(), primary_key)
}
pub fn from_row(row: &Row, keys: &[String]) -> Constraint {
Constraint {
entries: keys
.iter()
.map(|k| (k.clone(), row.get(k).cloned()))
.collect(),
}
}
}
pub fn key_matches_primary_key(key: &[&str], primary_key: &[String]) -> bool {
if key.len() != primary_key.len() {
return false;
}
let mut sorted = key.to_vec();
sorted.sort_by(|a, b| strings::utf16(a, b));
sorted.iter().zip(primary_key).all(|(a, b)| *a == b)
}
pub fn pull_simple_and_components(condition: &Condition) -> Vec<&Condition> {
match condition {
Condition::And(c) => c.iter().flat_map(pull_simple_and_components).collect(),
Condition::Simple { .. } => vec![condition],
Condition::Or(c) if c.len() == 1 => pull_simple_and_components(&c[0]),
_ => Vec::new(),
}
}
pub fn primary_key_constraint_from_filters(
condition: Option<&Condition>,
primary_key: &[String],
) -> Option<Constraint> {
let conditions = pull_simple_and_components(condition?);
if conditions.is_empty() {
return None;
}
let mut ret = Constraint::default();
for c in conditions {
let Condition::Simple {
op: SimpleOperator::Eq,
left: ValuePosition::Column(name),
right,
} = c
else {
continue;
};
let ValuePosition::Literal(value) = right else {
panic!("extractColumn: expected right side to be literal");
};
if primary_key.contains(name) {
ret.set(name, Some(value.clone()));
}
}
(ret.len() == primary_key.len()).then_some(ret)
}