Skip to content

Commit

Permalink
Support other databases
Browse files Browse the repository at this point in the history
  • Loading branch information
def- committed Nov 6, 2023
1 parent b9b6b8a commit 8c3521e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
4 changes: 0 additions & 4 deletions grammar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,13 @@ struct for_update_verify : prod_visitor {
table_or_query_name* tab = dynamic_cast<table_or_query_name*>(p);
if (tab) {
table *actual_table = dynamic_cast<table*>(tab->t);
if (actual_table && !actual_table->is_insertable)
throw("read only");
if (actual_table->name.find("pg_"))
throw("catalog");
}
// Syntax: ERROR: Expected joined table, found dot
//table_sample* sample = dynamic_cast<table_sample*>(p);
//if (sample) {
// table *actual_table = dynamic_cast<table*>(sample->t);
// if (actual_table && !actual_table->is_insertable)
// throw("read only");
// if (actual_table->name.find("pg_"))
// throw("catalog");
//}
Expand Down
30 changes: 19 additions & 11 deletions postgres.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,45 +192,53 @@ 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>();
string db = obj["db"].get<string>();
if (no_catalog && ((schema == "pg_catalog") || (schema == "mz_catalog") || (schema == "mz_internal") || (schema == "information_schema")))
continue;

tables.push_back(table(obj["name"].get<string>(),
schema,
((obj["insertable"].get<string>() == "YES") ? true : false),
db,
((obj["table_type"].get<string>() == "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();
}
for (auto row = r.begin(); row != r.end(); ++row) {
string name(row[0].as<string>());
string schema(row[1].as<string>());
string insertable(row[2].as<string>());
string db(row[2].as<string>());
string table_type(row[3].as<string>());

if (no_catalog && ((schema == "pg_catalog") || (schema == "mz_catalog") || (schema == "mz_internal") || (schema == "information_schema")))
continue;

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);
}
Expand Down
8 changes: 4 additions & 4 deletions relmodel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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() { };
};

Expand Down
4 changes: 2 additions & 2 deletions sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern "C" int table_callback(void *arg, int argc, char **argv, char **azColName
(void) argc; (void) azColName;
auto tables = (vector<table> *)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;
}
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 8c3521e

Please sign in to comment.