mysql_replication/events/
rows.rs1use super::{BinlogMode, Event, Unspecified};
2use crate::{Result, error::invalid, wire::Cursor};
3use std::{io::Write, marker::PhantomData};
4
5mod decoder;
6mod typed;
7mod value;
8
9pub use decoder::{ColumnDefinition, Row, RowField, RowFields, RowImage, Rows, RowsDecoder};
10pub use mysql_common::constants::ColumnType;
11pub use typed::{
12 CompleteImage, Full, ImageMode, Minimal, SparseImage, TypedRow, TypedRows, TypedRowsDecoder,
13 TypedRowsEvent,
14};
15pub use value::{DecimalValue, IntegerValue, RowValue};
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum RowsKind {
20 Write,
21 Update,
22 Delete,
23}
24
25#[derive(Debug, Clone, Copy)]
30pub struct RowsEvent<'a, M: BinlogMode = Unspecified> {
31 pub table_id: u64,
33 pub flags: u16,
35 pub kind: RowsKind,
36 pub encoded_images: &'a [u8],
38 pub(crate) post_header: usize,
39 pub(crate) v2: bool,
40 mode: PhantomData<M>,
41}
42impl<'a> RowsEvent<'a> {
43 pub fn with_layout(kind: RowsKind, post_header: usize, v2: bool) -> Self {
44 Self::for_mode(kind, post_header, v2)
45 }
46 pub fn rows(&self, decoder: &RowsDecoder) -> Result<Rows<'a>> {
48 decoder.rows(self)
49 }
50 pub fn with_mode<M: ImageMode>(&self, decoder: &RowsDecoder) -> Result<TypedRowsEvent<'a, M>> {
52 TypedRowsEvent::new(*self, decoder)
53 }
54}
55impl<'a, M: BinlogMode> RowsEvent<'a, M> {
56 pub(crate) fn for_mode(kind: RowsKind, post_header: usize, v2: bool) -> Self {
57 Self {
58 table_id: 0,
59 flags: 0,
60 kind,
61 encoded_images: &[],
62 post_header,
63 v2,
64 mode: PhantomData,
65 }
66 }
67 pub fn images(&self) -> Result<RowImages<'a>> {
68 let mut input = Cursor::new(self.encoded_images);
69 let extra_data = if self.v2 {
70 let length = input.le(2)? as usize;
71 input.take(
72 length
73 .checked_sub(2)
74 .ok_or_else(|| invalid("extra-data length < 2"))?,
75 )?
76 } else {
77 &[]
78 };
79 let column_count = input.lenenc()?;
80 if column_count == 0 || column_count > 4096 {
81 return Err(invalid("ROWS column count outside 1..4096"));
82 }
83 let first = input.take(column_count.div_ceil(8))?;
84 let second = if self.kind == RowsKind::Update {
85 input.take(column_count.div_ceil(8))?
86 } else {
87 first
88 };
89 let images = RowImages {
90 extra_data,
91 column_count,
92 before_columns: first,
93 after_columns: second,
94 rows: input.remaining(),
95 };
96 M::validate_images(&images)?;
97 Ok(images)
98 }
99
100 pub(crate) fn unclassified(&self) -> RowsEvent<'a> {
101 RowsEvent {
102 table_id: self.table_id,
103 flags: self.flags,
104 kind: self.kind,
105 encoded_images: self.encoded_images,
106 post_header: self.post_header,
107 v2: self.v2,
108 mode: PhantomData,
109 }
110 }
111}
112impl<'a, M: ImageMode> RowsEvent<'a, M> {
113 pub fn rows(&self, decoder: &RowsDecoder) -> Result<TypedRows<'a, M>> {
115 self.unclassified()
116 .with_mode::<M>(decoder)
117 .map(TypedRowsEvent::rows)
118 }
119}
120impl<'a, M: BinlogMode> Event<'a> for RowsEvent<'a, M> {
121 fn dump(&self, writer: &mut dyn Write) -> Result<()> {
122 writeln!(writer, "Table ID: {}", self.table_id)?;
123 writeln!(writer, "Flags: {}", self.flags)?;
124 writeln!(writer, "Kind: {:?}", self.kind)?;
125 writeln!(writer, "Encoded images: {:02x?}", self.encoded_images)?;
126 writeln!(writer)?;
127 Ok(())
128 }
129
130 fn decode(&mut self, data: &'a [u8]) -> Result<()> {
131 let width = self
132 .post_header
133 .checked_sub(if self.v2 { 4 } else { 2 })
134 .ok_or_else(|| invalid("short ROWS post-header"))?;
135 if !matches!(width, 4 | 6) {
136 return Err(invalid("unsupported ROWS table ID width"));
137 }
138 let mut input = Cursor::new(data);
139 let table_id = input.le(width)?;
140 let flags = input.le(2)? as u16;
141 self.table_id = table_id;
142 self.flags = flags;
143 self.encoded_images = input.remaining();
144 M::validate_event(self)
145 }
146}
147#[derive(Debug, Clone, Copy)]
148pub struct RowImages<'a> {
149 pub extra_data: &'a [u8],
150 pub column_count: usize,
151 pub before_columns: &'a [u8],
153 pub after_columns: &'a [u8],
155 pub rows: &'a [u8],
156}