mysql_handler/hton/notification_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//! `ha_notification_type` and `SelectExecutedIn` from `sql/handler.h`.
24
25/// Whether the notification fires before (pre) or after (post) the event.
26///
27/// Mirrors `enum ha_notification_type` in `mysql-server/sql/handler.h`. The
28/// pre-event variant is the engine's chance to veto by returning an error from
29/// the notification trait method; post-event is informational.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31#[non_exhaustive]
32pub enum HaNotificationType {
33 /// `HA_NOTIFY_PRE_EVENT`: the engine is asked before the event happens.
34 PreEvent,
35 /// `HA_NOTIFY_POST_EVENT`: the event already occurred.
36 PostEvent,
37}
38
39impl HaNotificationType {
40 /// Decode the C `enum ha_notification_type` value. Unknown values fall back
41 /// to [`HaNotificationType::PostEvent`] so the engine still observes a
42 /// defined variant.
43 #[must_use]
44 pub const fn from_raw(value: i32) -> Self {
45 match value {
46 0 => Self::PreEvent,
47 _ => Self::PostEvent,
48 }
49 }
50}
51
52/// Which engine MySQL executed a `SELECT` on, exposed to the post-select
53/// notification.
54///
55/// Mirrors `enum class SelectExecutedIn : bool` in `mysql-server/sql/handler.h`.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57#[non_exhaustive]
58pub enum SelectExecutedIn {
59 /// `kPrimaryEngine`: the primary (this) engine ran the query.
60 PrimaryEngine,
61 /// `kSecondaryEngine`: a secondary engine ran the query.
62 SecondaryEngine,
63}
64
65impl SelectExecutedIn {
66 /// Decode the C `enum class SelectExecutedIn : bool` value (raw `0` =
67 /// primary, `1` = secondary).
68 #[must_use]
69 pub const fn from_raw(value: bool) -> Self {
70 if value {
71 Self::SecondaryEngine
72 } else {
73 Self::PrimaryEngine
74 }
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn notification_type_from_raw() {
84 assert_eq!(
85 HaNotificationType::from_raw(0),
86 HaNotificationType::PreEvent
87 );
88 assert_eq!(
89 HaNotificationType::from_raw(1),
90 HaNotificationType::PostEvent
91 );
92 assert_eq!(
93 HaNotificationType::from_raw(7),
94 HaNotificationType::PostEvent
95 );
96 }
97
98 #[test]
99 fn select_executed_in_from_raw() {
100 assert_eq!(
101 SelectExecutedIn::from_raw(false),
102 SelectExecutedIn::PrimaryEngine
103 );
104 assert_eq!(
105 SelectExecutedIn::from_raw(true),
106 SelectExecutedIn::SecondaryEngine
107 );
108 }
109}