diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-08-08 12:48:37 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-08-08 16:15:07 +0100 |
commit | 5378ffd0aca410c50887fee57899776e62bfb766 (patch) | |
tree | 699e11a5b6cfcf7f6d2c29083ec84c7ed3fe36c5 | |
parent | Create indexes and keys after copying table data (diff) | |
download | mygrate-5378ffd0aca410c50887fee57899776e62bfb766.tar.bz2 mygrate-5378ffd0aca410c50887fee57899776e62bfb766.tar.xz mygrate-5378ffd0aca410c50887fee57899776e62bfb766.zip |
Extract WritePqCopyStrm into its own file
-rw-r--r-- | lib/dbTypes.h | 32 | ||||
-rw-r--r-- | lib/output/pq/updateDatabase.cpp | 97 | ||||
-rw-r--r-- | lib/output/pq/writePqCopyStrm.cpp | 82 | ||||
-rw-r--r-- | lib/output/pq/writePqCopyStrm.h | 38 |
4 files changed, 155 insertions, 94 deletions
diff --git a/lib/dbTypes.h b/lib/dbTypes.h index 0b7185c..42b242b 100644 --- a/lib/dbTypes.h +++ b/lib/dbTypes.h @@ -11,6 +11,38 @@ struct timespec; namespace MyGrate { + template<typename T> struct printer; + template<> struct printer<double> { + constexpr static const char * const fmt {"%f"}; + }; + template<> struct printer<float> { + constexpr static const char * const fmt {"%f"}; + }; + template<> struct printer<int8_t> { + constexpr static const char * const fmt {"%hhd"}; + }; + template<> struct printer<uint8_t> { + constexpr static const char * const fmt {"%hhu"}; + }; + template<> struct printer<int16_t> { + constexpr static const char * const fmt {"%hd"}; + }; + template<> struct printer<uint16_t> { + constexpr static const char * const fmt {"%hu"}; + }; + template<> struct printer<int32_t> { + constexpr static const char * const fmt {"%d"}; + }; + template<> struct printer<uint32_t> { + constexpr static const char * const fmt {"%u"}; + }; + template<> struct printer<int64_t> { + constexpr static const char * const fmt {"%ld"}; + }; + template<> struct printer<uint64_t> { + constexpr static const char * const fmt {"%lu"}; + }; + struct Date { inline Date() { } inline Date(uint16_t y, uint8_t m, uint8_t d) : year {y}, month {m}, day {d} { } diff --git a/lib/output/pq/updateDatabase.cpp b/lib/output/pq/updateDatabase.cpp index 46641fc..485c914 100644 --- a/lib/output/pq/updateDatabase.cpp +++ b/lib/output/pq/updateDatabase.cpp @@ -1,6 +1,7 @@ #include "updateDatabase.h" #include "pqConn.h" #include "typeMapper.h" +#include "writePqCopyStrm.h" #include <compileTimeFormatter.h> #include <cstdint> #include <dbRecordSet.h> @@ -129,99 +130,6 @@ namespace MyGrate::Output::Pq { }); } - struct WritePqCopyStream { - explicit WritePqCopyStream(FILE * o) : out {o} { } - WritePqCopyStream(const WritePqCopyStream &) = delete; - WritePqCopyStream(WritePqCopyStream &&) = delete; - WritePqCopyStream & operator=(const WritePqCopyStream &) = delete; - WritePqCopyStream & operator=(WritePqCopyStream &&) = delete; - - ~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<Date &>(v)); - fputc('T', out); - operator()(static_cast<Time &>(v)); - } - void - operator()(std::string_view v) const - { - auto pos {v.begin()}; - while (pos != v.end()) { - auto esc = std::find_if(pos, v.end(), [](unsigned char c) { - return std::iscntrl(c); - }); - if (esc != pos) { - fwrite(pos, esc - pos, 1, out); - pos = esc; - } - while (pos != v.end()) { - fprintf(out, "\\%03o", *pos); - pos++; - } - } - } - void operator()(BitSet) const - { - throw std::logic_error("bitset not implemented"); - } - void - operator()(Blob v) const - { - fputs("\\\\x", out); - std::for_each(v.begin(), v.end(), [this](auto b) { - fprintf(out, "%02hhx", (uint8_t)b); - }); - } - - FILE * out; - }; - void UpdateDatabase::copyTableContent(Input::MySQLConn * conn, const char * table, const TableDefPtr & tableDef) { @@ -240,14 +148,15 @@ namespace MyGrate::Output::Pq { auto sourceCursor {stmt->cursor()}; const auto cols = sourceCursor->columns(); + WritePqCopyStream cs {out}; while (sourceCursor->fetch()) { - WritePqCopyStream cs {out}; for (auto ordinal {0U}; ordinal < cols; ordinal += 1) { if (ordinal) { cs.nextField(); } sourceCursor->at(ordinal).visit(cs); } + cs.nextRecord(); } fclose(out); diff --git a/lib/output/pq/writePqCopyStrm.cpp b/lib/output/pq/writePqCopyStrm.cpp new file mode 100644 index 0000000..dd402e7 --- /dev/null +++ b/lib/output/pq/writePqCopyStrm.cpp @@ -0,0 +1,82 @@ +#include "writePqCopyStrm.h" +#include <cstdint> + +namespace MyGrate::Output::Pq { + + WritePqCopyStream::WritePqCopyStream(FILE * o) : out {o} { } + + void + WritePqCopyStream::nextRecord() + { + fputc('\n', out); + } + + void + WritePqCopyStream::nextField() + { + fputc('\t', out); + } + + void WritePqCopyStream::operator()(std::nullptr_t) const + { + fputs("\\N", out); + } + + void WritePqCopyStream::operator()(timespec) const + { + throw std::logic_error("timespec not implemented"); + } + + void + WritePqCopyStream::operator()(Date v) const + { + fprintf(out, "%d-%d-%d", v.year, v.month, v.day); + } + + void + WritePqCopyStream::operator()(Time v) const + { + fprintf(out, "%d:%d:%d", v.hour, v.minute, v.second); + } + + void + WritePqCopyStream::operator()(DateTime v) const + { + operator()(static_cast<Date &>(v)); + fputc('T', out); + operator()(static_cast<Time &>(v)); + } + + void + WritePqCopyStream::operator()(std::string_view v) const + { + auto pos {v.begin()}; + while (pos != v.end()) { + auto esc = std::find_if(pos, v.end(), [](unsigned char c) { + return std::iscntrl(c); + }); + if (esc != pos) { + fwrite(pos, esc - pos, 1, out); + pos = esc; + } + while (pos != v.end()) { + fprintf(out, "\\%03o", *pos); + pos++; + } + } + } + + void WritePqCopyStream::operator()(BitSet) const + { + throw std::logic_error("bitset not implemented"); + } + + void + WritePqCopyStream::operator()(Blob v) const + { + fputs("\\\\x", out); + std::for_each(v.begin(), v.end(), [this](auto b) { + fprintf(out, "%02hhx", (uint8_t)b); + }); + } +} diff --git a/lib/output/pq/writePqCopyStrm.h b/lib/output/pq/writePqCopyStrm.h new file mode 100644 index 0000000..e8a4fd3 --- /dev/null +++ b/lib/output/pq/writePqCopyStrm.h @@ -0,0 +1,38 @@ +#ifndef MYGRATE_OUTPUT_PQ_WRITEPQSTRM_H +#define MYGRATE_OUTPUT_PQ_WRITEPQSTRM_H + +#include <concepts> +#include <cstdio> +#include <dbTypes.h> + +namespace MyGrate::Output::Pq { + struct WritePqCopyStream { + explicit WritePqCopyStream(FILE * o); + + void nextRecord(); + void nextField(); + + void operator()(std::nullptr_t) const; + void + operator()(std::integral auto v) const + { + fprintf(out, printer<decltype(v)>::fmt, v); + } + void + operator()(std::floating_point auto v) const + { + fprintf(out, printer<decltype(v)>::fmt, v); + } + void operator()(timespec) const; + void operator()(Date v) const; + void operator()(Time v) const; + void operator()(DateTime v) const; + void operator()(std::string_view v) const; + void operator()(BitSet) const; + void operator()(Blob v) const; + + FILE * out; + }; +} + +#endif |