Skip to main content

uqa_execution/mutation/referential/
context.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Capabilities for referential action preparation and tuple rechecks.
8use crate::{
9    mutation::{
10        assignment::MutationAssignmentContext, constraints::ConstraintContext,
11        identity::MutationIdentifiers, triggers::context::TriggerContext,
12    },
13    query::locking::context::RowLockContext,
14};
15use uqa_core::DocId;
16use uqa_sql::{ast::ForeignKey, SQLError};
17/// Retained latest row images used only for referential checks under a fixed transaction snapshot.
18pub trait ReferentialReadSnapshot {
19    fn doc_ids(&self, table: &str) -> Result<Vec<DocId>, SQLError>;
20    /// Probe the same retained row generation used by this snapshot's document reads.
21    fn value_index_scan_key(
22        &self,
23        table: &str,
24        key: &uqa_storage::ValueIndexKey,
25        predicate: &uqa_core::Predicate,
26    ) -> Result<Option<uqa_core::PostingList>, SQLError>;
27    fn document(
28        &self,
29        table: &str,
30        doc_id: DocId,
31    ) -> Result<Option<uqa_storage::document_store::Document>, SQLError>;
32    fn metadata(
33        &self,
34        table: &str,
35        doc_id: DocId,
36    ) -> Result<Option<uqa_storage::DocumentMetadata>, SQLError>;
37}
38pub trait ReferentialSnapshots {
39    fn latest_reference_snapshot(&self) -> Result<Box<dyn ReferentialReadSnapshot + '_>, SQLError>;
40    fn transaction_document_metadata(
41        &self,
42        table: &str,
43        doc_id: DocId,
44    ) -> Result<Option<uqa_storage::DocumentMetadata>, SQLError>;
45}
46/// The deferral of the checks a change to a referenced row fires, under the constraint `derived` names when the foreign key derives one on the firing partition.
47pub trait ReferentialDeferrals {
48    fn defer_foreign_key_check(
49        &self,
50        constraint_table: &str,
51        firing_table: &str,
52        row_table: &str,
53        doc_id: DocId,
54        foreign_key: &ForeignKey,
55        derived: Option<&uqa_sql::ast::ReferencedPartitionConstraint>,
56    ) -> Result<(), SQLError>;
57    fn defer_foreign_key_parent_event(
58        &self,
59        constraint_table: &str,
60        firing_table: &str,
61        foreign_key: &ForeignKey,
62        derived: Option<&uqa_sql::ast::ReferencedPartitionConstraint>,
63    ) -> Result<(), SQLError>;
64}
65#[derive(Clone)]
66pub struct ReferentialContext<'a, S: Clone + 'static> {
67    pub constraints: ConstraintContext<'a>,
68    pub locking: RowLockContext<'a, S>,
69    pub assignment: MutationAssignmentContext<'a, S>,
70    pub identifiers: &'a dyn MutationIdentifiers,
71    pub triggers: TriggerContext<'a>,
72    pub deferrals: &'a dyn ReferentialDeferrals,
73    pub snapshots: &'a dyn ReferentialSnapshots,
74}
75impl<S: Clone + 'static> Copy for ReferentialContext<'_, S> {}