mysql_replication/events/
table_map.rs1use super::Event;
2use crate::{Result, error::invalid, wire::Cursor};
3use std::io::Write;
4
5#[derive(Debug, Clone, Copy)]
6pub struct TableMapEvent<'a> {
7 pub table_id: u64,
8 pub flags: u16,
9 pub schema: &'a [u8],
10 pub table: &'a [u8],
11 pub column_data: &'a [u8],
14 pub(crate) post_header: usize,
15}
16impl Default for TableMapEvent<'_> {
17 fn default() -> Self {
18 Self::with_post_header(8)
19 }
20}
21impl TableMapEvent<'_> {
22 pub fn with_post_header(post_header: usize) -> Self {
23 Self {
24 table_id: 0,
25 flags: 0,
26 schema: &[],
27 table: &[],
28 column_data: &[],
29 post_header,
30 }
31 }
32 pub fn columns(&self) -> Result<TableColumns<'_>> {
34 let mut input = Cursor::new(self.column_data);
35 let count = input.lenenc()?;
36 if count == 0 || count > 4096 {
37 return Err(invalid("TABLE_MAP column count outside 1..4096"));
38 }
39 let types = input.take(count)?;
40 let metadata_len = input.lenenc()?;
41 let metadata = input.take(metadata_len)?;
42 let nullable = input.take(count.div_ceil(8))?;
43 Ok(TableColumns {
44 count,
45 types,
46 metadata,
47 nullable,
48 optional_metadata: input.remaining(),
49 })
50 }
51}
52#[derive(Debug, Clone, Copy)]
53pub struct TableColumns<'a> {
54 pub count: usize,
55 pub types: &'a [u8],
56 pub metadata: &'a [u8],
57 pub nullable: &'a [u8],
58 pub optional_metadata: &'a [u8],
59}
60impl<'a> Event<'a> for TableMapEvent<'a> {
61 fn dump(&self, writer: &mut dyn Write) -> Result<()> {
62 writeln!(writer, "Table ID: {}", self.table_id)?;
63 writeln!(writer, "Flags: {}", self.flags)?;
64 writeln!(writer, "Schema: {}", String::from_utf8_lossy(self.schema))?;
65 writeln!(writer, "Table: {}", String::from_utf8_lossy(self.table))?;
66 writeln!(writer, "Column data: {:02x?}", self.column_data)?;
67 writeln!(writer)?;
68 Ok(())
69 }
70
71 fn decode(&mut self, data: &'a [u8]) -> Result<()> {
72 let width = match self.post_header {
73 6 => 4,
74 8 => 6,
75 _ => return Err(invalid("unsupported TABLE_MAP post-header")),
76 };
77 let mut input = Cursor::new(data);
78 let table_id = input.le(width)?;
79 let flags = input.le(2)? as u16;
80 let schema = input.name()?;
81 let table = input.name()?;
82 *self = Self {
83 table_id,
84 flags,
85 schema,
86 table,
87 column_data: input.remaining(),
88 post_header: self.post_header,
89 };
90 Ok(())
91 }
92}