pgLab/pglab/TablesTableModel.cpp
2022-01-17 05:53:56 +01:00

172 lines
4.3 KiB
C++

#include "TablesTableModel.h"
#include "ScopeGuard.h"
#include "catalog/PgDatabaseCatalog.h"
#include "catalog/PgClass.h"
#include "catalog/PgClassContainer.h"
#include "catalog/PgNamespace.h"
#include "catalog/PgNamespaceContainer.h"
#include "Pgsql_declare.h"
#include "CustomDataRole.h"
#include <QBrush>
TablesTableModel::TablesTableModel(QObject *parent)
: QAbstractTableModel(parent)
{}
void TablesTableModel::setNamespaceFilter(NamespaceFilter nsf)
{
m_namespaceFilter = nsf;
refresh();
}
void TablesTableModel::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat)
{
if (cat != m_catalog) {
m_catalog = cat;
refreshConnection = connect(m_catalog.get(), &PgDatabaseCatalog::refreshed,
this, &TablesTableModel::refresh);
}
refresh();
}
void TablesTableModel::refresh()
{
beginResetModel();
SCOPE_EXIT { endResetModel(); };
if (!m_catalog)
return;
// Later afscheiden naar filter functie
auto classes = m_catalog->classes();
m_tables.clear();
for (const auto &e : *classes) {
bool add = false;
if (e.kind == RelKind::Table || e.kind == RelKind::View
|| e.kind == RelKind::MaterializedView || e.kind == RelKind::ForeignTable) {
switch (m_namespaceFilter) {
case NamespaceFilter::User:
add = !e.ns().isSystemCatalog();
break;
case NamespaceFilter::PgCatalog:
add = e.ns().objectName() == "pg_catalog";
break;
case NamespaceFilter::InformationSchema:
add = e.ns().objectName() == "information_schema";
break;
}
}
if (add)
m_tables.push_back(e);
}
}
QVariant TablesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal) {
if (role == Qt::DisplayRole) {
switch (section) {
case NameCol: return tr("Name");
case NamespaceCol: return tr("Schema");
case KindCol: return tr("Kind");
case OwnerCol: return tr("Owner");
case TablespaceCol: return tr("Tablespace");
case OptionsCol: return tr("Options");
case AclCol: return tr("ACL");
case CommentCol: return tr("Comment");
case TotalSize: return tr("Total size");
case TableSize: return tr("Table size");
case IndexSize: return tr("Index size");
case ToastSize: return tr("TOAST size");
}
}
}
return QVariant();
}
int TablesTableModel::rowCount(const QModelIndex &) const
{
return static_cast<int>(m_tables.size());
}
int TablesTableModel::columnCount(const QModelIndex &) const
{
return colCount;
}
Oid TablesTableModel::getType(int column) const
{
Oid oid;
switch (column) {
case TotalSize:
case TableSize:
case IndexSize:
case ToastSize:
oid = Pgsql::int8_oid;
break;
case TablespaceCol:
case OwnerCol:
case NameCol:
case NamespaceCol:
case KindCol:
case OptionsCol:
case AclCol:
case CommentCol:
default:
oid = Pgsql::varchar_oid;
}
return oid;
}
QVariant TablesTableModel::getData(const QModelIndex &index) const
{
const auto &t = m_tables[index.row()];
switch (index.column()) {
case NameCol: return t.objectName();
case NamespaceCol: return t.nsName();
case KindCol: return t.typeName();
case OwnerCol: return t.ownerName();
case TablespaceCol: return getTablespaceDisplayString(*m_catalog, t.tablespace);
case OptionsCol: break;
case AclCol: return t.aclString();
case CommentCol: return t.description;
case TotalSize: return t.totalBytes;
case TableSize: return t.totalBytes - t.indexBytes - t.toastBytes;
case IndexSize: return t.indexBytes;
case ToastSize: return t.toastBytes;
}
return QVariant();
}
PgClass TablesTableModel::getTable(int row) const
{
return m_tables[row];
}
Oid TablesTableModel::getTableOid(int row) const
{
return m_tables[row].oid();
}
QVariant TablesTableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
return getData(index);
else if (role == CustomDataTypeRole)
return getType(index.column());
else if (role == CustomDataMeaningRole) {
switch (index.column()) {
case TotalSize:
case TableSize:
case IndexSize:
case ToastSize:
return static_cast<int>(DataMeaningBytes);
default:
return static_cast<int>(DataMeaningNormal);
}
}
return QVariant();
}