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
|
|
@ -52,16 +52,17 @@ PgConstraint PgConstraintContainer::loadElem(const Pgsql::Row &row)
|
|||
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.
|
||||
// auto res = std::find_if(m_container.begin(), m_container.end(),
|
||||
// [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.
|
||||
// return relid == c.relid &&
|
||||
// (std::find(attnum.begin(), attnum.end(), v.key) != attnum.end());
|
||||
// });
|
||||
auto res = std::copy_if(m_container.begin(), m_container.end(), std::back_inserter(result),
|
||||
[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.
|
||||
return c.type == ConstraintType::ForeignKey && relid == c.relid &&
|
||||
(std::find(c.key.begin(), c.key.end(), attnum) != c.key.end());
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ public:
|
|||
|
||||
virtual std::string getLoadQuery() const override;
|
||||
//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::optional<PgConstraint> getPrimaryForRelation(Oid relid) const;
|
||||
|
|
|
|||
|
|
@ -126,7 +126,50 @@ QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, cons
|
|||
% " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype)
|
||||
% " ON DELETE " % ForeignKeyActionToString(constraint.fdeltype)
|
||||
% 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 getIndexDefinition(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);
|
||||
/// 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue