Last column shows the full textual definition until I have decided on a better way to visualize the details.
168 lines
3.5 KiB
C++
168 lines
3.5 KiB
C++
#include "ConstraintModel.h"
|
|
#include "ScopeGuard.h"
|
|
#include "PgDatabaseCatalog.h"
|
|
#include "PgConstraintContainer.h"
|
|
#include "Pgsql_oids.h"
|
|
|
|
ConstraintModel::ConstraintModel(QObject *parent)
|
|
: BaseTableModel(parent)
|
|
{
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConstraintModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table)
|
|
{
|
|
beginResetModel();
|
|
SCOPE_EXIT { endResetModel(); };
|
|
|
|
m_table = table;
|
|
m_catalog = cat;
|
|
m_constraints = cat->constraints()->getConstraintsForRelation(table.oid);
|
|
|
|
std::sort(m_constraints.begin(), m_constraints.end(),
|
|
[] (auto &l, auto &r) {
|
|
return l.type < r.type ||
|
|
(l.type == r.type && l.name < r.name);
|
|
});
|
|
|
|
// // hide system columns
|
|
// auto si = table.hasoids
|
|
// ? std::remove_if(m_columns.begin(), m_columns.end(),
|
|
// [](PgAttribute &e) { return e.num <= 0 && e.name != "oid"; })
|
|
// : std::remove_if(m_columns.begin(), m_columns.end(),
|
|
// [](PgAttribute &e) { return e.num <= 0; });
|
|
// // move columns to end and remove them
|
|
// m_columns.erase(si, m_columns.end());
|
|
|
|
// // 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 ConstraintModel::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 NsCol:
|
|
c = tr("Schema");
|
|
break;
|
|
case SupportingIndexCol:
|
|
c = tr("Supporting index");
|
|
break;
|
|
case DefinitionCol:
|
|
c = tr("Definition");
|
|
break;
|
|
}
|
|
v = c;
|
|
}
|
|
}
|
|
return v;
|
|
}
|
|
|
|
int ConstraintModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
return m_constraints.size();
|
|
}
|
|
|
|
int ConstraintModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
return colCount;
|
|
}
|
|
|
|
//QVariant ConstraintModel::data(const QModelIndex &index, int role) const
|
|
//{
|
|
// QVariant v;
|
|
// if (!index.isValid())
|
|
// return QVariant();
|
|
|
|
// // FIXME: Implement me!
|
|
// return v;
|
|
//}
|
|
|
|
Oid ConstraintModel::getType(int column) const
|
|
{
|
|
Oid oid = Pgsql::varchar_oid;
|
|
|
|
return oid;
|
|
}
|
|
|
|
QString IconForConstraintType(ConstraintType ct)
|
|
{
|
|
QString s = ":/icons/constraints/";
|
|
switch (ct) {
|
|
case ConstraintType::Check:
|
|
s += "check.png";
|
|
break;
|
|
case ConstraintType::ForeignKey:
|
|
s += "foreignkey.png";
|
|
break;
|
|
case ConstraintType::PrimaryKey:
|
|
s += "primarykey.png";
|
|
break;
|
|
case ConstraintType::Unique:
|
|
s += "unique.png";
|
|
break;
|
|
case ConstraintType::ConstraintTrigger:
|
|
s = "CT";
|
|
break;
|
|
case ConstraintType::ExclusionConstraint:
|
|
s = "XC";
|
|
break;
|
|
default:
|
|
s = "?";
|
|
break;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
|
|
|
|
QVariant ConstraintModel::getData(const QModelIndex &index) const
|
|
{
|
|
QVariant v;
|
|
const int row = index.row();
|
|
const auto &t = m_constraints[row];
|
|
|
|
const int col = index.column();
|
|
QString s;
|
|
switch (col) {
|
|
case TypeCol:
|
|
s = IconForConstraintType(t.type);
|
|
break;
|
|
case NameCol:
|
|
s = t.name;
|
|
break;
|
|
case NsCol:
|
|
s = getNamespaceDisplayString(*m_catalog, t.connamespace);
|
|
break;
|
|
case SupportingIndexCol:
|
|
s = getIndexDisplayString(*m_catalog, t.indid);
|
|
break;
|
|
case DefinitionCol:
|
|
s = t.definition;
|
|
break;
|
|
}
|
|
v = s;
|
|
|
|
return v;
|
|
}
|