#[derive(Debug, Clone)]
enum Condition {
Equals(String, Value),
NotEquals(String, Value),
And(Box<Condition>, Box<Condition>),
Or(Box<Condition>, Box<Condition>),
}
fn row_matches_condition(&self, row: &Vec<Value>, condition: &Condition) -> bool {
match condition {
Condition::Equals(col, val) => {
if let Some(schema) = self.columns.schema.get(col) {
row.get(schema.order) == Some(val)
} else {
false
}
}
Condition::NotEquals(col, val) => {
if let Some(schema) = self.columns.schema.get(col) {
row.get(schema.order) != Some(val)
} else {
false
}
}
Condition::And(lhs, rhs) => {
self.row_matches_condition(row, lhs) && self.row_matches_condition(row, rhs)
}
Condition::Or(lhs, rhs) => {
self.row_matches_condition(row, lhs) || self.row_matches_condition(row, rhs)
}
}
}
fn find_rows_by_condition(&self, condition: &Condition) -> Vec<&Vec<Value>> {
self.rows
.iter()
.filter(|row| self.row_matches_condition(row, condition))
.collect()
}
//
let _ = table.update_value_by_column_condition(
"id",
&Value::Integer(2),
"name",
Value::Text("Carol".to_string()),
);
let condition = Condition::And(
Box::new(Condition::Equals("age".to_string(), Value::Integer(25))),
Box::new(Condition::NotEquals("name".to_string(), Value::Text("Bob".to_string()))),
);
let results = table.find_rows_by_condition(&condition);
for row in results {
println!("Match row: {:?}", row);
}