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