In the column list show foreign key constraint
This commit is contained in:
parent
8836611b62
commit
634345b38f
5 changed files with 65 additions and 12 deletions
|
|
@ -8,6 +8,7 @@
|
||||||
#include "PgTypeContainer.h"
|
#include "PgTypeContainer.h"
|
||||||
#include "PgIndexContainer.h"
|
#include "PgIndexContainer.h"
|
||||||
#include "ScopeGuard.h"
|
#include "ScopeGuard.h"
|
||||||
|
#include "SqlFormattingUtils.h"
|
||||||
#include <QBrush>
|
#include <QBrush>
|
||||||
|
|
||||||
void ColumnTableModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table)
|
void ColumnTableModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const PgClass &table)
|
||||||
|
|
@ -208,9 +209,12 @@ QVariant ColumnTableModel::getData(const QModelIndex &index) const
|
||||||
QString ColumnTableModel::getFKey(const PgAttribute &column) const
|
QString ColumnTableModel::getFKey(const PgAttribute &column) const
|
||||||
{
|
{
|
||||||
QString result;
|
QString result;
|
||||||
const PgConstraint *c = m_catalog->constraints()->getFKeyForTableColumn(column.relid, column.num);
|
auto&& list = m_catalog->constraints()->getFKeyForTableColumn(column.relid, column.num);
|
||||||
if (c) {
|
for (auto&& elem : list) {
|
||||||
result = c->name;
|
if (elem.key[0] == column.num) {
|
||||||
|
//result = elem.name;
|
||||||
|
result = getForeignKeyConstraintReferencesShort(*m_catalog, elem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,16 +52,17 @@ PgConstraint PgConstraintContainer::loadElem(const Pgsql::Row &row)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PgConstraint* PgConstraintContainer::getFKeyForTableColumn(Oid relid, int16_t attnum) const
|
std::vector<PgConstraint> PgConstraintContainer::getFKeyForTableColumn(Oid relid, int16_t attnum) const
|
||||||
{
|
{
|
||||||
const PgConstraint *result = nullptr;
|
//const PgConstraint *result = nullptr;
|
||||||
|
std::vector<PgConstraint> result;
|
||||||
// WHat do we want to find here? On ly single column constraints or all contstraints.
|
// WHat do we want to find here? On ly single column constraints or all contstraints.
|
||||||
// auto res = std::find_if(m_container.begin(), m_container.end(),
|
auto res = std::copy_if(m_container.begin(), m_container.end(), std::back_inserter(result),
|
||||||
// [relid, attnum] (const auto &c) {
|
[relid, attnum] (const auto &c) {
|
||||||
// // the find on v.key may not look super efficient but remember it in general only has one or two elements.
|
// the find on v.key may not look super efficient but remember it in general only has one or two elements.
|
||||||
// return relid == c.relid &&
|
return c.type == ConstraintType::ForeignKey && relid == c.relid &&
|
||||||
// (std::find(attnum.begin(), attnum.end(), v.key) != attnum.end());
|
(std::find(c.key.begin(), c.key.end(), attnum) != c.key.end());
|
||||||
// });
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ public:
|
||||||
|
|
||||||
virtual std::string getLoadQuery() const override;
|
virtual std::string getLoadQuery() const override;
|
||||||
//std::vector<PgConstraint> getIndexesForTable(Oid table_oid) const;
|
//std::vector<PgConstraint> getIndexesForTable(Oid table_oid) const;
|
||||||
const PgConstraint* getFKeyForTableColumn(Oid relid, int16_t attnum) const;
|
std::vector<PgConstraint> getFKeyForTableColumn(Oid relid, int16_t attnum) const;
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,50 @@ QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, cons
|
||||||
% " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype)
|
% " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype)
|
||||||
% " ON DELETE " % ForeignKeyActionToString(constraint.fdeltype)
|
% " ON DELETE " % ForeignKeyActionToString(constraint.fdeltype)
|
||||||
% deferrable % validated % ";";
|
% deferrable % validated % ";";
|
||||||
|
}
|
||||||
|
|
||||||
|
QString getForeignKeyConstraintReferences(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
||||||
|
{
|
||||||
|
PgClass fcls = catalog.classes()->getByKey(constraint.frelid);
|
||||||
|
QString deferrable;
|
||||||
|
QString validated;
|
||||||
|
if (!constraint.validated)
|
||||||
|
validated += " NOT VALID";
|
||||||
|
if (constraint.deferrable) {
|
||||||
|
deferrable = QLatin1String(" DEFERRABLE INITIALLY ") % (constraint.deferred ? "DEFERRED" : "IMMEDIATE");
|
||||||
|
}
|
||||||
|
|
||||||
|
return "REFERENCES "
|
||||||
|
% genFQTableName(catalog, fcls) % " ("
|
||||||
|
% getColumnNameList(catalog, constraint.frelid, constraint.fkey) % ") MATCH "
|
||||||
|
% ForeignKeyMatchToString(constraint.fmatchtype)
|
||||||
|
% " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype)
|
||||||
|
% " ON DELETE " % ForeignKeyActionToString(constraint.fdeltype)
|
||||||
|
% deferrable % validated;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString getForeignKeyConstraintReferencesShort(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
||||||
|
{
|
||||||
|
PgClass fcls = catalog.classes()->getByKey(constraint.frelid);
|
||||||
|
QString deferrable;
|
||||||
|
QString validated;
|
||||||
|
if (!constraint.validated)
|
||||||
|
validated += " NOT VALID";
|
||||||
|
if (constraint.deferrable) {
|
||||||
|
deferrable = QLatin1String(" DEFERRABLE") % (constraint.deferred ? " INITIALLY DEFERRED" : "");
|
||||||
|
}
|
||||||
|
QString on_update = constraint.fupdtype == ForeignKeyAction::NoAction ? QString() : " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype);
|
||||||
|
QString on_delete = constraint.fdeltype == ForeignKeyAction::NoAction ? QString() : " ON DELETE " % ForeignKeyActionToString(constraint.fdeltype);
|
||||||
|
QString match_type = constraint.fmatchtype == ForeignKeyMatch::Simple ? QString() : " MATCH " % ForeignKeyMatchToString(constraint.fmatchtype);
|
||||||
|
|
||||||
|
return genFQTableName(catalog, fcls) % " ("
|
||||||
|
% getColumnNameList(catalog, constraint.frelid, constraint.fkey) % ")"
|
||||||
|
% match_type
|
||||||
|
% on_update
|
||||||
|
% on_delete
|
||||||
|
% deferrable % validated;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,11 @@ QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgCo
|
||||||
QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||||
QString getIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index);
|
QString getIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index);
|
||||||
QString getDropIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index);
|
QString getDropIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index);
|
||||||
|
/// Returns the foreignKey specific part of the constraint definition
|
||||||
QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||||
|
/// Returns the REFERENCES construct as used directly after a column in the create table statement
|
||||||
|
QString getForeignKeyConstraintReferences(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||||
|
/// Same as above but shortened as much as possible by leaving out defaults
|
||||||
|
QString getForeignKeyConstraintReferencesShort(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||||
|
|
||||||
#endif // SQLFORMATTINGUTILS_H
|
#endif // SQLFORMATTINGUTILS_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue