summaryrefslogtreecommitdiff
path: root/lib/output
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output')
-rw-r--r--lib/output/pq/sql/insertColumn.sql2
-rw-r--r--lib/output/pq/sql/insertTable.sql3
-rw-r--r--lib/output/pq/sql/selectSourceSchema.sql3
-rw-r--r--lib/output/pq/updateDatabase.cpp31
-rw-r--r--lib/output/pq/updateDatabase.h5
5 files changed, 44 insertions, 0 deletions
diff --git a/lib/output/pq/sql/insertColumn.sql b/lib/output/pq/sql/insertColumn.sql
new file mode 100644
index 0000000..b8efde9
--- /dev/null
+++ b/lib/output/pq/sql/insertColumn.sql
@@ -0,0 +1,2 @@
+INSERT INTO mygrate.table_columns(column_name, mysql_ordinal, table_id)
+VALUES($1, $2, $3)
diff --git a/lib/output/pq/sql/insertTable.sql b/lib/output/pq/sql/insertTable.sql
new file mode 100644
index 0000000..4b559d0
--- /dev/null
+++ b/lib/output/pq/sql/insertTable.sql
@@ -0,0 +1,3 @@
+INSERT INTO mygrate.tables(table_name, source_id)
+VALUES($1, $2)
+RETURNING table_id
diff --git a/lib/output/pq/sql/selectSourceSchema.sql b/lib/output/pq/sql/selectSourceSchema.sql
new file mode 100644
index 0000000..a583c4f
--- /dev/null
+++ b/lib/output/pq/sql/selectSourceSchema.sql
@@ -0,0 +1,3 @@
+SELECT table_schema
+FROM mygrate.source s
+WHERE s.source_id = $1
diff --git a/lib/output/pq/updateDatabase.cpp b/lib/output/pq/updateDatabase.cpp
index 52388be..389d47a 100644
--- a/lib/output/pq/updateDatabase.cpp
+++ b/lib/output/pq/updateDatabase.cpp
@@ -1,19 +1,25 @@
#include "updateDatabase.h"
#include "pqConn.h"
+#include "typeMapper.h"
#include <compileTimeFormatter.h>
#include <cstdint>
#include <dbRecordSet.h>
#include <eventSourceBase.h>
#include <helpers.h>
+#include <input/mysqlConn.h>
#include <input/replStream.h>
+#include <input/sql/selectColumns.h>
#include <input/sql/showMasterStatus.h>
#include <memory>
+#include <output/pq/sql/insertColumn.h>
#include <output/pq/sql/insertSource.h>
+#include <output/pq/sql/insertTable.h>
#include <output/pq/sql/selectColumns.h>
#include <output/pq/sql/selectSource.h>
#include <output/pq/sql/selectSourceSchema.h>
#include <output/pq/sql/selectTables.h>
#include <stdexcept>
+#include <streamSupport.h>
namespace MyGrate::Output::Pq {
UpdateDatabase::UpdateDatabase(const char * const str, uint64_t s) :
@@ -56,4 +62,29 @@ namespace MyGrate::Output::Pq {
});
return UpdateDatabase(pq->connstr.c_str(), source_id);
}
+
+ void
+ UpdateDatabase::addTable(Input::MySQLConn * conn, const char * tableName)
+ {
+ auto cols = input::sql::selectColumns::execute(conn, tableName);
+ verify<std::logic_error>(cols->rows() > 0, "Table has no rows");
+ Tx {this}([&] {
+ const auto table_id = **output::pq::sql::insertTable::execute(this, tableName, source);
+ std::stringstream ct;
+ scprintf<"CREATE TABLE %?.%?(">(ct, schema, tableName);
+ TypeMapper tm;
+ for (auto col : *cols) {
+ output::pq::sql::insertColumn::execute(this, col[0], col.currentRow(), table_id);
+ if (col.currentRow())
+ ct << ',';
+ scprintf<"%? %?">(ct, col[0], tm.map(col[2], scprintf<"%?.%?">(tableName, col[0])));
+ if (!col[1])
+ ct << " not null";
+ if (col[3])
+ ct << " primary key";
+ }
+ ct << ")";
+ this->query(ct.str().c_str());
+ });
+ }
}
diff --git a/lib/output/pq/updateDatabase.h b/lib/output/pq/updateDatabase.h
index 3d3e80d..dd7aaa0 100644
--- a/lib/output/pq/updateDatabase.h
+++ b/lib/output/pq/updateDatabase.h
@@ -6,6 +6,9 @@
#include <eventHandlerBase.h>
#include <eventSourceBase.h>
+namespace MyGrate::Input {
+ class MySQLConn;
+}
namespace MyGrate::Output::Pq {
struct ColumnDef {
std::string name;
@@ -35,6 +38,8 @@ namespace MyGrate::Output::Pq {
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);
+
const uint64_t source;
const std::string schema;