mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-18 09:49:40 -06:00
Support deleting table constraints in Edit Table dialog
This adds support for deleting table constraints in the Edit Table dialog.
This commit is contained in:
@@ -30,9 +30,8 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier&
|
||||
connect(ui->treeWidget, &QTreeWidget::itemChanged, this, &EditTableDialog::fieldItemChanged);
|
||||
connect(ui->tableConstraints, &QTableWidget::itemChanged, this, &EditTableDialog::constraintItemChanged);
|
||||
|
||||
// TODO Remove this once we have added support for these buttons
|
||||
// TODO Remove this once we have added support for this button
|
||||
ui->buttonAddConstraint->setVisible(false);
|
||||
ui->buttonRemoveConstraint->setVisible(false);
|
||||
|
||||
// Set item delegate for foreign key column
|
||||
m_fkEditorDelegate = new ForeignKeyEditorDelegate(db, m_table, this);
|
||||
@@ -69,6 +68,12 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier&
|
||||
ui->labelEditWarning->setVisible(false);
|
||||
}
|
||||
|
||||
// Enable/disable remove constraint button depending on whether a constraint is selected
|
||||
connect(ui->tableConstraints, &QTableWidget::itemSelectionChanged, [this]() {
|
||||
bool hasSelection = ui->tableConstraints->selectionModel()->hasSelection();
|
||||
ui->buttonRemoveConstraint->setEnabled(hasSelection);
|
||||
});
|
||||
|
||||
// And create a savepoint
|
||||
pdb.setSavepoint(m_sRestorePointName);
|
||||
|
||||
@@ -818,3 +823,23 @@ void EditTableDialog::changeSchema(const QString& /*schema*/)
|
||||
// Update the SQL preview
|
||||
updateSqlText();
|
||||
}
|
||||
|
||||
void EditTableDialog::removeConstraint()
|
||||
{
|
||||
// Is there any item selected to delete?
|
||||
if(!ui->tableConstraints->currentItem())
|
||||
return;
|
||||
|
||||
// Find constraint to delete
|
||||
int row = ui->tableConstraints->currentRow();
|
||||
sqlb::StringVector columns = ui->tableConstraints->item(row, kConstraintColumns)->data(Qt::UserRole).value<sqlb::StringVector>();
|
||||
sqlb::ConstraintPtr constraint = ui->tableConstraints->item(row, kConstraintName)->data(Qt::UserRole).value<sqlb::ConstraintPtr>();
|
||||
|
||||
// Remove the constraint. If there is more than one constraint with this combination of columns and constraint type, only delete the first one.
|
||||
m_table.removeConstraint(columns, constraint);
|
||||
ui->tableConstraints->removeRow(ui->tableConstraints->currentRow());
|
||||
|
||||
// Update SQL and view
|
||||
updateSqlText();
|
||||
populateFields();
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ private slots:
|
||||
void moveDown();
|
||||
void setWithoutRowid(bool without_rowid);
|
||||
void changeSchema(const QString& schema);
|
||||
void removeConstraint();
|
||||
|
||||
private:
|
||||
Ui::EditTableDialog* ui;
|
||||
|
||||
@@ -618,6 +618,22 @@
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonRemoveConstraint</sender>
|
||||
<signal>clicked()</signal>
|
||||
<receiver>EditTableDialog</receiver>
|
||||
<slot>removeConstraint()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>186</x>
|
||||
<y>155</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>647</x>
|
||||
<y>157</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
<slots>
|
||||
<slot>fieldSelectionChanged()</slot>
|
||||
@@ -630,5 +646,6 @@
|
||||
<slot>moveDown()</slot>
|
||||
<slot>setWithoutRowid(bool)</slot>
|
||||
<slot>changeSchema(QString)</slot>
|
||||
<slot>removeConstraint()</slot>
|
||||
</slots>
|
||||
</ui>
|
||||
|
||||
@@ -509,6 +509,20 @@ void Table::setConstraint(const StringVector& vStrFields, ConstraintPtr constrai
|
||||
addConstraint(vStrFields, constraint);
|
||||
}
|
||||
|
||||
void Table::removeConstraint(const StringVector& vStrFields, ConstraintPtr constraint)
|
||||
{
|
||||
for(auto it = m_constraints.begin();it!=m_constraints.end();++it)
|
||||
{
|
||||
if(it->first == vStrFields && it->second->toSql(vStrFields) == constraint->toSql(vStrFields))
|
||||
{
|
||||
m_constraints.erase(it);
|
||||
|
||||
// Only remove the first constraint matching these criteria
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Table::removeConstraints(const StringVector& vStrFields, Constraint::ConstraintTypes type)
|
||||
{
|
||||
for(auto it = m_constraints.begin();it!=m_constraints.end();)
|
||||
|
||||
@@ -337,7 +337,8 @@ public:
|
||||
|
||||
void addConstraint(const StringVector& vStrFields, ConstraintPtr constraint);
|
||||
void setConstraint(const StringVector& vStrFields, ConstraintPtr constraint);
|
||||
void removeConstraints(const StringVector& vStrFields = StringVector(), Constraint::ConstraintTypes type = Constraint::NoType); //! Only removes the first constraint, if any
|
||||
void removeConstraint(const StringVector& vStrFields, ConstraintPtr constraint);
|
||||
void removeConstraints(const StringVector& vStrFields = StringVector(), Constraint::ConstraintTypes type = Constraint::NoType);
|
||||
ConstraintPtr constraint(const StringVector& vStrFields = StringVector(), Constraint::ConstraintTypes type = Constraint::NoType) const; //! Only returns the first constraint, if any
|
||||
std::vector<ConstraintPtr> constraints(const StringVector& vStrFields = StringVector(), Constraint::ConstraintTypes type = Constraint::NoType) const;
|
||||
ConstraintMap allConstraints() const { return m_constraints; }
|
||||
|
||||
Reference in New Issue
Block a user