176 lines
4.2 KiB
C++
176 lines
4.2 KiB
C++
#include "TablesTableModel.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::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 && !e.ns().isSystemCatalog()) ++n;
|
|
|
|
m_tables.clear();
|
|
m_tables.reserve(n); // reserve space
|
|
for (const auto &e : *classes) {
|
|
if (e.kind == RelKind::Table && !e.ns().isSystemCatalog()) {
|
|
m_tables.push_back(e);
|
|
}
|
|
}
|
|
|
|
doSort(1);
|
|
endResetModel();
|
|
}
|
|
|
|
void TablesTableModel::setSortOrder(int so)
|
|
{
|
|
beginResetModel();
|
|
doSort(so);
|
|
endResetModel();
|
|
}
|
|
|
|
namespace {
|
|
|
|
inline bool compareByName(PgClass l, PgClass r)
|
|
{
|
|
return l.objectName() < r.objectName()
|
|
|| (l.objectName() == r.objectName() && l.nsName() < r.nsName());
|
|
}
|
|
|
|
inline bool compareBySchema(PgClass l, PgClass r)
|
|
{
|
|
return l.nsName() < r.nsName()
|
|
|| (l.nsName() == r.nsName() && l.objectName() < r.objectName());
|
|
}
|
|
|
|
}
|
|
|
|
void TablesTableModel::sort(int column, Qt::SortOrder order)
|
|
{
|
|
if (column == NameCol) {
|
|
if (order == Qt::AscendingOrder)
|
|
std::sort(m_tables.begin(), m_tables.end(), compareByName);
|
|
else
|
|
std::sort(m_tables.begin(), m_tables.end(), [] (auto l, auto r) { return !compareByName(l, r); });
|
|
}
|
|
else if (column == NamespaceCol) {
|
|
if (order == Qt::AscendingOrder)
|
|
std::sort(m_tables.begin(), m_tables.end(), compareBySchema);
|
|
else
|
|
std::sort(m_tables.begin(), m_tables.end(), [] (auto l, auto r) { return !compareBySchema(l, r); });
|
|
}
|
|
|
|
emit layoutChanged();
|
|
}
|
|
|
|
void TablesTableModel::doSort(int )
|
|
{
|
|
// if (so == 1)
|
|
// std::sort(m_tables.begin(), m_tables.end(),
|
|
// [] (auto l, auto r) -> bool { return l.relnamespace_name < r.relnamespace_name
|
|
// || (l.relnamespace_name == r.relnamespace_name && l.name < r.name); });
|
|
// else
|
|
// std::sort(m_tables.begin(), m_tables.end(),
|
|
// [] (auto l, auto r) -> bool { return l.name < r.name; });
|
|
}
|
|
|
|
|
|
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 OwnerCol: return tr("Owner");
|
|
case TablespaceCol: return tr("Tablespace");
|
|
case OptionsCol: return tr("Options");
|
|
case AclCol: return tr("ACL");
|
|
}
|
|
}
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
// 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::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 OwnerCol: return t.ownerName();
|
|
case TablespaceCol: return getTablespaceDisplayString(*m_catalog, t.tablespace);
|
|
case OptionsCol: break;
|
|
case AclCol: return t.aclString();
|
|
}
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
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.objectName());
|
|
//}
|
|
|
|
QVariant TablesTableModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (role == Qt::DisplayRole)
|
|
return getData(index);
|
|
else if (role == CustomDataTypeRole)
|
|
return getType(index.column());
|
|
return QVariant();
|
|
}
|