diff --git a/lib/src/sparql/algebra.rs b/lib/src/sparql/algebra.rs index 227c1b08..aa0335c4 100644 --- a/lib/src/sparql/algebra.rs +++ b/lib/src/sparql/algebra.rs @@ -1462,58 +1462,53 @@ impl fmt::Display for QueryVariants { #[derive(Eq, PartialEq, Debug, Clone, Hash)] pub enum GraphUpdateOperation { /// [insert data](https://www.w3.org/TR/sparql11-update/#def_insertdataoperation) - OpInsertData { data: Vec }, + InsertData { data: Vec }, /// [delete data](https://www.w3.org/TR/sparql11-update/#def_deletedataoperation) - OpDeleteData { data: Vec }, + DeleteData { data: Vec }, /// [delete insert](https://www.w3.org/TR/sparql11-update/#def_deleteinsertoperation) - OpDeleteInsert { - with: Option, + DeleteInsert { delete: Option>, insert: Option>, using: Rc, algebra: Rc, }, /// [load](https://www.w3.org/TR/sparql11-update/#def_loadoperation) - OpLoad { + Load { silent: bool, from: NamedNode, to: Option, }, /// [clear](https://www.w3.org/TR/sparql11-update/#def_clearoperation) - OpClear { silent: bool, graph: GraphTarget }, + Clear { silent: bool, graph: GraphTarget }, /// [create](https://www.w3.org/TR/sparql11-update/#def_createoperation) - OpCreate { silent: bool, graph: NamedNode }, + Create { silent: bool, graph: NamedNode }, /// [drop](https://www.w3.org/TR/sparql11-update/#def_dropoperation) - OpDrop { silent: bool, graph: GraphTarget }, + Drop { silent: bool, graph: GraphTarget }, } impl fmt::Display for GraphUpdateOperation { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - GraphUpdateOperation::OpInsertData { data } => { + GraphUpdateOperation::InsertData { data } => { writeln!(f, "INSERT DATA {{")?; for quad in data { writeln!(f, "\t{}", quad)?; } write!(f, "}}") } - GraphUpdateOperation::OpDeleteData { data } => { + GraphUpdateOperation::DeleteData { data } => { writeln!(f, "DELETE DATA {{")?; for quad in data { writeln!(f, "\t{}", quad)?; } write!(f, "}}") } - GraphUpdateOperation::OpDeleteInsert { - with, + GraphUpdateOperation::DeleteInsert { delete, insert, using, algebra, } => { - if let Some(with) = with { - writeln!(f, "WITH {}", with)?; - } if let Some(delete) = delete { writeln!(f, "DELETE {{")?; for quad in delete { @@ -1543,7 +1538,7 @@ impl fmt::Display for GraphUpdateOperation { } ) } - GraphUpdateOperation::OpLoad { silent, from, to } => { + GraphUpdateOperation::Load { silent, from, to } => { write!(f, "LOAD ")?; if *silent { write!(f, "SILENT ")?; @@ -1554,21 +1549,21 @@ impl fmt::Display for GraphUpdateOperation { } Ok(()) } - GraphUpdateOperation::OpClear { silent, graph } => { + GraphUpdateOperation::Clear { silent, graph } => { write!(f, "CLEAR ")?; if *silent { write!(f, "SILENT ")?; } write!(f, "{}", graph) } - GraphUpdateOperation::OpCreate { silent, graph } => { + GraphUpdateOperation::Create { silent, graph } => { write!(f, "CREATE ")?; if *silent { write!(f, "SILENT ")?; } write!(f, "GRAPH {}", graph) } - GraphUpdateOperation::OpDrop { silent, graph } => { + GraphUpdateOperation::Drop { silent, graph } => { write!(f, "DROP ")?; if *silent { write!(f, "SILENT ")?; diff --git a/lib/src/sparql/parser.rs b/lib/src/sparql/parser.rs index 64a096dd..01402022 100644 --- a/lib/src/sparql/parser.rs +++ b/lib/src/sparql/parser.rs @@ -473,8 +473,7 @@ fn copy_graph( Variable::new("o"), ) .into()]); - GraphUpdateOperation::OpDeleteInsert { - with: None, + GraphUpdateOperation::DeleteInsert { delete: None, insert: Some(vec![QuadPattern::new( Variable::new("s"), @@ -953,23 +952,23 @@ parser! { //[31] rule Load() -> Vec = i("LOAD") _ silent:Update1_silent() _ from:iri() _ to:Load_to()? { - vec![GraphUpdateOperation::OpLoad { silent, from, to }] + vec![GraphUpdateOperation::Load { silent, from, to }] } rule Load_to() -> NamedNode = i("INTO") _ g: GraphRef() { g } //[32] rule Clear() -> Vec = i("CLEAR") _ silent:Update1_silent() _ graph:GraphRefAll() { - vec![GraphUpdateOperation::OpClear { silent, graph }] + vec![GraphUpdateOperation::Clear { silent, graph }] } //[33] rule Drop() -> Vec = i("DROP") _ silent:Update1_silent() _ graph:GraphRefAll() { - vec![GraphUpdateOperation::OpDrop { silent, graph }] + vec![GraphUpdateOperation::Drop { silent, graph }] } //[34] rule Create() -> Vec = i("CREATE") _ silent:Update1_silent() _ graph:GraphRef() { - vec![GraphUpdateOperation::OpCreate { silent, graph }] + vec![GraphUpdateOperation::Create { silent, graph }] } //[35] @@ -983,24 +982,24 @@ parser! { rule Move() -> Vec = i("MOVE") _ silent:Update1_silent() _ from:GraphOrDefault() _ i("TO") _ to:GraphOrDefault() { // Rewriting defined by https://www.w3.org/TR/sparql11-update/#move let bgp = GraphPattern::BGP(vec![TriplePattern::new(Variable::new("s"), Variable::new("p"), Variable::new("o")).into()]); - vec![GraphUpdateOperation::OpDrop { silent, graph: to.clone().into() }, copy_graph(from.clone(), to), GraphUpdateOperation::OpDrop { silent, graph: from.into() }] + vec![GraphUpdateOperation::Drop { silent, graph: to.clone().into() }, copy_graph(from.clone(), to), GraphUpdateOperation::Drop { silent, graph: from.into() }] } //[37] rule Copy() -> Vec = i("COPY") _ silent:Update1_silent() _ from:GraphOrDefault() _ i("TO") _ to:GraphOrDefault() { // Rewriting defined by https://www.w3.org/TR/sparql11-update/#copy let bgp = GraphPattern::BGP(vec![TriplePattern::new(Variable::new("s"), Variable::new("p"), Variable::new("o")).into()]); - vec![GraphUpdateOperation::OpDrop { silent, graph: to.clone().into() }, copy_graph(from, to)] + vec![GraphUpdateOperation::Drop { silent, graph: to.clone().into() }, copy_graph(from, to)] } //[38] rule InsertData() -> Vec = i("INSERT") _ i("DATA") _ data:QuadData() { - vec![GraphUpdateOperation::OpInsertData { data }] + vec![GraphUpdateOperation::InsertData { data }] } //[39] rule DeleteData() -> Vec = i("DELETE") _ i("DATA") _ data:QuadData() { - vec![GraphUpdateOperation::OpDeleteData { data }] + vec![GraphUpdateOperation::DeleteData { data }] } //[40] @@ -1013,8 +1012,7 @@ parser! { bgp } }).fold(GraphPattern::BGP(Vec::new()), new_join); - vec![GraphUpdateOperation::OpDeleteInsert { - with: None, + vec![GraphUpdateOperation::DeleteInsert { delete: Some(d), insert: None, using: Rc::new(DatasetSpec::default()), @@ -1024,9 +1022,25 @@ parser! { //[41] rule Modify() -> Vec = with:Modify_with() _ c:Modify_clauses() _ using:(UsingClause() ** (_)) _ i("WHERE") _ algebra:GroupGraphPattern() { - let (delete, insert) = c; - vec![GraphUpdateOperation::OpDeleteInsert { - with, + let (mut delete, mut insert) = c; + let mut algebra = algebra; + + if let Some(with) = with { + // We inject WITH everywhere + delete = delete.map(|quads| quads.into_iter().map(|q| if q.graph_name.is_none() { + QuadPattern::new(q.subject, q.predicate, q.object, Some(with.clone().into())) + } else { + q + }).collect()); + insert = insert.map(|quads| quads.into_iter().map(|q| if q.graph_name.is_none() { + QuadPattern::new(q.subject, q.predicate, q.object, Some(with.clone().into())) + } else { + q + }).collect()); + algebra = GraphPattern::Graph(with.into(), Box::new(algebra)); + } + + vec![GraphUpdateOperation::DeleteInsert { delete, insert, using: Rc::new(using.into_iter().fold(DatasetSpec::default(), |mut a, b| a + b)),