Skip to main content

uqa_sql/ast/
locking.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use serde::{Deserialize, Serialize};
8
9/// Explicit table-lock modes; compatibility is owned by the execution lock manager.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11pub enum TableLockMode {
12    AccessShare,
13    RowShare,
14    RowExclusive,
15    ShareUpdateExclusive,
16    Share,
17    ShareRowExclusive,
18    Exclusive,
19    AccessExclusive,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub struct LockTableTarget {
24    pub name: String,
25    pub include_descendants: bool,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29pub struct LockTableStmt {
30    pub targets: Vec<LockTableTarget>,
31    pub mode: TableLockMode,
32    pub nowait: bool,
33}
34
35/// One `PostgreSQL` row-locking clause, including optional `OF` targets and the `NOWAIT` / `SKIP LOCKED` wait policy.
36#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37pub struct LockingClause {
38    pub strength: LockStrength,
39    pub wait: LockWait,
40    /// Relation names from `OF t [, ...]`. Empty means every lockable relation in the query block.
41    pub relations: Vec<String>,
42}
43
44/// `PostgreSQL` row-lock strength, strongest last.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
46pub enum LockStrength {
47    ForKeyShare,
48    ForShare,
49    ForNoKeyUpdate,
50    ForUpdate,
51}
52
53impl LockStrength {
54    /// SQL keyword phrase used in `PostgreSQL` error messages.
55    #[must_use]
56    pub const fn sql_name(self) -> &'static str {
57        match self {
58            Self::ForKeyShare => "FOR KEY SHARE",
59            Self::ForShare => "FOR SHARE",
60            Self::ForNoKeyUpdate => "FOR NO KEY UPDATE",
61            Self::ForUpdate => "FOR UPDATE",
62        }
63    }
64}
65
66/// Wait policy for a row-locking clause.
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
68pub enum LockWait {
69    Block,
70    SkipLocked,
71    NoWait,
72}