2017-12-10 10:35:46 +01:00
|
|
|
|
#include "TablesTableModel.h"
|
2018-12-16 10:17:59 +01:00
|
|
|
|
#include "catalog/PgDatabaseCatalog.h"
|
|
|
|
|
|
#include "catalog/PgClass.h"
|
|
|
|
|
|
#include "catalog/PgClassContainer.h"
|
|
|
|
|
|
#include "catalog/PgNamespace.h"
|
|
|
|
|
|
#include "catalog/PgNamespaceContainer.h"
|
2017-12-10 10:35:46 +01:00
|
|
|
|
#include "Pgsql_declare.h"
|
2018-09-02 10:30:30 +00:00
|
|
|
|
#include "CustomDataRole.h"
|
2017-12-12 20:13:53 +01:00
|
|
|
|
#include <QBrush>
|
2017-12-10 10:35:46 +01:00
|
|
|
|
|
|
|
|
|
|
TablesTableModel::TablesTableModel(QObject *parent)
|
2018-09-02 10:30:30 +00:00
|
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
|
|
{}
|
2017-12-10 10:35:46 +01:00
|
|
|
|
|
2018-12-29 10:56:24 +01:00
|
|
|
|
void TablesTableModel::setNamespaceFilter(NamespaceFilter nsf)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_namespaceFilter = nsf;
|
|
|
|
|
|
reloadData();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-10 10:35:46 +01:00
|
|
|
|
void TablesTableModel::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_catalog = cat;
|
2018-12-29 10:56:24 +01:00
|
|
|
|
reloadData();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TablesTableModel::reloadData()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_catalog)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
beginResetModel();
|
2017-12-10 10:35:46 +01:00
|
|
|
|
|
|
|
|
|
|
// Later afscheiden naar filter functie
|
2018-12-29 10:56:24 +01:00
|
|
|
|
auto classes = m_catalog->classes();
|
2017-12-10 10:35:46 +01:00
|
|
|
|
|
|
|
|
|
|
// How many?
|
|
|
|
|
|
|
|
|
|
|
|
m_tables.clear();
|
|
|
|
|
|
for (const auto &e : *classes) {
|
2018-12-29 10:56:24 +01:00
|
|
|
|
bool add = false;
|
2018-12-29 11:19:12 +01:00
|
|
|
|
if (e.kind == RelKind::Table || e.kind == RelKind::View
|
|
|
|
|
|
|| e.kind == RelKind::MaterializedView || e.kind == RelKind::ForeignTable) {
|
2018-12-29 10:56:24 +01:00
|
|
|
|
switch (m_namespaceFilter) {
|
|
|
|
|
|
case TablesTableModel::User:
|
|
|
|
|
|
add = !e.ns().isSystemCatalog();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TablesTableModel::PgCatalog:
|
|
|
|
|
|
add = e.ns().objectName() == "pg_catalog";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TablesTableModel::InformationSchema:
|
|
|
|
|
|
add = e.ns().objectName() == "information_schema";
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2017-12-10 10:35:46 +01:00
|
|
|
|
}
|
2018-12-29 10:56:24 +01:00
|
|
|
|
if (add)
|
|
|
|
|
|
m_tables.push_back(e);
|
2017-12-10 10:35:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-07 09:02:53 +01:00
|
|
|
|
doSort(1);
|
2017-12-10 10:35:46 +01:00
|
|
|
|
endResetModel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-07 09:02:53 +01:00
|
|
|
|
void TablesTableModel::setSortOrder(int so)
|
|
|
|
|
|
{
|
|
|
|
|
|
beginResetModel();
|
|
|
|
|
|
doSort(so);
|
|
|
|
|
|
endResetModel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-07 09:57:59 +02:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
inline bool compareByName(PgClass l, PgClass r)
|
|
|
|
|
|
{
|
2018-11-25 19:45:06 +01:00
|
|
|
|
return l.objectName() < r.objectName()
|
|
|
|
|
|
|| (l.objectName() == r.objectName() && l.nsName() < r.nsName());
|
2018-07-07 09:57:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool compareBySchema(PgClass l, PgClass r)
|
|
|
|
|
|
{
|
2018-11-25 19:45:06 +01:00
|
|
|
|
return l.nsName() < r.nsName()
|
|
|
|
|
|
|| (l.nsName() == r.nsName() && l.objectName() < r.objectName());
|
2018-07-07 09:57:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 )
|
2018-01-07 09:02:53 +01:00
|
|
|
|
{
|
2018-07-07 09:57:59 +02:00
|
|
|
|
// 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; });
|
2018-01-07 09:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-10 10:35:46 +01:00
|
|
|
|
|
|
|
|
|
|
QVariant TablesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (orientation == Qt::Horizontal) {
|
|
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
|
|
switch (section) {
|
2018-11-25 09:06:01 +01:00
|
|
|
|
case NameCol: return tr("Name");
|
|
|
|
|
|
case NamespaceCol: return tr("Schema");
|
2018-12-29 11:19:12 +01:00
|
|
|
|
case KindCol: return tr("Kind");
|
2018-11-25 09:06:01 +01:00
|
|
|
|
case OwnerCol: return tr("Owner");
|
|
|
|
|
|
case TablespaceCol: return tr("Tablespace");
|
|
|
|
|
|
case OptionsCol: return tr("Options");
|
|
|
|
|
|
case AclCol: return tr("ACL");
|
2017-12-10 10:35:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-11-25 09:06:01 +01:00
|
|
|
|
return QVariant();
|
2017-12-10 10:35:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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:
|
2017-12-12 20:13:53 +01:00
|
|
|
|
case NamespaceCol:
|
2018-12-29 11:19:12 +01:00
|
|
|
|
case KindCol:
|
2017-12-10 10:35:46 +01:00
|
|
|
|
case OptionsCol:
|
2018-11-25 09:06:01 +01:00
|
|
|
|
case AclCol:
|
2017-12-10 10:35:46 +01:00
|
|
|
|
default:
|
2018-09-09 18:52:32 +02:00
|
|
|
|
oid = Pgsql::varchar_oid;
|
2017-12-10 10:35:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
return oid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariant TablesTableModel::getData(const QModelIndex &index) const
|
|
|
|
|
|
{
|
|
|
|
|
|
const auto &t = m_tables[index.row()];
|
|
|
|
|
|
switch (index.column()) {
|
2018-11-25 19:45:06 +01:00
|
|
|
|
case NameCol: return t.objectName();
|
2018-11-30 18:41:38 +01:00
|
|
|
|
case NamespaceCol: return t.nsName();
|
2018-12-29 11:19:12 +01:00
|
|
|
|
case KindCol: return t.typeName();
|
2018-11-25 19:45:06 +01:00
|
|
|
|
case OwnerCol: return t.ownerName();
|
2018-11-25 09:06:01 +01:00
|
|
|
|
case TablespaceCol: return getTablespaceDisplayString(*m_catalog, t.tablespace);
|
|
|
|
|
|
case OptionsCol: break;
|
2018-12-27 12:05:48 +01:00
|
|
|
|
case AclCol: return t.aclString();
|
2017-12-10 10:35:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-25 09:06:01 +01:00
|
|
|
|
return QVariant();
|
2017-12-10 10:35:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-17 19:54:23 +01:00
|
|
|
|
PgClass TablesTableModel::getTable(int row) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_tables[row];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-12 20:13:53 +01:00
|
|
|
|
Oid TablesTableModel::getTableOid(int row) const
|
|
|
|
|
|
{
|
2018-11-25 19:45:06 +01:00
|
|
|
|
return m_tables[row].oid();
|
2017-12-12 20:13:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-25 19:45:06 +01:00
|
|
|
|
//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());
|
|
|
|
|
|
//}
|
2017-12-12 20:13:53 +01:00
|
|
|
|
|
|
|
|
|
|
QVariant TablesTableModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
|
{
|
2018-09-02 10:30:30 +00:00
|
|
|
|
if (role == Qt::DisplayRole)
|
|
|
|
|
|
return getData(index);
|
|
|
|
|
|
else if (role == CustomDataTypeRole)
|
|
|
|
|
|
return getType(index.column());
|
|
|
|
|
|
return QVariant();
|
2017-12-12 20:13:53 +01:00
|
|
|
|
}
|