summaryrefslogtreecommitdiff
path: root/lib/output/pq/updateDatabase.h
blob: 79df9a9e31c7b473c44b8991b2c87fc312a0de88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef MYGRATE_OUTPUT_PQ_UPDATEDATABASE_H
#define MYGRATE_OUTPUT_PQ_UPDATEDATABASE_H

#include "pqConn.h"
#include <cstdint>
#include <eventHandlerBase.h>
#include <eventSourceBase.h>

namespace MyGrate::Input {
	class MySQLConn;
}
namespace MyGrate::Output::Pq {
	struct ColumnDef {
		ColumnDef(std::string, std::size_t, bool);

		std::string name;
		std::size_t ordinal;
		bool is_pk;
	};
	using ColumnDefPtr = std::unique_ptr<ColumnDef>;

	class TableOutput {
	public:
		TableOutput() = default;
		TableOutput(const RecordSet &, std::string_view name);

		std::vector<ColumnDefPtr> columns;
	};
	using TableDefPtr = std::unique_ptr<TableOutput>;

	class ConfigError : public std::runtime_error {
		using std::runtime_error::runtime_error;
	};

	class UpdateDatabase : public PqConn, public EventHandlerBase {
	public:
		UpdateDatabase(const char * const str, uint64_t source);

		EventSourceBasePtr getSource();

		static UpdateDatabase createNew(PqConn *, const char * host, const char * un, const char * pw, unsigned short p,
				const char * db, int sid, const char * sc);

		void addTable(Input::MySQLConn *, const char * tableName);
		void copyTableContent(Input::MySQLConn *, const char * tableName);

		const uint64_t source;
		const std::string schema;
		const std::string database;

	private:
		UpdateDatabase(PqConn &&, uint64_t source);
		UpdateDatabase(PqConn &&, uint64_t source, RecordSetPtr cfg);
		std::map<std::string, TableDefPtr, std::less<>> tables;
	};
}

#endif