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.
89use std::io;
1011use 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};
2021/// 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.
30subtype: Const<IntvarEventType, u8>,
31/// The value of the variable.
32value: RawInt<LeU64>,
33}
3435impl IntvarEvent {
36/// Creates a new instance.
37pub fn new(subtype: IntvarEventType, value: u64) -> Self {
38Self {
39 subtype: Const::new(subtype),
40 value: RawInt::new(value),
41 }
42 }
4344/// Returns the `subtype` value.
45 ///
46 /// `subtype` is a one byte identifying the type of variable stored.
47pub fn subtype(&self) -> IntvarEventType {
48self.subtype.0
49}
5051/// Returns the `value` value.
52 ///
53 /// `value` is the value of the variable.
54pub fn value(&self) -> u64 {
55self.value.0
56}
5758/// Sets the `subtype` value.
59pub fn with_subtype(mut self, subtype: IntvarEventType) -> Self {
60self.subtype = Const::new(subtype);
61self
62}
6364/// Sets the `value` value.
65pub fn with_value(mut self, value: u64) -> Self {
66self.value = RawInt::new(value);
67self
68}
69}
7071impl<'de> MyDeserialize<'de> for IntvarEvent {
72const SIZE: Option<usize> = Some(9);
73type Ctx = BinlogCtx<'de>;
7475fn deserialize(_ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
76Ok(Self {
77 subtype: buf.parse_unchecked(())?,
78 value: buf.parse_unchecked(())?,
79 })
80 }
81}
8283impl MySerialize for IntvarEvent {
84fn serialize(&self, buf: &mut Vec<u8>) {
85self.subtype.serialize(&mut *buf);
86self.value.serialize(&mut *buf);
87 }
88}
8990impl<'a> BinlogEvent<'a> for IntvarEvent {
91const EVENT_TYPE: EventType = EventType::INTVAR_EVENT;
92}
9394impl<'a> BinlogStruct<'a> for IntvarEvent {
95fn len(&self, _version: BinlogVersion) -> usize {
969
97}
98}