poc Add support for reloading the catalog

Reload works and the column page reacts correctly. Others to be checked
and fixed.
This commit is contained in:
eelke 2019-10-06 13:52:45 +02:00
parent 83122e89df
commit 60d8f36328
8 changed files with 89 additions and 40 deletions

View file

@ -31,38 +31,12 @@ namespace {
void ColumnTableModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
{
beginResetModel();
SCOPE_EXIT { endResetModel(); };
if (cat != m_catalog) {
m_catalog = cat;
refreshConnection = connect(m_catalog.get(), &PgDatabaseCatalog::refreshed, this, &ColumnTableModel::refresh);
}
m_table = table;
m_catalog = cat;
if (m_table) {
m_columns = cat->attributes()->getColumnsForRelation(table->oid());
// hide system and dropped columns
auto column_filter_pred = table->hasoids ? ColumnFilterWithOidsPred : ColumnFilterWithoutOidsPred;
auto si = std::remove_if(m_columns.begin(), m_columns.end(), column_filter_pred);
// move columns to end and remove them
m_columns.erase(si, m_columns.end());
// sort remaining columns by order in table
std::sort(m_columns.begin(), m_columns.end(),
[] (auto &l, auto &r) -> bool { return l.num < r.num; });
}
else
m_columns.clear();
if (m_table) {
m_indexes = m_catalog->indexes()->getIndexesForTable(table->oid());
std::sort(m_indexes.begin(), m_indexes.end(),
[] (const auto &l, const auto &r) -> bool
{
return l.isprimary > r.isprimary
|| (l.isprimary == r.isprimary && l.oid() < r.oid());
});
}
else
m_indexes.clear();
refresh();
}
QVariant ColumnTableModel::headerData(int section, Qt::Orientation orientation, int role) const
@ -245,6 +219,42 @@ QString ColumnTableModel::getFKey(const PgAttribute &column) const
return result;
}
void ColumnTableModel::refresh()
{
beginResetModel();
SCOPE_EXIT { endResetModel(); };
if (m_table) {
m_columns = m_catalog->attributes()->getColumnsForRelation(m_table->oid());
// hide system and dropped columns
auto column_filter_pred = m_table->hasoids ? ColumnFilterWithOidsPred : ColumnFilterWithoutOidsPred;
auto si = std::remove_if(m_columns.begin(), m_columns.end(), column_filter_pred);
// move columns to end and remove them
m_columns.erase(si, m_columns.end());
// sort remaining columns by order in table
std::sort(m_columns.begin(), m_columns.end(),
[] (auto &l, auto &r) -> bool { return l.num < r.num; });
}
else
m_columns.clear();
if (m_table) {
m_indexes = m_catalog->indexes()->getIndexesForTable(m_table->oid());
std::sort(m_indexes.begin(), m_indexes.end(),
[] (const auto &l, const auto &r) -> bool
{
return l.isprimary > r.isprimary
|| (l.isprimary == r.isprimary && l.oid() < r.oid());
});
}
else
m_indexes.clear();
emit
}
QVariant ColumnTableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::ForegroundRole && index.column() == TypeCol) {