2018-01-06 21:22:22 +01:00
|
|
|
|
#include "IndexModel.h"
|
2018-12-16 10:17:59 +01:00
|
|
|
|
#include "catalog/PgDatabaseCatalog.h"
|
|
|
|
|
|
#include "catalog/PgIndexContainer.h"
|
2018-01-06 21:22:22 +01:00
|
|
|
|
#include "Pgsql_oids.h"
|
|
|
|
|
|
#include "ScopeGuard.h"
|
2018-09-02 10:30:30 +00:00
|
|
|
|
#include "CustomDataRole.h"
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
2018-11-18 20:24:27 +01:00
|
|
|
|
void IndexModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
|
2019-10-06 14:03:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (cat != m_catalog) {
|
|
|
|
|
|
m_catalog = cat;
|
|
|
|
|
|
refreshConnection = connect(m_catalog.get(), &PgDatabaseCatalog::refreshed, this, &IndexModel::refresh);
|
|
|
|
|
|
}
|
|
|
|
|
|
m_table = table;
|
|
|
|
|
|
refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IndexModel::refresh()
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
beginResetModel();
|
|
|
|
|
|
SCOPE_EXIT { endResetModel(); };
|
|
|
|
|
|
|
2019-10-06 14:03:46 +02:00
|
|
|
|
if (m_table)
|
|
|
|
|
|
m_indexes = m_catalog->indexes()->getIndexesForTable(m_table->oid());
|
2018-11-18 20:24:27 +01:00
|
|
|
|
else
|
|
|
|
|
|
m_indexes.clear();
|
2018-01-06 21:22:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-15 13:31:37 +01:00
|
|
|
|
int IndexModel::rowCount(const QModelIndex &/*parent*/) const
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
2018-08-25 18:11:12 +02:00
|
|
|
|
return (int)m_indexes.size();
|
2018-01-06 21:22:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-15 13:31:37 +01:00
|
|
|
|
int IndexModel::columnCount(const QModelIndex &/*parent*/) const
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
return colCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-15 13:31:37 +01:00
|
|
|
|
Oid IndexModel::getType(int /*column*/) const
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
2018-08-25 18:11:12 +02:00
|
|
|
|
case AmCol:
|
|
|
|
|
|
c = tr("AM");
|
|
|
|
|
|
break;
|
2018-01-06 21:22:22 +01:00
|
|
|
|
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";
|
2018-01-06 21:22:22 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case NameCol:
|
2018-11-25 19:45:06 +01:00
|
|
|
|
v = dat.objectName(); // getIndexDisplayString(*m_catalog, dat.indexrelid);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
2018-08-25 18:11:12 +02:00
|
|
|
|
case AmCol:
|
|
|
|
|
|
v = dat.getAm();
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2018-01-06 21:22:22 +01:00
|
|
|
|
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;
|
|
|
|
|
|
}
|