In the list of columns displayed for a table a set of columns is appended describing the indexes on the table.

This commit is contained in:
eelke 2017-12-17 19:34:28 +01:00
parent aef9b914b1
commit 172e2bcd1d
9 changed files with 95 additions and 43 deletions

View file

@ -4,6 +4,7 @@
#include "PgAttributeContainer.h"
#include "PgType.h"
#include "PgTypeContainer.h"
#include "PgIndexContainer.h"
#include "ScopeGuard.h"
#include <QBrush>
@ -23,6 +24,14 @@ void ColumnTableModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, Oid
// sort remaining columns by order in table
std::sort(m_columns.begin(), m_columns.end(),
[] (auto &l, auto &r) -> bool { return l.num < r.num; });
m_indexes = m_catalog->indexes()->getIndexesForTable(table_oid);
std::sort(m_indexes.begin(), m_indexes.end(),
[] (const auto &l, const auto &r) -> bool
{
return l.isprimary > r.isprimary
|| (l.isprimary == r.isprimary && l.indexrelid < r.indexrelid);
});
}
QVariant ColumnTableModel::headerData(int section, Qt::Orientation orientation, int role) const
@ -31,22 +40,33 @@ QVariant ColumnTableModel::headerData(int section, Qt::Orientation orientation,
if (orientation == Qt::Horizontal) {
if (role == Qt::DisplayRole) {
QString c;
switch (section) {
case NameCol:
c = tr("Name");
break;
case TypeCol:
c = tr("Type");
break;
case NullCol:
c = tr("Nullable");
break;
case DefaultCol:
c = tr("Default");
break;
case CollationCol:
c = tr("Collation");
break;
if (section >= colCount) {
const auto &tbl_idx = m_indexes[section - colCount];
if (tbl_idx.isprimary)
c = "Pri";
else if (tbl_idx.isunique)
c = "Unq";
else
c = "Idx";
}
else {
switch (section) {
case NameCol:
c = tr("Name");
break;
case TypeCol:
c = tr("Type");
break;
case NullCol:
c = tr("Nullable");
break;
case DefaultCol:
c = tr("Default");
break;
case CollationCol:
c = tr("Collation");
break;
}
}
v = c;
}
@ -62,7 +82,7 @@ int ColumnTableModel::rowCount(const QModelIndex &/*parent*/) const
int ColumnTableModel::columnCount(const QModelIndex &/*parent*/) const
{
return colCount;
return colCount + m_indexes.size();
}
Oid ColumnTableModel::getType(int /*column*/) const
@ -86,26 +106,42 @@ Oid ColumnTableModel::getType(int /*column*/) const
QVariant ColumnTableModel::getData(const QModelIndex &index) const
{
QVariant v;
const auto &t = m_columns[index.row()];
QString s;
switch (index.column()) {
case NameCol:
s = t.name;
break;
case TypeCol:
s = getTypeDisplayString(*m_catalog, t.typid);
break;
case NullCol:
s = t.notnull ? "NOT NULL" : "";
break;
case DefaultCol:
s = "";
break;
case CollationCol:
s = ""; //t.collation;
break;
const int row = index.row();
const auto &t = m_columns[row];
const int col = index.column();
if (col >= colCount) {
const auto &tbl_idx = m_indexes[col - colCount];
int i = 1;
for (auto attr : tbl_idx.key) {
if (attr == t.num) {
v = i;
break;
}
++i;
}
}
else {
QString s;
switch (col) {
case NameCol:
s = t.name;
break;
case TypeCol:
s = getTypeDisplayString(*m_catalog, t.typid);
break;
case NullCol:
s = t.notnull ? "NOT NULL" : "";
break;
case DefaultCol:
s = "";
break;
case CollationCol:
s = ""; //t.collation;
break;
}
v = s;
}
v = s;
return v;
}