Tab with list of sequences and create sql for selected sequence.
This commit is contained in:
parent
769307c821
commit
437736a023
19 changed files with 507 additions and 63 deletions
|
|
@ -12,7 +12,8 @@ std::string PgClassContainer::getLoadQuery() const
|
|||
" reltuples, reltoastrelid, relisshared, relpersistence, "
|
||||
" relkind, relhasoids, relispopulated, relfrozenxid, relminmxid, "
|
||||
" reloptions, relacl \n"
|
||||
"FROM pg_catalog.pg_class";
|
||||
"FROM pg_catalog.pg_class \n"
|
||||
"WHERE relkind IN ('r', 'i', 'p', 'I')";
|
||||
}
|
||||
|
||||
PgClass PgClassContainer::loadElem(const Pgsql::Row &row)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "PgContainer.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
|
||||
IPgContainer::IPgContainer(PgDatabaseCatalog& cat)
|
||||
: m_catalog(cat)
|
||||
|
|
@ -9,3 +10,26 @@ bool IPgContainer::minimumVersion(int required_version) const
|
|||
{
|
||||
return m_catalog.serverVersion() >= required_version;
|
||||
}
|
||||
|
||||
bool IPgContainer::lessThenVersion(int required_version) const
|
||||
{
|
||||
return m_catalog.serverVersion() < required_version;
|
||||
}
|
||||
|
||||
void IPgContainer::loadAll(Pgsql::Connection &conn)
|
||||
{
|
||||
std::string q = getLoadQuery();
|
||||
Pgsql::Result result = conn.query(q.c_str());
|
||||
if (result && result.resultStatus() == PGRES_TUPLES_OK) {
|
||||
load(result);
|
||||
}
|
||||
else {
|
||||
auto details = result.diagDetails();
|
||||
if (details.state == "42501") { // permission denied
|
||||
// ignore this for now
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Query failed\n" + details.errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,8 +18,11 @@ public:
|
|||
|
||||
virtual std::string getLoadQuery() const = 0;
|
||||
virtual void load(const Pgsql::Result &res) = 0;
|
||||
virtual void loadAll(Pgsql::Connection &conn);
|
||||
|
||||
bool minimumVersion(int required_version) const;
|
||||
bool lessThenVersion(int required_version) const;
|
||||
|
||||
protected:
|
||||
PgDatabaseCatalog& m_catalog;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
#include "PgCollationContainer.h"
|
||||
#include "PgInheritsContainer.h"
|
||||
#include "PgLanguageContainer.h"
|
||||
#include "PgSequenceContainer.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
#include "Pgsql_oids.h"
|
||||
|
||||
|
|
@ -137,56 +138,69 @@ void PgDatabaseCatalog::loadAll(Pgsql::Connection &conn,
|
|||
loadInfo(conn);
|
||||
const int count = 12;
|
||||
int n = 0;
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
|
||||
auto pf = [&] () -> bool {
|
||||
return progress_callback && !progress_callback(++n, count);
|
||||
};
|
||||
|
||||
if (pf()) return;
|
||||
|
||||
// First load server objects
|
||||
load2(m_authIds, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_tablespaces, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_databases, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
|
||||
// Load database objects
|
||||
load2(m_languages, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_namespaces, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_collations, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_classes, conn); // needs namespaces
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_attributes, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_constraints, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_indexes, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_ams, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_triggers, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_types, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_procs, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_inherits, conn);
|
||||
progress_callback && progress_callback(++n, count);
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_sequences, conn);
|
||||
// {
|
||||
// if (!m_sequences)
|
||||
// m_sequences = std::make_shared<PgSequenceContainer>(*this);
|
||||
// m_sequences->load(conn);
|
||||
|
||||
// }
|
||||
pf();
|
||||
|
||||
refreshed(this, All);
|
||||
}
|
||||
|
|
@ -206,27 +220,6 @@ void PgDatabaseCatalog::loadInfo(Pgsql::Connection &conn)
|
|||
m_dbName = conn.getDBName();
|
||||
}
|
||||
|
||||
void load(Pgsql::Connection &conn, IPgContainer &pg_cont)
|
||||
{
|
||||
//QThread::msleep(400);
|
||||
std::string q = pg_cont.getLoadQuery();
|
||||
Pgsql::Result result = conn.query(q.c_str());
|
||||
if (result && result.resultStatus() == PGRES_TUPLES_OK) {
|
||||
//boost::timer::auto_cpu_timer t;
|
||||
pg_cont.load(result);
|
||||
}
|
||||
else {
|
||||
auto details = result.diagDetails();
|
||||
if (details.state == "42501") { // permission denied
|
||||
// ignore this for now
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Query failed\n" + details.errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const QString& PgDatabaseCatalog::serverVersionString() const
|
||||
{
|
||||
return m_serverVersionString;
|
||||
|
|
@ -311,3 +304,8 @@ std::shared_ptr<const PgLanguageContainer> PgDatabaseCatalog::languages() const
|
|||
{
|
||||
return m_languages;
|
||||
}
|
||||
|
||||
std::shared_ptr<const PgSequenceContainer> PgDatabaseCatalog::sequences() const
|
||||
{
|
||||
return m_sequences;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ class PgProcContainer;
|
|||
class PgCollationContainer;
|
||||
class PgInheritsContainer;
|
||||
class PgLanguageContainer;
|
||||
class PgSequenceContainer;
|
||||
|
||||
class PgDatabaseCatalog: public QObject, public std::enable_shared_from_this<PgDatabaseCatalog> {
|
||||
Q_OBJECT
|
||||
|
|
@ -63,6 +64,7 @@ public:
|
|||
std::shared_ptr<const PgCollationContainer> collations() const;
|
||||
std::shared_ptr<const PgInheritsContainer> inherits() const;
|
||||
std::shared_ptr<const PgLanguageContainer> languages() const;
|
||||
std::shared_ptr<const PgSequenceContainer> sequences() const;
|
||||
|
||||
enum RefreshFlag {
|
||||
Attributes = 1,
|
||||
|
|
@ -103,6 +105,7 @@ private:
|
|||
std::shared_ptr<PgCollationContainer> m_collations;
|
||||
std::shared_ptr<PgInheritsContainer> m_inherits;
|
||||
std::shared_ptr<PgLanguageContainer> m_languages;
|
||||
std::shared_ptr<PgSequenceContainer> m_sequences;
|
||||
|
||||
template <typename T>
|
||||
void load2(std::shared_ptr<T> &ptr, Pgsql::Connection &conn)
|
||||
|
|
@ -110,7 +113,7 @@ private:
|
|||
if (!ptr)
|
||||
ptr = std::make_shared<T>(*this);
|
||||
|
||||
load(conn, *ptr);
|
||||
ptr->loadAll(conn);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
23
pglablib/catalog/PgSequence.cpp
Normal file
23
pglablib/catalog/PgSequence.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "PgSequence.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "PgTypeContainer.h"
|
||||
#include <QStringBuilder>
|
||||
|
||||
QString PgSequence::createSql() const
|
||||
{
|
||||
QString sql;
|
||||
sql = "CREATE";
|
||||
// [ TEMPORARY | TEMP ]
|
||||
sql += " SEQUENCE " % fullyQualifiedQuotedObjectName();
|
||||
if (catalog().serverVersion() >= 100000)
|
||||
sql += "\n AS " % catalog().types()->getByKey(typid)->objectName();
|
||||
sql += QString("\n INCREMENT %1\n MINVALUE %2"
|
||||
" MAXVALUE %3\n START %4 CACHE %5")
|
||||
.arg(increment).arg(min).arg(max).arg(start).arg(cache);
|
||||
if (cycled)
|
||||
sql += "\n CYCLE";
|
||||
|
||||
// [ OWNED BY { table_name.column_name | NONE } ]
|
||||
sql += ";";
|
||||
return sql;
|
||||
}
|
||||
23
pglablib/catalog/PgSequence.h
Normal file
23
pglablib/catalog/PgSequence.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef PGSEQUENCE_H
|
||||
#define PGSEQUENCE_H
|
||||
|
||||
#include "catalog/PgClass.h"
|
||||
|
||||
class PgSequence: public PgClass {
|
||||
public:
|
||||
Oid typid = InvalidOid;
|
||||
int64_t last = 0;
|
||||
int64_t start = 0;
|
||||
int64_t increment = 0;
|
||||
int64_t max = 0;
|
||||
int64_t min = 0;
|
||||
int64_t cache = 0;
|
||||
int64_t log = 0;
|
||||
bool cycled = false;
|
||||
bool called = false;
|
||||
|
||||
using PgClass::PgClass;
|
||||
QString createSql() const;
|
||||
};
|
||||
|
||||
#endif // PGSEQUENCE_H
|
||||
83
pglablib/catalog/PgSequenceContainer.cpp
Normal file
83
pglablib/catalog/PgSequenceContainer.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#include "PgSequenceContainer.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
#include "Pgsql_Col.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "PgNamespaceContainer.h"
|
||||
#include <iterator>
|
||||
|
||||
std::string PgSequenceContainer::getLoadQuery() const
|
||||
{
|
||||
std::string select = "SELECT pg_class.oid, relname, relnamespace, reltype, reloftype, "
|
||||
" relowner, relam, relfilenode, reltablespace, relpages, "
|
||||
" reltuples, reltoastrelid, relisshared, relpersistence, "
|
||||
" relkind, relhasoids, relispopulated, relfrozenxid, relminmxid, "
|
||||
" reloptions, relacl \n";
|
||||
std::string from =
|
||||
"FROM pg_catalog.pg_class \n";
|
||||
|
||||
// Starting with pg10 constanst data of sequences is retrieved from pg_sequence
|
||||
// instead of SELECTing the sequence
|
||||
if (minimumVersion(100000)) {
|
||||
select +=
|
||||
" , seqtypid, seqstart, seqincrement, seqmax, \n"
|
||||
" seqmin, seqcache, seqcycle \n";
|
||||
from +=
|
||||
" JOIN pg_catalog.pg_sequence ON (pg_class.oid=seqrelid) \n";
|
||||
}
|
||||
|
||||
return select +
|
||||
from +
|
||||
"WHERE relkind='S'";
|
||||
}
|
||||
|
||||
PgSequence PgSequenceContainer::loadElem(const Pgsql::Row &row)
|
||||
{
|
||||
Pgsql::Col col(row);
|
||||
Oid class_oid = col.nextValue();
|
||||
QString name = col.nextValue();
|
||||
Oid schema_oid = col.nextValue();
|
||||
|
||||
PgSequence v(m_catalog, class_oid, name, schema_oid);
|
||||
Oid owner ;
|
||||
AclList acl_list;
|
||||
col >> v.type >> v.oftype
|
||||
>> owner >> v.am >> v.filenode >> v.tablespace >> v.pages_est
|
||||
>> v.tuples_est >> v.toastrelid >> v.isshared >> v.persistence
|
||||
>> v.kind >> v.hasoids >> v.ispopulated >> v.frozenxid >> v.minmxid
|
||||
>> v.options >> acl_list;
|
||||
// Read pg_sequence fields
|
||||
if (minimumVersion(100000))
|
||||
col >> v.typid >> v.start >> v.increment >> v.max >> v.min >> v.cache >> v.cycled;
|
||||
|
||||
v.setOwnerOid(owner);
|
||||
v.setAcls(std::move(acl_list));
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
void PgSequenceContainer::loadAll(Pgsql::Connection &conn)
|
||||
{
|
||||
IPgContainer::loadAll(conn);
|
||||
|
||||
for (auto && seq : m_container) {
|
||||
auto fqn = seq.fullyQualifiedQuotedObjectName().toUtf8();
|
||||
// SELECTs on a sequence return less fields starting with pg10
|
||||
// the missing fields are now in pg_sequence
|
||||
std::string q("SELECT last_value, log_cnt, is_called");
|
||||
if (lessThenVersion(100000)) {
|
||||
q += ", start_value, increment_by, max_value, \n"
|
||||
" min_value, cache_value, is_cycled";
|
||||
}
|
||||
q += "\nFROM " + std::string(fqn.data(), static_cast<size_t>(fqn.count()));
|
||||
auto && res = conn.query(q.c_str());
|
||||
if (res.rows() == 1) {
|
||||
Pgsql::Row row(res, 0);
|
||||
Pgsql::Col col(row);
|
||||
col >> seq.last >> seq.log >> seq.called;
|
||||
if (lessThenVersion(100000)) {
|
||||
col >> seq.start >> seq.increment >> seq.max >> seq.min >> seq.cache >> seq.cycled;
|
||||
seq.typid = Pgsql::int8_oid; // bigint used to be the only choice
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
25
pglablib/catalog/PgSequenceContainer.h
Normal file
25
pglablib/catalog/PgSequenceContainer.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef PGSEQUENCECONTAINER_H
|
||||
#define PGSEQUENCECONTAINER_H
|
||||
|
||||
#include "PgContainer.h"
|
||||
#include "PgSequence.h"
|
||||
|
||||
namespace Pgsql {
|
||||
|
||||
class Result;
|
||||
|
||||
}
|
||||
|
||||
class PgSequenceContainer: public PgContainer<PgSequence> {
|
||||
public:
|
||||
using PgContainer<PgSequence>::PgContainer;
|
||||
|
||||
std::string getLoadQuery() const override;
|
||||
|
||||
void loadAll(Pgsql::Connection &conn) override;
|
||||
protected:
|
||||
PgSequence loadElem(const Pgsql::Row &row) override;
|
||||
|
||||
};
|
||||
|
||||
#endif // PGSEQUENCECONTAINER_H
|
||||
|
|
@ -82,7 +82,9 @@ SOURCES += \
|
|||
model/CollationModelFactory.cpp \
|
||||
catalog/PgLanguageContainer.cpp \
|
||||
catalog/PgLanguage.cpp \
|
||||
catalog/PgAcl.cpp
|
||||
catalog/PgAcl.cpp \
|
||||
catalog/PgSequence.cpp \
|
||||
catalog/PgSequenceContainer.cpp
|
||||
|
||||
HEADERS += \
|
||||
Pglablib.h \
|
||||
|
|
@ -149,7 +151,9 @@ HEADERS += \
|
|||
model/CollationModelFactory.h \
|
||||
catalog/PgLanguageContainer.h \
|
||||
catalog/PgLanguage.h \
|
||||
catalog/PgAcl.h
|
||||
catalog/PgAcl.h \
|
||||
catalog/PgSequence.h \
|
||||
catalog/PgSequenceContainer.h
|
||||
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue