pgrx_pg_sys/submodules/
transaction_id.rs

1//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
2//LICENSE
3//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
4//LICENSE
5//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
6//LICENSE
7//LICENSE All rights reserved.
8//LICENSE
9//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
10use pgrx_sql_entity_graph::metadata::{
11    ArgumentError, Returns, ReturnsError, SqlMapping, SqlTranslatable,
12};
13use std::fmt::{self, Debug, Display, Formatter};
14
15pub type MultiXactId = TransactionId;
16
17/// An `xid` type from PostgreSQL
18#[repr(transparent)]
19#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
20#[derive(serde::Deserialize, serde::Serialize)]
21pub struct TransactionId(u32);
22
23impl TransactionId {
24    pub const INVALID: Self = Self(0);
25    pub const BOOTSTRAP: Self = Self(1);
26    pub const FROZEN: Self = Self(2);
27    pub const FIRST_NORMAL: Self = Self(3);
28    pub const MAX: Self = Self(u32::MAX);
29
30    pub const fn from_inner(xid: u32) -> Self {
31        Self(xid)
32    }
33
34    pub const fn into_inner(self) -> u32 {
35        self.0
36    }
37}
38
39impl Default for TransactionId {
40    fn default() -> Self {
41        Self::INVALID
42    }
43}
44
45impl From<u32> for TransactionId {
46    #[inline]
47    fn from(xid: u32) -> Self {
48        Self::from_inner(xid)
49    }
50}
51
52impl From<TransactionId> for u32 {
53    #[inline]
54    fn from(xid: TransactionId) -> Self {
55        xid.into_inner()
56    }
57}
58
59impl From<TransactionId> for crate::Datum {
60    fn from(xid: TransactionId) -> Self {
61        xid.into_inner().into()
62    }
63}
64
65unsafe impl SqlTranslatable for TransactionId {
66    fn argument_sql() -> Result<SqlMapping, ArgumentError> {
67        Ok(SqlMapping::literal("xid"))
68    }
69
70    fn return_sql() -> Result<Returns, ReturnsError> {
71        Ok(Returns::One(SqlMapping::literal("xid")))
72    }
73}
74
75impl Debug for TransactionId {
76    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
77        Debug::fmt(&self.0, f)
78    }
79}
80
81impl Display for TransactionId {
82    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
83        Display::fmt(&self.0, f)
84    }
85}