The create table statement now lists the inherited tables and inherited columns are commented out.

This commit is contained in:
eelke 2018-12-03 21:03:49 +01:00
parent 498233d58c
commit 6c76c70a97
11 changed files with 118 additions and 6 deletions

View file

@ -24,6 +24,7 @@ public:
bool hasdef = false; bool hasdef = false;
char identity = ' '; char identity = ' ';
bool isdropped = false; bool isdropped = false;
bool islocal = true;
Oid collation = InvalidOid; Oid collation = InvalidOid;
QString acl; QString acl;
QString options; QString options;

View file

@ -12,7 +12,7 @@ std::string PgAttributeContainer::getLoadQuery() const
std::string q = R"__( std::string q = R"__(
SELECT attrelid, attname, atttypid, attstattarget, SELECT attrelid, attname, atttypid, attstattarget,
attnum, attndims, atttypmod, attnotnull, atthasdef, attisdropped, attnum, attndims, atttypmod, attnotnull, atthasdef, attisdropped,
attcollation, attacl, attoptions, pg_get_expr(adbin, adrelid) AS def_value)__"; attislocal, attcollation, attacl, attoptions, pg_get_expr(adbin, adrelid) AS def_value)__";
if (m_catalog.serverVersion() >= 100000) if (m_catalog.serverVersion() >= 100000)
q += ", attidentity"; q += ", attidentity";
q += q +=
@ -27,7 +27,7 @@ PgAttribute PgAttributeContainer::loadElem(const Pgsql::Row &row)
PgAttribute v; PgAttribute v;
col >> v.relid >> v.name >> v.typid >> v.stattarget col >> v.relid >> v.name >> v.typid >> v.stattarget
>> v.num >> v.ndims >> v.typmod >> v.notnull >> v.hasdef >> v.isdropped >> v.num >> v.ndims >> v.typmod >> v.notnull >> v.hasdef >> v.isdropped
>> v.collation >> v.acl >> v.options >> v.defaultValue; >> v.islocal >> v.collation >> v.acl >> v.options >> v.defaultValue;
if (m_catalog.serverVersion() >= 100000) if (m_catalog.serverVersion() >= 100000)
col >> v.identity; col >> v.identity;

View file

@ -1,7 +1,9 @@
#include "PgClass.h" #include "PgClass.h"
#include "PgAttributeContainer.h" #include "PgAttributeContainer.h"
#include "PgClassContainer.h"
#include "PgDatabaseCatalog.h" #include "PgDatabaseCatalog.h"
#include "PgConstraintContainer.h" #include "PgConstraintContainer.h"
#include "PgInheritsContainer.h"
#include <QStringBuilder> #include <QStringBuilder>
#include "SqlFormattingUtils.h" #include "SqlFormattingUtils.h"
@ -93,6 +95,7 @@ QString PgClass::createTableSql() const
first = false; first = false;
} }
else sql += ",\n "; else sql += ",\n ";
if (!col.islocal) sql += "-- ";
sql += col.columnDefinition(catalog()); sql += col.columnDefinition(catalog());
} }
// { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ] // { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
@ -110,7 +113,20 @@ QString PgClass::createTableSql() const
} }
sql += ")"; sql += ")";
{
// [ INHERITS ( parent_table [, ... ] ) ] // [ INHERITS ( parent_table [, ... ] ) ]
auto parents = catalog().inherits()->getParentsOf(oid());
if (!parents.empty()) {
sql += "\nINHERITS (";
bool first = true;
for (auto parent_oid : parents) {
if (first) first = false;
else sql += ", ";
sql += catalog().classes()->getByKey(parent_oid)->fullyQualifiedQuotedObjectName();
}
sql += ")";
}
}
// [ PARTITION BY { RANGE | LIST } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ] // [ PARTITION BY { RANGE | LIST } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]
// [ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ] // [ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]
// [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ] // [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]

View file

@ -14,6 +14,7 @@
#include "PgTypeContainer.h" #include "PgTypeContainer.h"
#include "PgProcContainer.h" #include "PgProcContainer.h"
#include "PgCollationContainer.h" #include "PgCollationContainer.h"
#include "PgInheritsContainer.h"
#include "Pgsql_Connection.h" #include "Pgsql_Connection.h"
#include "Pgsql_oids.h" #include "Pgsql_oids.h"
@ -178,6 +179,9 @@ void PgDatabaseCatalog::loadAll(Pgsql::Connection &conn,
if (progress_callback && !progress_callback(++n, count)) if (progress_callback && !progress_callback(++n, count))
return; return;
load2(m_procs, conn); load2(m_procs, conn);
if (progress_callback && !progress_callback(++n, count))
return;
load2(m_inherits, conn);
progress_callback && progress_callback(++n, count); progress_callback && progress_callback(++n, count);
refreshed(this, All); refreshed(this, All);
@ -293,3 +297,8 @@ std::shared_ptr<const PgCollationContainer> PgDatabaseCatalog::collations() cons
{ {
return m_collations; return m_collations;
} }
std::shared_ptr<const PgInheritsContainer> PgDatabaseCatalog::inherits() const
{
return m_inherits;
}

View file

@ -28,6 +28,7 @@ class PgTriggerContainer;
class PgTypeContainer; class PgTypeContainer;
class PgProcContainer; class PgProcContainer;
class PgCollationContainer; class PgCollationContainer;
class PgInheritsContainer;
class PgDatabaseCatalog: public QObject, public std::enable_shared_from_this<PgDatabaseCatalog> { class PgDatabaseCatalog: public QObject, public std::enable_shared_from_this<PgDatabaseCatalog> {
@ -60,6 +61,7 @@ public:
std::shared_ptr<const PgTypeContainer> types() const; std::shared_ptr<const PgTypeContainer> types() const;
std::shared_ptr<const PgProcContainer> procs() const; std::shared_ptr<const PgProcContainer> procs() const;
std::shared_ptr<const PgCollationContainer> collations() const; std::shared_ptr<const PgCollationContainer> collations() const;
std::shared_ptr<const PgInheritsContainer> inherits() const;
enum RefreshFlag { enum RefreshFlag {
Attributes = 1, Attributes = 1,
@ -98,6 +100,7 @@ private:
std::shared_ptr<PgTypeContainer> m_types; std::shared_ptr<PgTypeContainer> m_types;
std::shared_ptr<PgProcContainer> m_procs; std::shared_ptr<PgProcContainer> m_procs;
std::shared_ptr<PgCollationContainer> m_collations; std::shared_ptr<PgCollationContainer> m_collations;
std::shared_ptr<PgInheritsContainer> m_inherits;
template <typename T> template <typename T>
void load2(std::shared_ptr<T> &ptr, Pgsql::Connection &conn) void load2(std::shared_ptr<T> &ptr, Pgsql::Connection &conn)

3
pglablib/PgInherits.cpp Normal file
View file

@ -0,0 +1,3 @@
#include "PgInherits.h"
PgInherits::PgInherits() = default;

25
pglablib/PgInherits.h Normal file
View file

@ -0,0 +1,25 @@
#ifndef PGINHERITS_H
#define PGINHERITS_H
#include "Pgsql_declare.h"
#include <QString>
#include <vector>
#include <tuple>
class PgInherits {
public:
using Key = std::tuple<Oid, int32_t>;
Oid relid = InvalidOid; // oid
Oid parent = InvalidOid; // oid
int32_t seqno = 0; // integer
PgInherits();
bool operator==(Key _k) const { return relid == std::get<0>(_k) && seqno == std::get<1>(_k); }
bool operator<(Key _k) const { return relid < std::get<0>(_k) || (relid == std::get<0>(_k) && seqno < std::get<1>(_k)); }
bool operator<(const PgInherits &rhs) const { return relid < rhs.relid || (relid == rhs.relid && seqno < rhs.seqno); }
};
#endif // PGINHERITS_H

View file

@ -0,0 +1,29 @@
#include "PgInheritsContainer.h"
#include "Pgsql_Col.h"
#include "PgDatabaseCatalog.h"
#include <algorithm>
std::vector<Oid> PgInheritsContainer::getParentsOf(Oid oid) const
{
std::vector<Oid> result;
auto&& iter = std::lower_bound(m_container.begin(), m_container.end(), PgInherits::Key{ oid, 1 });
for (;iter != m_container.end() && iter->relid == oid; ++iter)
result.push_back(iter->parent);
return result;
}
std::string PgInheritsContainer::getLoadQuery() const
{
return
"SELECT inhrelid, inhparent, inhseqno \n"
" FROM pg_inherits";
}
PgInherits PgInheritsContainer::loadElem(const Pgsql::Row &row)
{
Pgsql::Col col(row);
PgInherits v;
col >> v.relid >> v.parent >> v.seqno;
return v;
}

View file

@ -0,0 +1,22 @@
#ifndef PGINHERITSCONTAINER_H
#define PGINHERITSCONTAINER_H
#include "PgContainer.h"
#include "PgInherits.h"
#include "Pgsql_declare.h"
class PgInheritsContainer : public PgContainer<PgInherits, PgInherits::Key> {
public:
using PgContainer<PgInherits, PgInherits::Key>::PgContainer;
virtual std::string getLoadQuery() const override;
/// Returns the parents in the correct order
std::vector<Oid> getParentsOf(Oid oid) const;
protected:
PgInherits loadElem(const Pgsql::Row &row) override;
};
#endif // PGINHERITSCONTAINER_H

View file

@ -317,7 +317,7 @@ QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, cons
% ForeignKeyMatchToString(constraint.fmatchtype) % ForeignKeyMatchToString(constraint.fmatchtype)
% " 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) QString getForeignKeyConstraintReferences(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)

View file

@ -73,7 +73,9 @@ codebuilder/StructureTemplate.cpp \
PgOwnedObject.cpp \ PgOwnedObject.cpp \
PgNamespaceObject.cpp \ PgNamespaceObject.cpp \
PgCollation.cpp \ PgCollation.cpp \
PgCollationContainer.cpp PgCollationContainer.cpp \
PgInherits.cpp \
PgInheritsContainer.cpp
HEADERS += \ HEADERS += \
Pglablib.h \ Pglablib.h \
@ -129,7 +131,9 @@ codebuilder/StructureTemplate.h \
PgOwnedObject.h \ PgOwnedObject.h \
PgNamespaceObject.h \ PgNamespaceObject.h \
PgCollation.h \ PgCollation.h \
PgCollationContainer.h PgCollationContainer.h \
PgInherits.h \
PgInheritsContainer.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib