mysql_handler/hton/tablespace_kind.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//! `ts_command_type` and `Tablespace_type` from `sql/handler.h`.
24
25/// The tablespace DDL command MySQL is asking the engine to validate.
26///
27/// Mirrors `enum ts_command_type` in `mysql-server/sql/handler.h`.
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29#[non_exhaustive]
30pub enum TsCommandType {
31 /// `TS_CMD_NOT_DEFINED = -1`
32 NotDefined,
33 /// `CREATE_TABLESPACE = 0`
34 CreateTablespace,
35 /// `ALTER_TABLESPACE = 1`
36 AlterTablespace,
37 /// `CREATE_LOGFILE_GROUP = 2`
38 CreateLogfileGroup,
39 /// `ALTER_LOGFILE_GROUP = 3`
40 AlterLogfileGroup,
41 /// `DROP_TABLESPACE = 4`
42 DropTablespace,
43 /// `DROP_LOGFILE_GROUP = 5`
44 DropLogfileGroup,
45 /// `CHANGE_FILE_TABLESPACE = 6`
46 ChangeFileTablespace,
47 /// `ALTER_ACCESS_MODE_TABLESPACE = 7`
48 AlterAccessModeTablespace,
49 /// `CREATE_UNDO_TABLESPACE = 8`
50 CreateUndoTablespace,
51 /// `ALTER_UNDO_TABLESPACE = 9`
52 AlterUndoTablespace,
53 /// `DROP_UNDO_TABLESPACE = 10`
54 DropUndoTablespace,
55 /// Forward-compatible fallback for any value MySQL adds in the future.
56 Unknown,
57}
58
59impl TsCommandType {
60 /// Decode the C `enum ts_command_type` value. The C enum uses -1 for the
61 /// not-defined sentinel; unknown positive values map to
62 /// [`TsCommandType::Unknown`].
63 #[must_use]
64 pub const fn from_raw(value: i32) -> Self {
65 match value {
66 -1 => Self::NotDefined,
67 0 => Self::CreateTablespace,
68 1 => Self::AlterTablespace,
69 2 => Self::CreateLogfileGroup,
70 3 => Self::AlterLogfileGroup,
71 4 => Self::DropTablespace,
72 5 => Self::DropLogfileGroup,
73 6 => Self::ChangeFileTablespace,
74 7 => Self::AlterAccessModeTablespace,
75 8 => Self::CreateUndoTablespace,
76 9 => Self::AlterUndoTablespace,
77 10 => Self::DropUndoTablespace,
78 _ => Self::Unknown,
79 }
80 }
81}
82
83/// The classification of a data-dictionary tablespace.
84///
85/// Mirrors `enum class Tablespace_type` in `mysql-server/sql/handler.h`.
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87#[non_exhaustive]
88pub enum TablespaceType {
89 /// `SPACE_TYPE_DICTIONARY`
90 Dictionary,
91 /// `SPACE_TYPE_SYSTEM`
92 System,
93 /// `SPACE_TYPE_UNDO`
94 Undo,
95 /// `SPACE_TYPE_TEMPORARY`
96 Temporary,
97 /// `SPACE_TYPE_SHARED`
98 Shared,
99 /// `SPACE_TYPE_IMPLICIT`
100 Implicit,
101}
102
103impl TablespaceType {
104 /// Convert to the raw C value the shim writes into `Tablespace_type*`.
105 #[must_use]
106 pub const fn to_raw(self) -> u32 {
107 match self {
108 Self::Dictionary => 0,
109 Self::System => 1,
110 Self::Undo => 2,
111 Self::Temporary => 3,
112 Self::Shared => 4,
113 Self::Implicit => 5,
114 }
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 #[test]
123 fn ts_command_known_values() {
124 assert_eq!(TsCommandType::from_raw(-1), TsCommandType::NotDefined);
125 assert_eq!(TsCommandType::from_raw(0), TsCommandType::CreateTablespace);
126 assert_eq!(
127 TsCommandType::from_raw(10),
128 TsCommandType::DropUndoTablespace
129 );
130 }
131
132 #[test]
133 fn ts_command_unknown_falls_back() {
134 assert_eq!(TsCommandType::from_raw(99), TsCommandType::Unknown);
135 assert_eq!(TsCommandType::from_raw(-99), TsCommandType::Unknown);
136 }
137
138 #[test]
139 fn tablespace_type_to_raw_round_trip() {
140 let pairs = [
141 (TablespaceType::Dictionary, 0),
142 (TablespaceType::System, 1),
143 (TablespaceType::Undo, 2),
144 (TablespaceType::Temporary, 3),
145 (TablespaceType::Shared, 4),
146 (TablespaceType::Implicit, 5),
147 ];
148 for (v, raw) in pairs {
149 assert_eq!(v.to_raw(), raw);
150 }
151 }
152}