Refactor the way we store a database schema

This commit does a lot of refactoring. But most noticeably it changes
two things:

1) Instead of saving all objects (tables, views, indices, triggers) of a
   schema in a common map, we now store tables/views, indices and
   triggers in three separate maps. This has a number of benefits:
   - It resembles more closely how SQLite stores its data internally and
     therefore achieves greater compatability e.g. for databases with a
     view and a trigger with the same name.
   - It reduces the need for runtime polymorphism. This makes the code
     run a bit faster.
   - By working with explicit types more often more error checking can
     be done at compile time, making the code less error prone.
   - The code becomes a lot clearer to read.

2) By making View inherit form Table, views are now a sort of tables.
   This has the following benefits:
   - This is a again how SQLite stores views internally which again
     should increase compatibility a bit.
   - We mostly treat views and tables the same anyway and with these
     changes we can unify the code for them even more.
This commit is contained in:
Martin Kleusberg
2021-01-29 17:18:09 +01:00
parent 13ab455d5c
commit 02db68107a
17 changed files with 324 additions and 418 deletions

View File

@@ -189,25 +189,22 @@ void AddRecordDialog::populateFields()
bool auto_increment = false;
// Initialize fields, fks and pk differently depending on whether it's a table or a view.
const sqlb::ObjectPtr obj = pdb.getObjectByName(curTable);
if (obj->type() == sqlb::Object::Table)
const sqlb::TablePtr table = pdb.getTableByName(curTable);
fields = table->fields;
if (!table->isView())
{
sqlb::TablePtr m_table = pdb.getObjectByName<sqlb::Table>(curTable);
fields = m_table->fields;
std::transform(fields.begin(), fields.end(), std::back_inserter(fks), [m_table](const auto& f) {
return m_table->constraint({f.name()}, sqlb::Constraint::ForeignKeyConstraintType);
std::transform(fields.begin(), fields.end(), std::back_inserter(fks), [table](const auto& f) {
return table->constraint({f.name()}, sqlb::Constraint::ForeignKeyConstraintType);
});
const auto pk_constraint = m_table->primaryKey();
const auto pk_constraint = table->primaryKey();
if(pk_constraint)
{
pk = pk_constraint->columnList();
auto_increment = pk_constraint->autoIncrement();
}
} else {
sqlb::ViewPtr m_view = pdb.getObjectByName<sqlb::View>(curTable);
fields = m_view->fields;
// It's a view
fks.resize(fields.size(), sqlb::ConstraintPtr(nullptr));
pk = pseudo_pk;
}