oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
//! SPARQL Update operation executor.
//!
//! Implements the SPARQL 1.1 Update operations (INSERT DATA, DELETE DATA,
//! DELETE WHERE, CLEAR, DROP, CREATE, INSERT/DELETE) over an in-memory quad store.
//! Supports atomic execution sequences with rollback on error.

use std::collections::{HashMap, HashSet};

/// A concrete quad (subject, predicate, object, optional graph).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Quad {
    pub subject: String,
    pub predicate: String,
    pub object: String,
    /// `None` means the default graph.
    pub graph: Option<String>,
}

impl Quad {
    /// Create a quad in the default graph.
    pub fn default_graph(s: &str, p: &str, o: &str) -> Self {
        Quad {
            subject: s.to_string(),
            predicate: p.to_string(),
            object: o.to_string(),
            graph: None,
        }
    }

    /// Create a quad in a named graph.
    pub fn named_graph(s: &str, p: &str, o: &str, g: &str) -> Self {
        Quad {
            subject: s.to_string(),
            predicate: p.to_string(),
            object: o.to_string(),
            graph: Some(g.to_string()),
        }
    }
}

/// A term in a triple pattern (may be a concrete term or a variable).
#[derive(Debug, Clone, PartialEq)]
pub enum PatternTerm {
    Iri(String),
    Literal(String),
    Variable(String),
    Blank(String),
}

/// A triple pattern used in WHERE / DELETE WHERE clauses.
#[derive(Debug, Clone)]
pub struct TriplePattern {
    pub subject: PatternTerm,
    pub predicate: PatternTerm,
    pub object: PatternTerm,
}

/// A quad template used in INSERT clauses (graph is optional pattern term).
#[derive(Debug, Clone)]
pub struct QuadTemplate {
    pub subject: PatternTerm,
    pub predicate: PatternTerm,
    pub object: PatternTerm,
    pub graph: Option<PatternTerm>,
}

/// Target graph specifier for CLEAR / DROP.
#[derive(Debug, Clone)]
pub enum GraphTarget {
    Named(String),
    Default,
    All,
    NamedGraphs,
}

/// A SPARQL Update operation.
#[derive(Debug, Clone)]
pub enum UpdateOperation {
    /// INSERT DATA { quads }
    InsertData { quads: Vec<Quad> },
    /// DELETE DATA { quads }
    DeleteData { quads: Vec<Quad> },
    /// DELETE WHERE { patterns } (in optional named graph)
    DeleteWhere {
        graph: Option<String>,
        patterns: Vec<TriplePattern>,
    },
    /// CLEAR GRAPH / DEFAULT / ALL / NAMED
    Clear { graph: GraphTarget },
    /// DROP GRAPH (silent ignores GraphNotFound)
    Drop { graph: GraphTarget, silent: bool },
    /// CREATE GRAPH
    Create { graph: String, silent: bool },
    /// DELETE { patterns } INSERT { templates } WHERE { patterns }
    InsertDelete {
        delete_patterns: Vec<TriplePattern>,
        insert_templates: Vec<QuadTemplate>,
        where_patterns: Vec<TriplePattern>,
        using_graph: Option<String>,
    },
}

/// Summary of what a single (or sequence of) operation(s) accomplished.
#[derive(Debug, Default)]
pub struct UpdateResult {
    pub operations_applied: usize,
    pub triples_inserted: usize,
    pub triples_deleted: usize,
}

/// Errors that can occur during SPARQL Update execution.
#[derive(Debug)]
pub enum UpdateError {
    InvalidIri(String),
    GraphNotFound(String),
    GraphAlreadyExists(String),
    VariableInDeleteData(String),
}

impl std::fmt::Display for UpdateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UpdateError::InvalidIri(s) => write!(f, "Invalid IRI: {}", s),
            UpdateError::GraphNotFound(s) => write!(f, "Graph not found: {}", s),
            UpdateError::GraphAlreadyExists(s) => write!(f, "Graph already exists: {}", s),
            UpdateError::VariableInDeleteData(s) => {
                write!(f, "Variable not allowed in DELETE DATA: {}", s)
            }
        }
    }
}

impl std::error::Error for UpdateError {}

/// In-memory quad store that executes SPARQL Update operations.
///
/// The store maps `Option<String>` (graph name) to sets of (s, p, o) triples.
/// `None` represents the default graph. Named graphs must be explicitly created
/// (or are auto-created on first INSERT DATA).
#[derive(Debug, Default)]
pub struct UpdateProcessor {
    store: HashMap<Option<String>, HashSet<(String, String, String)>>,
}

impl UpdateProcessor {
    /// Create an empty update processor with an empty default graph.
    pub fn new() -> Self {
        let mut store = HashMap::new();
        store.insert(None, HashSet::new()); // ensure default graph exists
        UpdateProcessor { store }
    }

    /// Execute a single SPARQL Update operation.
    pub fn execute(&mut self, op: UpdateOperation) -> Result<UpdateResult, UpdateError> {
        let mut result = UpdateResult::default();
        self.apply_op(op, &mut result)?;
        Ok(result)
    }

    /// Execute a sequence of operations atomically.
    ///
    /// If any operation fails, all changes made so far in this call are rolled back.
    pub fn execute_sequence(
        &mut self,
        ops: Vec<UpdateOperation>,
    ) -> Result<UpdateResult, UpdateError> {
        let backup = self.store.clone();
        let mut result = UpdateResult::default();

        for op in ops {
            if let Err(e) = self.apply_op(op, &mut result) {
                // Rollback
                self.store = backup;
                return Err(e);
            }
        }
        Ok(result)
    }

    /// Total number of triples across all graphs (including default).
    pub fn triple_count(&self) -> usize {
        self.store.values().map(|s| s.len()).sum()
    }

    /// Number of named graphs (not counting the default graph).
    pub fn graph_count(&self) -> usize {
        self.store.keys().filter(|k| k.is_some()).count()
    }

    /// List all named graphs (not the default graph).
    pub fn list_graphs(&self) -> Vec<String> {
        let mut graphs: Vec<String> = self.store.keys().filter_map(|k| k.clone()).collect();
        graphs.sort();
        graphs
    }

    /// Check if a triple is present in the given graph.
    pub fn contains(&self, graph: Option<&str>, s: &str, p: &str, o: &str) -> bool {
        let key = graph.map(|g| g.to_string());
        self.store
            .get(&key)
            .is_some_and(|triples| triples.contains(&(s.to_string(), p.to_string(), o.to_string())))
    }

    // ── Internal helpers ─────────────────────────────────────────────────────

    fn apply_op(
        &mut self,
        op: UpdateOperation,
        result: &mut UpdateResult,
    ) -> Result<(), UpdateError> {
        result.operations_applied += 1;
        match op {
            UpdateOperation::InsertData { quads } => self.op_insert_data(quads, result),
            UpdateOperation::DeleteData { quads } => self.op_delete_data(quads, result),
            UpdateOperation::DeleteWhere { graph, patterns } => {
                self.op_delete_where(graph.as_deref(), &patterns, result)
            }
            UpdateOperation::Clear { graph } => self.op_clear(graph, result),
            UpdateOperation::Drop { graph, silent } => self.op_drop(graph, silent, result),
            UpdateOperation::Create { graph, silent } => self.op_create(&graph, silent, result),
            UpdateOperation::InsertDelete {
                delete_patterns,
                insert_templates,
                where_patterns,
                using_graph,
            } => self.op_insert_delete(
                &delete_patterns,
                &insert_templates,
                &where_patterns,
                using_graph.as_deref(),
                result,
            ),
        }
    }

    fn op_insert_data(
        &mut self,
        quads: Vec<Quad>,
        result: &mut UpdateResult,
    ) -> Result<(), UpdateError> {
        for quad in quads {
            let graph_entry = self
                .store
                .entry(quad.graph.clone())
                .or_insert_with(HashSet::new);
            let triple = (quad.subject, quad.predicate, quad.object);
            if graph_entry.insert(triple) {
                result.triples_inserted += 1;
            }
        }
        Ok(())
    }

    fn op_delete_data(
        &mut self,
        quads: Vec<Quad>,
        result: &mut UpdateResult,
    ) -> Result<(), UpdateError> {
        // DELETE DATA must not contain variables
        for quad in &quads {
            for term in [&quad.subject, &quad.predicate, &quad.object] {
                if term.starts_with('?') {
                    return Err(UpdateError::VariableInDeleteData(term.clone()));
                }
            }
        }
        for quad in quads {
            let key = quad.graph.clone();
            if let Some(graph_entry) = self.store.get_mut(&key) {
                let triple = (quad.subject, quad.predicate, quad.object);
                if graph_entry.remove(&triple) {
                    result.triples_deleted += 1;
                }
            }
        }
        Ok(())
    }

    fn op_delete_where(
        &mut self,
        graph: Option<&str>,
        patterns: &[TriplePattern],
        result: &mut UpdateResult,
    ) -> Result<(), UpdateError> {
        let key = graph.map(|g| g.to_string());
        let matching: Vec<(String, String, String)> = if let Some(triples) = self.store.get(&key) {
            triples
                .iter()
                .filter(|t| patterns.iter().all(|p| Self::match_pattern(t, p)))
                .cloned()
                .collect()
        } else {
            vec![]
        };
        if let Some(graph_entry) = self.store.get_mut(&key) {
            for triple in matching {
                if graph_entry.remove(&triple) {
                    result.triples_deleted += 1;
                }
            }
        }
        Ok(())
    }

    fn op_clear(
        &mut self,
        target: GraphTarget,
        result: &mut UpdateResult,
    ) -> Result<(), UpdateError> {
        match target {
            GraphTarget::Named(g) => {
                let key = Some(g.clone());
                if let Some(triples) = self.store.get_mut(&key) {
                    result.triples_deleted += triples.len();
                    triples.clear();
                }
            }
            GraphTarget::Default => {
                if let Some(triples) = self.store.get_mut(&None) {
                    result.triples_deleted += triples.len();
                    triples.clear();
                }
            }
            GraphTarget::All => {
                for triples in self.store.values_mut() {
                    result.triples_deleted += triples.len();
                    triples.clear();
                }
            }
            GraphTarget::NamedGraphs => {
                for (key, triples) in self.store.iter_mut() {
                    if key.is_some() {
                        result.triples_deleted += triples.len();
                        triples.clear();
                    }
                }
            }
        }
        Ok(())
    }

    fn op_drop(
        &mut self,
        target: GraphTarget,
        silent: bool,
        result: &mut UpdateResult,
    ) -> Result<(), UpdateError> {
        match target {
            GraphTarget::Named(g) => {
                let key = Some(g.clone());
                if self.store.remove(&key).is_some() {
                    result.triples_deleted += 0; // already counted by removing
                } else if !silent {
                    return Err(UpdateError::GraphNotFound(g));
                }
            }
            GraphTarget::Default => {
                if let Some(triples) = self.store.get_mut(&None) {
                    result.triples_deleted += triples.len();
                    triples.clear();
                }
            }
            GraphTarget::All => {
                let named_keys: Vec<Option<String>> =
                    self.store.keys().filter(|k| k.is_some()).cloned().collect();
                for key in named_keys {
                    self.store.remove(&key);
                }
                if let Some(triples) = self.store.get_mut(&None) {
                    result.triples_deleted += triples.len();
                    triples.clear();
                }
            }
            GraphTarget::NamedGraphs => {
                let named_keys: Vec<Option<String>> =
                    self.store.keys().filter(|k| k.is_some()).cloned().collect();
                for key in named_keys {
                    self.store.remove(&key);
                }
            }
        }
        Ok(())
    }

    fn op_create(
        &mut self,
        graph: &str,
        silent: bool,
        _result: &mut UpdateResult,
    ) -> Result<(), UpdateError> {
        let key = Some(graph.to_string());
        if let std::collections::hash_map::Entry::Vacant(e) = self.store.entry(key) {
            e.insert(HashSet::new());
        } else if !silent {
            return Err(UpdateError::GraphAlreadyExists(graph.to_string()));
        }
        Ok(())
    }

    fn op_insert_delete(
        &mut self,
        delete_patterns: &[TriplePattern],
        insert_templates: &[QuadTemplate],
        where_patterns: &[TriplePattern],
        using_graph: Option<&str>,
        result: &mut UpdateResult,
    ) -> Result<(), UpdateError> {
        // Evaluate WHERE clause to get bindings
        let key = using_graph.map(|g| g.to_string());
        let matched: Vec<(String, String, String)> = if let Some(triples) = self.store.get(&key) {
            triples
                .iter()
                .filter(|t| where_patterns.iter().all(|p| Self::match_pattern(t, p)))
                .cloned()
                .collect()
        } else {
            vec![]
        };

        // Build bindings from matched triples (simplified: bind ?s ?p ?o from WHERE patterns)
        let bindings: Vec<HashMap<String, String>> = matched
            .iter()
            .map(|(s, p, o)| {
                let mut m = HashMap::new();
                for pat in where_patterns {
                    if let PatternTerm::Variable(v) = &pat.subject {
                        m.insert(v.clone(), s.clone());
                    }
                    if let PatternTerm::Variable(v) = &pat.predicate {
                        m.insert(v.clone(), p.clone());
                    }
                    if let PatternTerm::Variable(v) = &pat.object {
                        m.insert(v.clone(), o.clone());
                    }
                }
                m
            })
            .collect();

        // Apply DELETE templates using bindings
        let mut to_delete: Vec<(Option<String>, String, String, String)> = vec![];
        for binding in &bindings {
            for pat in delete_patterns {
                if let (Some(s), Some(p), Some(o)) = (
                    Self::resolve_term(&pat.subject, binding),
                    Self::resolve_term(&pat.predicate, binding),
                    Self::resolve_term(&pat.object, binding),
                ) {
                    to_delete.push((key.clone(), s, p, o));
                }
            }
        }
        for (g, s, p, o) in to_delete {
            if let Some(triples) = self.store.get_mut(&g) {
                if triples.remove(&(s, p, o)) {
                    result.triples_deleted += 1;
                }
            }
        }

        // Apply INSERT templates using bindings
        for binding in &bindings {
            for tmpl in insert_templates {
                let graph_key = tmpl
                    .graph
                    .as_ref()
                    .and_then(|g| Self::resolve_term(g, binding));

                if let (Some(s), Some(p), Some(o)) = (
                    Self::resolve_term(&tmpl.subject, binding),
                    Self::resolve_term(&tmpl.predicate, binding),
                    Self::resolve_term(&tmpl.object, binding),
                ) {
                    let triples = self.store.entry(graph_key).or_insert_with(HashSet::new);
                    if triples.insert((s, p, o)) {
                        result.triples_inserted += 1;
                    }
                }
            }
        }

        Ok(())
    }

    /// Returns true if the triple matches ALL provided patterns (AND semantics).
    fn match_pattern(triple: &(String, String, String), pattern: &TriplePattern) -> bool {
        let (s, p, o) = triple;
        let match_term = |term: &PatternTerm, value: &str| -> bool {
            match term {
                PatternTerm::Variable(_) => true,
                PatternTerm::Iri(v) | PatternTerm::Literal(v) | PatternTerm::Blank(v) => v == value,
            }
        };
        match_term(&pattern.subject, s)
            && match_term(&pattern.predicate, p)
            && match_term(&pattern.object, o)
    }

    /// Resolve a PatternTerm to a concrete string using variable bindings.
    fn resolve_term(term: &PatternTerm, bindings: &HashMap<String, String>) -> Option<String> {
        match term {
            PatternTerm::Variable(v) => bindings.get(v).cloned(),
            PatternTerm::Iri(s) | PatternTerm::Literal(s) | PatternTerm::Blank(s) => {
                Some(s.clone())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn iri(s: &str) -> String {
        s.to_string()
    }

    // ── InsertData ────────────────────────────────────────────────────────────

    #[test]
    fn test_insert_data_default_graph() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![Quad::default_graph("s1", "p1", "o1")],
        })
        .unwrap();
        assert!(proc.contains(None, "s1", "p1", "o1"));
    }

    #[test]
    fn test_insert_data_named_graph() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![Quad::named_graph("s", "p", "o", "http://g1")],
        })
        .unwrap();
        assert!(proc.contains(Some("http://g1"), "s", "p", "o"));
    }

    #[test]
    fn test_insert_data_multiple_quads() {
        let mut proc = UpdateProcessor::new();
        let result = proc
            .execute(UpdateOperation::InsertData {
                quads: vec![
                    Quad::default_graph("s1", "p1", "o1"),
                    Quad::default_graph("s2", "p2", "o2"),
                    Quad::default_graph("s3", "p3", "o3"),
                ],
            })
            .unwrap();
        assert_eq!(result.triples_inserted, 3);
        assert_eq!(proc.triple_count(), 3);
    }

    #[test]
    fn test_insert_data_deduplication() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![Quad::default_graph("s", "p", "o")],
        })
        .unwrap();
        let result = proc
            .execute(UpdateOperation::InsertData {
                quads: vec![Quad::default_graph("s", "p", "o")],
            })
            .unwrap();
        // Duplicate triple — not inserted again
        assert_eq!(result.triples_inserted, 0);
        assert_eq!(proc.triple_count(), 1);
    }

    #[test]
    fn test_insert_data_result_counts() {
        let mut proc = UpdateProcessor::new();
        let result = proc
            .execute(UpdateOperation::InsertData {
                quads: vec![Quad::default_graph("s", "p", "o")],
            })
            .unwrap();
        assert_eq!(result.operations_applied, 1);
        assert_eq!(result.triples_inserted, 1);
        assert_eq!(result.triples_deleted, 0);
    }

    // ── DeleteData ────────────────────────────────────────────────────────────

    #[test]
    fn test_delete_data_existing_triple() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![Quad::default_graph("s", "p", "o")],
        })
        .unwrap();
        let result = proc
            .execute(UpdateOperation::DeleteData {
                quads: vec![Quad::default_graph("s", "p", "o")],
            })
            .unwrap();
        assert_eq!(result.triples_deleted, 1);
        assert!(!proc.contains(None, "s", "p", "o"));
    }

    #[test]
    fn test_delete_data_nonexistent_triple() {
        let mut proc = UpdateProcessor::new();
        let result = proc
            .execute(UpdateOperation::DeleteData {
                quads: vec![Quad::default_graph("s", "p", "o")],
            })
            .unwrap();
        assert_eq!(result.triples_deleted, 0);
    }

    #[test]
    fn test_delete_data_variable_error() {
        let mut proc = UpdateProcessor::new();
        let result = proc.execute(UpdateOperation::DeleteData {
            quads: vec![Quad::default_graph("?s", "p", "o")],
        });
        assert!(result.is_err());
        match result.unwrap_err() {
            UpdateError::VariableInDeleteData(v) => assert_eq!(v, "?s"),
            e => panic!("wrong error: {:?}", e),
        }
    }

    // ── DeleteWhere ───────────────────────────────────────────────────────────

    #[test]
    fn test_delete_where_all_variables() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("s1", "p1", "o1"),
                Quad::default_graph("s2", "p2", "o2"),
            ],
        })
        .unwrap();
        let result = proc
            .execute(UpdateOperation::DeleteWhere {
                graph: None,
                patterns: vec![TriplePattern {
                    subject: PatternTerm::Variable("s".into()),
                    predicate: PatternTerm::Variable("p".into()),
                    object: PatternTerm::Variable("o".into()),
                }],
            })
            .unwrap();
        assert_eq!(result.triples_deleted, 2);
        assert_eq!(proc.triple_count(), 0);
    }

    #[test]
    fn test_delete_where_specific_predicate() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("s1", "type", "A"),
                Quad::default_graph("s2", "type", "B"),
                Quad::default_graph("s3", "name", "foo"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::DeleteWhere {
            graph: None,
            patterns: vec![TriplePattern {
                subject: PatternTerm::Variable("s".into()),
                predicate: PatternTerm::Iri("type".into()),
                object: PatternTerm::Variable("o".into()),
            }],
        })
        .unwrap();
        assert_eq!(proc.triple_count(), 1);
        assert!(proc.contains(None, "s3", "name", "foo"));
    }

    #[test]
    fn test_delete_where_named_graph() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::named_graph("s", "p", "o", "http://g"),
                Quad::default_graph("s", "p", "o"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::DeleteWhere {
            graph: Some("http://g".into()),
            patterns: vec![TriplePattern {
                subject: PatternTerm::Variable("s".into()),
                predicate: PatternTerm::Variable("p".into()),
                object: PatternTerm::Variable("o".into()),
            }],
        })
        .unwrap();
        assert!(!proc.contains(Some("http://g"), "s", "p", "o"));
        assert!(proc.contains(None, "s", "p", "o")); // default graph unaffected
    }

    // ── Clear ─────────────────────────────────────────────────────────────────

    #[test]
    fn test_clear_default_graph() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("s1", "p", "o"),
                Quad::named_graph("s2", "p", "o", "http://g"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::Clear {
            graph: GraphTarget::Default,
        })
        .unwrap();
        assert_eq!(proc.triple_count(), 1); // named graph untouched
        assert!(proc.contains(Some("http://g"), "s2", "p", "o"));
    }

    #[test]
    fn test_clear_named_graph() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("s1", "p", "o"),
                Quad::named_graph("s2", "p", "o", "http://g"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::Clear {
            graph: GraphTarget::Named("http://g".into()),
        })
        .unwrap();
        assert_eq!(proc.triple_count(), 1); // default graph untouched
    }

    #[test]
    fn test_clear_all() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("s1", "p", "o"),
                Quad::named_graph("s2", "p", "o", "http://g1"),
                Quad::named_graph("s3", "p", "o", "http://g2"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::Clear {
            graph: GraphTarget::All,
        })
        .unwrap();
        assert_eq!(proc.triple_count(), 0);
    }

    #[test]
    fn test_clear_named_graphs_only() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("s1", "p", "o"),
                Quad::named_graph("s2", "p", "o", "http://g"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::Clear {
            graph: GraphTarget::NamedGraphs,
        })
        .unwrap();
        assert_eq!(proc.triple_count(), 1); // default graph unchanged
    }

    // ── Drop ─────────────────────────────────────────────────────────────────

    #[test]
    fn test_drop_named_graph() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::Create {
            graph: "http://g".into(),
            silent: false,
        })
        .unwrap();
        assert_eq!(proc.graph_count(), 1);
        proc.execute(UpdateOperation::Drop {
            graph: GraphTarget::Named("http://g".into()),
            silent: false,
        })
        .unwrap();
        assert_eq!(proc.graph_count(), 0);
    }

    #[test]
    fn test_drop_nonexistent_silent_ok() {
        let mut proc = UpdateProcessor::new();
        let result = proc.execute(UpdateOperation::Drop {
            graph: GraphTarget::Named("http://missing".into()),
            silent: true,
        });
        assert!(result.is_ok());
    }

    #[test]
    fn test_drop_nonexistent_not_silent_error() {
        let mut proc = UpdateProcessor::new();
        let result = proc.execute(UpdateOperation::Drop {
            graph: GraphTarget::Named("http://missing".into()),
            silent: false,
        });
        assert!(result.is_err());
        match result.unwrap_err() {
            UpdateError::GraphNotFound(g) => assert_eq!(g, "http://missing"),
            e => panic!("wrong error: {:?}", e),
        }
    }

    #[test]
    fn test_drop_all() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("s", "p", "o"),
                Quad::named_graph("s", "p", "o", "http://g"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::Drop {
            graph: GraphTarget::All,
            silent: false,
        })
        .unwrap();
        assert_eq!(proc.triple_count(), 0);
        assert_eq!(proc.graph_count(), 0);
    }

    #[test]
    fn test_drop_named_graphs_only() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("s", "p", "o"),
                Quad::named_graph("s", "p", "o", "http://g"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::Drop {
            graph: GraphTarget::NamedGraphs,
            silent: false,
        })
        .unwrap();
        assert_eq!(proc.triple_count(), 1);
        assert_eq!(proc.graph_count(), 0);
    }

    // ── Create ────────────────────────────────────────────────────────────────

    #[test]
    fn test_create_graph() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::Create {
            graph: "http://new".into(),
            silent: false,
        })
        .unwrap();
        assert_eq!(proc.graph_count(), 1);
        assert!(proc.list_graphs().contains(&"http://new".to_string()));
    }

    #[test]
    fn test_create_duplicate_error() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::Create {
            graph: "http://g".into(),
            silent: false,
        })
        .unwrap();
        let result = proc.execute(UpdateOperation::Create {
            graph: "http://g".into(),
            silent: false,
        });
        assert!(result.is_err());
        match result.unwrap_err() {
            UpdateError::GraphAlreadyExists(g) => assert_eq!(g, "http://g"),
            e => panic!("wrong error: {:?}", e),
        }
    }

    #[test]
    fn test_create_duplicate_silent_ok() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::Create {
            graph: "http://g".into(),
            silent: false,
        })
        .unwrap();
        let result = proc.execute(UpdateOperation::Create {
            graph: "http://g".into(),
            silent: true,
        });
        assert!(result.is_ok());
    }

    // ── InsertDelete ──────────────────────────────────────────────────────────

    #[test]
    fn test_insert_delete_basic() {
        let mut proc = UpdateProcessor::new();
        // Insert starting data
        proc.execute(UpdateOperation::InsertData {
            quads: vec![Quad::default_graph("s", "age", "30")],
        })
        .unwrap();

        // DELETE { ?s age ?old } INSERT { ?s age ?old } WHERE { ?s age ?old }
        // (trivial case: re-inserts same data, deletes same data)
        let result = proc
            .execute(UpdateOperation::InsertDelete {
                delete_patterns: vec![TriplePattern {
                    subject: PatternTerm::Variable("s".into()),
                    predicate: PatternTerm::Iri("age".into()),
                    object: PatternTerm::Variable("old".into()),
                }],
                insert_templates: vec![QuadTemplate {
                    subject: PatternTerm::Variable("s".into()),
                    predicate: PatternTerm::Iri("age".into()),
                    object: PatternTerm::Literal("31".into()),
                    graph: None,
                }],
                where_patterns: vec![TriplePattern {
                    subject: PatternTerm::Variable("s".into()),
                    predicate: PatternTerm::Iri("age".into()),
                    object: PatternTerm::Variable("old".into()),
                }],
                using_graph: None,
            })
            .unwrap();
        assert!(result.triples_deleted >= 1);
        assert!(proc.contains(None, "s", "age", "31"));
    }

    // ── execute_sequence rollback ─────────────────────────────────────────────

    #[test]
    fn test_execute_sequence_success() {
        let mut proc = UpdateProcessor::new();
        let result = proc
            .execute_sequence(vec![
                UpdateOperation::InsertData {
                    quads: vec![Quad::default_graph("s1", "p", "o1")],
                },
                UpdateOperation::InsertData {
                    quads: vec![Quad::default_graph("s2", "p", "o2")],
                },
            ])
            .unwrap();
        assert_eq!(result.triples_inserted, 2);
        assert_eq!(proc.triple_count(), 2);
    }

    #[test]
    fn test_execute_sequence_rollback_on_error() {
        let mut proc = UpdateProcessor::new();
        // Pre-populate
        proc.execute(UpdateOperation::InsertData {
            quads: vec![Quad::default_graph("pre", "p", "o")],
        })
        .unwrap();

        let before_count = proc.triple_count();

        // sequence: valid insert + invalid delete-data with variable → rolls back
        let result = proc.execute_sequence(vec![
            UpdateOperation::InsertData {
                quads: vec![Quad::default_graph("new", "p", "o")],
            },
            UpdateOperation::DeleteData {
                quads: vec![Quad::default_graph("?variable", "p", "o")], // ERROR
            },
        ]);

        assert!(result.is_err());
        // State rolled back to before_count
        assert_eq!(proc.triple_count(), before_count);
    }

    #[test]
    fn test_execute_sequence_create_and_insert() {
        let mut proc = UpdateProcessor::new();
        proc.execute_sequence(vec![
            UpdateOperation::Create {
                graph: "http://g".into(),
                silent: false,
            },
            UpdateOperation::InsertData {
                quads: vec![Quad::named_graph("s", "p", "o", "http://g")],
            },
        ])
        .unwrap();
        assert_eq!(proc.graph_count(), 1);
        assert!(proc.contains(Some("http://g"), "s", "p", "o"));
    }

    // ── Metadata queries ──────────────────────────────────────────────────────

    #[test]
    fn test_triple_count_empty() {
        let proc = UpdateProcessor::new();
        assert_eq!(proc.triple_count(), 0);
    }

    #[test]
    fn test_graph_count_empty() {
        let proc = UpdateProcessor::new();
        assert_eq!(proc.graph_count(), 0);
    }

    #[test]
    fn test_list_graphs_empty() {
        let proc = UpdateProcessor::new();
        assert!(proc.list_graphs().is_empty());
    }

    #[test]
    fn test_list_graphs_multiple() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::Create {
            graph: "http://g2".into(),
            silent: false,
        })
        .unwrap();
        proc.execute(UpdateOperation::Create {
            graph: "http://g1".into(),
            silent: false,
        })
        .unwrap();
        let graphs = proc.list_graphs();
        assert_eq!(
            graphs,
            vec!["http://g1".to_string(), "http://g2".to_string()]
        );
    }

    #[test]
    fn test_contains_true() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![Quad::default_graph("s", "p", "o")],
        })
        .unwrap();
        assert!(proc.contains(None, "s", "p", "o"));
    }

    #[test]
    fn test_contains_false() {
        let proc = UpdateProcessor::new();
        assert!(!proc.contains(None, "s", "p", "o"));
    }

    #[test]
    fn test_triple_count_after_delete() {
        let mut proc = UpdateProcessor::new();
        proc.execute(UpdateOperation::InsertData {
            quads: vec![
                Quad::default_graph("a", "p", "o"),
                Quad::default_graph("b", "p", "o"),
            ],
        })
        .unwrap();
        proc.execute(UpdateOperation::DeleteData {
            quads: vec![Quad::default_graph("a", "p", "o")],
        })
        .unwrap();
        assert_eq!(proc.triple_count(), 1);
    }
}