comments on tables

Show them in the list of tables.
Genereate SQL to set the comment.
This commit is contained in:
eelke 2021-03-08 16:59:13 +01:00
parent 7fbc828326
commit 9d58af8cd2
5 changed files with 34 additions and 18 deletions

View file

@ -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<int>(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();

View file

@ -22,6 +22,7 @@ public:
TablespaceCol,
OptionsCol,
AclCol,
CommentCol,
colCount };
TablesTableModel(QObject *parent);

View file

@ -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<PgClass> &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<PgClass> &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);
}

View file

@ -49,6 +49,8 @@ public:
std::vector<QString> options;
QString viewdef;
QString description; // from pg_description
using PgNamespaceObject::PgNamespaceObject;
QString kindString() const;

View file

@ -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);