use super::{BinlogMode, Event, Unspecified};
use crate::{Result, error::invalid, wire::Cursor};
use std::{io::Write, marker::PhantomData};
mod decoder;
mod typed;
mod value;
pub use decoder::{ColumnDefinition, Row, RowField, RowFields, RowImage, Rows, RowsDecoder};
pub use mysql_common::constants::ColumnType;
pub use typed::{
CompleteImage, Full, ImageMode, Minimal, SparseImage, TypedRow, TypedRows, TypedRowsDecoder,
TypedRowsEvent,
};
pub use value::{DecimalValue, IntegerValue, RowValue};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RowsKind {
Write,
Update,
Delete,
}
#[derive(Debug, Clone, Copy)]
pub struct RowsEvent<'a, M: BinlogMode = Unspecified> {
pub table_id: u64,
pub flags: u16,
pub kind: RowsKind,
pub encoded_images: &'a [u8],
pub(crate) post_header: usize,
pub(crate) v2: bool,
mode: PhantomData<M>,
}
impl<'a> RowsEvent<'a> {
pub fn with_layout(kind: RowsKind, post_header: usize, v2: bool) -> Self {
Self::for_mode(kind, post_header, v2)
}
pub fn rows(&self, decoder: &RowsDecoder) -> Result<Rows<'a>> {
decoder.rows(self)
}
pub fn with_mode<M: ImageMode>(&self, decoder: &RowsDecoder) -> Result<TypedRowsEvent<'a, M>> {
TypedRowsEvent::new(*self, decoder)
}
}
impl<'a, M: BinlogMode> RowsEvent<'a, M> {
pub(crate) fn for_mode(kind: RowsKind, post_header: usize, v2: bool) -> Self {
Self {
table_id: 0,
flags: 0,
kind,
encoded_images: &[],
post_header,
v2,
mode: PhantomData,
}
}
pub fn images(&self) -> Result<RowImages<'a>> {
let mut input = Cursor::new(self.encoded_images);
let extra_data = if self.v2 {
let length = input.le(2)? as usize;
input.take(
length
.checked_sub(2)
.ok_or_else(|| invalid("extra-data length < 2"))?,
)?
} else {
&[]
};
let column_count = input.lenenc()?;
if column_count == 0 || column_count > 4096 {
return Err(invalid("ROWS column count outside 1..4096"));
}
let first = input.take(column_count.div_ceil(8))?;
let second = if self.kind == RowsKind::Update {
input.take(column_count.div_ceil(8))?
} else {
first
};
let images = RowImages {
extra_data,
column_count,
before_columns: first,
after_columns: second,
rows: input.remaining(),
};
M::validate_images(&images)?;
Ok(images)
}
pub(crate) fn unclassified(&self) -> RowsEvent<'a> {
RowsEvent {
table_id: self.table_id,
flags: self.flags,
kind: self.kind,
encoded_images: self.encoded_images,
post_header: self.post_header,
v2: self.v2,
mode: PhantomData,
}
}
}
impl<'a, M: ImageMode> RowsEvent<'a, M> {
pub fn rows(&self, decoder: &RowsDecoder) -> Result<TypedRows<'a, M>> {
self.unclassified()
.with_mode::<M>(decoder)
.map(TypedRowsEvent::rows)
}
}
impl<'a, M: BinlogMode> Event<'a> for RowsEvent<'a, M> {
fn dump(&self, writer: &mut dyn Write) -> Result<()> {
writeln!(writer, "Table ID: {}", self.table_id)?;
writeln!(writer, "Flags: {}", self.flags)?;
writeln!(writer, "Kind: {:?}", self.kind)?;
writeln!(writer, "Encoded images: {:02x?}", self.encoded_images)?;
writeln!(writer)?;
Ok(())
}
fn decode(&mut self, data: &'a [u8]) -> Result<()> {
let width = self
.post_header
.checked_sub(if self.v2 { 4 } else { 2 })
.ok_or_else(|| invalid("short ROWS post-header"))?;
if !matches!(width, 4 | 6) {
return Err(invalid("unsupported ROWS table ID width"));
}
let mut input = Cursor::new(data);
let table_id = input.le(width)?;
let flags = input.le(2)? as u16;
self.table_id = table_id;
self.flags = flags;
self.encoded_images = input.remaining();
M::validate_event(self)
}
}
#[derive(Debug, Clone, Copy)]
pub struct RowImages<'a> {
pub extra_data: &'a [u8],
pub column_count: usize,
pub before_columns: &'a [u8],
pub after_columns: &'a [u8],
pub rows: &'a [u8],
}