From af32ac991f1dd3d9138227436b5759889c1aaf77 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 22 May 2021 14:01:04 +0100 Subject: Move everything into more sensible places --- lib/output/dumpToConsole.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++ lib/output/dumpToConsole.h | 23 +++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 lib/output/dumpToConsole.cpp create mode 100644 lib/output/dumpToConsole.h (limited to 'lib/output') diff --git a/lib/output/dumpToConsole.cpp b/lib/output/dumpToConsole.cpp new file mode 100644 index 0000000..6ece800 --- /dev/null +++ b/lib/output/dumpToConsole.cpp @@ -0,0 +1,81 @@ +#include "dumpToConsole.h" +#include "../compileTimeFormatter.h" +#include "../row.h" +#include "../streamSupport.h" + +namespace MyGrate::Output { + void + DumpToConsole::tableMap(MyGrate::MariaDB_Event_Ptr event) + { + const auto & tm = event->event.table_map; + AdHoc::scprintf<"Table map %?.%? -> %?\n">(std::cout, tm.database, tm.table, tm.table_id); + EventHandlerBase::tableMap(std::move(event)); + } + + void + DumpToConsole::insertRow(MyGrate::MariaDB_Event_Ptr event) + { + const auto & rs = event->event.rows; + AdHoc::scprintf<"Insert into %?\n">(std::cout, rs.table_id); + dumpRowData(event->event.rows); + } + + void + DumpToConsole::updateRow(MyGrate::MariaDB_Event_Ptr event) + { + const auto & rs = event->event.rows; + AdHoc::scprintf<"Update %?\n">(std::cout, rs.table_id); + dumpRowPairData(event->event.rows); + } + + void + DumpToConsole::deleteRow(MyGrate::MariaDB_Event_Ptr event) + { + const auto & rs = event->event.rows; + AdHoc::scprintf<"Delete from %?\n">(std::cout, rs.table_id); + dumpRowData(event->event.rows); + } + + struct write { + template + void + operator()(const T & v) const + { + AdHoc::scprintf<"\t\t%?\n">(std::cout, v); + } + + void + operator()(const uint8_t & v) const + { + AdHoc::scprintf<"\t\t%d\n">(std::cout, v); + } + + void + operator()(const int8_t & v) const + { + AdHoc::scprintf<"\t\t%d\n">(std::cout, v); + } + }; + + void + DumpToConsole::dumpRowData(const st_mariadb_rpl_rows_event & row) const + { + Row r {row, tableMaps.at(row.table_id)->event.table_map}; + std::for_each(r.begin(), r.end(), [](auto && fv) { + std::visit(write {}, fv); + }); + } + + void + DumpToConsole::dumpRowPairData(const st_mariadb_rpl_rows_event & row) const + { + RowPair rp {row, tableMaps.at(row.table_id)->event.table_map}; + std::for_each(rp.first.begin(), rp.first.end(), [](auto && fv) { + std::visit(write {}, fv); + }); + std::for_each(rp.second.begin(), rp.second.end(), [](auto && fv) { + std::visit(write {}, fv); + }); + } + +} diff --git a/lib/output/dumpToConsole.h b/lib/output/dumpToConsole.h new file mode 100644 index 0000000..98769a6 --- /dev/null +++ b/lib/output/dumpToConsole.h @@ -0,0 +1,23 @@ +#ifndef MYGRATE_OUTPUT_DUMPTOCONSOLE_H +#define MYGRATE_OUTPUT_DUMPTOCONSOLE_H + +#include "../eventHandlerBase.h" + +namespace MyGrate::Output { + class DumpToConsole : public MyGrate::EventHandlerBase { + protected: + void tableMap(MyGrate::MariaDB_Event_Ptr event) override; + + void insertRow(MyGrate::MariaDB_Event_Ptr event) override; + + void updateRow(MyGrate::MariaDB_Event_Ptr event) override; + + void deleteRow(MyGrate::MariaDB_Event_Ptr event) override; + + private: + void dumpRowData(const st_mariadb_rpl_rows_event & row) const; + void dumpRowPairData(const st_mariadb_rpl_rows_event & row) const; + }; +} + +#endif -- cgit v1.2.3