From 7804cdf8ecbc2bb40231199669444665df597eb6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 4 Jul 2021 19:32:42 +0100 Subject: Support copying exist table data --- lib/output/pq/updateDatabase.cpp | 116 +++++++++++++++++++++++++++++++++++++++ lib/output/pq/updateDatabase.h | 1 + test/sql/fillTestTable.sql | 2 + test/test-e2e.cpp | 5 +- 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 test/sql/fillTestTable.sql diff --git a/lib/output/pq/updateDatabase.cpp b/lib/output/pq/updateDatabase.cpp index 3829da9..457f744 100644 --- a/lib/output/pq/updateDatabase.cpp +++ b/lib/output/pq/updateDatabase.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -92,4 +93,119 @@ namespace MyGrate::Output::Pq { }); tables.emplace(tableName, std::move(tableDef)); } + + struct WritePqCopyStream { + ~WritePqCopyStream() + { + fputc('\n', out); + } + + void + nextField() + { + fputc('\t', out); + } + + void operator()(std::nullptr_t) const + { + fputs("\\N", out); + } +#define BASIC_PRINT(T, fmt) \ + void operator()(T v) const \ + { \ + fprintf(out, fmt, v); \ + } + BASIC_PRINT(double, "%f") + BASIC_PRINT(float, "%f") + BASIC_PRINT(int8_t, "%hhd") + BASIC_PRINT(uint8_t, "%hhu") + BASIC_PRINT(int16_t, "%hd") + BASIC_PRINT(uint16_t, "%hu") + BASIC_PRINT(int32_t, "%d") + BASIC_PRINT(uint32_t, "%u") + BASIC_PRINT(int64_t, "%ld") + BASIC_PRINT(uint64_t, "%lu") +#undef BASIC_PRINT + void operator()(timespec) const + { + throw std::logic_error("timespec not implemented"); + } + void + operator()(Date v) const + { + fprintf(out, "%d-%d-%d", v.year, v.month, v.day); + } + void + operator()(Time v) const + { + fprintf(out, "%d:%d:%d", v.hour, v.minute, v.second); + } + void + operator()(DateTime v) const + { + operator()(static_cast(v)); + fputc('T', out); + operator()(static_cast