2017-12-30 12:57:55 +01:00
|
|
|
|
#include "ConstraintModel.h"
|
|
|
|
|
|
#include "ScopeGuard.h"
|
|
|
|
|
|
#include "PgDatabaseCatalog.h"
|
|
|
|
|
|
#include "PgConstraintContainer.h"
|
|
|
|
|
|
#include "Pgsql_oids.h"
|
|
|
|
|
|
|
|
|
|
|
|
ConstraintModel::ConstraintModel(QObject *parent)
|
|
|
|
|
|
: BaseTableModel(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-18 20:24:27 +01:00
|
|
|
|
void ConstraintModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
|
2017-12-30 12:57:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
beginResetModel();
|
|
|
|
|
|
SCOPE_EXIT { endResetModel(); };
|
|
|
|
|
|
|
|
|
|
|
|
m_table = table;
|
|
|
|
|
|
m_catalog = cat;
|
2018-11-18 20:24:27 +01:00
|
|
|
|
if (table) {
|
2018-11-25 19:45:06 +01:00
|
|
|
|
m_constraints = cat->constraints()->getConstraintsForRelation(table->oid());
|
2018-11-18 20:24:27 +01:00
|
|
|
|
std::sort(m_constraints.begin(), m_constraints.end(),
|
|
|
|
|
|
[] (auto &l, auto &r) {
|
|
|
|
|
|
return l.type < r.type ||
|
2018-11-30 18:41:38 +01:00
|
|
|
|
(l.type == r.type && l.objectName() < r.objectName());
|
2018-11-18 20:24:27 +01:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
m_constraints.clear();
|
2017-12-30 12:57:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2018-01-06 21:22:22 +01:00
|
|
|
|
// case DefinitionCol:
|
|
|
|
|
|
// c = tr("Definition");
|
|
|
|
|
|
// break;
|
2017-12-30 12:57:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
v = c;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return v;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-09 18:52:32 +02:00
|
|
|
|
int ConstraintModel::rowCount(const QModelIndex &) const
|
2017-12-30 12:57:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
return m_constraints.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-09 18:52:32 +02:00
|
|
|
|
int ConstraintModel::columnCount(const QModelIndex &) const
|
2017-12-30 12:57:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
return colCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//QVariant ConstraintModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
|
//{
|
|
|
|
|
|
// QVariant v;
|
|
|
|
|
|
// if (!index.isValid())
|
|
|
|
|
|
// return QVariant();
|
|
|
|
|
|
|
|
|
|
|
|
// // FIXME: Implement me!
|
|
|
|
|
|
// return v;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
2018-09-09 18:52:32 +02:00
|
|
|
|
Oid ConstraintModel::getType(int ) const
|
2017-12-30 12:57:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
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:
|
2018-11-30 18:41:38 +01:00
|
|
|
|
s = t.objectName();
|
2017-12-30 12:57:55 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NsCol:
|
2018-11-30 18:41:38 +01:00
|
|
|
|
s = t.nsName();
|
2017-12-30 12:57:55 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case SupportingIndexCol:
|
|
|
|
|
|
s = getIndexDisplayString(*m_catalog, t.indid);
|
|
|
|
|
|
break;
|
2018-01-06 21:22:22 +01:00
|
|
|
|
// case DefinitionCol:
|
|
|
|
|
|
// s = t.definition;
|
|
|
|
|
|
// break;
|
2017-12-30 12:57:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
v = s;
|
|
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
|
}
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
const PgConstraint& ConstraintModel::constraint(int row)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_constraints[row];
|
|
|
|
|
|
}
|