pgLab/pglab/TablesTableModel.cpp

177 lines
3.4 KiB
C++
Raw Normal View History

#include "TablesTableModel.h"
#include "PgDatabaseCatalog.h"
#include "PgClass.h"
#include "PgClassContainer.h"
#include "PgNamespace.h"
#include "PgNamespaceContainer.h"
#include "Pgsql_declare.h"
#include <QBrush>
TablesTableModel::TablesTableModel(QObject *parent)
: BaseTableModel(parent)
{
}
void TablesTableModel::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat)
{
beginResetModel();
m_catalog = cat;
// Later afscheiden naar filter functie
auto classes = cat->classes();
// How many?
int n = 0;
for (const auto &e : *classes)
if (e.kind == RelKind::Table) ++n;
m_tables.clear();
m_tables.reserve(n); // reserve space
for (const auto &e : *classes) {
if (e.kind == RelKind::Table) {
m_tables.push_back(e);
}
}
doSort(1);
endResetModel();
}
void TablesTableModel::setSortOrder(int so)
{
beginResetModel();
doSort(so);
endResetModel();
}
void TablesTableModel::doSort(int so)
{
if (so == 1)
std::sort(m_tables.begin(), m_tables.end(),
[] (auto l, auto r) -> bool { return l.relnamespace < r.relnamespace
|| (l.relnamespace == r.relnamespace && l.name < r.name); });
}
QVariant TablesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant v;
if (orientation == Qt::Horizontal) {
if (role == Qt::DisplayRole) {
switch (section) {
case NameCol:
v = tr("Name");
break;
case NamespaceCol:
v = tr("Schema");
break;
case OwnerCol:
v = tr("Owner");
break;
case TablespaceCol:
v = tr("Tablespace");
break;
case OptionsCol:
v = tr("Options");
break;
// case AclCol:
// v = tr("ACL");
// break;
}
}
}
return v;
}
// Basic functionality:
int TablesTableModel::rowCount(const QModelIndex &) const
{
return m_tables.size();
}
int TablesTableModel::columnCount(const QModelIndex &) const
{
return colCount;
}
Oid TablesTableModel::getType(int column) const
{
Oid oid;
switch (column) {
case TablespaceCol:
case OwnerCol:
case NameCol:
case NamespaceCol:
case OptionsCol:
// case AclCol:
default:
oid = Pgsql::VARCHAROID;
}
return oid;
}
QVariant TablesTableModel::getData(const QModelIndex &index) const
{
QVariant v;
const auto &t = m_tables[index.row()];
switch (index.column()) {
case NameCol:
v = t.name; //formatTableName(t);
break;
case NamespaceCol:
v = getNamespaceDisplayString(*m_catalog, t.relnamespace);
break;
case OwnerCol:
v = getRoleDisplayString(*m_catalog, t.owner);
break;
case TablespaceCol:
v = getTablespaceDisplayString(*m_catalog, t.tablespace);
break;
case OptionsCol:
v = t.options;
break;
// case AclCol:
// v = t.acl;
// break;
}
return v;
}
PgClass TablesTableModel::getTable(int row) const
{
return m_tables[row];
}
Oid TablesTableModel::getTableOid(int row) const
{
return m_tables[row].oid;
}
QString TablesTableModel::formatTableName(const PgClass &cls) const
{
const char * format = "%2 (%1)";
QString ns_name = getNamespaceDisplayString(*m_catalog, cls.relnamespace);
return QString(format).arg(ns_name).arg(cls.name);
}
QVariant TablesTableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::ForegroundRole) {
const auto &t = m_tables[index.row()];
auto ns = m_catalog->namespaces()->getByKey(t.relnamespace);
if (ns.isSystemCatalog()) {
switch (index.column()) {
case NameCol:
case NamespaceCol:
return QBrush(Qt::blue);
break;
}
}
}
return BaseTableModel::data(index, role);
}