mysql_common/binlog/events/
begin_load_query_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::{
10    borrow::Cow,
11    cmp::min,
12    io::{self},
13};
14
15use saturating::Saturating as S;
16
17use crate::{
18    binlog::{
19        consts::{BinlogVersion, EventType},
20        BinlogCtx, BinlogEvent, BinlogStruct,
21    },
22    io::ParseBuf,
23    misc::raw::{bytes::EofBytes, int::LeU32, RawBytes, RawInt},
24    proto::{MyDeserialize, MySerialize},
25};
26
27use super::BinlogEventHeader;
28
29/// Begin load query event.
30///
31/// Used for LOAD DATA INFILE statements as of MySQL 5.0.
32#[derive(Debug, Clone, Eq, PartialEq, Hash)]
33pub struct BeginLoadQueryEvent<'a> {
34    file_id: RawInt<LeU32>,
35    block_data: RawBytes<'a, EofBytes>,
36}
37
38impl<'a> BeginLoadQueryEvent<'a> {
39    pub fn new(file_id: u32) -> Self {
40        Self {
41            file_id: RawInt::new(file_id),
42            block_data: Default::default(),
43        }
44    }
45
46    /// Sets the `file_id` value.
47    pub fn with_file_id(mut self, file_id: u32) -> Self {
48        self.file_id.0 = file_id;
49        self
50    }
51
52    /// Sets the `block_data` value.
53    pub fn with_block_data(mut self, block_data: impl Into<Cow<'a, [u8]>>) -> Self {
54        self.block_data = RawBytes::new(block_data);
55        self
56    }
57
58    /// Returns the `file_id` value.
59    ///
60    /// `file_id` is the ID of the file to begin load to.
61    pub fn file_id(&self) -> u32 {
62        self.file_id.0
63    }
64
65    /// Returns the `block_data` value.
66    ///
67    /// `block_data` is the data to load.
68    pub fn block_data(&'a self) -> &'a [u8] {
69        self.block_data.as_bytes()
70    }
71
72    pub fn into_owned(self) -> BeginLoadQueryEvent<'static> {
73        BeginLoadQueryEvent {
74            file_id: self.file_id,
75            block_data: self.block_data.into_owned(),
76        }
77    }
78}
79
80impl<'de> MyDeserialize<'de> for BeginLoadQueryEvent<'de> {
81    const SIZE: Option<usize> = None;
82    type Ctx = BinlogCtx<'de>;
83
84    fn deserialize(_ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
85        Ok(Self {
86            file_id: buf.parse(())?,
87            block_data: buf.parse(())?,
88        })
89    }
90}
91
92impl MySerialize for BeginLoadQueryEvent<'_> {
93    fn serialize(&self, buf: &mut Vec<u8>) {
94        self.file_id.serialize(&mut *buf);
95        self.block_data.serialize(&mut *buf);
96    }
97}
98
99impl<'a> BinlogStruct<'a> for BeginLoadQueryEvent<'a> {
100    /// Returns length of this load event in bytes.
101    fn len(&self, _version: BinlogVersion) -> usize {
102        let mut len = S(0);
103
104        len += S(4);
105        len += S(self.block_data.len());
106
107        min(len.0, u32::MAX as usize - BinlogEventHeader::LEN)
108    }
109}
110
111impl<'a> BinlogEvent<'a> for BeginLoadQueryEvent<'a> {
112    const EVENT_TYPE: EventType = EventType::BEGIN_LOAD_QUERY_EVENT;
113}