From 8c3521e21a6e7d16eb142533dc7523933f00db99 Mon Sep 17 00:00:00 2001 From: Dennis Felsing Date: Mon, 6 Nov 2023 15:53:29 +0000 Subject: [PATCH] Support other databases --- grammar.cc | 4 ---- postgres.cc | 30 +++++++++++++++++++----------- relmodel.hh | 8 ++++---- sqlite.cc | 4 ++-- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/grammar.cc b/grammar.cc index 4d26dac..19c213b 100644 --- a/grammar.cc +++ b/grammar.cc @@ -274,8 +274,6 @@ struct for_update_verify : prod_visitor { table_or_query_name* tab = dynamic_cast(p); if (tab) { table *actual_table = dynamic_cast(tab->t); - if (actual_table && !actual_table->is_insertable) - throw("read only"); if (actual_table->name.find("pg_")) throw("catalog"); } @@ -283,8 +281,6 @@ struct for_update_verify : prod_visitor { //table_sample* sample = dynamic_cast(p); //if (sample) { // table *actual_table = dynamic_cast(sample->t); - // if (actual_table && !actual_table->is_insertable) - // throw("read only"); // if (actual_table->name.find("pg_")) // throw("catalog"); //} diff --git a/postgres.cc b/postgres.cc index c07a423..ba66c50 100644 --- a/postgres.cc +++ b/postgres.cc @@ -192,22 +192,30 @@ schema_pqxx::schema_pqxx(std::string &conninfo, bool no_catalog, bool dump_state if (read_state && !dump_state) { for (const auto &obj : data["tables"]) { string schema = obj["schema"].get(); + string db = obj["db"].get(); if (no_catalog && ((schema == "pg_catalog") || (schema == "mz_catalog") || (schema == "mz_internal") || (schema == "information_schema"))) continue; tables.push_back(table(obj["name"].get(), schema, - ((obj["insertable"].get() == "YES") ? true : false), + db, ((obj["table_type"].get() == "BASE TABLE") ? true : false))); } } else { - r = w.exec("select table_name, " - "table_schema, " - "true, " //"is_insertable_into, " # column "is_insertable_into" does not exist - "table_type " - "from information_schema.tables " - "where table_name not like 'mz_dataflow_operator_reachability%' " // https://github.com/MaterializeInc/materialize/issues/18296 - "and table_name not like '%_raw' " // Can be huge, easy to go OoM + r = w.exec("SELECT " + " r.name AS table_name, " + " s.name AS table_schema, " + " COALESCE(d.name, '') as table_catalog, " + " CASE r.type " + " WHEN 'materialized-view' THEN 'MATERIALIZED VIEW' " + " WHEN 'table' THEN 'BASE TABLE' " + " ELSE pg_catalog.upper(r.type) " + " END AS table_type " + "FROM mz_catalog.mz_relations r " + "JOIN mz_catalog.mz_schemas s ON s.id = r.schema_id " + "LEFT JOIN mz_catalog.mz_databases d ON d.id = s.database_id " + "where r.name not like 'mz_dataflow_operator_reachability%' " // https://github.com/MaterializeInc/materialize/issues/18296 + "and r.name not like '%_raw' " // Can be huge, easy to go OoM ); if (dump_state) { data["tables"] = json::array(); @@ -215,7 +223,7 @@ schema_pqxx::schema_pqxx(std::string &conninfo, bool no_catalog, bool dump_state for (auto row = r.begin(); row != r.end(); ++row) { string name(row[0].as()); string schema(row[1].as()); - string insertable(row[2].as()); + string db(row[2].as()); string table_type(row[3].as()); if (no_catalog && ((schema == "pg_catalog") || (schema == "mz_catalog") || (schema == "mz_internal") || (schema == "information_schema"))) @@ -223,14 +231,14 @@ schema_pqxx::schema_pqxx(std::string &conninfo, bool no_catalog, bool dump_state tables.push_back(table(name, schema, - ((insertable == "YES") ? true : false), + db, ((table_type == "BASE TABLE") ? true : false))); if (dump_state) { json obj; obj["name"] = name, obj["schema"] = schema; - obj["insertable"] = insertable; + obj["db"] = db; obj["table_type"] = table_type; data["tables"].push_back(obj); } diff --git a/relmodel.hh b/relmodel.hh index 41fb88f..d58c35d 100644 --- a/relmodel.hh +++ b/relmodel.hh @@ -63,16 +63,16 @@ struct aliased_relation : named_relation { }; struct table : named_relation { + string db; string schema; - bool is_insertable; bool is_base_table; vector constraints; - table(string name, string schema, bool insertable, bool base_table) + table(string name, string schema, string db, bool base_table) : named_relation(name), schema(schema), - is_insertable(insertable), + db(db), is_base_table(base_table) { } - virtual string ident() { return schema + "." + name; } + virtual string ident() { return db.empty() ? ("\"" + schema + "\".\"" + name + "\"") : ("\"" + db + "\".\"" + schema + "\".\"" + name + "\""); } virtual ~table() { }; }; diff --git a/sqlite.cc b/sqlite.cc index acab950..20e9da9 100644 --- a/sqlite.cc +++ b/sqlite.cc @@ -45,7 +45,7 @@ extern "C" int table_callback(void *arg, int argc, char **argv, char **azColName (void) argc; (void) azColName; auto tables = (vector *)arg; bool view = (string("view") == argv[0]); - table tab(argv[2], "main", !view, !view); + table tab(argv[2], "main", "sqlite", !view); tables->push_back(tab); return 0; } @@ -109,7 +109,7 @@ schema_sqlite::schema_sqlite(std::string &conninfo, bool no_catalog) if (!no_catalog) { // sqlite_master doesn't list itself, do it manually - table tab("sqlite_master", "main", false, false); + table tab("sqlite_master", "main", "sqlite", false); tables.push_back(tab); }