mysql_common/binlog/events/
write_rows_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 bitvec::prelude::*;
10
11use std::io::{self};
12
13use crate::{
14    binlog::{
15        consts::{BinlogVersion, EventType, RowsEventFlags},
16        BinlogCtx, BinlogEvent, BinlogStruct,
17    },
18    io::ParseBuf,
19    proto::{MyDeserialize, MySerialize},
20};
21
22use super::{rows_event::RowsEventCtx, RowsEvent, RowsEventRows, TableMapEvent};
23
24/// Write rows event.
25///
26/// Used for row-based binary logging. Contains the row data to insert.
27#[derive(Debug, Clone, Eq, PartialEq, Hash)]
28#[repr(transparent)]
29pub struct WriteRowsEvent<'a>(RowsEvent<'a>);
30
31impl<'a> WriteRowsEvent<'a> {
32    /// Returns the number that identifies the table (see `TableMapEvent`).
33    pub fn table_id(&self) -> u64 {
34        self.0.table_id()
35    }
36
37    /// Returns the number of columns in the table.
38    pub fn num_columns(&self) -> u64 {
39        self.0.num_columns()
40    }
41
42    /// Returns columns in the after-image (only for WRITE and UPDATE).
43    ///
44    /// Each bit indicates whether corresponding column is used in the image.
45    pub fn columns_after_image(&'a self) -> &'a BitSlice<u8> {
46        self.0.columns_after_image().expect("must be here")
47    }
48
49    /// Returns raw rows data.
50    pub fn rows_data(&'a self) -> &'a [u8] {
51        self.0.rows_data()
52    }
53
54    /// Returns event flags (unknown bits are truncated).
55    pub fn flags(&'a self) -> RowsEventFlags {
56        self.0.flags()
57    }
58
59    /// Returns raw event flags (unknown bits are preserved).
60    pub fn flags_raw(&'a self) -> u16 {
61        self.0.flags_raw()
62    }
63
64    /// Returns an iterator over event's rows given the corresponding `TableMapEvent`.
65    pub fn rows(&'a self, table_map_event: &'a TableMapEvent<'a>) -> RowsEventRows<'a> {
66        RowsEventRows::new(&self.0, table_map_event, ParseBuf(self.rows_data()))
67    }
68
69    pub fn into_owned(self) -> WriteRowsEvent<'static> {
70        WriteRowsEvent(self.0.into_owned())
71    }
72}
73
74impl<'de> MyDeserialize<'de> for WriteRowsEvent<'de> {
75    const SIZE: Option<usize> = RowsEvent::SIZE;
76    type Ctx = BinlogCtx<'de>;
77
78    fn deserialize(ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
79        let ctx = RowsEventCtx {
80            event_type: Self::EVENT_TYPE,
81            binlog_ctx: ctx,
82        };
83        buf.parse(ctx).map(Self)
84    }
85}
86
87impl MySerialize for WriteRowsEvent<'_> {
88    fn serialize(&self, buf: &mut Vec<u8>) {
89        self.0.serialize(&mut *buf);
90    }
91}
92
93impl<'a> BinlogStruct<'a> for WriteRowsEvent<'a> {
94    fn len(&self, version: BinlogVersion) -> usize {
95        self.0.len(version)
96    }
97}
98
99impl<'a> BinlogEvent<'a> for WriteRowsEvent<'a> {
100    const EVENT_TYPE: EventType = EventType::WRITE_ROWS_EVENT;
101}