decode_rows/
decode_rows.rs1use mysql_replication::{
3 Error, EventData, Result,
4 events::{BinlogMode, Full, Minimal, RowImage, RowValue},
5 file::BinlogFile,
6 parser::ParserConfig,
7};
8use std::{
9 fs::File,
10 io::{BufReader, Write},
11};
12
13fn print_image(output: &mut dyn Write, label: &str, image: &RowImage<'_>) -> Result<()> {
14 for field in image.fields() {
15 let field = field?;
16 match field.value {
17 RowValue::Decimal(value) => {
18 writeln!(output, "{label}[{}] = {value}", field.column.index)?
19 }
20 RowValue::Integer(value) => writeln!(
22 output,
23 "{label}[{}] = signed:{} unsigned:{}",
24 field.column.index,
25 value.signed(),
26 value.unsigned()
27 )?,
28 value => writeln!(output, "{label}[{}] = {value:?}", field.column.index)?,
29 }
30 }
31 Ok(())
32}
33
34fn run<M: BinlogMode>(path: &str, output: &mut dyn Write) -> Result<()> {
35 let mut reader =
36 BinlogFile::<_, M>::new(BufReader::new(File::open(path)?), ParserConfig::default())?;
37 while let Some(event) = reader.next_event()? {
38 if let EventData::Rows(event) = event.event {
39 for row in event.rows()? {
40 let (before, after) = row?.operation.into_images();
41 for (label, image) in [("before", before), ("after", after)] {
42 if let Some(image) = image {
43 print_image(output, label, image.as_ref())?;
44 }
45 }
46 }
47 }
48 }
49 Ok(())
50}
51
52fn main() -> Result<()> {
53 let args: Vec<_> = std::env::args().collect();
54 execute(&args, &mut std::io::stdout().lock())
55}
56
57fn execute(args: &[String], output: &mut dyn Write) -> Result<()> {
58 match args {
59 [_, path] => run::<Minimal>(path, output),
60 [_, path, flag, mode] if flag == "--row-image" => match mode.as_str() {
61 "minimal" => run::<Minimal>(path, output),
62 "full" => run::<Full>(path, output),
63 _ => Err(Error::Invalid("row image must be minimal or full".into())),
64 },
65 _ => {
66 eprintln!(
67 "usage: cargo run -p mysql-replication --example decode_rows -- BINLOG_FILE [--row-image minimal|full]"
68 );
69 Ok(())
70 }
71 }
72}
73
74#[cfg(test)]
75#[path = "../tests/support/rows_example.rs"]
76mod tests;