Skip to main content

mysql_handler/hton/
recover_xa_state.rs

1// Copyright (C) 2026 ren-yamanashi
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of this program hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, see <https://www.gnu.org/licenses/>.
22
23//! `enum_ha_recover_xa_state` from `sql/handler.h`.
24
25/// State of an externally coordinated XA transaction reported by the
26/// engine to the transaction coordinator during recovery.
27///
28/// Mirrors `enum class enum_ha_recover_xa_state : int` in
29/// `mysql-server/sql/handler.h`. The `NOT_FOUND = -1` sentinel exists
30/// upstream as a lookup result, not as something an engine reports, so
31/// it is not represented here.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33#[non_exhaustive]
34pub enum RecoverXaState {
35    /// `PREPARED_IN_SE = 0`
36    PreparedInSe,
37    /// `PREPARED_IN_TC = 1`
38    PreparedInTc,
39    /// `COMMITTED_WITH_ONEPHASE = 2`
40    CommittedWithOnephase,
41    /// `COMMITTED = 3`
42    Committed,
43    /// `ROLLEDBACK = 4`
44    RolledBack,
45}
46
47impl RecoverXaState {
48    /// Encode this state as the raw `enum_ha_recover_xa_state` integer the
49    /// shim hands back to MySQL.
50    #[must_use]
51    pub const fn to_raw(self) -> i32 {
52        match self {
53            Self::PreparedInSe => 0,
54            Self::PreparedInTc => 1,
55            Self::CommittedWithOnephase => 2,
56            Self::Committed => 3,
57            Self::RolledBack => 4,
58        }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn raw_values_match_upstream() {
68        assert_eq!(RecoverXaState::PreparedInSe.to_raw(), 0);
69        assert_eq!(RecoverXaState::PreparedInTc.to_raw(), 1);
70        assert_eq!(RecoverXaState::CommittedWithOnephase.to_raw(), 2);
71        assert_eq!(RecoverXaState::Committed.to_raw(), 3);
72        assert_eq!(RecoverXaState::RolledBack.to_raw(), 4);
73    }
74}