2017-12-12 20:13:53 +01:00
|
|
|
|
#include "PgAttributeContainer.h"
|
|
|
|
|
|
#include "Pgsql_Col.h"
|
2018-11-29 20:21:36 +01:00
|
|
|
|
#include "PgDatabaseCatalog.h"
|
2017-12-12 20:13:53 +01:00
|
|
|
|
|
2017-12-19 18:57:05 +01:00
|
|
|
|
//SELECT attname, pg_get_expr(adbin, adrelid) AS def_value
|
|
|
|
|
|
//FROM pg_attribute
|
|
|
|
|
|
// JOIN pg_attrdef ON attrelid=adrelid AND attnum=adnum
|
|
|
|
|
|
//WHERE atthasdef=true
|
|
|
|
|
|
|
2017-12-12 20:13:53 +01:00
|
|
|
|
std::string PgAttributeContainer::getLoadQuery() const
|
|
|
|
|
|
{
|
2018-11-29 20:21:36 +01:00
|
|
|
|
std::string q = R"__(
|
2017-12-19 18:57:05 +01:00
|
|
|
|
SELECT attrelid, attname, atttypid, attstattarget,
|
2018-11-18 20:24:27 +01:00
|
|
|
|
attnum, attndims, atttypmod, attnotnull, atthasdef, attisdropped,
|
2021-03-08 17:23:34 +01:00
|
|
|
|
attislocal, attcollation, attacl, attoptions, pg_get_expr(def.adbin, def.adrelid) AS def_value,
|
|
|
|
|
|
cs.relname AS sername, ns.nspname AS serschema, d.description)__";
|
2018-11-29 20:21:36 +01:00
|
|
|
|
if (m_catalog.serverVersion() >= 100000)
|
|
|
|
|
|
q += ", attidentity";
|
2022-09-06 11:17:18 +00:00
|
|
|
|
if (m_catalog.serverVersion() >= 120000)
|
|
|
|
|
|
q += ", attgenerated";
|
|
|
|
|
|
q += R"__(
|
2019-12-01 06:40:11 +01:00
|
|
|
|
FROM pg_catalog.pg_attribute AS att
|
|
|
|
|
|
LEFT JOIN pg_attrdef AS def ON attrelid=adrelid AND attnum=adnum
|
|
|
|
|
|
LEFT JOIN (pg_depend JOIN pg_class cs ON classid='pg_class'::regclass AND objid=cs.oid AND cs.relkind='S') ON refobjid=att.attrelid AND refobjsubid=att.attnum
|
2021-03-08 17:23:34 +01:00
|
|
|
|
LEFT JOIN pg_namespace ns ON ns.oid=cs.relnamespace
|
|
|
|
|
|
LEFT JOIN pg_catalog.pg_description AS d ON (objoid=attrelid AND d.objsubid=attnum))__";
|
2019-12-01 06:40:11 +01:00
|
|
|
|
|
2018-11-29 20:21:36 +01:00
|
|
|
|
return q;
|
2017-12-12 20:13:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PgAttribute PgAttributeContainer::loadElem(const Pgsql::Row &row)
|
|
|
|
|
|
{
|
|
|
|
|
|
Pgsql::Col col(row);
|
|
|
|
|
|
PgAttribute v;
|
|
|
|
|
|
col >> v.relid >> v.name >> v.typid >> v.stattarget
|
2018-11-18 20:24:27 +01:00
|
|
|
|
>> v.num >> v.ndims >> v.typmod >> v.notnull >> v.hasdef >> v.isdropped
|
2019-12-01 06:40:11 +01:00
|
|
|
|
>> v.islocal >> v.collation >> v.acl >> v.options >> v.defaultValue
|
2021-03-08 17:23:34 +01:00
|
|
|
|
>> v.sername >> v.serschema >> v.description;
|
2018-11-29 20:21:36 +01:00
|
|
|
|
if (m_catalog.serverVersion() >= 100000)
|
|
|
|
|
|
col >> v.identity;
|
2022-09-06 11:17:18 +00:00
|
|
|
|
if (m_catalog.serverVersion() >= 120000)
|
|
|
|
|
|
col >> v.generated;
|
2018-11-29 20:21:36 +01:00
|
|
|
|
|
2017-12-12 20:13:53 +01:00
|
|
|
|
return v;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<PgAttribute> PgAttributeContainer::getColumnsForRelation(Oid oid) const
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<PgAttribute> result;
|
|
|
|
|
|
for (const auto &e : m_container)
|
|
|
|
|
|
if (e.relid == oid)
|
|
|
|
|
|
result.push_back(e);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|