summaryrefslogtreecommitdiff
path: root/lib/output/pq
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output/pq')
-rw-r--r--lib/output/pq/pqStmt.cpp2
-rw-r--r--lib/output/pq/sql/selectColumns.sql6
-rw-r--r--lib/output/pq/sql/selectSource.sql3
-rw-r--r--lib/output/pq/sql/selectTables.sql6
-rw-r--r--lib/output/pq/sql/updateSourcePosition.sql4
5 files changed, 20 insertions, 1 deletions
diff --git a/lib/output/pq/pqStmt.cpp b/lib/output/pq/pqStmt.cpp
index 04b48c6..078c239 100644
--- a/lib/output/pq/pqStmt.cpp
+++ b/lib/output/pq/pqStmt.cpp
@@ -46,7 +46,7 @@ namespace MyGrate::Output::Pq {
if (const auto i = c->stmts.find(q); i != c->stmts.end()) {
return i->second;
}
- auto nam {AdHoc::scprintf<"pst%0x">(c->stmts.size())};
+ auto nam {scprintf<"pst%0x">(c->stmts.size())};
ResPtr res {PQprepare(c->conn.get(), nam.c_str(), q, (int)n, nullptr), PQclear};
verify<std::runtime_error>(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q);
return c->stmts.emplace(q, std::move(nam)).first->second;
diff --git a/lib/output/pq/sql/selectColumns.sql b/lib/output/pq/sql/selectColumns.sql
new file mode 100644
index 0000000..e9cd107
--- /dev/null
+++ b/lib/output/pq/sql/selectColumns.sql
@@ -0,0 +1,6 @@
+SELECT t.table_name, STRING_AGG(c.column_name, '|' ORDER BY c.ordinal_position)
+FROM mygrate.source s
+ JOIN information_schema.tables t USING(table_schema)
+ LEFT OUTER JOIN information_schema.columns c USING(table_schema, table_name)
+WHERE s.id = $1
+GROUP BY t.table_name
diff --git a/lib/output/pq/sql/selectSource.sql b/lib/output/pq/sql/selectSource.sql
new file mode 100644
index 0000000..3048410
--- /dev/null
+++ b/lib/output/pq/sql/selectSource.sql
@@ -0,0 +1,3 @@
+SELECT host, username, password, port, filename, position, serverid, table_schema
+FROM mygrate.source s
+WHERE s.id = $1
diff --git a/lib/output/pq/sql/selectTables.sql b/lib/output/pq/sql/selectTables.sql
new file mode 100644
index 0000000..389dfe2
--- /dev/null
+++ b/lib/output/pq/sql/selectTables.sql
@@ -0,0 +1,6 @@
+SELECT t.table_name, STRING_AGG(k.column_name, '|' ORDER BY k.ordinal_position) pk_cols
+FROM mygrate.source s
+ JOIN information_schema.tables t USING(table_schema)
+ LEFT OUTER JOIN information_schema.key_column_usage k USING(table_schema, table_name)
+WHERE s.id = $1
+GROUP BY t.table_name
diff --git a/lib/output/pq/sql/updateSourcePosition.sql b/lib/output/pq/sql/updateSourcePosition.sql
new file mode 100644
index 0000000..3d3d665
--- /dev/null
+++ b/lib/output/pq/sql/updateSourcePosition.sql
@@ -0,0 +1,4 @@
+UPDATE mygrate.source SET
+ filename = $1,
+ position = $2
+WHERE id = $3