@@ -112,12 +112,12 @@ class EditDelegate: public QStyledItemDelegate {
112112 }
113113};
114114
115- AddRecordDialog::AddRecordDialog (DBBrowserDB& db, const sqlb::ObjectIdentifier& tableName, QWidget* parent)
115+ AddRecordDialog::AddRecordDialog (DBBrowserDB& db, const sqlb::ObjectIdentifier& tableName, QWidget* parent, const QString &pseudo_pk )
116116 : QDialog(parent),
117117 ui(new Ui::AddRecordDialog),
118118 pdb(db),
119119 curTable(tableName),
120- m_table(*(pdb.getObjectByName<sqlb::Table>(curTable)) )
120+ pseudo_pk(pseudo_pk )
121121{
122122 // Create UI
123123 ui->setupUi (this );
@@ -179,10 +179,28 @@ void AddRecordDialog::populateFields()
179179 ui->treeWidget ->setItemDelegateForColumn (kType , new NoEditDelegate (this ));
180180 ui->treeWidget ->setItemDelegateForColumn (kValue , new EditDelegate (this ));
181181
182- const auto & fields = m_table.fields ;
183- const QStringList& pk = m_table.primaryKey ();
184- for (const sqlb::Field& f : fields)
182+ sqlb::FieldVector fields;
183+ QVector<sqlb::ConstraintPtr> fks;
184+ QStringList pk;
185+
186+ // Initialize fields, fks and pk differently depending on whether it's a table or a view.
187+ if (pseudo_pk.isNull ())
188+ {
189+ sqlb::TablePtr m_table = pdb.getObjectByName <sqlb::Table>(curTable);
190+ fields = m_table->fields ;
191+ for (const sqlb::Field& f : fields)
192+ fks.append (m_table->constraint ({f.name ()}, sqlb::Constraint::ForeignKeyConstraintType));
193+ pk = m_table->primaryKey ();
194+ } else {
195+ sqlb::ViewPtr m_view = pdb.getObjectByName <sqlb::View>(curTable);
196+ fields = m_view->fields ;
197+ fks.fill (sqlb::ConstraintPtr (nullptr ), fields.size ());
198+ pk = QStringList (pseudo_pk);
199+ }
200+
201+ for (uint i = 0 ; i < fields.size (); i++)
185202 {
203+ const sqlb::Field& f = fields[i];
186204 QTreeWidgetItem *tbitem = new QTreeWidgetItem (ui->treeWidget );
187205
188206 tbitem->setFlags (Qt::ItemIsEnabled | Qt::ItemIsEditable);
@@ -199,7 +217,7 @@ void AddRecordDialog::populateFields()
199217 }
200218 if (contains (pk, f.name ()))
201219 tbitem->setIcon (kName , QIcon (" :/icons/field_key" ));
202- else if (m_table. constraint ({f. name ()}, sqlb::Constraint::ForeignKeyConstraintType) )
220+ else if (fks[i] )
203221 tbitem->setIcon (kName , QIcon (" :/icons/field_fk" ));
204222 else
205223 tbitem->setIcon (kName , QIcon (" :/icons/field" ));
@@ -216,7 +234,7 @@ void AddRecordDialog::populateFields()
216234 if (!f.check ().isEmpty ())
217235 toolTip.append (tr (" Check constraint:\t %1\n " ).arg (f.check ()));
218236
219- auto fk = std::dynamic_pointer_cast<sqlb::ForeignKeyClause>(m_table. constraint ({f. name ()}, sqlb::Constraint::ForeignKeyConstraintType) );
237+ auto fk = std::dynamic_pointer_cast<sqlb::ForeignKeyClause>(fks[i] );
220238 if (fk)
221239 toolTip.append (tr (" Foreign key:\t %1\n " ).arg (fk->toString ()));
222240
0 commit comments