From 9d58af8cd2b145699065dec0fa00b0e7ba491bf8 Mon Sep 17 00:00:00 2001 From: eelke Date: Mon, 8 Mar 2021 16:59:13 +0100 Subject: [PATCH] comments on tables Show them in the list of tables. Genereate SQL to set the comment. --- pglab/TablesTableModel.cpp | 5 +++- pglab/TablesTableModel.h | 1 + pglab/widgets/CatalogTablesPage.cpp | 37 +++++++++++++++++---------- pglablib/catalog/PgClass.h | 2 ++ pglablib/catalog/PgClassContainer.cpp | 7 ++--- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/pglab/TablesTableModel.cpp b/pglab/TablesTableModel.cpp index bb7de12..5524514 100644 --- a/pglab/TablesTableModel.cpp +++ b/pglab/TablesTableModel.cpp @@ -131,6 +131,7 @@ QVariant TablesTableModel::headerData(int section, Qt::Orientation orientation, case TablespaceCol: return tr("Tablespace"); case OptionsCol: return tr("Options"); case AclCol: return tr("ACL"); + case CommentCol: return tr("Comment"); } } } @@ -140,7 +141,7 @@ QVariant TablesTableModel::headerData(int section, Qt::Orientation orientation, // Basic functionality: int TablesTableModel::rowCount(const QModelIndex &) const { - return m_tables.size(); + return static_cast(m_tables.size()); } int TablesTableModel::columnCount(const QModelIndex &) const @@ -159,6 +160,7 @@ Oid TablesTableModel::getType(int column) const case KindCol: case OptionsCol: case AclCol: + case CommentCol: default: oid = Pgsql::varchar_oid; } @@ -176,6 +178,7 @@ QVariant TablesTableModel::getData(const QModelIndex &index) const case TablespaceCol: return getTablespaceDisplayString(*m_catalog, t.tablespace); case OptionsCol: break; case AclCol: return t.aclString(); + case CommentCol: return t.description; } return QVariant(); diff --git a/pglab/TablesTableModel.h b/pglab/TablesTableModel.h index 52e8814..6d8441a 100644 --- a/pglab/TablesTableModel.h +++ b/pglab/TablesTableModel.h @@ -22,6 +22,7 @@ public: TablespaceCol, OptionsCol, AclCol, + CommentCol, colCount }; TablesTableModel(QObject *parent); diff --git a/pglab/widgets/CatalogTablesPage.cpp b/pglab/widgets/CatalogTablesPage.cpp index b3c196c..b2cdf1d 100644 --- a/pglab/widgets/CatalogTablesPage.cpp +++ b/pglab/widgets/CatalogTablesPage.cpp @@ -10,6 +10,7 @@ #include "SqlCodePreview.h" #include "TablesTableModel.h" #include "TriggerPage.h" +#include "SqlFormattingUtils.h" #include "catalog/PgIndexContainer.h" #include "catalog/PgTriggerContainer.h" #include "widgets/CatalogConstraintPage.h" @@ -186,22 +187,26 @@ void CatalogTablesPage::updateSqlTab(const std::optional &table) // table details (inherits etc) // Indexes - drop_sql += "-- drop Indexes\n"; - create_sql += "-- create Indexes\n"; auto && indexes = m_catalog->indexes()->getIndexesForTable(table->oid()); - for (auto && index : indexes) { - drop_sql += index.dropSql() % "\n"; - create_sql += index.createSql() % "\n"; - } + if (!indexes.empty()) { + drop_sql += "-- drop Indexes\n"; + create_sql += "-- create Indexes\n"; + for (auto && index : indexes) { + drop_sql += index.dropSql() % "\n"; + create_sql += index.createSql() % "\n"; + } + } // Triggers - drop_sql += "-- drop Triggers\n"; - create_sql += "-- create Triggers\n"; - auto && triggers = m_catalog->triggers()->getTriggersForRelation(table->oid()); - for (auto && trg : triggers) { - drop_sql += trg.dropSql() % "\n"; - create_sql += trg.createSql() % "\n"; - } + auto && triggers = m_catalog->triggers()->getTriggersForRelation(table->oid()); + if (!triggers.empty()) { + drop_sql += "-- drop Triggers\n"; + create_sql += "-- create Triggers\n"; + for (auto && trg : triggers) { + drop_sql += trg.dropSql() % "\n"; + create_sql += trg.createSql() % "\n"; + } + } // Privileges create_sql += "-- set Privileges\n"; @@ -209,6 +214,10 @@ void CatalogTablesPage::updateSqlTab(const std::optional &table) // Comments create_sql += "-- set Comments table + columns\n"; - // + if (!table->description.isEmpty()) { + create_sql += "COMMENT ON TABLE " + table->fullyQualifiedQuotedObjectName() + + " IS " + dollarQuoteString(table->description) + ";\n"; + } + // m_tableSql->setPlainText(drop_sql % "\n\n" % create_sql); } diff --git a/pglablib/catalog/PgClass.h b/pglablib/catalog/PgClass.h index f6c2aad..8510345 100644 --- a/pglablib/catalog/PgClass.h +++ b/pglablib/catalog/PgClass.h @@ -49,6 +49,8 @@ public: std::vector options; QString viewdef; + QString description; // from pg_description + using PgNamespaceObject::PgNamespaceObject; QString kindString() const; diff --git a/pglablib/catalog/PgClassContainer.cpp b/pglablib/catalog/PgClassContainer.cpp index c34eab4..9ae7657 100644 --- a/pglablib/catalog/PgClassContainer.cpp +++ b/pglablib/catalog/PgClassContainer.cpp @@ -11,13 +11,14 @@ std::string PgClassContainer::getLoadQuery() const " relowner, relam, relfilenode, reltablespace, relpages, " " reltuples, reltoastrelid, relisshared, relpersistence, " " relkind, relispopulated, relfrozenxid, relminmxid, " - " reloptions, relacl, pg_get_viewdef(oid)"; + " reloptions, d.description, relacl, pg_get_viewdef(oid)"; if (lessThenVersion(120000)) q += ", relhasoids "; q += "\nFROM pg_catalog.pg_class \n" + " LEFT JOIN pg_catalog.pg_description AS d ON (objoid=pg_class.oid AND objsubid=0) \n" "WHERE relkind IN ('r', 'i', 'p', 'I', 'v', 'm', 'f')"; return q; @@ -35,8 +36,8 @@ PgClass PgClassContainer::loadElem(const Pgsql::Row &row) col >> v.type >> v.oftype >> owner >> v.am >> v.filenode >> v.tablespace >> v.pages_est >> v.tuples_est >> v.toastrelid >> v.isshared >> v.persistence - >> v.kind >> v.ispopulated >> v.frozenxid >> v.minmxid - >> v.options; + >> v.kind >> v.ispopulated >> v.frozenxid >> v.minmxid + >> v.options >> v.description; v.setOwnerOid(owner);