Skip to main content

uqa_execution/query/locking/
context.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Capabilities required by query tuple locking, separated by state ownership.
8
9use super::{
10    CteScope, LockStrength, PhysicalOperator, QueryBlockPlan, QueryPlan, RelationPersistence,
11    RowLockRetryCache, SQLError, SQLParam,
12};
13use crate::row_locks::{
14    binding::{RelationLockCatalog, RelationLockSession},
15    retry_cache::CommittedRowSource,
16    session::RowLockSession,
17    RelationLockMode, RowLockAcquisition,
18};
19use std::sync::Arc;
20use uqa_core::memory::{Produced, ProductionControl};
21use uqa_core::{CancellationToken, DocId};
22use uqa_sql::{
23    ast::{ColumnDef, TableKeyConstraint},
24    routines::RoutineResolution,
25};
26
27pub trait LockingCatalog: RoutineResolution + RelationLockCatalog {
28    fn hierarchy_scan_tables_with_control(
29        &self,
30        table: &str,
31        descendants: bool,
32        control: &ProductionControl<'_>,
33    ) -> Result<Produced<Vec<String>>, SQLError>;
34    fn resolve_relation_with_control(
35        &self,
36        name: &str,
37        bound: bool,
38        control: &ProductionControl<'_>,
39    ) -> Result<Produced<uqa_sql::catalog::resolution::RelationResolution>, SQLError>;
40    /// Lend the selected stored view generation with registry guards released before traversal.
41    fn with_view_plan_with_control(
42        &self,
43        name: &str,
44        control: &ProductionControl<'_>,
45        read: &mut dyn FnMut(&QueryPlan) -> Result<(), SQLError>,
46    ) -> Result<bool, SQLError>;
47    fn view_exists_with_control(
48        &self,
49        name: &str,
50        control: &uqa_core::memory::ProductionControl<'_>,
51    ) -> Result<bool, SQLError>;
52    fn table_persistence(&self, name: &str) -> Result<Option<RelationPersistence>, String>;
53    fn referenceable_keys(&self, table: &str) -> Result<Vec<TableKeyConstraint>, String>;
54    fn table_columns(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String>;
55}
56
57pub trait QueryRowLockSession: RowLockSession + RelationLockSession + Sync {
58    fn lock_relation(&self, table: &str, mode: RelationLockMode) -> Result<(), SQLError>;
59    fn rollback_row_lock_acquisition(&self, acquisition: RowLockAcquisition);
60    fn row_changed_in_open_transaction(&self, table: &str, doc_id: DocId)
61        -> Result<bool, SQLError>;
62    fn row_lock_change_requires_recheck(&self) -> Result<bool, SQLError>;
63    fn current_transaction_is_read_only(&self) -> bool;
64    fn statement_row_lock_cache(&self) -> Result<Arc<RowLockRetryCache>, SQLError>;
65}
66
67pub trait RowLockReadSource: CommittedRowSource + Sync {
68    fn get_document(&self, table: &str, doc_id: DocId) -> Result<Option<Document>, SQLError>;
69    fn get_document_for_mutation(
70        &self,
71        table: &str,
72        doc_id: DocId,
73    ) -> Result<Option<Document>, SQLError>;
74}
75
76pub trait RowLockScopeSource<S: Clone>: Sync {
77    fn production_resources(&self) -> Result<uqa_core::memory::OwnedProductionControl, SQLError>;
78    fn current_routine_scope(&self) -> Result<CteScope<S>, SQLError>;
79    fn transition_relation_names_with_control(
80        &self,
81        control: &ProductionControl<'_>,
82    ) -> Result<uqa_sql::binding::production::BindingNameSet, SQLError>;
83}
84
85pub trait RowRecheckBuilder<S: Clone>: Sync {
86    fn build_recheck<'a>(
87        &'a self,
88        statement: &QueryBlockPlan,
89        params: &'a [SQLParam],
90        ctes: &mut CteScope<S>,
91        ordered: bool,
92        projections: &[super::super::PhysicalProjection],
93    ) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError>;
94}
95
96#[derive(Clone)]
97pub struct RowLockContext<'a, S: Clone> {
98    pub catalog: &'a dyn LockingCatalog,
99    pub session: &'a dyn QueryRowLockSession,
100    pub rows: &'a dyn RowLockReadSource,
101    pub scopes: &'a dyn RowLockScopeSource<S>,
102    pub recheck: &'a dyn RowRecheckBuilder<S>,
103    pub cancellation: &'a CancellationToken,
104}
105
106impl<S: Clone> Copy for RowLockContext<'_, S> {}
107
108pub fn update_lock_strength(
109    catalog: &dyn LockingCatalog,
110    table: &str,
111    columns: &[String],
112) -> LockStrength {
113    let Ok(keys) = catalog.referenceable_keys(table) else {
114        return LockStrength::ForUpdate;
115    };
116    let Ok(Some(definitions)) = catalog.table_columns(table) else {
117        return LockStrength::ForUpdate;
118    };
119    uqa_sql::semantics::locking::update_lock_strength(&keys, &definitions, columns)
120}
121
122use uqa_storage::document_store::Document;