mysql_common/binlog/events/
intvar_event.rs

1// Copyright (c) 2021 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::io;
10
11use crate::{
12    binlog::{
13        consts::{BinlogVersion, EventType, IntvarEventType},
14        BinlogCtx, BinlogEvent, BinlogStruct,
15    },
16    io::ParseBuf,
17    misc::raw::{int::*, Const},
18    proto::{MyDeserialize, MySerialize},
19};
20
21/// Integer based session-variables event.
22///
23/// Written every time a statement uses an AUTO_INCREMENT column or the LAST_INSERT_ID() function;
24/// precedes other events for the statement. This is written only before a QUERY_EVENT
25/// and is not used with row-based logging. An INTVAR_EVENT is written with a "subtype"
26/// in the event data part.
27#[derive(Debug, Clone, Eq, PartialEq, Hash)]
28pub struct IntvarEvent {
29    /// One byte identifying the type of variable stored.
30    subtype: Const<IntvarEventType, u8>,
31    /// The value of the variable.
32    value: RawInt<LeU64>,
33}
34
35impl IntvarEvent {
36    /// Creates a new instance.
37    pub fn new(subtype: IntvarEventType, value: u64) -> Self {
38        Self {
39            subtype: Const::new(subtype),
40            value: RawInt::new(value),
41        }
42    }
43
44    /// Returns the `subtype` value.
45    ///
46    /// `subtype` is a one byte identifying the type of variable stored.
47    pub fn subtype(&self) -> IntvarEventType {
48        self.subtype.0
49    }
50
51    /// Returns the `value` value.
52    ///
53    /// `value` is the value of the variable.
54    pub fn value(&self) -> u64 {
55        self.value.0
56    }
57
58    /// Sets the `subtype` value.
59    pub fn with_subtype(mut self, subtype: IntvarEventType) -> Self {
60        self.subtype = Const::new(subtype);
61        self
62    }
63
64    /// Sets the `value` value.
65    pub fn with_value(mut self, value: u64) -> Self {
66        self.value = RawInt::new(value);
67        self
68    }
69}
70
71impl<'de> MyDeserialize<'de> for IntvarEvent {
72    const SIZE: Option<usize> = Some(9);
73    type Ctx = BinlogCtx<'de>;
74
75    fn deserialize(_ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
76        Ok(Self {
77            subtype: buf.parse_unchecked(())?,
78            value: buf.parse_unchecked(())?,
79        })
80    }
81}
82
83impl MySerialize for IntvarEvent {
84    fn serialize(&self, buf: &mut Vec<u8>) {
85        self.subtype.serialize(&mut *buf);
86        self.value.serialize(&mut *buf);
87    }
88}
89
90impl<'a> BinlogEvent<'a> for IntvarEvent {
91    const EVENT_TYPE: EventType = EventType::INTVAR_EVENT;
92}
93
94impl<'a> BinlogStruct<'a> for IntvarEvent {
95    fn len(&self, _version: BinlogVersion) -> usize {
96        9
97    }
98}