Code refactoring

This commit refactors vast parts of the sqlitetypes.h interface. Its
main goals are: less code, easier code, a more modern interface, reduced
likelihood for strange errors and more flexibility for future
extensions.

The main reason why the sqlitetypes.h functions were working so well in
DB4S was not because they were that stable but because they were
extremely interlinked with the rest of the code. This is fine because we
do not plan to ship them as a separate library. But it makes it hard to
find the obvious spot to fix an issue or to put a new function. It can
always be done in the sqlitetypes function or in the rest of the DB4S
code because it is just not clear what the interface between the two
should look like. This is supposed to be improved by this commit. One
main thing here is to make ownership of objects a bit clearer.

In theory the new code should be faster too but that difference will be
neglectable from a user POV.

This commit also fixes a hidden bug which caused all table constraints
to be removed in the Edit Table dialog when a single field was removed
from the table.

This is all still WIP and more work is needed to be done here.
This commit is contained in:
Martin Kleusberg
2018-09-04 19:46:39 +02:00
parent f3e6aec57d
commit bf505edf66
16 changed files with 685 additions and 792 deletions

View File

@@ -88,7 +88,7 @@ ForeignKeyEditorDelegate::ForeignKeyEditorDelegate(const DBBrowserDB& db, sqlb::
if((*jt)->type() == sqlb::Object::Types::Table)
{
QString tableName = (*jt)->name();
m_tablesIds.insert(tableName, (*jt).dynamicCast<sqlb::Table>()->fieldNames());
m_tablesIds.insert(tableName, std::dynamic_pointer_cast<sqlb::Table>(*jt)->fieldNames());
}
}
}
@@ -123,9 +123,9 @@ void ForeignKeyEditorDelegate::setEditorData(QWidget* editor, const QModelIndex&
ForeignKeyEditor* fkEditor = static_cast<ForeignKeyEditor*>(editor);
int column = index.row(); // weird? I know right
sqlb::FieldPtr field = m_table.fields().at(column);
QSharedPointer<sqlb::ForeignKeyClause> fk = m_table.constraint({field}, sqlb::Constraint::ForeignKeyConstraintType).dynamicCast<sqlb::ForeignKeyClause>();
if (!fk.isNull()) {
const sqlb::Field& field = m_table.fields.at(column);
auto fk = std::dynamic_pointer_cast<sqlb::ForeignKeyClause>(m_table.constraint({field.name()}, sqlb::Constraint::ForeignKeyConstraintType));
if (fk) {
fkEditor->tablesComboBox->setCurrentText(fk->table());
fkEditor->clauseEdit->setText(fk->constraint());
if (!fk->columns().isEmpty())
@@ -141,10 +141,10 @@ void ForeignKeyEditorDelegate::setModelData(QWidget* editor, QAbstractItemModel*
QString sql = fkEditor->getSql();
int column = index.row();
sqlb::FieldPtr field = m_table.fields().at(column);
const sqlb::Field& field = m_table.fields.at(column);
if (sql.isEmpty()) {
// Remove the foreign key
m_table.removeConstraints({field}, sqlb::Constraint::ConstraintTypes::ForeignKeyConstraintType);
m_table.removeConstraints({field.name()}, sqlb::Constraint::ConstraintTypes::ForeignKeyConstraintType);
} else {
// Set the foreign key
sqlb::ForeignKeyClause* fk = new sqlb::ForeignKeyClause;
@@ -162,7 +162,7 @@ void ForeignKeyEditorDelegate::setModelData(QWidget* editor, QAbstractItemModel*
fk->setConstraint(clause);
}
m_table.setConstraint({field}, sqlb::ConstraintPtr(fk));
m_table.setConstraint({field.name()}, sqlb::ConstraintPtr(fk));
}
model->setData(index, sql);