1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2021 Anatoly Ikorsky
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use bitvec::prelude::*;

use std::io::{self};

use crate::{
    binlog::{
        consts::{BinlogVersion, EventType, RowsEventFlags},
        BinlogCtx, BinlogEvent, BinlogStruct,
    },
    io::ParseBuf,
    proto::{MyDeserialize, MySerialize},
};

use super::{rows_event::RowsEventCtx, RowsEvent, RowsEventRows, TableMapEvent};

/// Delete rows event.
///
/// Used for row-based binary logging. Contains as much data as needed to identify a row.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct DeleteRowsEvent<'a>(RowsEvent<'a>);

impl<'a> DeleteRowsEvent<'a> {
    /// Returns the number that identifies the table (see `TableMapEvent`).
    pub fn table_id(&self) -> u64 {
        self.0.table_id()
    }

    /// Returns the number of columns in the table.
    pub fn num_columns(&self) -> u64 {
        self.0.num_columns()
    }

    /// Returns columns in the before-image (only for DELETE and UPDATE).
    ///
    /// Each bit indicates whether corresponding column is used in the image.
    pub fn columns_before_image(&'a self) -> &'a BitSlice<u8> {
        self.0.columns_before_image().expect("must be here")
    }

    /// Returns raw rows data.
    pub fn rows_data(&'a self) -> &'a [u8] {
        self.0.rows_data()
    }

    /// Returns event flags (unknown bits are truncated).
    pub fn flags(&'a self) -> RowsEventFlags {
        self.0.flags()
    }

    /// Returns raw event flags (unknown bits are preserved).
    pub fn flags_raw(&'a self) -> u16 {
        self.0.flags_raw()
    }

    /// Returns an iterator over event's rows given the corresponding `TableMapEvent`.
    pub fn rows(&'a self, table_map_event: &'a TableMapEvent<'a>) -> RowsEventRows<'a> {
        RowsEventRows::new(&self.0, table_map_event, ParseBuf(self.rows_data()))
    }

    pub fn into_owned(self) -> DeleteRowsEvent<'static> {
        DeleteRowsEvent(self.0.into_owned())
    }
}

impl<'de> MyDeserialize<'de> for DeleteRowsEvent<'de> {
    const SIZE: Option<usize> = RowsEvent::SIZE;
    type Ctx = BinlogCtx<'de>;

    fn deserialize(ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
        let ctx = RowsEventCtx {
            event_type: Self::EVENT_TYPE,
            binlog_ctx: ctx,
        };
        buf.parse(ctx).map(Self)
    }
}

impl MySerialize for DeleteRowsEvent<'_> {
    fn serialize(&self, buf: &mut Vec<u8>) {
        self.0.serialize(&mut *buf);
    }
}

impl<'a> BinlogStruct<'a> for DeleteRowsEvent<'a> {
    fn len(&self, version: BinlogVersion) -> usize {
        self.0.len(version)
    }
}

impl<'a> BinlogEvent<'a> for DeleteRowsEvent<'a> {
    const EVENT_TYPE: EventType = EventType::DELETE_ROWS_EVENT;
}