mysql_plugin_api/
types.rs

1use std::ffi::c_void;
2use std::os::raw::c_ulong;
3
4/// For each client connection we create a separate thread with THD serving as a thread/connection descriptor.
5type THD = c_void;
6
7/// The type of plugin being declared.
8#[derive(Eq, PartialEq, Copy, Clone)]
9#[cfg_attr(test, derive(Debug))]
10#[non_exhaustive]
11#[repr(i32)]
12pub enum PluginType {
13    /// User-Defined Function.
14    Udf = 0,
15
16    /// Storage engine.
17    Storage = 1,
18}
19
20/// The license under which the plugin is being published.
21#[derive(Eq, PartialEq, Copy, Clone)]
22#[cfg_attr(test, derive(Debug))]
23#[repr(i32)]
24pub enum License {
25    /// A proprietary license.
26    Proprietary = 0,
27
28    /// The GNU General Public License.
29    Gpl = 1,
30
31    /// The BSD license.
32    Bsd = 2,
33}
34
35/// Information about a storage engine plugin.
36#[derive(Copy, Clone)]
37#[cfg_attr(test, derive(Debug))]
38#[repr(C)]
39pub struct StorageEngineInfo {
40    /// This needs to be set to `MYSQL_HANDLERTON_INTERFACE_VERSION`.
41    pub interface_version: i32,
42}
43
44#[derive(Eq, PartialEq, Copy, Clone)]
45#[cfg_attr(test, derive(Debug))]
46#[allow(missing_docs)]
47#[repr(C)]
48pub enum ShowCompOption {
49    Yes,
50    No,
51    Disabled,
52}
53
54/// Legacy Database Type
55#[derive(Eq, PartialEq, Copy, Clone)]
56#[cfg_attr(test, derive(Debug))]
57#[allow(missing_docs)]
58#[repr(C)]
59pub enum LegacyDbType {
60    Unknown = 0,
61    DiabIsam = 1,
62    Hash,
63    MIsam,
64    PIsam,
65    RmsIsam,
66    Heam,
67    Isam,
68    MrgIsam,
69    MyIsam,
70    MrgMyIsam,
71    BerkeleyDb,
72    InnoDb,
73    Gemini,
74    NDbCluster,
75    ExampleDb,
76    ArchiveDb,
77    CsvDb,
78    FederatedDb,
79    BlackholeDb,
80    PartitionDb,
81    // No longer used.
82    Binlog,
83    Solid,
84    Pbxt,
85    TableFunction,
86    MemCache,
87    Falcon,
88    Maria,
89    /** Performance schema engine. */
90    PerformanceSchema,
91    TempTable,
92    FirstDynamic = 42,
93    Default = 127,  // Must be last
94}
95
96/// Close-connection notification.
97pub type CloseConnectionFunc = extern "C" fn(hton: *mut Handlerton, thd: *mut THD) -> i32;
98
99/// Terminate-connection/statement notification.
100pub type KillConnectionFunc = extern "C" fn(hton: *mut Handlerton, thd: *mut THD);
101
102/// Shut down all storage engine background tasks that might access the data dictionary, before the main shutdown.
103pub type PreDdShutdownFunc = extern "C" fn(hton: *mut Handlerton);
104
105/// The HANDLER-singleTON.
106#[derive(Copy, Clone)]
107#[cfg_attr(test, derive(Debug))]
108#[allow(missing_docs)]
109#[repr(C)]
110pub struct Handlerton {
111    /// Whether the plugin should be shown.
112    pub state: ShowCompOption,
113
114    /// Historical number used for frm file to determine the correct storage engine.
115    pub db_type: LegacyDbType,
116
117    /// Memory area of the storage engine.
118    pub slot: u32,
119
120    /// To store per-savepoint data storage engine is provided with an area of a requested size (0 is ok here).
121    pub savepoint_offset: u32,
122
123    pub close_connection: *const CloseConnectionFunc,
124
125    pub kill_connection: *const KillConnectionFunc,
126
127    pub pre_dd_shutdown: *const PreDdShutdownFunc,
128}
129
130/// Function to invoke when plugin is loaded.
131pub type InitFunc = extern "C" fn(*mut c_void) -> i32;
132
133/// Function to invoke when plugin is uninstalled.
134pub type CheckUninstallFunc = extern "C" fn(*mut c_void) -> i32;
135
136/// Function to invoke when plugin is unloaded.
137pub type DeinitFunc = extern "C" fn(*mut c_void) -> i32;
138
139/// A plugin description.
140#[cfg_attr(test, derive(Debug))]
141#[repr(C)]
142pub struct Plugin {
143    /// The type of the plugin.
144    pub plugin_type: PluginType,
145
146    /// Pointer to one of the supported info structures.
147    ///
148    /// These are:
149    ///  * `StorageEngineInfo`
150    pub info: *const c_void,
151
152    /// The name of the plugin.
153    pub name: *const u8,
154
155    /// The author (Person or organization).
156    pub author: *const u8,
157
158    /// The description.
159    pub descr: *const u8,
160
161    /// The license under which the plugin is published.
162    pub license: License,
163
164    /// Function to invoke when plugin is loaded.
165    pub init: *const InitFunc,
166
167    /// Function to invoke when plugin is uninstalled.
168    pub check_uninstall: *const CheckUninstallFunc,
169
170    /// Function to invoke when plugin is unloaded.
171    pub deinit: *const DeinitFunc,
172
173    /// Version number of the plugin.
174    pub version: u32,
175
176    /// TODO: SHOW STATUS Server status variable
177    pub status_vars: *const c_void,
178
179    /// TODO: Definition of system vars structure for access their information in the plugin
180    pub system_vars: *const c_void,
181
182    /// Reserved for dependency checking.
183    pub reserved: *const c_void,
184
185    /// Flags for the plugin.
186    pub flags: c_ulong,
187}
188
189unsafe impl Sync for Plugin {}
190
191impl Plugin {
192    /// Create a Plugin information structure with all fields being zero.
193    pub const fn zero() -> Self {
194        Self {
195            plugin_type: PluginType::Udf,
196            info: std::ptr::null_mut(),
197            name: std::ptr::null(),
198            author: std::ptr::null(),
199            descr: std::ptr::null(),
200            license: License::Proprietary,
201            init: std::ptr::null(),
202            check_uninstall: std::ptr::null(),
203            deinit: std::ptr::null(),
204            version: 0,
205            status_vars: std::ptr::null(),
206            system_vars: std::ptr::null(),
207            reserved: std::ptr::null(),
208            flags: 0,
209        }
210    }
211}
212
213#[cfg(test)]
214mod tests {
215    use std::ffi::c_void;
216
217    use crate::constants::MYSQL_HANDLERTON_INTERFACE_VERSION;
218    use crate::types::{License, Plugin, PluginType, StorageEngineInfo};
219
220    #[test]
221    fn create_plugin_struct() {
222        let info = StorageEngineInfo {
223            interface_version: MYSQL_HANDLERTON_INTERFACE_VERSION,
224        };
225
226        let plugin = Plugin {
227            plugin_type: PluginType::Storage,
228            info: &info as *const _ as *const c_void,
229            name: b"example\0" as *const u8,
230            author: b"Felix Bytow\0" as *const u8,
231            descr: b"Example storage engine in Rust\0" as *const u8,
232            license: License::Bsd,
233            ..Plugin::zero()
234        };
235
236        let _plugins: [Plugin; 2] = [
237            plugin,
238            Plugin::zero(),
239        ];
240    }
241}