From aef9b914b14d1e23834507ab2fa28374d2a273e6 Mon Sep 17 00:00:00 2001 From: eelke Date: Sun, 17 Dec 2017 11:28:20 +0100 Subject: [PATCH] Loading of index definitions --- pglab/PgDatabaseCatalog.cpp | 16 ++++++++++++++- pglab/PgDatabaseCatalog.h | 6 +++++- pglab/PgIndex.cpp | 7 ++----- pglab/PgIndex.h | 41 +++++++++++++++++++++---------------- pglab/PgIndexContainer.cpp | 28 +++++++++++++++++++++++++ pglab/PgIndexContainer.h | 18 ++++++++++++++++ pglab/pglab.pro | 6 ++++-- 7 files changed, 95 insertions(+), 27 deletions(-) create mode 100644 pglab/PgIndexContainer.cpp create mode 100644 pglab/PgIndexContainer.h diff --git a/pglab/PgDatabaseCatalog.cpp b/pglab/PgDatabaseCatalog.cpp index 3fe5f2a..52706cf 100644 --- a/pglab/PgDatabaseCatalog.cpp +++ b/pglab/PgDatabaseCatalog.cpp @@ -4,6 +4,7 @@ #include "PgAuthIdContainer.h" #include "PgClassContainer.h" #include "PgDatabaseContainer.h" +#include "PgIndexContainer.h" #include "PgNamespaceContainer.h" #include "PgTypeContainer.h" #include "Pgsql_Connection.h" @@ -80,11 +81,11 @@ PgDatabaseCatalog::~PgDatabaseCatalog() void PgDatabaseCatalog::loadAll(Pgsql::Connection &conn) { loadInfo(conn); - loadAttributes(conn); loadAuthIds(conn); loadClasses(conn); loadDatabases(conn); + loadIndexes(conn); loadNamespaces(conn); loadTypes(conn); } @@ -145,6 +146,14 @@ void PgDatabaseCatalog::loadDatabases(Pgsql::Connection &conn) load(conn, *m_databases); } +void PgDatabaseCatalog::loadIndexes(Pgsql::Connection &conn) +{ + if (!m_indexes) + m_indexes = std::make_shared(shared_from_this()); + + load(conn, *m_indexes); +} + void PgDatabaseCatalog::loadNamespaces(Pgsql::Connection &conn) { if (!m_namespaces) @@ -192,6 +201,11 @@ std::shared_ptr PgDatabaseCatalog::databases() const return m_databases; } +std::shared_ptr PgDatabaseCatalog::indexes() const +{ + return m_indexes; +} + std::shared_ptr PgDatabaseCatalog::namespaces() const { return m_namespaces; diff --git a/pglab/PgDatabaseCatalog.h b/pglab/PgDatabaseCatalog.h index 0bdd7c4..ae745a5 100644 --- a/pglab/PgDatabaseCatalog.h +++ b/pglab/PgDatabaseCatalog.h @@ -16,6 +16,7 @@ class PgAttributeContainer; class PgAuthIdContainer; class PgClassContainer; class PgDatabaseContainer; +class PgIndexContainer; class PgNamespaceContainer; class PgTypeContainer; @@ -29,12 +30,13 @@ public: void loadAll(Pgsql::Connection &conn); + void loadInfo(Pgsql::Connection &conn); void loadAttributes(Pgsql::Connection &conn); void loadAuthIds(Pgsql::Connection &conn); void loadClasses(Pgsql::Connection &conn); void loadDatabases(Pgsql::Connection &conn); - void loadInfo(Pgsql::Connection &conn); + void loadIndexes(Pgsql::Connection &conn); void loadNamespaces(Pgsql::Connection &conn); void loadTypes(Pgsql::Connection &conn); @@ -45,6 +47,7 @@ public: std::shared_ptr authIds() const; std::shared_ptr classes() const; std::shared_ptr databases() const; + std::shared_ptr indexes() const; std::shared_ptr namespaces() const; std::shared_ptr types() const; private: @@ -55,6 +58,7 @@ private: std::shared_ptr m_authIds; std::shared_ptr m_classes; std::shared_ptr m_databases; + std::shared_ptr m_indexes; std::shared_ptr m_namespaces; std::shared_ptr m_types; }; diff --git a/pglab/PgIndex.cpp b/pglab/PgIndex.cpp index e9d6531..1b28fb0 100644 --- a/pglab/PgIndex.cpp +++ b/pglab/PgIndex.cpp @@ -1,6 +1,3 @@ -#include "PgIndex.h" +#include "PgIndex.h" -PgIndex::PgIndex() -{ - -} +PgIndex::PgIndex() = default; diff --git a/pglab/PgIndex.h b/pglab/PgIndex.h index 487f36f..f92e3a6 100644 --- a/pglab/PgIndex.h +++ b/pglab/PgIndex.h @@ -8,27 +8,32 @@ class PgIndex { public: - Oid indexrelid; - Oid indrelid; - int16_t indnatts; - bool indisunique; - bool indisprimary; - bool indisexclusion; - bool indimmediate; - bool indisclustered; - bool indisvalid; - bool indcheckxmin; - bool indisready; - bool indislive; - bool indisreplident; - std::vector indkey; - std::vector indcollation; + Oid indexrelid = InvalidOid; + Oid relid = InvalidOid; + int16_t natts = 0; + bool isunique = false; + bool isprimary = false; + bool isexclusion = false; + bool immediate = false; + bool isclustered = false; + bool isvalid = false; + bool checkxmin = false; + bool isready = false; + bool islive = false; + bool isreplident = false; + std::vector key; + std::vector collation; std::vector indclass; - std::vector indoption; - QString indexprs; - QString indpred; + std::vector option; + QString exprs; + QString pred; PgIndex(); + + bool operator==(Oid _oid) const { return indexrelid == _oid; } + //bool operator==(const QString &n) const { return name == n; } + bool operator<(Oid _oid) const { return indexrelid < _oid; } + bool operator<(const PgIndex &rhs) const { return indexrelid < rhs.indexrelid; } }; #endif // PGINDEX_H diff --git a/pglab/PgIndexContainer.cpp b/pglab/PgIndexContainer.cpp new file mode 100644 index 0000000..c02539c --- /dev/null +++ b/pglab/PgIndexContainer.cpp @@ -0,0 +1,28 @@ +#include "PgIndexContainer.h" +#include "Pgsql_Col.h" +#include + +std::string PgIndexContainer::getLoadQuery() const +{ + return R"__( +SELECT indexrelid, indrelid, indnatts, indisunique, indisprimary, + indisexclusion, indimmediate, indisclustered, indisvalid, + indcheckxmin, indisready, indislive, indisreplident, indkey, + indcollation, indclass, indoption, indexprs, indpred +FROM pg_index)__"; +} + +PgIndex PgIndexContainer::loadElem(const Pgsql::Row &row) +{ + Pgsql::Col col(row); + PgIndex v; + col >> v.indexrelid >> v.relid >> v.natts >> v.isunique + >> v.isprimary >> v.isexclusion >> v.immediate >> v.isclustered + >> v.isvalid >> v.checkxmin >> v.isready >> v.islive >> v.isreplident; + col.getAsVector(std::back_inserter(v.key)); + col.getAsVector(std::back_inserter(v.collation)); + col.getAsVector(std::back_inserter(v.indclass)); + col.getAsVector(std::back_inserter(v.option)); + col >> v.exprs >> v.pred; + return v; +} diff --git a/pglab/PgIndexContainer.h b/pglab/PgIndexContainer.h new file mode 100644 index 0000000..7df8b68 --- /dev/null +++ b/pglab/PgIndexContainer.h @@ -0,0 +1,18 @@ +#ifndef PGINDEXCONTAINER_H +#define PGINDEXCONTAINER_H + +#include "PgContainer.h" +#include "PgIndex.h" +#include "Pgsql_declare.h" +#include + +class PgIndexContainer : public PgContainer { +public: + using PgContainer::PgContainer; + + virtual std::string getLoadQuery() const override; +protected: + virtual PgIndex loadElem(const Pgsql::Row &row) override; +}; + +#endif // PGINDEXCONTAINER_H diff --git a/pglab/pglab.pro b/pglab/pglab.pro index 837103a..6da9d3f 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -79,7 +79,8 @@ SOURCES += main.cpp\ PgAttribute.cpp \ PgContainer.cpp \ PgAttributeContainer.cpp \ - PgIndex.cpp + PgIndex.cpp \ + PgIndexContainer.cpp HEADERS += \ QueryResultModel.h \ @@ -130,7 +131,8 @@ HEADERS += \ ColumnTableModel.h \ PgAttribute.h \ PgAttributeContainer.h \ - PgIndex.h + PgIndex.h \ + PgIndexContainer.h FORMS += mainwindow.ui \ DatabaseWindow.ui \