mysql_handler/hton/binlog_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//! `enum_binlog_func` and `enum_binlog_command` from `sql/handler.h`.
24
25/// The binlog-related function MySQL is asking the engine to perform.
26///
27/// Mirrors `enum enum_binlog_func` in `mysql-server/sql/handler.h`.
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29#[non_exhaustive]
30pub enum BinlogFunc {
31 /// `BFN_RESET_LOGS = 1`
32 ResetLogs,
33 /// `BFN_RESET_SLAVE = 2`
34 ResetSlave,
35 /// `BFN_BINLOG_WAIT = 3`
36 BinlogWait,
37 /// `BFN_BINLOG_END = 4`
38 BinlogEnd,
39 /// `BFN_BINLOG_PURGE_FILE = 5`
40 BinlogPurgeFile,
41 /// `BFN_BINLOG_PURGE_WAIT = 6`
42 BinlogPurgeWait,
43 /// Forward-compatible fallback for any value MySQL adds in the future.
44 Unknown,
45}
46
47impl BinlogFunc {
48 /// Decode the C `enum enum_binlog_func` value. Unknown values map to
49 /// [`BinlogFunc::Unknown`].
50 #[must_use]
51 pub const fn from_raw(value: u32) -> Self {
52 match value {
53 1 => Self::ResetLogs,
54 2 => Self::ResetSlave,
55 3 => Self::BinlogWait,
56 4 => Self::BinlogEnd,
57 5 => Self::BinlogPurgeFile,
58 6 => Self::BinlogPurgeWait,
59 _ => Self::Unknown,
60 }
61 }
62}
63
64/// The DDL command flavour MySQL is logging through the binary log.
65///
66/// Mirrors `enum enum_binlog_command` in `mysql-server/sql/handler.h`.
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68#[non_exhaustive]
69pub enum BinlogCommand {
70 /// `LOGCOM_CREATE_TABLE`
71 CreateTable,
72 /// `LOGCOM_ALTER_TABLE`
73 AlterTable,
74 /// `LOGCOM_RENAME_TABLE`
75 RenameTable,
76 /// `LOGCOM_DROP_TABLE`
77 DropTable,
78 /// `LOGCOM_CREATE_DB`
79 CreateDb,
80 /// `LOGCOM_ALTER_DB`
81 AlterDb,
82 /// `LOGCOM_DROP_DB`
83 DropDb,
84 /// Forward-compatible fallback for any value MySQL adds in the future.
85 Unknown,
86}
87
88impl BinlogCommand {
89 /// Decode the C `enum enum_binlog_command` value. Unknown values map to
90 /// [`BinlogCommand::Unknown`].
91 #[must_use]
92 pub const fn from_raw(value: u32) -> Self {
93 match value {
94 0 => Self::CreateTable,
95 1 => Self::AlterTable,
96 2 => Self::RenameTable,
97 3 => Self::DropTable,
98 4 => Self::CreateDb,
99 5 => Self::AlterDb,
100 6 => Self::DropDb,
101 _ => Self::Unknown,
102 }
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn binlog_func_known_values() {
112 assert_eq!(BinlogFunc::from_raw(1), BinlogFunc::ResetLogs);
113 assert_eq!(BinlogFunc::from_raw(6), BinlogFunc::BinlogPurgeWait);
114 }
115
116 #[test]
117 fn binlog_func_unknown_falls_back() {
118 assert_eq!(BinlogFunc::from_raw(0), BinlogFunc::Unknown);
119 assert_eq!(BinlogFunc::from_raw(99), BinlogFunc::Unknown);
120 }
121
122 #[test]
123 fn binlog_command_known_values() {
124 assert_eq!(BinlogCommand::from_raw(0), BinlogCommand::CreateTable);
125 assert_eq!(BinlogCommand::from_raw(6), BinlogCommand::DropDb);
126 }
127
128 #[test]
129 fn binlog_command_unknown_falls_back() {
130 assert_eq!(BinlogCommand::from_raw(99), BinlogCommand::Unknown);
131 }
132}