On the list of indexes of a table show which were created as part of the creation of a constraint
The explicit column is true when the index was manually created In SQL only a comment is given that the index was implicitly created
This commit is contained in:
parent
60bbb4c445
commit
fd5ad9bbf0
6 changed files with 56 additions and 20 deletions
|
|
@ -36,9 +36,16 @@ int IndexModel::columnCount(const QModelIndex &/*parent*/) const
|
||||||
return colCount;
|
return colCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
Oid IndexModel::getType(int /*column*/) const
|
Oid IndexModel::getType(int column) const
|
||||||
{
|
{
|
||||||
|
switch (column) {
|
||||||
|
case SizeCol:
|
||||||
|
return Pgsql::int8_oid;
|
||||||
|
case ExplicitIndexCol:
|
||||||
|
return Pgsql::bool_oid;
|
||||||
|
default:
|
||||||
return Pgsql::varchar_oid;
|
return Pgsql::varchar_oid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant IndexModel::headerData(int section, Qt::Orientation orientation, int role) const
|
QVariant IndexModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
|
|
@ -66,6 +73,9 @@ QVariant IndexModel::headerData(int section, Qt::Orientation orientation, int ro
|
||||||
case SizeCol:
|
case SizeCol:
|
||||||
c = tr("Size");
|
c = tr("Size");
|
||||||
break;
|
break;
|
||||||
|
case ExplicitIndexCol:
|
||||||
|
c = tr("Explicit");
|
||||||
|
break;
|
||||||
// case DefinitionCol:
|
// case DefinitionCol:
|
||||||
// c = tr("Definition");
|
// c = tr("Definition");
|
||||||
// break;
|
// break;
|
||||||
|
|
@ -106,6 +116,9 @@ QVariant IndexModel::getData(const QModelIndex &index) const
|
||||||
case SizeCol:
|
case SizeCol:
|
||||||
v = dat.sizeBytes;
|
v = dat.sizeBytes;
|
||||||
break;
|
break;
|
||||||
|
case ExplicitIndexCol:
|
||||||
|
v = !dat.isSupportingIndex();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ public:
|
||||||
ColumnsCol, ///
|
ColumnsCol, ///
|
||||||
ConditionCol,
|
ConditionCol,
|
||||||
SizeCol,
|
SizeCol,
|
||||||
|
ExplicitIndexCol,
|
||||||
colCount };
|
colCount };
|
||||||
|
|
||||||
// oid
|
// oid
|
||||||
|
|
|
||||||
|
|
@ -57,9 +57,6 @@ std::vector<PgConstraint> PgConstraintContainer::getFKeyForTableColumn(Oid relid
|
||||||
std::vector<PgConstraint> PgConstraintContainer::getConstraintsForRelation(Oid relid) const
|
std::vector<PgConstraint> PgConstraintContainer::getConstraintsForRelation(Oid relid) const
|
||||||
{
|
{
|
||||||
std::vector<PgConstraint> result;
|
std::vector<PgConstraint> result;
|
||||||
// for (const auto &e : m_container)
|
|
||||||
// if (e.relid == relid)
|
|
||||||
// result.push_back(e);
|
|
||||||
std::copy_if(m_container.begin(), m_container.end(), std::back_inserter(result),
|
std::copy_if(m_container.begin(), m_container.end(), std::back_inserter(result),
|
||||||
[relid] (auto && item) { return item.relid == relid; });
|
[relid] (auto && item) { return item.relid == relid; });
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -80,10 +77,15 @@ std::optional<PgConstraint> PgConstraintContainer::getPrimaryForRelation(Oid rel
|
||||||
std::vector<PgConstraint> PgConstraintContainer::getReferencedForRelation(Oid relid) const
|
std::vector<PgConstraint> PgConstraintContainer::getReferencedForRelation(Oid relid) const
|
||||||
{
|
{
|
||||||
std::vector<PgConstraint> result;
|
std::vector<PgConstraint> result;
|
||||||
// for (const auto &e : m_container)
|
|
||||||
// if (e.frelid == relid)
|
|
||||||
// result.push_back(e);
|
|
||||||
std::copy_if(m_container.begin(), m_container.end(), std::back_inserter(result),
|
std::copy_if(m_container.begin(), m_container.end(), std::back_inserter(result),
|
||||||
[relid] (auto && item) { return item.frelid == relid; });
|
[relid] (auto && item) { return item.frelid == relid; });
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<PgConstraint> PgConstraintContainer::getSupportedByIndex(Oid indexrelid) const
|
||||||
|
{
|
||||||
|
std::vector<PgConstraint> result;
|
||||||
|
std::copy_if(m_container.begin(), m_container.end(), std::back_inserter(result),
|
||||||
|
[indexrelid] (auto && item) { return item.indid == indexrelid; });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ public:
|
||||||
std::vector<PgConstraint> getConstraintsForRelation(Oid relid) const;
|
std::vector<PgConstraint> getConstraintsForRelation(Oid relid) const;
|
||||||
std::optional<PgConstraint> getPrimaryForRelation(Oid relid) const;
|
std::optional<PgConstraint> getPrimaryForRelation(Oid relid) const;
|
||||||
std::vector<PgConstraint> getReferencedForRelation(Oid relid) const;
|
std::vector<PgConstraint> getReferencedForRelation(Oid relid) const;
|
||||||
|
std::vector<PgConstraint> getSupportedByIndex(Oid indexrelid) const;
|
||||||
protected:
|
protected:
|
||||||
virtual PgConstraint loadElem(const Pgsql::Row &row) override;
|
virtual PgConstraint loadElem(const Pgsql::Row &row) override;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#include "PgIndex.h"
|
#include "PgIndex.h"
|
||||||
#include "PgDatabaseCatalog.h"
|
#include "PgDatabaseCatalog.h"
|
||||||
#include "PgClassContainer.h"
|
#include "PgClassContainer.h"
|
||||||
|
#include "PgConstraint.h"
|
||||||
|
#include "PgConstraintContainer.h"
|
||||||
#include "PgAmContainer.h"
|
#include "PgAmContainer.h"
|
||||||
#include <QStringBuilder>
|
#include <QStringBuilder>
|
||||||
|
|
||||||
|
|
@ -19,6 +21,8 @@ QString PgIndex::getAm() const
|
||||||
|
|
||||||
QString PgIndex::createSql() const
|
QString PgIndex::createSql() const
|
||||||
{
|
{
|
||||||
|
if (isSupportingIndex())
|
||||||
|
return "-- implicitly created index";
|
||||||
return definition + ";";
|
return definition + ";";
|
||||||
|
|
||||||
// const PgClass *table_class = catalog.classes()->getByKey(index.relid);
|
// const PgClass *table_class = catalog.classes()->getByKey(index.relid);
|
||||||
|
|
@ -69,6 +73,9 @@ QString PgIndex::createSql() const
|
||||||
|
|
||||||
QString PgIndex::dropSql() const
|
QString PgIndex::dropSql() const
|
||||||
{
|
{
|
||||||
|
if (isSupportingIndex())
|
||||||
|
return "-- implicitly created index";
|
||||||
|
|
||||||
QString result;
|
QString result;
|
||||||
result = "DROP INDEX "
|
result = "DROP INDEX "
|
||||||
% fullyQualifiedQuotedObjectName()
|
% fullyQualifiedQuotedObjectName()
|
||||||
|
|
@ -80,3 +87,17 @@ QString PgIndex::typeName() const
|
||||||
{
|
{
|
||||||
return "INDEX";
|
return "INDEX";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PgIndex::isSupportingIndex() const
|
||||||
|
{
|
||||||
|
// This function looks at the supporting index for constaints but this is not completely the
|
||||||
|
// same thing. For example a fkey is supported by a unique constraint on the referred table but this
|
||||||
|
// index has not been created as part of the constraint creation so it does not count.
|
||||||
|
//
|
||||||
|
// For now we assume that when the constraint and index are on the same table the index was created
|
||||||
|
// implicitly.
|
||||||
|
|
||||||
|
auto c = catalog().constraints()->getSupportedByIndex(oid());
|
||||||
|
auto find_result = std::find_if(c.begin(), c.end(), [this] (auto && item) { return item.relid == relid; });
|
||||||
|
return (find_result != c.end());
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@
|
||||||
class PgIndex : public PgNamespaceObject {
|
class PgIndex : public PgNamespaceObject {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Oid indexrelid = InvalidOid; // oid of pg_class for this index
|
|
||||||
Oid relid = InvalidOid; // oid of table (pg_class) where this is an index on
|
Oid relid = InvalidOid; // oid of table (pg_class) where this is an index on
|
||||||
int16_t natts = 0;
|
int16_t natts = 0;
|
||||||
bool isunique = false;
|
bool isunique = false;
|
||||||
|
|
@ -34,15 +33,14 @@ public:
|
||||||
using PgNamespaceObject::PgNamespaceObject;
|
using PgNamespaceObject::PgNamespaceObject;
|
||||||
QString getAm() const;
|
QString getAm() const;
|
||||||
|
|
||||||
// bool operator==(Oid _oid) const { return indexrelid == _oid; }
|
QString createSql() const override;
|
||||||
// //bool operator==(const QString &n) const { return name == n; }
|
QString dropSql() const override;
|
||||||
// bool operator<(Oid _oid) const { return indexrelid < _oid; }
|
|
||||||
// bool operator<(const PgIndex &rhs) const { return indexrelid < rhs.indexrelid; }
|
|
||||||
|
|
||||||
QString createSql() const;
|
|
||||||
QString dropSql() const;
|
|
||||||
|
|
||||||
QString typeName() const override;
|
QString typeName() const override;
|
||||||
|
|
||||||
|
/// Returns true when this index has been created as part of the creation
|
||||||
|
/// of a constaint.
|
||||||
|
bool isSupportingIndex() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PGINDEX_H
|
#endif // PGINDEX_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue