pgLab/pglablib/PgConstraintContainer.cpp

89 lines
2.8 KiB
C++
Raw Normal View History

#include "PgConstraintContainer.h"
#include "Pgsql_Col.h"
#include <algorithm>
std::string PgConstraintContainer::getLoadQuery() const
{
std::string q = R"__(
SELECT oid, conname, connamespace, contype, condeferrable,
condeferred, convalidated, conrelid, contypid, conindid,
confrelid, confupdtype, confdeltype, confmatchtype,
conislocal, coninhcount, connoinherit, conkey, confkey,
conpfeqop, conppeqop, conffeqop, conexclop, conbin, consrc,
pg_get_constraintdef(oid)
FROM pg_constraint)__";
// auto cat = m_catalogue.lock();
// if (cat && cat->serverVersion() >= 90400)
// q += ", indisreplident ";
// q += "\nFROM pg_index";
return q;
}
//void operator<<(std::vector<int16_t> &s, const Pgsql::Value &v)
//{
//}
//void operator<<(std::vector<Oid> &s, const Pgsql::Value &v)
//{
//}
PgConstraint PgConstraintContainer::loadElem(const Pgsql::Row &row)
{
using namespace Pgsql;
Col col(row);
PgConstraint v;
col >> v.oid >> v.name >> v.connamespace >> v.type >> v.deferrable
>> v.deferred >> v.validated >> v.relid >> v.typid >> v.indid
>> v.frelid >> v.fupdtype >> v.fdeltype >> v.fmatchtype
>> v.islocal >> v.inhcount >> v.noinherit;
col.getAsArray<int16_t>(std::back_inserter(v.key), NullHandling::Ignore);
col.getAsArray<int16_t>(std::back_inserter(v.fkey), NullHandling::Ignore);
col.getAsArray<Oid>(std::back_inserter(v.pfeqop), NullHandling::Ignore);
col.getAsArray<Oid>(std::back_inserter(v.ppeqop), NullHandling::Ignore);
col.getAsArray<Oid>(std::back_inserter(v.ffeqop), NullHandling::Ignore);
col.getAsArray<Oid>(std::back_inserter(v.exclop), NullHandling::Ignore);
col >> v.bin >> v.src >> v.definition;
return v;
}
std::vector<PgConstraint> PgConstraintContainer::getFKeyForTableColumn(Oid relid, int16_t attnum) const
{
//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::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;
}
std::vector<PgConstraint> PgConstraintContainer::getConstraintsForRelation(Oid relid) const
{
std::vector<PgConstraint> result;
for (const auto &e : m_container)
if (e.relid == relid)
result.push_back(e);
return result;
}
2018-01-08 20:45:52 +01:00
std::optional<PgConstraint> PgConstraintContainer::getPrimaryForRelation(Oid relid) const
2018-01-08 20:45:52 +01:00
{
std::optional<PgConstraint> result;
2018-01-08 20:45:52 +01:00
for (const auto &e : m_container) {
if (e.relid == relid && e.type == ConstraintType::PrimaryKey) {
result = e;
break;
}
}
return result;
}