uqa_execution/query/locking/
context.rs1use 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::{collections::BTreeSet, sync::Arc};
20use uqa_core::{CancellationToken, DocId};
21use uqa_sql::{
22 ast::{ColumnDef, TableKeyConstraint},
23 routines::RoutineResolution,
24};
25
26pub trait LockingCatalog: RoutineResolution + RelationLockCatalog {
27 fn hierarchy_scan_tables(
28 &self,
29 table: &str,
30 descendants: bool,
31 ) -> Result<Vec<String>, SQLError>;
32 fn resolve_relation(
33 &self,
34 name: &str,
35 bound: bool,
36 ) -> Result<Option<(String, &'static str)>, SQLError>;
37 fn view_plan(&self, name: &str) -> Result<Option<QueryPlan>, SQLError>;
38 fn table_persistence(&self, name: &str) -> Result<Option<RelationPersistence>, String>;
39 fn referenceable_keys(&self, table: &str) -> Result<Vec<TableKeyConstraint>, String>;
40 fn table_columns(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String>;
41}
42
43pub trait QueryRowLockSession: RowLockSession + RelationLockSession + Sync {
44 fn lock_relation(&self, table: &str, mode: RelationLockMode) -> Result<(), SQLError>;
45 fn rollback_row_lock_acquisition(&self, acquisition: RowLockAcquisition);
46 fn row_changed_in_open_transaction(&self, table: &str, doc_id: DocId)
47 -> Result<bool, SQLError>;
48 fn row_lock_change_requires_recheck(&self) -> Result<bool, SQLError>;
49 fn current_transaction_is_read_only(&self) -> bool;
50 fn statement_row_lock_cache(&self) -> Result<Arc<RowLockRetryCache>, SQLError>;
51}
52
53pub trait RowLockReadSource: CommittedRowSource + Sync {
54 fn get_document(&self, table: &str, doc_id: DocId) -> Result<Option<Document>, SQLError>;
55 fn get_document_for_mutation(
56 &self,
57 table: &str,
58 doc_id: DocId,
59 ) -> Result<Option<Document>, SQLError>;
60}
61
62pub trait RowLockScopeSource<S: Clone>: Sync {
63 fn current_routine_scope(&self) -> CteScope<S>;
64 fn transition_relation_names(&self) -> BTreeSet<String>;
65}
66
67pub trait RowRecheckBuilder<S: Clone>: Sync {
68 fn build_recheck<'a>(
69 &'a self,
70 statement: &QueryBlockPlan,
71 params: &'a [SQLParam],
72 ctes: &mut CteScope<S>,
73 ordered: bool,
74 projections: &[super::super::PhysicalProjection],
75 ) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError>;
76}
77
78#[derive(Clone)]
79pub struct RowLockContext<'a, S: Clone> {
80 pub catalog: &'a dyn LockingCatalog,
81 pub session: &'a dyn QueryRowLockSession,
82 pub rows: &'a dyn RowLockReadSource,
83 pub scopes: &'a dyn RowLockScopeSource<S>,
84 pub recheck: &'a dyn RowRecheckBuilder<S>,
85 pub cancellation: &'a CancellationToken,
86}
87
88impl<S: Clone> Copy for RowLockContext<'_, S> {}
89
90pub fn update_lock_strength(
91 catalog: &dyn LockingCatalog,
92 table: &str,
93 columns: &[String],
94) -> LockStrength {
95 let Ok(keys) = catalog.referenceable_keys(table) else {
96 return LockStrength::ForUpdate;
97 };
98 let Ok(Some(definitions)) = catalog.table_columns(table) else {
99 return LockStrength::ForUpdate;
100 };
101 uqa_sql::semantics::locking::update_lock_strength(&keys, &definitions, columns)
102}
103
104use uqa_storage::document_store::Document;