From 3f2f9a05c9526778da0ce552fd370a6f2ada5528 Mon Sep 17 00:00:00 2001 From: ccamel Date: Thu, 10 Aug 2023 19:03:45 +0200 Subject: [PATCH] feat(cognitarium): support 'delete where {}' form --- contracts/okp4-cognitarium/src/contract.rs | 33 ++++++++++++++++------ 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/contracts/okp4-cognitarium/src/contract.rs b/contracts/okp4-cognitarium/src/contract.rs index 5faeae15..6585850c 100644 --- a/contracts/okp4-cognitarium/src/contract.rs +++ b/contracts/okp4-cognitarium/src/contract.rs @@ -49,7 +49,10 @@ pub fn execute( pub mod execute { use super::*; - use crate::msg::{DataFormat, Prefix, SelectItem, TriplePattern, WhereClause}; + use crate::msg::{ + DataFormat, Prefix, SelectItem, SimpleWhereCondition, TriplePattern, WhereClause, + WhereCondition, + }; use crate::querier::{PlanBuilder, QueryEngine}; use crate::rdf::{Atom, TripleReader}; use crate::storer::StoreEngine; @@ -87,20 +90,30 @@ pub mod execute { info: MessageInfo, prefixes: Vec, delete: Vec, - r#where: Option, + r#where: WhereClause, ) -> Result { verify_owner(&deps, &info)?; - let plan = - PlanBuilder::new(deps.storage, &prefixes).build_plan(&r#where.unwrap_or_default())?; - - let variables = delete + let patterns: Vec = if delete.is_empty() { + r#where + .iter() + .map(|c| match c { + WhereCondition::Simple(SimpleWhereCondition::TriplePattern(tp)) => { + Ok(tp.clone()) + } + }) + .collect::>()? + } else { + delete + }; + let variables = patterns .iter() .flat_map(|tp| tp.variables()) .collect::>() .into_iter() .map(SelectItem::Variable) .collect(); + let plan = PlanBuilder::new(deps.storage, &prefixes).build_plan(&r#where)?; let response = QueryEngine::new(deps.storage).select(plan, variables)?; let atoms: Vec = if response.results.bindings.is_empty() { @@ -110,8 +123,12 @@ pub mod execute { .results .bindings .iter() - .flat_map(|row| delete.iter().map(|pattern| pattern.resolve(row, &prefixes))) - .collect::, _>>()? + .flat_map(|row| { + patterns + .iter() + .map(|pattern| pattern.resolve(row, &prefixes)) + }) + .collect::>()? }; let mut store = StoreEngine::new(deps.storage)?;