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/// One `PostgreSQL` row-locking clause, including optional `OF` targets and the `NOWAIT` / `SKIP LOCKED` wait policy.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct LockingClause {
12    pub strength: LockStrength,
13    pub wait: LockWait,
14    /// Relation names from `OF t [, ...]`. Empty means every lockable relation in the query block.
15    pub relations: Vec<String>,
16}
17
18/// `PostgreSQL` row-lock strength, strongest last.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
20pub enum LockStrength {
21    ForKeyShare,
22    ForShare,
23    ForNoKeyUpdate,
24    ForUpdate,
25}
26
27impl LockStrength {
28    /// SQL keyword phrase used in `PostgreSQL` error messages.
29    #[must_use]
30    pub const fn sql_name(self) -> &'static str {
31        match self {
32            Self::ForKeyShare => "FOR KEY SHARE",
33            Self::ForShare => "FOR SHARE",
34            Self::ForNoKeyUpdate => "FOR NO KEY UPDATE",
35            Self::ForUpdate => "FOR UPDATE",
36        }
37    }
38}
39
40/// Wait policy for a row-locking clause.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
42pub enum LockWait {
43    Block,
44    SkipLocked,
45    NoWait,
46}