From f2808de613f07c06786fd0bc086e4798c386c88d Mon Sep 17 00:00:00 2001 From: eelke Date: Sat, 9 Feb 2019 20:37:34 +0100 Subject: [PATCH] Sequence and Function pages are now properly filtered on namespace. --- pglab/CatalogInspector.cpp | 14 ++++---- pglab/CatalogInspector.h | 5 +-- pglab/NamespaceFilter.h | 8 +++++ pglab/ProcTableModel.cpp | 42 +++++++++++++++++++++--- pglab/ProcTableModel.h | 8 +++-- pglab/SequenceModel.cpp | 44 ++++++++++++++++++++++---- pglab/SequenceModel.h | 8 +++-- pglab/TablesTableModel.cpp | 6 ++-- pglab/TablesTableModel.h | 6 ++-- pglab/pglab.pro | 3 +- pglab/widgets/CatalogFunctionsPage.cpp | 6 ++++ pglab/widgets/CatalogFunctionsPage.h | 2 ++ pglab/widgets/CatalogSequencesPage.cpp | 6 ++++ pglab/widgets/CatalogSequencesPage.h | 2 ++ pglab/widgets/CatalogTablesPage.cpp | 3 +- pglab/widgets/CatalogTablesPage.h | 6 ++-- pglablib/catalog/PgClass.h | 15 +-------- 17 files changed, 136 insertions(+), 48 deletions(-) create mode 100644 pglab/NamespaceFilter.h diff --git a/pglab/CatalogInspector.cpp b/pglab/CatalogInspector.cpp index 4ee0a3d..fd23765 100644 --- a/pglab/CatalogInspector.cpp +++ b/pglab/CatalogInspector.cpp @@ -56,17 +56,19 @@ void CatalogInspector::setCatalog(std::shared_ptr cat) m_sequencesPage->setCatalog(cat); } -void CatalogInspector::setNamespaceFilter(TablesTableModel::NamespaceFilter filter) +void CatalogInspector::setNamespaceFilter(NamespaceFilter filter) { m_tablesPage->setNamespaceFilter(filter); + m_functionsPage->setNamespaceFilter(filter); + m_sequencesPage->setNamespaceFilter(filter); QString hint = "Catalog instpector"; QString caption = "Inspector"; switch (filter) { - case TablesTableModel::PgCatalog: + case NamespaceFilter::PgCatalog: hint += " - pg_catalog"; caption = "pg_catalog"; break; - case TablesTableModel::InformationSchema: + case NamespaceFilter::InformationSchema: hint += " - information_schema"; caption = "information_schema"; break; @@ -94,9 +96,9 @@ void CatalogInspectorModule::moduleAction_open( auto ct = new CatalogInspector(context, nullptr); context->addContentWidget(this, ct); auto nsf = params.at("namespace-filter").toString(); - TablesTableModel::NamespaceFilter filter = TablesTableModel::User; - if (nsf == "pg_catalog") filter = TablesTableModel::PgCatalog; - else if (nsf == "information_schema") filter = TablesTableModel::InformationSchema; + NamespaceFilter filter = NamespaceFilter::User; + if (nsf == "pg_catalog") filter = NamespaceFilter::PgCatalog; + else if (nsf == "information_schema") filter = NamespaceFilter::InformationSchema; ct->setNamespaceFilter(filter); } diff --git a/pglab/CatalogInspector.h b/pglab/CatalogInspector.h index d5e1153..dfe50cb 100644 --- a/pglab/CatalogInspector.h +++ b/pglab/CatalogInspector.h @@ -3,13 +3,14 @@ #include #include -#include "TablesTableModel.h" +#include "NamespaceFilter.h" #include "plugin_support/PluginContentWidget.h" #include "plugin_support/PluginModule.h" class CatalogFunctionsPage; class CatalogSequencesPage; class CatalogTablesPage; +class PgDatabaseCatalog; class QTabWidget; class CatalogInspector : public PluginContentWidget { @@ -19,7 +20,7 @@ public: ~CatalogInspector(); void setCatalog(std::shared_ptr cat); - void setNamespaceFilter(TablesTableModel::NamespaceFilter filter); + void setNamespaceFilter(NamespaceFilter filter); private: QTabWidget *m_tabWidget = nullptr; CatalogTablesPage *m_tablesPage = nullptr; diff --git a/pglab/NamespaceFilter.h b/pglab/NamespaceFilter.h new file mode 100644 index 0000000..de00980 --- /dev/null +++ b/pglab/NamespaceFilter.h @@ -0,0 +1,8 @@ +#ifndef NAMESPACEFILTER_H +#define NAMESPACEFILTER_H + +enum class NamespaceFilter { + User, PgCatalog, InformationSchema +}; + +#endif // NAMESPACEFILTER_H diff --git a/pglab/ProcTableModel.cpp b/pglab/ProcTableModel.cpp index 377e55e..1fa52d8 100644 --- a/pglab/ProcTableModel.cpp +++ b/pglab/ProcTableModel.cpp @@ -2,6 +2,7 @@ #include "catalog/PgDatabaseCatalog.h" #include "catalog/PgProcContainer.h" #include "catalog/PgLanguageContainer.h" +#include "catalog/PgNamespace.h" #include "CustomDataRole.h" ProcTableModel::ProcTableModel(QObject *parent) @@ -27,17 +28,48 @@ QVariant ProcTableModel::headerData(int section, Qt::Orientation orientation, in void ProcTableModel::setCatalog(std::shared_ptr cat) { + m_catalog = cat; + reloadData(); +} + +void ProcTableModel::setNamespaceFilter(NamespaceFilter filter) +{ + m_namespaceFilter = filter; + reloadData(); +} + +void ProcTableModel::reloadData() +{ + if (!m_catalog) + return; + beginResetModel(); - m_catalog = cat; - m_procs = cat->procs(); + auto && procs = m_catalog->procs(); + m_procs.clear(); + for (auto&& p : *procs) { + bool add = false; + switch (m_namespaceFilter) { + case NamespaceFilter::User: + add = !p.ns().isSystemCatalog(); + break; + case NamespaceFilter::PgCatalog: + add = p.ns().objectName() == "pg_catalog"; + break; + case NamespaceFilter::InformationSchema: + add = p.ns().objectName() == "information_schema"; + break; + } + if (add) + m_procs.push_back(p); + } endResetModel(); } int ProcTableModel::rowCount(const QModelIndex &) const { - return m_procs ? static_cast(m_procs->count()) : 0; + return static_cast(m_procs.size()); } int ProcTableModel::columnCount(const QModelIndex &) const @@ -60,7 +92,7 @@ QVariant ProcTableModel::data(const QModelIndex &index, int role) const PgProc ProcTableModel::proc(int row) const { - return m_procs->getByIdx(row); + return m_procs.at(static_cast(row)); } Oid ProcTableModel::getType(int ) const @@ -77,7 +109,7 @@ Oid ProcTableModel::getType(int ) const QVariant ProcTableModel::getData(const QModelIndex &index) const { - auto&& t = m_procs->getByIdx(index.row()); + auto&& t = m_procs.at(static_cast(index.row())); switch (index.column()) { case NameCol: return t.objectName(); case NamespaceCol: return t.nsName(); diff --git a/pglab/ProcTableModel.h b/pglab/ProcTableModel.h index 2a49a6d..6a07d17 100644 --- a/pglab/ProcTableModel.h +++ b/pglab/ProcTableModel.h @@ -1,9 +1,10 @@ #ifndef PROCTABLEMODEL_H #define PROCTABLEMODEL_H -#include #include "catalog/PgClass.h" #include "catalog/PgProc.h" +#include "NamespaceFilter.h" +#include #include class PgDatabaseCatalog; @@ -34,6 +35,7 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; void setCatalog(std::shared_ptr cat); + void setNamespaceFilter(NamespaceFilter filter); // Basic functionality: int rowCount(const QModelIndex &parent = QModelIndex()) const override; @@ -44,8 +46,10 @@ public: private: std::shared_ptr m_catalog; - std::shared_ptr m_procs; + NamespaceFilter m_namespaceFilter = NamespaceFilter::User; + std::vector m_procs; + void reloadData(); Oid getType(int column) const; QVariant getData(const QModelIndex &index) const; diff --git a/pglab/SequenceModel.cpp b/pglab/SequenceModel.cpp index a18203e..6a4867b 100644 --- a/pglab/SequenceModel.cpp +++ b/pglab/SequenceModel.cpp @@ -1,5 +1,6 @@ #include "SequenceModel.h" #include "catalog/PgDatabaseCatalog.h" +#include "catalog/PgNamespace.h" #include "catalog/PgSequenceContainer.h" SequenceModel::SequenceModel(QObject * parent) @@ -31,22 +32,53 @@ QVariant SequenceModel::headerData(int section, Qt::Orientation orientation, int void SequenceModel::setCatalog(std::shared_ptr cat) { + m_catalog = cat; + reloadData(); +} + +void SequenceModel::setNamespaceFilter(NamespaceFilter filter) +{ + m_namespaceFilter = filter; + reloadData(); +} + +void SequenceModel::reloadData() +{ + if (!m_catalog) + return; + beginResetModel(); - m_catalog = cat; - m_sequences = cat->sequences(); + auto && seqs = m_catalog->sequences(); + m_sequences.clear(); + for (auto&& s : *seqs) { + bool add = false; + switch (m_namespaceFilter) { + case NamespaceFilter::User: + add = !s.ns().isSystemCatalog(); + break; + case NamespaceFilter::PgCatalog: + add = s.ns().objectName() == "pg_catalog"; + break; + case NamespaceFilter::InformationSchema: + add = s.ns().objectName() == "information_schema"; + break; + } + if (add) + m_sequences.push_back(s); + } endResetModel(); } PgSequence SequenceModel::sequence(int row) const { - return m_sequences->getByIdx(row); + return m_sequences.at(static_cast(row)); } int SequenceModel::rowCount(const QModelIndex &) const { - return m_sequences ? static_cast(m_sequences->count()) : 0; + return static_cast(m_sequences.size()); } int SequenceModel::columnCount(const QModelIndex &) const @@ -56,11 +88,11 @@ int SequenceModel::columnCount(const QModelIndex &) const QVariant SequenceModel::data(const QModelIndex &index, int role) const { - if (!m_sequences) + if (m_sequences.empty()) return {}; int row = index.row(); - auto && seq = m_sequences->getByIdx(row); + auto && seq = m_sequences.at(static_cast(row)); if (role == Qt::DisplayRole) { switch (index.column()) { case NameCol: return seq.objectName(); diff --git a/pglab/SequenceModel.h b/pglab/SequenceModel.h index 9b3ce5a..f055f05 100644 --- a/pglab/SequenceModel.h +++ b/pglab/SequenceModel.h @@ -1,8 +1,8 @@ #ifndef SEQUENCEMODEL_H #define SEQUENCEMODEL_H +#include "NamespaceFilter.h" #include -//#include "catalog/PgClass.h" #include "catalog/PgSequence.h" #include @@ -34,6 +34,7 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; void setCatalog(std::shared_ptr cat); + void SequenceModel::setNamespaceFilter(NamespaceFilter filter); int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; @@ -42,7 +43,10 @@ public: PgSequence sequence(int row) const; private: std::shared_ptr m_catalog; - std::shared_ptr m_sequences; + std::vector m_sequences; + NamespaceFilter m_namespaceFilter = NamespaceFilter::User; + + void reloadData(); }; #endif // SEQUENCEMODEL_H diff --git a/pglab/TablesTableModel.cpp b/pglab/TablesTableModel.cpp index db3336a..0c4f767 100644 --- a/pglab/TablesTableModel.cpp +++ b/pglab/TablesTableModel.cpp @@ -43,13 +43,13 @@ void TablesTableModel::reloadData() if (e.kind == RelKind::Table || e.kind == RelKind::View || e.kind == RelKind::MaterializedView || e.kind == RelKind::ForeignTable) { switch (m_namespaceFilter) { - case TablesTableModel::User: + case NamespaceFilter::User: add = !e.ns().isSystemCatalog(); break; - case TablesTableModel::PgCatalog: + case NamespaceFilter::PgCatalog: add = e.ns().objectName() == "pg_catalog"; break; - case TablesTableModel::InformationSchema: + case NamespaceFilter::InformationSchema: add = e.ns().objectName() == "information_schema"; break; } diff --git a/pglab/TablesTableModel.h b/pglab/TablesTableModel.h index 534c663..4aa1ba7 100644 --- a/pglab/TablesTableModel.h +++ b/pglab/TablesTableModel.h @@ -2,6 +2,7 @@ #define TABLESTABLEMODEL_H #include "BaseTableModel.h" +#include "NamespaceFilter.h" #include "catalog/PgClass.h" #include #include @@ -25,9 +26,6 @@ public: TablesTableModel(QObject *parent); - enum NamespaceFilter { - User, PgCatalog, InformationSchema - }; void setNamespaceFilter(NamespaceFilter nsf); void setCatalog(std::shared_ptr cat); @@ -49,7 +47,7 @@ private: using t_Tables = std::vector; std::shared_ptr m_catalog; - NamespaceFilter m_namespaceFilter = User; + NamespaceFilter m_namespaceFilter = NamespaceFilter::User; t_Tables m_tables; void reloadData(); diff --git a/pglab/pglab.pro b/pglab/pglab.pro index a99f3b7..478e312 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -170,7 +170,8 @@ CustomDataRole.h \ widgets/CatalogConstraintPage.h \ widgets/CatalogTablesPage.h \ widgets/CatalogFunctionsPage.h \ - widgets/CatalogSequencesPage.h + widgets/CatalogSequencesPage.h \ + NamespaceFilter.h FORMS += \ ConnectionManagerWindow.ui \ diff --git a/pglab/widgets/CatalogFunctionsPage.cpp b/pglab/widgets/CatalogFunctionsPage.cpp index bc650b8..2cf0a71 100644 --- a/pglab/widgets/CatalogFunctionsPage.cpp +++ b/pglab/widgets/CatalogFunctionsPage.cpp @@ -37,6 +37,7 @@ CatalogFunctionsPage::CatalogFunctionsPage(QWidget *parent) m_functionTable->setModel(m_sortFilterProxy); m_functionTable->setSortingEnabled(true); m_functionTable->setSelectionBehavior(QAbstractItemView::SelectRows); + m_functionTable->setSelectionMode(QAbstractItemView::SingleSelection); connect(m_functionTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &CatalogFunctionsPage::functionTable_currentRowChanged); @@ -60,6 +61,11 @@ void CatalogFunctionsPage::setCatalog(std::shared_ptr c m_functionTable->resizeColumnsToContents(); } +void CatalogFunctionsPage::setNamespaceFilter(NamespaceFilter filter) +{ + m_model->setNamespaceFilter(filter); +} + void CatalogFunctionsPage::functionTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) { if (current.row() != previous.row()) { diff --git a/pglab/widgets/CatalogFunctionsPage.h b/pglab/widgets/CatalogFunctionsPage.h index 64994f3..a0205d1 100644 --- a/pglab/widgets/CatalogFunctionsPage.h +++ b/pglab/widgets/CatalogFunctionsPage.h @@ -4,6 +4,7 @@ #include #include #include +#include "NamespaceFilter.h" class PgLabTableView; class PgDatabaseCatalog; @@ -19,6 +20,7 @@ public: explicit CatalogFunctionsPage(QWidget *parent = nullptr); void setCatalog(std::shared_ptr cat); + void setNamespaceFilter(NamespaceFilter filter); signals: public slots: diff --git a/pglab/widgets/CatalogSequencesPage.cpp b/pglab/widgets/CatalogSequencesPage.cpp index ecf61b3..e8774ae 100644 --- a/pglab/widgets/CatalogSequencesPage.cpp +++ b/pglab/widgets/CatalogSequencesPage.cpp @@ -24,6 +24,7 @@ CatalogSequencesPage::CatalogSequencesPage(QWidget *parent) m_sequenceTable->setModel(m_sortFilterProxy); m_sequenceTable->setSortingEnabled(true); m_sequenceTable->setSelectionBehavior(QAbstractItemView::SelectRows); + m_sequenceTable->setSelectionMode(QAbstractItemView::SingleSelection); connect(m_sequenceTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &CatalogSequencesPage::sequenceTable_currentRowChanged); @@ -42,6 +43,11 @@ void CatalogSequencesPage::setCatalog(std::shared_ptr c m_sequenceTable->resizeColumnsToContents(); } +void CatalogSequencesPage::setNamespaceFilter(NamespaceFilter filter) +{ + m_model->setNamespaceFilter(filter); +} + void CatalogSequencesPage::sequenceTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) { if (current.row() != previous.row()) { diff --git a/pglab/widgets/CatalogSequencesPage.h b/pglab/widgets/CatalogSequencesPage.h index 9780999..ea486c6 100644 --- a/pglab/widgets/CatalogSequencesPage.h +++ b/pglab/widgets/CatalogSequencesPage.h @@ -1,6 +1,7 @@ #ifndef SEQUENCESPAGES_H #define SEQUENCESPAGES_H +#include "NamespaceFilter.h" #include #include #include @@ -18,6 +19,7 @@ public: CatalogSequencesPage(QWidget *parent = nullptr); void setCatalog(std::shared_ptr cat); + void setNamespaceFilter(NamespaceFilter filter); public slots: void sequenceTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); diff --git a/pglab/widgets/CatalogTablesPage.cpp b/pglab/widgets/CatalogTablesPage.cpp index 75d840f..391475a 100644 --- a/pglab/widgets/CatalogTablesPage.cpp +++ b/pglab/widgets/CatalogTablesPage.cpp @@ -7,6 +7,7 @@ #include "PropertiesPage.h" #include "ResultTableModelUtil.h" #include "SqlCodePreview.h" +#include "TablesTableModel.h" #include "TriggerPage.h" #include "catalog/PgIndexContainer.h" #include "catalog/PgTriggerContainer.h" @@ -101,7 +102,7 @@ void CatalogTablesPage::setCatalog(std::shared_ptr cat) m_triggerPage->setCatalog(cat); } -void CatalogTablesPage::setNamespaceFilter(TablesTableModel::NamespaceFilter filter) +void CatalogTablesPage::setNamespaceFilter(NamespaceFilter filter) { m_tablesModel->setNamespaceFilter(filter); } diff --git a/pglab/widgets/CatalogTablesPage.h b/pglab/widgets/CatalogTablesPage.h index bc44413..4272143 100644 --- a/pglab/widgets/CatalogTablesPage.h +++ b/pglab/widgets/CatalogTablesPage.h @@ -1,9 +1,11 @@ #ifndef CATALOGTABLESPAGE_H #define CATALOGTABLESPAGE_H +#include "NamespaceFilter.h" #include #include -#include "TablesTableModel.h" +#include +#include class CatalogConstraintPage; class CatalogIndexPage; @@ -27,7 +29,7 @@ public: explicit CatalogTablesPage(QWidget * parent = nullptr); void setCatalog(std::shared_ptr cat); - void setNamespaceFilter(TablesTableModel::NamespaceFilter filter); + void setNamespaceFilter(NamespaceFilter filter); void retranslateUi(bool all = true); private: diff --git a/pglablib/catalog/PgClass.h b/pglablib/catalog/PgClass.h index 3a49951..3a8ff3f 100644 --- a/pglablib/catalog/PgClass.h +++ b/pglablib/catalog/PgClass.h @@ -30,12 +30,6 @@ void operator<<(RelKind &s, const Pgsql::Value &v); class PgClass: public PgNamespaceObject { public: - -// Oid oid = InvalidOid; -// QString name; -// Oid relnamespace = InvalidOid; -// QString relnamespace_name; // Transient, cached value from relnamespace -// bool system_namespace = false; // Transient, cached value from relnamespace Oid type = InvalidOid; Oid oftype = InvalidOid; //Oid owner = InvalidOid; @@ -56,15 +50,8 @@ public: using PgNamespaceObject::PgNamespaceObject; -// virtual QString objectName() const override; - -// bool operator==(Oid _oid) const { return oid == _oid; } -// bool operator==(const QString &n) const { return objectName() == n; } -// bool operator<(Oid _oid) const { return oid < _oid; } -// bool operator<(const PgClass &rhs) const { return oid < rhs.oid; } - QString kindString() const; - QString createSql() const; + QString createSql() const override; QString typeName() const override; QString aclAllPattern() const override;