pgLab/pglab/IndexModel.cpp

107 lines
2.1 KiB
C++
Raw Normal View History

#include "IndexModel.h"
#include "catalog/PgDatabaseCatalog.h"
#include "catalog/PgIndexContainer.h"
#include "Pgsql_oids.h"
#include "ScopeGuard.h"
2018-09-02 10:30:30 +00:00
#include "CustomDataRole.h"
void IndexModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
{
beginResetModel();
SCOPE_EXIT { endResetModel(); };
m_catalog = cat;
m_table = table;
if (table)
m_indexes = cat->indexes()->getIndexesForTable(table->oid());
else
m_indexes.clear();
}
2018-01-15 13:31:37 +01:00
int IndexModel::rowCount(const QModelIndex &/*parent*/) const
{
return (int)m_indexes.size();
}
2018-01-15 13:31:37 +01:00
int IndexModel::columnCount(const QModelIndex &/*parent*/) const
{
return colCount;
}
2018-01-15 13:31:37 +01:00
Oid IndexModel::getType(int /*column*/) const
{
return Pgsql::varchar_oid;
}
QVariant IndexModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant v;
if (orientation == Qt::Horizontal) {
if (role == Qt::DisplayRole) {
QString c;
switch (section) {
case TypeCol:
c = tr("Type");
break;
case NameCol:
c = tr("Name");
break;
case AmCol:
c = tr("AM");
break;
case ColumnsCol:
c = tr("On");
break;
case ConditionCol:
c = tr("Condition");
break;
// case DefinitionCol:
// c = tr("Definition");
// break;
}
v = c;
}
}
return v;
}
QVariant IndexModel::getData(const QModelIndex &index) const
{
QVariant v;
int rij = index.row();
const auto &dat = m_indexes[rij];
switch (index.column()) {
case TypeCol:
if (dat.isprimary) v = ":/icons/constraints/primarykey.png";
else if (dat.isunique) v = ":/icons/constraints/unique.png";
2019-02-09 11:36:37 +01:00
else v = ":/icons/constraints/index.png";
break;
case NameCol:
v = dat.objectName(); // getIndexDisplayString(*m_catalog, dat.indexrelid);
break;
case AmCol:
v = dat.getAm();
break;
case ColumnsCol:
break;
case ConditionCol:
break;
}
return v;
}
2018-01-15 13:31:37 +01:00
QVariant IndexModel::data(const QModelIndex &index, int role) const
{
QVariant v;
if (role == Qt::DisplayRole)
v = getData(index);
2018-09-02 10:30:30 +00:00
else if (role == CustomDataTypeRole)
2018-01-15 13:31:37 +01:00
v = getType(index.column());
return v;
}