diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-06-06 00:59:03 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-06-06 00:59:03 +0100 |
commit | 51ac5d2007f1caf6bc6e65b485ba896357696654 (patch) | |
tree | db0566c8ca0c1ba1131e3c22d6cb886c6eefa2fb | |
parent | Add conversion operators to get common types from DbValues (diff) | |
download | mygrate-51ac5d2007f1caf6bc6e65b485ba896357696654.tar.bz2 mygrate-51ac5d2007f1caf6bc6e65b485ba896357696654.tar.xz mygrate-51ac5d2007f1caf6bc6e65b485ba896357696654.zip |
Add RecordSet RowView
This represents a view into a recordset by row number and provides easy
access to values in a column by index and the ability to create a new
object instance by N columns which are passed to the object's
constructor.
-rw-r--r-- | lib/dbRecordSet.cpp | 17 | ||||
-rw-r--r-- | lib/dbRecordSet.h | 26 | ||||
-rw-r--r-- | lib/input/replStream.cpp | 4 | ||||
-rw-r--r-- | lib/input/replStream.h | 2 | ||||
-rw-r--r-- | lib/output/pq/updateDatabase.cpp | 3 |
5 files changed, 48 insertions, 4 deletions
diff --git a/lib/dbRecordSet.cpp b/lib/dbRecordSet.cpp new file mode 100644 index 0000000..66ade0e --- /dev/null +++ b/lib/dbRecordSet.cpp @@ -0,0 +1,17 @@ +#include "dbRecordSet.h" + +namespace MyGrate { + RowView::RowView(const RecordSet * rs, std::size_t rw) : recordSet {rs}, row {rw} { } + + DbValue + RowView::operator[](std::size_t col) const + { + return recordSet->at(row, col); + } + + RowView + RecordSet::operator[](std::size_t row) const + { + return {this, row}; + } +} diff --git a/lib/dbRecordSet.h b/lib/dbRecordSet.h index 9bddc01..5bd6967 100644 --- a/lib/dbRecordSet.h +++ b/lib/dbRecordSet.h @@ -5,6 +5,31 @@ #include <memory> namespace MyGrate { + class RecordSet; + + class RowView { + public: + RowView(const RecordSet * rs, std::size_t rw = 0); + + DbValue operator[](std::size_t col) const; + + template<typename S, std::size_t N, typename Indices = std::make_index_sequence<N>> + auto + create() const + { + return create<S>(Indices {}); + } + + template<typename S, std::size_t... I> auto create(std::index_sequence<I...>) const + { + return std::make_unique<S>((*this)[I]...); + } + + private: + const RecordSet * recordSet; + std::size_t row; + }; + class RecordSet { public: virtual ~RecordSet() = default; @@ -12,6 +37,7 @@ namespace MyGrate { virtual std::size_t rows() const = 0; virtual std::size_t columns() const = 0; virtual DbValue at(std::size_t, std::size_t) const = 0; + RowView operator[](std::size_t row) const; }; using RecordSetPtr = std::unique_ptr<RecordSet>; } diff --git a/lib/input/replStream.cpp b/lib/input/replStream.cpp index c3dea9a..1ec696a 100644 --- a/lib/input/replStream.cpp +++ b/lib/input/replStream.cpp @@ -9,9 +9,9 @@ #include <utility> namespace MyGrate::Input { - ReplicationStream::ReplicationStream(const char * const host, const char * const user, const char * const pass, + ReplicationStream::ReplicationStream(const std::string & host, const std::string & user, const std::string & pass, unsigned short port, uint64_t sid, std::string fn, uint64_t pos) : - MySQLConn {host, user, pass, port}, + MySQLConn {host.c_str(), user.c_str(), pass.c_str(), port}, serverid {sid}, filename {std::move(fn)}, position {pos} { } diff --git a/lib/input/replStream.h b/lib/input/replStream.h index 01c9ef3..bc47267 100644 --- a/lib/input/replStream.h +++ b/lib/input/replStream.h @@ -13,7 +13,7 @@ namespace MyGrate { namespace MyGrate::Input { class ReplicationStream : public EventSourceBase, MySQLConn { public: - ReplicationStream(const char * const host, const char * const user, const char * const pass, + ReplicationStream(const std::string & host, const std::string & user, const std::string & pass, unsigned short port, uint64_t serverid, std::string filename, uint64_t position); void readEvents(EventHandlerBase &) override; diff --git a/lib/output/pq/updateDatabase.cpp b/lib/output/pq/updateDatabase.cpp index ebcabb3..352517e 100644 --- a/lib/output/pq/updateDatabase.cpp +++ b/lib/output/pq/updateDatabase.cpp @@ -4,6 +4,7 @@ #include <dbRecordSet.h> #include <eventSourceBase.h> #include <helpers.h> +#include <input/replStream.h> #include <memory> #include <output/pq/sql/selectSource.h> #include <stdexcept> @@ -16,6 +17,6 @@ namespace MyGrate::Output::Pq { { auto srcrec = output::pq::sql::selectSource::execute(this, source); verify<std::runtime_error>(srcrec->rows() == 1, "Wrong number of source config rows"); - return {}; + return (*srcrec)[0].create<Input::ReplicationStream, 7>(); } } |