Updating of detail tabs besides list of tables works correctly again.
Had stopped working because the catalog now behaves a little different, returning nullptr instead of an invalid element.
This commit is contained in:
parent
fcb191f2cc
commit
52011a9842
12 changed files with 114 additions and 92 deletions
|
|
@ -11,35 +11,56 @@
|
|||
#include "SqlFormattingUtils.h"
|
||||
#include <QBrush>
|
||||
|
||||
void ColumnTableModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table)
|
||||
namespace {
|
||||
|
||||
// Filter dropped and system columns but keep the oid column
|
||||
bool ColumnFilterWithOidsPred(PgAttribute &e)
|
||||
{
|
||||
return e.isdropped || (e.num <= 0 && e.name != "oid");
|
||||
}
|
||||
|
||||
// Filter dropped and system columns
|
||||
bool ColumnFilterWithoutOidsPred(PgAttribute &e)
|
||||
{
|
||||
return e.isdropped || e.num <= 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ColumnTableModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
|
||||
{
|
||||
beginResetModel();
|
||||
SCOPE_EXIT { endResetModel(); };
|
||||
|
||||
m_table = table;
|
||||
m_catalog = cat;
|
||||
m_columns = cat->attributes()->getColumnsForRelation(table.oid);
|
||||
|
||||
// hide system columns
|
||||
auto si = table.hasoids
|
||||
? std::remove_if(m_columns.begin(), m_columns.end(),
|
||||
[](PgAttribute &e) { return e.num <= 0 && e.name != "oid"; })
|
||||
: std::remove_if(m_columns.begin(), m_columns.end(),
|
||||
[](PgAttribute &e) { return e.num <= 0; });
|
||||
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();
|
||||
|
||||
m_indexes = m_catalog->indexes()->getIndexesForTable(table.oid);
|
||||
|
||||
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.indexrelid < r.indexrelid);
|
||||
});
|
||||
}
|
||||
else
|
||||
m_indexes.clear();
|
||||
}
|
||||
|
||||
QVariant ColumnTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
|
|
@ -224,6 +245,9 @@ QVariant ColumnTableModel::data(const QModelIndex &index, int role) const
|
|||
if (role == Qt::ForegroundRole && index.column() == TypeCol) {
|
||||
QVariant v;
|
||||
const auto &t = m_columns[index.row()];
|
||||
if (t.typid == InvalidOid)
|
||||
v = QBrush(Qt::black);
|
||||
else {
|
||||
auto c = m_catalog->types()->getByKey(t.typid);
|
||||
switch (c->category) {
|
||||
case TypCategory::Boolean:
|
||||
|
|
@ -252,6 +276,7 @@ QVariant ColumnTableModel::data(const QModelIndex &index, int role) const
|
|||
default:
|
||||
v = QBrush(Qt::black);
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
if (role == Qt::TextAlignmentRole) {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include "PgClass.h"
|
||||
#include "PgIndex.h"
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
|
|
@ -25,7 +26,7 @@ public:
|
|||
|
||||
|
||||
using BaseTableModel::BaseTableModel;
|
||||
void setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table);
|
||||
void setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table);
|
||||
|
||||
// Header:
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
|
|
@ -42,7 +43,7 @@ protected:
|
|||
using t_Columns = std::vector<PgAttribute>;
|
||||
|
||||
std::shared_ptr<const PgDatabaseCatalog> m_catalog;
|
||||
PgClass m_table;
|
||||
std::optional<PgClass> m_table;
|
||||
t_Columns m_columns;
|
||||
std::vector<PgIndex> m_indexes;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,42 +12,23 @@ ConstraintModel::ConstraintModel(QObject *parent)
|
|||
|
||||
|
||||
|
||||
void ConstraintModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table)
|
||||
void ConstraintModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
|
||||
{
|
||||
beginResetModel();
|
||||
SCOPE_EXIT { endResetModel(); };
|
||||
|
||||
m_table = table;
|
||||
m_catalog = cat;
|
||||
m_constraints = cat->constraints()->getConstraintsForRelation(table.oid);
|
||||
|
||||
if (table) {
|
||||
m_constraints = cat->constraints()->getConstraintsForRelation(table->oid);
|
||||
std::sort(m_constraints.begin(), m_constraints.end(),
|
||||
[] (auto &l, auto &r) {
|
||||
return l.type < r.type ||
|
||||
(l.type == r.type && l.name < r.name);
|
||||
});
|
||||
|
||||
// // hide system columns
|
||||
// auto si = table.hasoids
|
||||
// ? std::remove_if(m_columns.begin(), m_columns.end(),
|
||||
// [](PgAttribute &e) { return e.num <= 0 && e.name != "oid"; })
|
||||
// : std::remove_if(m_columns.begin(), m_columns.end(),
|
||||
// [](PgAttribute &e) { return e.num <= 0; });
|
||||
// // 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; });
|
||||
|
||||
// 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.indexrelid < r.indexrelid);
|
||||
// });
|
||||
|
||||
}
|
||||
else
|
||||
m_constraints.clear();
|
||||
}
|
||||
|
||||
QVariant ConstraintModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include "PgClass.h"
|
||||
#include "PgConstraint.h"
|
||||
#include <QAbstractTableModel>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
|
|
@ -23,7 +24,7 @@ public:
|
|||
|
||||
explicit ConstraintModel(QObject *parent = nullptr);
|
||||
|
||||
void setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table);
|
||||
void setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table);
|
||||
|
||||
|
||||
// Header:
|
||||
|
|
@ -43,7 +44,7 @@ protected:
|
|||
|
||||
private:
|
||||
std::shared_ptr<const PgDatabaseCatalog> m_catalog;
|
||||
PgClass m_table;
|
||||
std::optional<PgClass> m_table;
|
||||
|
||||
using t_Constraints = std::vector<PgConstraint>;
|
||||
t_Constraints m_constraints;
|
||||
|
|
|
|||
|
|
@ -5,15 +5,17 @@
|
|||
#include "ScopeGuard.h"
|
||||
#include "CustomDataRole.h"
|
||||
|
||||
void IndexModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table)
|
||||
void IndexModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
|
||||
{
|
||||
beginResetModel();
|
||||
SCOPE_EXIT { endResetModel(); };
|
||||
|
||||
m_catalog = cat;
|
||||
m_table = table;
|
||||
|
||||
m_indexes = cat->indexes()->getIndexesForTable(table.oid);
|
||||
if (table)
|
||||
m_indexes = cat->indexes()->getIndexesForTable(table->oid);
|
||||
else
|
||||
m_indexes.clear();
|
||||
}
|
||||
|
||||
int IndexModel::rowCount(const QModelIndex &/*parent*/) const
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include "PgClass.h"
|
||||
#include "PgIndex.h"
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
|
@ -36,7 +37,7 @@ public:
|
|||
// comment
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
void setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table);
|
||||
void setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table);
|
||||
|
||||
|
||||
// Basic functionality:
|
||||
|
|
@ -52,7 +53,7 @@ protected:
|
|||
|
||||
private:
|
||||
std::shared_ptr<const PgDatabaseCatalog> m_catalog;
|
||||
PgClass m_table;
|
||||
std::optional<PgClass> m_table;
|
||||
|
||||
using t_Indexes = std::vector<PgIndex>;
|
||||
t_Indexes m_indexes;
|
||||
|
|
|
|||
|
|
@ -139,21 +139,29 @@ void TablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
|
|||
void TablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
||||
{
|
||||
if (current.row() != previous.row()) {
|
||||
if (current.isValid()) {
|
||||
PgClass table = m_tablesModel->getTable(current.row());
|
||||
selectedTableChanged(table);
|
||||
}
|
||||
else
|
||||
selectedTableChanged({});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TablesPage::tableListTable_layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint )
|
||||
{
|
||||
auto&& index = ui->tableListTable->selectionModel()->currentIndex();
|
||||
if (index.isValid()) {
|
||||
PgClass table = m_tablesModel->getTable(index.row());
|
||||
selectedTableChanged(table);
|
||||
}
|
||||
else
|
||||
selectedTableChanged({});
|
||||
}
|
||||
|
||||
|
||||
void TablesPage::selectedTableChanged(const PgClass & table)
|
||||
void TablesPage::selectedTableChanged(const std::optional<PgClass> &table)
|
||||
{
|
||||
m_columnsModel->setData(m_catalog, table);
|
||||
ui->columnsTable->resizeColumnsToContents();
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <QWidget>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <QItemSelection>
|
||||
|
||||
namespace Ui {
|
||||
|
|
@ -25,7 +26,7 @@ class TablesPage : public QWidget
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit TablesPage(MainWindow *parent = 0);
|
||||
explicit TablesPage(MainWindow *parent = nullptr);
|
||||
~TablesPage();
|
||||
|
||||
void setCatalog(std::shared_ptr<PgDatabaseCatalog> cat);
|
||||
|
|
@ -46,7 +47,7 @@ private:
|
|||
void retranslateUi(bool all = true);
|
||||
QWidget* addDetailTab(QWidget *contents);
|
||||
|
||||
void selectedTableChanged(const PgClass & table);
|
||||
void selectedTableChanged(const std::optional<PgClass> &table);
|
||||
private slots:
|
||||
|
||||
void tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous);
|
||||
|
|
|
|||
|
|
@ -45,9 +45,9 @@ void TriggerPage::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat)
|
|||
}
|
||||
|
||||
|
||||
void TriggerPage::setFilter(const PgClass &cls)
|
||||
void TriggerPage::setFilter(const std::optional<PgClass> &cls)
|
||||
{
|
||||
m_sortFilterProxy->setOidFilterTable(cls.oid, FirstHiddenValue);
|
||||
m_sortFilterProxy->setOidFilterTable(cls ? cls->oid : InvalidOid, FirstHiddenValue);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <QSplitter>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
class QTableView;
|
||||
class SqlCodePreview;
|
||||
|
|
@ -20,7 +21,7 @@ public:
|
|||
// TriggerPage(QWidget *parent = nullptr);
|
||||
|
||||
void setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat);
|
||||
void setFilter(const PgClass &cls);
|
||||
void setFilter(const std::optional<PgClass> &cls);
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ public:
|
|||
int32_t typmod = -1;
|
||||
bool notnull = false;
|
||||
bool hasdef = false;
|
||||
bool isdropped = false;
|
||||
Oid collation = InvalidOid;
|
||||
QString acl;
|
||||
QString options;
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ std::string PgAttributeContainer::getLoadQuery() const
|
|||
{
|
||||
return R"__(
|
||||
SELECT attrelid, attname, atttypid, attstattarget,
|
||||
attnum, attndims, atttypmod, attnotnull, atthasdef,
|
||||
attnum, attndims, atttypmod, attnotnull, atthasdef, attisdropped,
|
||||
attcollation, attacl, attoptions, pg_get_expr(adbin, adrelid) AS def_value
|
||||
FROM pg_catalog.pg_attribute
|
||||
LEFT JOIN pg_attrdef ON attrelid=adrelid AND attnum=adnum)__";
|
||||
|
|
@ -21,7 +21,7 @@ PgAttribute PgAttributeContainer::loadElem(const Pgsql::Row &row)
|
|||
Pgsql::Col col(row);
|
||||
PgAttribute v;
|
||||
col >> v.relid >> v.name >> v.typid >> v.stattarget
|
||||
>> v.num >> v.ndims >> v.typmod >> v.notnull >> v.hasdef
|
||||
>> v.num >> v.ndims >> v.typmod >> v.notnull >> v.hasdef >> v.isdropped
|
||||
>> v.collation >> v.acl >> v.options >> v.defaultValue;
|
||||
return v;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue