Support foreign key constraints referring to the same table (#933)

This commit is contained in:
Vladyslav
2017-01-13 20:12:02 +02:00
committed by GitHub
parent 11ca36ef9d
commit 46a55bdc78
4 changed files with 48 additions and 14 deletions

View File

@@ -78,7 +78,13 @@ ForeignKeyEditorDelegate::ForeignKeyEditorDelegate(const DBBrowserDB& db, sqlb::
, m_db(db)
, m_table(table)
{
const auto objects = m_db.getBrowsableObjects();
for (auto obj : objects) {
if ("table" == obj.gettype()) {
QString tableName = obj.table.name();
m_tablesIds.insert(tableName, obj.table.fieldNames());
}
}
}
QWidget* ForeignKeyEditorDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
@@ -99,6 +105,9 @@ QWidget* ForeignKeyEditorDelegate::createEditor(QWidget* parent, const QStyleOpt
box->setCurrentIndex(0);
});
editor->tablesComboBox->clear();
editor->tablesComboBox->addItems(m_tablesIds.keys());
return editor;
}
@@ -106,17 +115,6 @@ void ForeignKeyEditorDelegate::setEditorData(QWidget* editor, const QModelIndex&
{
ForeignKeyEditor* fkEditor = static_cast<ForeignKeyEditor*>(editor);
m_tablesIds.clear();
const auto objects = m_db.getBrowsableObjects();
for (auto obj : objects) {
if ("table" == obj.gettype()) {
QString tableName = obj.table.name();
m_tablesIds.insert(tableName, obj.table.fieldNames());
}
}
fkEditor->tablesComboBox->addItems(m_tablesIds.keys());
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>();
@@ -170,4 +168,12 @@ void ForeignKeyEditorDelegate::updateEditorGeometry(QWidget* editor, const QStyl
editor->setGeometry(option.rect);
}
void ForeignKeyEditorDelegate::updateTablesList(const QString& oldTableName)
{
// this is used for recursive table constraints when
// table column references column within same table
m_tablesIds.remove(oldTableName);
m_tablesIds.insert(m_table.name(), m_table.fieldNames());
}
#include "ForeignKeyEditorDelegate.moc"