From 206d33d0962128b10d1630b4e1f5511d952172b3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 24 Jul 2021 14:07:02 +0100 Subject: Fix up insert/delete SQL generation --- lib/output/pq/updateDatabase.cpp | 12 ++++++------ test/test-e2e.cpp | 8 +++++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/output/pq/updateDatabase.cpp b/lib/output/pq/updateDatabase.cpp index 22ae346..4830fd5 100644 --- a/lib/output/pq/updateDatabase.cpp +++ b/lib/output/pq/updateDatabase.cpp @@ -300,8 +300,7 @@ namespace MyGrate::Output::Pq { scprintf<"DELETE FROM %?.%? ">(ou, schema, selected->first); for (const auto & col : out->columns) { if (col->is_pk) { - scprintf<"%? %? = $%?">( - ou, kordinal == out->columns.size() ? " WHERE " : " AND ", col->name, kordinal + 1); + scprintf<"%? %? = $%?">(ou, !kordinal ? " WHERE " : " AND ", col->name, kordinal + 1); kordinal++; } } @@ -329,19 +328,20 @@ namespace MyGrate::Output::Pq { e->event.rows.column_count == out->columns.size(), "Incorrect number of columns in row data"); if (!out->insertInto) { std::stringstream ou; - std::size_t ordinal {0}, kordinal {out->columns.size()}; + std::size_t ordinal {0}, vordinal {0}; scprintf<"INSERT INTO %?.%? ">(ou, schema, selected->first); for (const auto & col : out->columns) { - scprintf<"%? %?">(ou, !ordinal ? "(" : ", ", col->name); + scprintf<"%? %?">(ou, !ordinal++ ? "(" : ", ", col->name); } ou << ") VALUES"; for (const auto & col : out->columns) { - scprintf<"%? $%?">(ou, !ordinal ? "(" : ", ", col->name); + scprintf<"%? $%?">(ou, !vordinal++ ? "(" : ", ", vordinal); + (void)col; } ou << ")"; - out->insertInto = prepare(ou.str().c_str(), kordinal); + out->insertInto = prepare(ou.str().c_str(), out->columns.size()); } std::vector updateValues; updateValues.reserve(out->columns.size()); diff --git a/test/test-e2e.cpp b/test/test-e2e.cpp index 60dca86..e541ec9 100644 --- a/test/test-e2e.cpp +++ b/test/test-e2e.cpp @@ -38,7 +38,13 @@ BOOST_AUTO_TEST_CASE(e2e) std::thread repl {&MyGrate::EventSourceBase::readEvents, src.get(), std::ref(out)}; auto upd = mym.prepare("UPDATE session SET session_id = ? WHERE id = ?", 2); - upd->execute(std::array {"food", 1}); + upd->execute({"food", 1}); + auto del = mym.prepare("DELETE FROM session WHERE id = ?", 2); + del->execute({1}); + auto ins = mym.prepare("INSERT INTO session(session_id, username, user_lvl, ip_addr, port, created, modified) \ + VALUES(?, ?, ?, ?, ?, now(), now())", + 5); + ins->execute({"hashyhash", "testuser", "groupadm", "10.10.0.1", 2433}); sleep(1); -- cgit v1.2.3