From 046cdae1a14a686238ab91b1f883335b2de5a78c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 1 Jun 2021 00:17:38 +0100 Subject: Generate DbStmt templates from .sql files m4 generator and related code. Reshuffles some CTF stuff to avoid pulling in all of CTF and iostream for its fixed_string. Moves CTF out of AdHoc namespace. Add some initial SQL statements. --- lib/output/dumpToConsole.cpp | 14 +++++++------- lib/output/pq/pqStmt.cpp | 2 +- lib/output/pq/sql/selectColumns.sql | 6 ++++++ lib/output/pq/sql/selectSource.sql | 3 +++ lib/output/pq/sql/selectTables.sql | 6 ++++++ lib/output/pq/sql/updateSourcePosition.sql | 4 ++++ 6 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 lib/output/pq/sql/selectColumns.sql create mode 100644 lib/output/pq/sql/selectSource.sql create mode 100644 lib/output/pq/sql/selectTables.sql create mode 100644 lib/output/pq/sql/updateSourcePosition.sql (limited to 'lib/output') diff --git a/lib/output/dumpToConsole.cpp b/lib/output/dumpToConsole.cpp index 81d9edc..1085bb5 100644 --- a/lib/output/dumpToConsole.cpp +++ b/lib/output/dumpToConsole.cpp @@ -17,7 +17,7 @@ namespace MyGrate::Output { 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); + scprintf<"Table map %?.%? -> %?\n">(std::cout, tm.database, tm.table, tm.table_id); EventHandlerBase::tableMap(std::move(event)); } @@ -25,7 +25,7 @@ namespace MyGrate::Output { DumpToConsole::insertRow(MyGrate::MariaDB_Event_Ptr event) { const auto & rs = event->event.rows; - AdHoc::scprintf<"Insert into %?\n">(std::cout, rs.table_id); + scprintf<"Insert into %?\n">(std::cout, rs.table_id); dumpRowData(event->event.rows); } @@ -33,7 +33,7 @@ namespace MyGrate::Output { DumpToConsole::updateRow(MyGrate::MariaDB_Event_Ptr event) { const auto & rs = event->event.rows; - AdHoc::scprintf<"Update %?\n">(std::cout, rs.table_id); + scprintf<"Update %?\n">(std::cout, rs.table_id); dumpRowPairData(event->event.rows); } @@ -41,7 +41,7 @@ namespace MyGrate::Output { DumpToConsole::deleteRow(MyGrate::MariaDB_Event_Ptr event) { const auto & rs = event->event.rows; - AdHoc::scprintf<"Delete from %?\n">(std::cout, rs.table_id); + scprintf<"Delete from %?\n">(std::cout, rs.table_id); dumpRowData(event->event.rows); } @@ -50,19 +50,19 @@ namespace MyGrate::Output { void operator()(const T & v) const { - AdHoc::scprintf<"\t\t%?\n">(std::cout, v); + scprintf<"\t\t%?\n">(std::cout, v); } void operator()(const uint8_t & v) const { - AdHoc::scprintf<"\t\t%d\n">(std::cout, v); + scprintf<"\t\t%d\n">(std::cout, v); } void operator()(const int8_t & v) const { - AdHoc::scprintf<"\t\t%d\n">(std::cout, v); + scprintf<"\t\t%d\n">(std::cout, v); } }; diff --git a/lib/output/pq/pqStmt.cpp b/lib/output/pq/pqStmt.cpp index 04b48c6..078c239 100644 --- a/lib/output/pq/pqStmt.cpp +++ b/lib/output/pq/pqStmt.cpp @@ -46,7 +46,7 @@ namespace MyGrate::Output::Pq { if (const auto i = c->stmts.find(q); i != c->stmts.end()) { return i->second; } - auto nam {AdHoc::scprintf<"pst%0x">(c->stmts.size())}; + auto nam {scprintf<"pst%0x">(c->stmts.size())}; ResPtr res {PQprepare(c->conn.get(), nam.c_str(), q, (int)n, nullptr), PQclear}; verify(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q); return c->stmts.emplace(q, std::move(nam)).first->second; diff --git a/lib/output/pq/sql/selectColumns.sql b/lib/output/pq/sql/selectColumns.sql new file mode 100644 index 0000000..e9cd107 --- /dev/null +++ b/lib/output/pq/sql/selectColumns.sql @@ -0,0 +1,6 @@ +SELECT t.table_name, STRING_AGG(c.column_name, '|' ORDER BY c.ordinal_position) +FROM mygrate.source s + JOIN information_schema.tables t USING(table_schema) + LEFT OUTER JOIN information_schema.columns c USING(table_schema, table_name) +WHERE s.id = $1 +GROUP BY t.table_name diff --git a/lib/output/pq/sql/selectSource.sql b/lib/output/pq/sql/selectSource.sql new file mode 100644 index 0000000..3048410 --- /dev/null +++ b/lib/output/pq/sql/selectSource.sql @@ -0,0 +1,3 @@ +SELECT host, username, password, port, filename, position, serverid, table_schema +FROM mygrate.source s +WHERE s.id = $1 diff --git a/lib/output/pq/sql/selectTables.sql b/lib/output/pq/sql/selectTables.sql new file mode 100644 index 0000000..389dfe2 --- /dev/null +++ b/lib/output/pq/sql/selectTables.sql @@ -0,0 +1,6 @@ +SELECT t.table_name, STRING_AGG(k.column_name, '|' ORDER BY k.ordinal_position) pk_cols +FROM mygrate.source s + JOIN information_schema.tables t USING(table_schema) + LEFT OUTER JOIN information_schema.key_column_usage k USING(table_schema, table_name) +WHERE s.id = $1 +GROUP BY t.table_name diff --git a/lib/output/pq/sql/updateSourcePosition.sql b/lib/output/pq/sql/updateSourcePosition.sql new file mode 100644 index 0000000..3d3d665 --- /dev/null +++ b/lib/output/pq/sql/updateSourcePosition.sql @@ -0,0 +1,4 @@ +UPDATE mygrate.source SET + filename = $1, + position = $2 +WHERE id = $3 -- cgit v1.2.3