Rework of catalog objects. Several of them are now inheriting from common
base classes that implement common functionality.
This commit is contained in:
parent
840af1e0a9
commit
73c4cf4790
45 changed files with 340 additions and 265 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#include "PgClass.h"
|
||||
|
||||
|
||||
void operator<<(RelPersistence &s, const Pgsql::Value &v)
|
||||
{
|
||||
//s = static_cast<T>(v);
|
||||
|
|
@ -48,3 +49,8 @@ void operator<<(RelKind &s, const Pgsql::Value &v)
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//QString PgClass::objectName() const
|
||||
//{
|
||||
// return name;
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
#define PGCLASS_H
|
||||
|
||||
#include "Pgsql_Value.h"
|
||||
#include "PgNamespaceObject.h"
|
||||
#include "PgOwnedObject.h"
|
||||
#include <QString>
|
||||
#include <libpq-fe.h>
|
||||
|
||||
|
|
@ -27,17 +29,17 @@ enum class RelKind {
|
|||
void operator<<(RelKind &s, const Pgsql::Value &v);
|
||||
|
||||
|
||||
class PgClass {
|
||||
class PgClass: public PgNamespaceObject, public PgOwnedObject {
|
||||
public:
|
||||
|
||||
Oid oid = InvalidOid;
|
||||
QString name;
|
||||
Oid relnamespace = InvalidOid;
|
||||
QString relnamespace_name; // Transient, cached value from relnamespace
|
||||
bool system_namespace = false; // Transient, cached value from relnamespace
|
||||
// Oid oid = InvalidOid;
|
||||
// QString name;
|
||||
// Oid relnamespace = InvalidOid;
|
||||
// QString relnamespace_name; // Transient, cached value from relnamespace
|
||||
// bool system_namespace = false; // Transient, cached value from relnamespace
|
||||
Oid type = InvalidOid;
|
||||
Oid oftype = InvalidOid;
|
||||
Oid owner = InvalidOid;
|
||||
//Oid owner = InvalidOid;
|
||||
Oid am = InvalidOid;
|
||||
Oid filenode = InvalidOid;
|
||||
Oid tablespace = InvalidOid;
|
||||
|
|
@ -54,10 +56,14 @@ public:
|
|||
QString acl;
|
||||
std::vector<QString> options;
|
||||
|
||||
bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
bool operator==(const QString &n) const { return name == n; }
|
||||
bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
bool operator<(const PgClass &rhs) const { return oid < rhs.oid; }
|
||||
using PgNamespaceObject::PgNamespaceObject;
|
||||
|
||||
// virtual QString objectName() const override;
|
||||
|
||||
// bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
// bool operator==(const QString &n) const { return objectName() == n; }
|
||||
// bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
// bool operator<(const PgClass &rhs) const { return oid < rhs.oid; }
|
||||
|
||||
private:
|
||||
};
|
||||
|
|
|
|||
|
|
@ -18,17 +18,22 @@ std::string PgClassContainer::getLoadQuery() const
|
|||
PgClass PgClassContainer::loadElem(const Pgsql::Row &row)
|
||||
{
|
||||
Pgsql::Col col(row);
|
||||
PgClass v;
|
||||
col >> v.oid >> v.name >> v.relnamespace >> v.type >> v.oftype
|
||||
>> v.owner >> v.am >> v.filenode >> v.tablespace >> v.pages_est
|
||||
Oid class_oid = col.nextValue();
|
||||
QString name = col.nextValue();
|
||||
Oid schema_oid = col.nextValue();
|
||||
|
||||
PgClass v(m_catalog, class_oid, name, schema_oid);
|
||||
Oid owner ;
|
||||
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.acl >> v.options;
|
||||
|
||||
auto&& ns = m_catalog.namespaces()->getByKey(v.relnamespace);
|
||||
if (ns) {
|
||||
v.relnamespace_name = ns->name;
|
||||
v.system_namespace = ns->isSystemCatalog();
|
||||
}
|
||||
v.setOwnerOid(m_catalog, owner);
|
||||
// auto&& ns = m_catalog.namespaces()->getByKey(v.relnamespace);
|
||||
// if (ns) {
|
||||
// v.relnamespace_name = ns->objectName();
|
||||
// v.system_namespace = ns->isSystemCatalog();
|
||||
// }
|
||||
return v;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,2 @@
|
|||
#include "PgDatabase.h"
|
||||
#include "PgDatabase.h"
|
||||
|
||||
PgDatabase::PgDatabase()
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
#ifndef PGDATABASE_H
|
||||
#define PGDATABASE_H
|
||||
|
||||
#include "PgServerObject.h"
|
||||
#include <QString>
|
||||
#include <libpq-fe.h>
|
||||
|
||||
class PgDatabase {
|
||||
class PgDatabase: public PgServerObject {
|
||||
public:
|
||||
PgDatabase();
|
||||
|
||||
Oid oid = InvalidOid;
|
||||
QString name;
|
||||
// Oid oid = InvalidOid;
|
||||
// QString name;
|
||||
Oid dba; // owner?
|
||||
int encoding;
|
||||
QString collate;
|
||||
|
|
@ -20,12 +20,14 @@ public:
|
|||
Oid tablespace;
|
||||
QString acl;//"ARRAY";"YES"
|
||||
|
||||
bool isValid() const { return oid != InvalidOid; }
|
||||
using PgServerObject::PgServerObject;
|
||||
|
||||
bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
bool operator==(const QString &n) const { return name == n; }
|
||||
bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
bool operator<(const PgDatabase &rhs) const { return oid < rhs.oid; }
|
||||
bool isValid() const { return oid() != InvalidOid; }
|
||||
|
||||
// bool operator==(Oid _oid) const { return oid() == _oid; }
|
||||
// bool operator==(const QString &n) const { return objectName() == n; }
|
||||
// bool operator<(Oid _oid) const { return oid() < _oid; }
|
||||
// bool operator<(const PgDatabase &rhs) const { return oid() < rhs.oid(); }
|
||||
};
|
||||
|
||||
#endif // PGDATABASE_H
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ QString getNamespaceDisplayString(const PgDatabaseCatalog &cat, Oid oid)
|
|||
auto nss = cat.namespaces();
|
||||
auto ns = nss->getByKey(oid);
|
||||
if (ns)
|
||||
result = ns->name; //QString("ns %1").arg(oid);
|
||||
result = ns->objectName(); //QString("ns %1").arg(oid);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ QString getClassDisplayString(const PgDatabaseCatalog &cat, Oid oid)
|
|||
auto l = cat.classes();
|
||||
auto e = l->getByKey(oid);
|
||||
if (e)
|
||||
result = e->name;
|
||||
result = e->objectName();
|
||||
return result;
|
||||
|
||||
}
|
||||
|
|
@ -146,25 +146,29 @@ void PgDatabaseCatalog::loadAll(Pgsql::Connection &conn,
|
|||
int n = 0;
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_namespaces, conn);
|
||||
|
||||
// First load server objects
|
||||
load2(m_authIds, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_tablespaces, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_databases, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
|
||||
// Load database objects
|
||||
load2(m_namespaces, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_classes, conn); // needs namespaces
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_attributes, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_authIds, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_constraints, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_databases, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
load2(m_indexes, conn);
|
||||
|
|
|
|||
|
|
@ -10,8 +10,11 @@ std::string PgDatabaseContainer::getLoadQuery() const
|
|||
PgDatabase PgDatabaseContainer::loadElem(const Pgsql::Row &row)
|
||||
{
|
||||
Pgsql::Col col(row);
|
||||
PgDatabase v;
|
||||
col >> v.oid >> v.name >> v.dba >> v.encoding >> v.collate >> v.ctype >> v.isTemplate
|
||||
|
||||
Oid oid = col.nextValue();
|
||||
QString name = col.nextValue();
|
||||
PgDatabase v(m_catalog, oid, name);
|
||||
col >> v.dba >> v.encoding >> v.collate >> v.ctype >> v.isTemplate
|
||||
>> v.allowConn >> v.connLimit >> v.tablespace >> v.acl;
|
||||
return v;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,16 +7,11 @@ QString PgIndex::getAm() const
|
|||
{
|
||||
auto&& cat = catalog();
|
||||
QString result;
|
||||
auto idxcls = cat.classes()->getByKey(indexrelid);
|
||||
auto idxcls = cat.classes()->getByKey(oid());
|
||||
if (idxcls) {
|
||||
auto am = cat.ams()->getByKey(idxcls->am);
|
||||
if (am)
|
||||
result = am->name;
|
||||
result = am->name; // objectName();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QString PgIndex::objectName() const
|
||||
{
|
||||
return getAm();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
#ifndef PGINDEX_H
|
||||
#define PGINDEX_H
|
||||
|
||||
#include "PgSchemaObject.h"
|
||||
#include "PgNamespaceObject.h"
|
||||
#include "Pgsql_declare.h"
|
||||
#include <QString>
|
||||
#include <vector>
|
||||
|
||||
class PgIndex : public PgSchemaObject {
|
||||
class PgIndex : public PgNamespaceObject {
|
||||
public:
|
||||
|
||||
Oid indexrelid = InvalidOid; // oid of pg_class for this index
|
||||
// Oid indexrelid = InvalidOid; // oid of pg_class for this index
|
||||
Oid relid = InvalidOid; // oid of table (pg_class) where this is an index on
|
||||
int16_t natts = 0;
|
||||
bool isunique = false;
|
||||
|
|
@ -30,14 +30,13 @@ public:
|
|||
QString pred;
|
||||
QString definition;
|
||||
|
||||
using PgSchemaObject::PgSchemaObject;
|
||||
using PgNamespaceObject::PgNamespaceObject;
|
||||
QString getAm() const;
|
||||
virtual QString objectName() const override;
|
||||
|
||||
bool operator==(Oid _oid) const { return indexrelid == _oid; }
|
||||
//bool operator==(const QString &n) const { return name == n; }
|
||||
bool operator<(Oid _oid) const { return indexrelid < _oid; }
|
||||
bool operator<(const PgIndex &rhs) const { return indexrelid < rhs.indexrelid; }
|
||||
// bool operator==(Oid _oid) const { return indexrelid == _oid; }
|
||||
// //bool operator==(const QString &n) const { return name == n; }
|
||||
// bool operator<(Oid _oid) const { return indexrelid < _oid; }
|
||||
// bool operator<(const PgIndex &rhs) const { return indexrelid < rhs.indexrelid; }
|
||||
};
|
||||
|
||||
#endif // PGINDEX_H
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "PgIndexContainer.h"
|
||||
#include "PgClassContainer.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "Pgsql_Col.h"
|
||||
#include <iterator>
|
||||
|
|
@ -21,8 +22,13 @@ SELECT indexrelid, indrelid, indnatts, indisunique, indisprimary,
|
|||
PgIndex PgIndexContainer::loadElem(const Pgsql::Row &row)
|
||||
{
|
||||
Pgsql::Col col(row);
|
||||
PgIndex v(m_catalog);
|
||||
col >> v.indexrelid >> v.relid >> v.natts >> v.isunique
|
||||
Oid indexrelid = col.nextValue();
|
||||
auto&& cls = m_catalog.classes()->getByKey(indexrelid);
|
||||
auto&& name = cls->objectName();
|
||||
auto&& nsoid = cls->nsOid();
|
||||
|
||||
PgIndex v(m_catalog, indexrelid, name, nsoid);
|
||||
col >> v.relid >> v.natts >> v.isunique
|
||||
>> v.isprimary >> v.isexclusion >> v.immediate >> v.isclustered
|
||||
>> v.isvalid >> v.checkxmin >> v.isready >> v.islive;
|
||||
col.getAsVector<int16_t>(std::back_inserter(v.key));
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
#include "PgNamespace.h"
|
||||
|
||||
PgNamespace::PgNamespace() = default;
|
||||
|
||||
bool PgNamespace::isSystemCatalog() const
|
||||
{
|
||||
return name.startsWith("pg_")
|
||||
|| name == "information_schema";
|
||||
auto&& n = objectName();
|
||||
return n.startsWith("pg_")
|
||||
|| n == "information_schema";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,26 @@
|
|||
#ifndef PGNAMESPACE_H
|
||||
#define PGNAMESPACE_H
|
||||
|
||||
#include "PgDatabaseObject.h"
|
||||
#include <QString>
|
||||
#include <libpq-fe.h>
|
||||
|
||||
class PgNamespace {
|
||||
public:
|
||||
PgNamespace();
|
||||
|
||||
Oid oid = InvalidOid;
|
||||
QString name;
|
||||
/// Object representing a namespace within a database
|
||||
class PgNamespace: public PgDatabaseObject {
|
||||
public:
|
||||
|
||||
// Oid oid = InvalidOid;
|
||||
// QString name;
|
||||
Oid owner = InvalidOid;
|
||||
QString acl;
|
||||
|
||||
bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
bool operator==(const QString &n) const { return name == n; }
|
||||
bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
bool operator<(const PgNamespace &rhs) const { return oid < rhs.oid; }
|
||||
using PgDatabaseObject::PgDatabaseObject;
|
||||
|
||||
// bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
// bool operator==(const QString &n) const { return objectName() == n; }
|
||||
// bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
// bool operator<(const PgNamespace &rhs) const { return oid < rhs.oid; }
|
||||
|
||||
bool isSystemCatalog() const;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -11,8 +11,11 @@ std::string PgNamespaceContainer::getLoadQuery() const
|
|||
PgNamespace PgNamespaceContainer::loadElem(const Pgsql::Row &row)
|
||||
{
|
||||
Pgsql::Col col(row);
|
||||
PgNamespace v;
|
||||
col >> v.oid >> v.name >> v.owner >> v.acl;
|
||||
|
||||
Oid oid = col.nextValue();
|
||||
QString name = col.nextValue();
|
||||
PgNamespace v(m_catalog, oid, name);
|
||||
col >> v.owner >> v.acl;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
|
|
|||
40
pglablib/PgNamespaceObject.cpp
Normal file
40
pglablib/PgNamespaceObject.cpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include "PgNamespaceObject.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "PgNamespace.h"
|
||||
#include "PgNamespaceContainer.h"
|
||||
#include "SqlFormattingUtils.h"
|
||||
|
||||
PgNamespaceObject::PgNamespaceObject(PgDatabaseCatalog& cat, Oid oid, const QString &name, Oid schema_oid)
|
||||
: PgDatabaseObject(cat, oid, name)
|
||||
, m_schemaOid(schema_oid)
|
||||
{}
|
||||
|
||||
Oid PgNamespaceObject::nsOid() const
|
||||
{
|
||||
return m_schemaOid;
|
||||
}
|
||||
|
||||
//void PgSchemaObject::setSchemaOid(Oid oid)
|
||||
//{
|
||||
// m_schemaOid = oid;
|
||||
//}
|
||||
|
||||
QString PgNamespaceObject::nsName() const
|
||||
{
|
||||
return ns().objectName();
|
||||
}
|
||||
|
||||
QString PgNamespaceObject::quotedNsName() const
|
||||
{
|
||||
return quoteIdent(nsName());
|
||||
}
|
||||
|
||||
QString PgNamespaceObject::fullyQualifiedQuotedObjectName() const
|
||||
{
|
||||
return quotedNsName() + "." + quotedObjectName();
|
||||
}
|
||||
|
||||
const PgNamespace& PgNamespaceObject::ns() const
|
||||
{
|
||||
return *catalog().namespaces()->getByKey(m_schemaOid);
|
||||
}
|
||||
|
|
@ -8,13 +8,15 @@
|
|||
class PgNamespace;
|
||||
|
||||
/// Base class for database objects that are part of a specific schema
|
||||
class PgSchemaObject: public PgDatabaseObject {
|
||||
class PgNamespaceObject: public PgDatabaseObject {
|
||||
public:
|
||||
using PgDatabaseObject::PgDatabaseObject;
|
||||
|
||||
Oid schemaOid() const;
|
||||
void setSchemaOid(Oid oid);
|
||||
QString quotedSchemaName() const;
|
||||
PgNamespaceObject(PgDatabaseCatalog& cat, Oid oid, const QString &name, Oid schema_oid);
|
||||
|
||||
Oid nsOid() const;
|
||||
// void setSchemaOid(Oid oid);
|
||||
QString nsName() const;
|
||||
QString quotedNsName() const;
|
||||
/// Returns the schema name and object name with proper quotes
|
||||
QString fullyQualifiedQuotedObjectName() const;
|
||||
|
||||
|
|
@ -1,13 +1,25 @@
|
|||
#include "PgObject.h"
|
||||
#include "SqlFormattingUtils.h"
|
||||
|
||||
PgObject::PgObject(PgDatabaseCatalog& cat)
|
||||
PgObject::PgObject(PgDatabaseCatalog& cat, Oid oid, const QString &name)
|
||||
: m_catalog(&cat)
|
||||
, m_oid(oid)
|
||||
, m_name(name)
|
||||
{}
|
||||
|
||||
PgObject::~PgObject()
|
||||
{}
|
||||
|
||||
Oid PgObject::oid() const
|
||||
{
|
||||
return m_oid;
|
||||
}
|
||||
|
||||
const QString& PgObject::objectName() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
QString PgObject::quotedObjectName() const
|
||||
{
|
||||
return quoteIdent(objectName());
|
||||
|
|
@ -17,3 +29,8 @@ const PgDatabaseCatalog& PgObject::catalog() const
|
|||
{
|
||||
return *m_catalog;
|
||||
}
|
||||
|
||||
void test(PgObject a, PgObject b)
|
||||
{
|
||||
a = b;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,32 @@
|
|||
#ifndef PGOBJECT_H
|
||||
#define PGOBJECT_H
|
||||
|
||||
#include <libpq-fe.h>
|
||||
#include <QString>
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
|
||||
class PgObject {
|
||||
public:
|
||||
explicit PgObject(PgDatabaseCatalog& cat);
|
||||
explicit PgObject(PgDatabaseCatalog& cat, Oid oid, const QString &name);
|
||||
virtual ~PgObject();
|
||||
|
||||
virtual QString objectName() const = 0;
|
||||
Oid oid() const;
|
||||
const QString& objectName() const;
|
||||
/// Default implementation uses objectName and add quotes when needed.
|
||||
virtual QString quotedObjectName() const;
|
||||
|
||||
bool operator==(Oid _oid) const { return m_oid == _oid; }
|
||||
bool operator==(const QString &n) const { return m_name == n; }
|
||||
bool operator<(Oid _oid) const { return m_oid < _oid; }
|
||||
bool operator<(const PgObject &rhs) const { return m_oid < rhs.m_oid; }
|
||||
protected:
|
||||
const PgDatabaseCatalog& catalog() const;
|
||||
|
||||
private:
|
||||
PgDatabaseCatalog* m_catalog;
|
||||
|
||||
Oid m_oid;
|
||||
QString m_name;
|
||||
};
|
||||
|
||||
#endif // PGOBJECT_H
|
||||
|
|
|
|||
25
pglablib/PgOwnedObject.cpp
Normal file
25
pglablib/PgOwnedObject.cpp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include "PgOwnedObject.h"
|
||||
#include "PgAuthId.h"
|
||||
#include "PgAuthIdContainer.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
|
||||
void PgOwnedObject::setOwnerOid(PgDatabaseCatalog& cat, Oid oid)
|
||||
{
|
||||
m_ownerOid = oid;
|
||||
m_owner = cat.authIds()->getByKey(oid);
|
||||
}
|
||||
|
||||
Oid PgOwnedObject::ownerOid() const
|
||||
{
|
||||
return m_ownerOid;
|
||||
}
|
||||
|
||||
QString PgOwnedObject::ownerName() const
|
||||
{
|
||||
return m_owner->name;
|
||||
}
|
||||
|
||||
const PgAuthId* PgOwnedObject::owner() const
|
||||
{
|
||||
return m_owner;
|
||||
}
|
||||
22
pglablib/PgOwnedObject.h
Normal file
22
pglablib/PgOwnedObject.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef PGOWNEDOBJECT_H
|
||||
#define PGOWNEDOBJECT_H
|
||||
|
||||
#include <QString>
|
||||
#include <libpq-fe.h>
|
||||
#include <memory>
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
class PgAuthId;
|
||||
class PgOwnedObject {
|
||||
public:
|
||||
void setOwnerOid(PgDatabaseCatalog& cat, Oid oid);
|
||||
|
||||
Oid ownerOid() const;
|
||||
QString ownerName() const;
|
||||
const PgAuthId* owner() const;
|
||||
private:
|
||||
Oid m_ownerOid = InvalidOid;
|
||||
const PgAuthId * m_owner;
|
||||
};
|
||||
|
||||
#endif // PGOWNEDOBJECT_H
|
||||
|
|
@ -74,11 +74,6 @@ namespace {
|
|||
|
||||
}
|
||||
|
||||
QString PgProc::objectName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
void PgProc::setArgs(
|
||||
std::vector<Oid> argtypes,
|
||||
std::vector<Oid> allargtypes,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef PGPROC_H
|
||||
#define PGPROC_H
|
||||
|
||||
#include "PgSchemaObject.h"
|
||||
#include "PgNamespaceObject.h"
|
||||
#include <QString>
|
||||
#include <libpq-fe.h>
|
||||
#include "Pgsql_Value.h"
|
||||
|
|
@ -24,12 +24,12 @@ public:
|
|||
};
|
||||
|
||||
|
||||
class PgProc: public PgSchemaObject {
|
||||
class PgProc: public PgNamespaceObject {
|
||||
public:
|
||||
using PgSchemaObject::PgSchemaObject;
|
||||
using PgNamespaceObject::PgNamespaceObject;
|
||||
|
||||
Oid oid = InvalidOid; // oid
|
||||
QString name; // name
|
||||
// Oid oid = InvalidOid; // oid
|
||||
// QString name; // name
|
||||
// Oid pronamespace = InvalidOid; // oid, namespace
|
||||
Oid owner = InvalidOid; // oid
|
||||
Oid lang = InvalidOid; // oid
|
||||
|
|
@ -65,12 +65,10 @@ public:
|
|||
);
|
||||
const std::vector<Arg>& args() const;
|
||||
|
||||
bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
bool operator==(const QString &n) const { return name == n; }
|
||||
bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
bool operator<(const PgProc &rhs) const { return oid < rhs.oid; }
|
||||
|
||||
virtual QString objectName() const override;
|
||||
// bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
// bool operator==(const QString &n) const { return name == n; }
|
||||
// bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
// bool operator<(const PgProc &rhs) const { return oid < rhs.oid; }
|
||||
|
||||
QString createSql() const;
|
||||
QString argListWithNames(bool multiline = false) const;
|
||||
|
|
|
|||
|
|
@ -26,23 +26,26 @@ std::string PgProcContainer::getLoadQuery() const
|
|||
PgProc PgProcContainer::loadElem(const Pgsql::Row &row)
|
||||
{
|
||||
Pgsql::Col col(row);
|
||||
PgProc v(m_catalog);
|
||||
Oid namespace_oid;
|
||||
Oid oid = col.nextValue();
|
||||
QString name = col.nextValue();
|
||||
Oid namespace_oid = col.nextValue();
|
||||
|
||||
PgProc v(m_catalog, oid, name, namespace_oid);
|
||||
|
||||
col >> v.owner >> v.lang >> v.cost >> v.rows
|
||||
>> v.variadic >> v.transform >> v.isagg >> v.iswindow >> v.secdef >> v.leakproof
|
||||
>> v.isstrict >> v.retset >> v.provolatile >> v.nargs >> v.nargdefaults
|
||||
>> v.rettype;
|
||||
|
||||
std::vector<Oid> argtypes; // oid[]
|
||||
std::vector<Oid> allargtypes; // oid[]
|
||||
std::vector<char> argmodes; // char[]
|
||||
std::vector<QString> argnames; // text[]
|
||||
std::optional<QString> argdefaults; // pg_node_tree
|
||||
|
||||
col >> v.oid >> v.name >> namespace_oid >> v.owner >> v.lang >> v.cost >> v.rows
|
||||
>> v.variadic >> v.transform >> v.isagg >> v.iswindow >> v.secdef >> v.leakproof
|
||||
>> v.isstrict >> v.retset >> v.provolatile >> v.nargs >> v.nargdefaults
|
||||
>> v.rettype;
|
||||
col.getAsVector<Oid>(std::back_inserter(argtypes));
|
||||
col >> allargtypes >> argmodes >> argnames >> argdefaults
|
||||
>> v.src >> v.bin >> v.config >> v.acl;
|
||||
|
||||
v.setSchemaOid(namespace_oid);
|
||||
v.setArgs(argtypes, allargtypes, argmodes, argnames, argdefaults);
|
||||
|
||||
if (minimumVersion(90500)) {
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
#include "PgSchemaObject.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "PgNamespace.h"
|
||||
#include "PgNamespaceContainer.h"
|
||||
#include "SqlFormattingUtils.h"
|
||||
|
||||
Oid PgSchemaObject::schemaOid() const
|
||||
{
|
||||
return m_schemaOid;
|
||||
}
|
||||
|
||||
void PgSchemaObject::setSchemaOid(Oid oid)
|
||||
{
|
||||
m_schemaOid = oid;
|
||||
}
|
||||
|
||||
QString PgSchemaObject::quotedSchemaName() const
|
||||
{
|
||||
return quoteIdent(ns().name);
|
||||
}
|
||||
|
||||
QString PgSchemaObject::fullyQualifiedQuotedObjectName() const
|
||||
{
|
||||
return quotedSchemaName() + "." + quotedObjectName();
|
||||
}
|
||||
|
||||
const PgNamespace& PgSchemaObject::ns() const
|
||||
{
|
||||
return *catalog().namespaces()->getByKey(m_schemaOid);
|
||||
}
|
||||
|
|
@ -5,16 +5,11 @@
|
|||
#include "SqlFormattingUtils.h"
|
||||
#include <QStringBuilder>
|
||||
|
||||
QString PgTrigger::objectName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
QString PgTrigger::dropSql()
|
||||
{
|
||||
if (m_dropSql.isEmpty()) {
|
||||
auto&& fqtablename = genFQTableName(catalog(), *catalog().classes()->getByKey(relid));
|
||||
m_dropSql = "DROP TRIGGER " % quoteIdent(name)
|
||||
auto&& fqtablename = catalog().classes()->getByKey(relid)->fullyQualifiedQuotedObjectName(); // genFQTableName(catalog(), *catalog().classes()->getByKey(relid));
|
||||
m_dropSql = "DROP TRIGGER " % quotedObjectName()
|
||||
% " ON " % fqtablename % ";";
|
||||
}
|
||||
return m_dropSql;
|
||||
|
|
@ -23,8 +18,8 @@ QString PgTrigger::dropSql()
|
|||
QString PgTrigger::createSql()
|
||||
{
|
||||
if (m_createSql.isEmpty()) {
|
||||
auto&& fqtablename = genFQTableName(catalog(), *catalog().classes()->getByKey(relid));
|
||||
auto&& triggername = quoteIdent(name);
|
||||
auto&& fqtablename = catalog().classes()->getByKey(relid)->fullyQualifiedQuotedObjectName(); //genFQTableName(catalog(), *catalog().classes()->getByKey(relid));
|
||||
auto&& triggername = quotedObjectName();
|
||||
|
||||
if (constraint != InvalidOid)
|
||||
m_createSql += "CREATE CONSTRAINT TRIGGER ";
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
#ifndef PGTRIGGER_H
|
||||
#define PGTRIGGER_H
|
||||
|
||||
#include "PgSchemaObject.h"
|
||||
#include "PgNamespaceObject.h"
|
||||
#include "Pgsql_Value.h"
|
||||
#include <QString>
|
||||
#include <libpq-fe.h>
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
|
||||
class PgTrigger: public PgSchemaObject {
|
||||
class PgTrigger: public PgNamespaceObject {
|
||||
public:
|
||||
Oid oid = InvalidOid;
|
||||
// Oid oid = InvalidOid;
|
||||
Oid relid;
|
||||
QString name;
|
||||
// QString name;
|
||||
Oid foid;
|
||||
int16_t type;
|
||||
char enabled;
|
||||
|
|
@ -29,15 +29,15 @@ public:
|
|||
QString oldtable; // >= 10.0
|
||||
QString newtable; // >= 10.0
|
||||
|
||||
using PgSchemaObject::PgSchemaObject;
|
||||
using PgNamespaceObject::PgNamespaceObject;
|
||||
|
||||
virtual QString objectName() const override;
|
||||
// virtual QString objectName() const override;
|
||||
|
||||
|
||||
bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
bool operator==(const QString &n) const { return name == n; }
|
||||
bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
bool operator<(const PgTrigger &rhs) const { return oid < rhs.oid; }
|
||||
// bool operator==(Oid _oid) const { return oid == _oid; }
|
||||
// bool operator==(const QString &n) const { return name == n; }
|
||||
// bool operator<(Oid _oid) const { return oid < _oid; }
|
||||
// bool operator<(const PgTrigger &rhs) const { return oid < rhs.oid; }
|
||||
|
||||
static constexpr int TriggerTypeRow = (1 << 0);
|
||||
static constexpr int TriggerTypeBefore = (1 << 1);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
std::string PgTriggerContainer::getLoadQuery() const
|
||||
{
|
||||
std::string q =
|
||||
"SELECT oid, tgrelid, tgname, tgfoid, tgtype, tgenabled, tgisinternal, tgconstrrelid, \n"
|
||||
"SELECT oid, tgname, tgrelid, tgfoid, tgtype, tgenabled, tgisinternal, tgconstrrelid, \n"
|
||||
" tgconstrindid, tgconstraint, tgdeferrable, tginitdeferred, tgnargs, tgattr, \n"
|
||||
" tgargs, COALESCE(substring(pg_get_triggerdef(oid), 'WHEN (.*) EXECUTE PROCEDURE'), substring(pg_get_triggerdef(oid), 'WHEN (.*) \\$trigger')) AS whenclause";
|
||||
if (minimumVersion(90600)) {
|
||||
|
|
@ -23,8 +23,10 @@ PgTrigger PgTriggerContainer::loadElem(const Pgsql::Row &row)
|
|||
{
|
||||
|
||||
Pgsql::Col col(row);
|
||||
PgTrigger v(m_catalog);
|
||||
col >> v.oid >> v.relid >> v.name >> v.foid >> v.type >> v.enabled >> v.isinternal >> v.constrrelid
|
||||
Oid oid = col.nextValue();
|
||||
QString name = col.nextValue();
|
||||
PgTrigger v(m_catalog, oid, name, InvalidOid);
|
||||
col >> v.relid >> v.foid >> v.type >> v.enabled >> v.isinternal >> v.constrrelid
|
||||
>> v.constrindid >> v.constraint >> v.deferrable >> v.initdeferred >> v.nargs >> v.attr;
|
||||
const unsigned char * args_bytea = reinterpret_cast<const unsigned char *>(col.nextValue().c_str());
|
||||
if (args_bytea) {
|
||||
|
|
|
|||
|
|
@ -18,5 +18,5 @@ UpdatePtr QueryGeneratorFactory::update(QString ns, QString table_name, QString
|
|||
UpdatePtr QueryGeneratorFactory::update(const PgClass &table_class, QString alias)
|
||||
{
|
||||
//QString nsname = m_catalog->namespaces()->getByKey(table_class.relnamespace_name)
|
||||
return update(table_class.relnamespace_name, table_class.name, alias);
|
||||
return update(table_class.nsName(), table_class.objectName(), alias);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -244,26 +244,26 @@ QString dollarQuoteString(const QString &value)
|
|||
|
||||
|
||||
|
||||
QString genSchemaPrefix(const PgNamespace &ns)
|
||||
{
|
||||
QString str;
|
||||
if (!ns.name.isEmpty()) {
|
||||
str = quoteIdent(ns.name) % QString::fromUtf16(u".");
|
||||
}
|
||||
return str;
|
||||
}
|
||||
//QString genSchemaPrefix(const PgNamespace &ns)
|
||||
//{
|
||||
// QString str;
|
||||
// if (!ns.name.isEmpty()) {
|
||||
// str = quoteIdent(ns.name) % QString::fromUtf16(u".");
|
||||
// }
|
||||
// return str;
|
||||
//}
|
||||
|
||||
|
||||
QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls)
|
||||
{
|
||||
auto ns = catalog.namespaces()->getByKey(cls.relnamespace);
|
||||
|
||||
return genSchemaPrefix(*ns) % quoteIdent(cls.name);
|
||||
}
|
||||
//QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls)
|
||||
//{
|
||||
// auto ns = catalog.namespaces()->getByKey(cls.relnamespace);
|
||||
////cls.fullyQualifiedQuotedObjectName()
|
||||
// return ns->quotedObjectName() % "." % cls.quotedObjectName();
|
||||
//}
|
||||
|
||||
QString genAlterTable(const PgDatabaseCatalog &catalog, const PgClass &cls)
|
||||
{
|
||||
return "ALTER TABLE " % genFQTableName(catalog, cls);
|
||||
return "ALTER TABLE " % cls.fullyQualifiedQuotedObjectName(); // genFQTableName(catalog, cls);
|
||||
}
|
||||
|
||||
QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
||||
|
|
@ -312,7 +312,7 @@ QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, cons
|
|||
|
||||
return "\n FOREIGN KEY ("
|
||||
% getColumnNameList(catalog, constraint.relid, constraint.key) % ")\n REFERENCES "
|
||||
% genFQTableName(catalog, *fcls) % " ("
|
||||
% fcls->fullyQualifiedQuotedObjectName() % " ("
|
||||
% getColumnNameList(catalog, constraint.frelid, constraint.fkey) % ")\n MATCH "
|
||||
% ForeignKeyMatchToString(constraint.fmatchtype)
|
||||
% " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype)
|
||||
|
|
@ -332,7 +332,7 @@ QString getForeignKeyConstraintReferences(const PgDatabaseCatalog &catalog, cons
|
|||
}
|
||||
|
||||
return "REFERENCES "
|
||||
% genFQTableName(catalog, *fcls) % " ("
|
||||
% fcls->fullyQualifiedQuotedObjectName() % " ("
|
||||
% getColumnNameList(catalog, constraint.frelid, constraint.fkey) % ") MATCH "
|
||||
% ForeignKeyMatchToString(constraint.fmatchtype)
|
||||
% " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype)
|
||||
|
|
@ -356,7 +356,7 @@ QString getForeignKeyConstraintReferencesShort(const PgDatabaseCatalog &catalog,
|
|||
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) % " ("
|
||||
return fcls->fullyQualifiedQuotedObjectName() % " ("
|
||||
% getColumnNameList(catalog, constraint.frelid, constraint.fkey) % ")"
|
||||
% match_type
|
||||
% on_update
|
||||
|
|
@ -406,7 +406,7 @@ QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstr
|
|||
return result;
|
||||
}
|
||||
|
||||
QString getIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index)
|
||||
QString getIndexDefinition(const PgDatabaseCatalog &, const PgIndex &index)
|
||||
{
|
||||
// const PgClass *table_class = catalog.classes()->getByKey(index.relid);
|
||||
// const PgClass *index_class = catalog.classes()->getByKey(index.indexrelid);
|
||||
|
|
@ -464,40 +464,8 @@ QString getDropIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &
|
|||
|
||||
QString result;
|
||||
result = "DROP INDEX "
|
||||
% quoteIdent(getIndexDisplayString(catalog, index.indexrelid))
|
||||
% index.fullyQualifiedQuotedObjectName() // quoteIdent(getIndexDisplayString(catalog, index.indexrelid))
|
||||
% ";";
|
||||
// % quoteIdent(index_class.name)
|
||||
// % "\n ON " % genFQTableName(catalog, table_class);
|
||||
//// % "\n USING " % index_class.am lookup in pg_am table
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
wxString sql;
|
||||
|
||||
sql = wxT("(") + GetQuotedFkColumns()
|
||||
+ wxT(")\n REFERENCES ") + GetQuotedSchemaPrefix(GetRefSchema()) + qtIdent(GetReferences())
|
||||
+ wxT(" (") + GetQuotedRefColumns()
|
||||
+ wxT(")");
|
||||
|
||||
if (GetDatabase()->BackendMinimumVersion(7, 4) || GetMatch() == wxT("FULL"))
|
||||
sql += wxT(" MATCH ") + GetMatch();
|
||||
|
||||
sql += wxT("\n ON UPDATE ") + GetOnUpdate()
|
||||
+ wxT(" ON DELETE ") + GetOnDelete();
|
||||
if (GetDeferrable())
|
||||
{
|
||||
sql += wxT(" DEFERRABLE INITIALLY ");
|
||||
if (GetDeferred())
|
||||
sql += wxT("DEFERRED");
|
||||
else
|
||||
sql += wxT("IMMEDIATE");
|
||||
}
|
||||
|
||||
if (GetDatabase()->BackendMinimumVersion(9, 1) && !GetValid())
|
||||
sql += wxT("\n NOT VALID");
|
||||
|
||||
return sql;
|
||||
*/
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ QString quoteIdent(QString ident);
|
|||
QString dollarQuoteString(const QString &value);
|
||||
|
||||
|
||||
QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls);
|
||||
//QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls);
|
||||
QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||
QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||
QString getIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index);
|
||||
|
|
|
|||
|
|
@ -68,9 +68,10 @@ codebuilder/StructureTemplate.cpp \
|
|||
PgTriggerContainer.cpp \
|
||||
PgProc.cpp \
|
||||
PgProcContainer.cpp \
|
||||
PgSchemaObject.cpp \
|
||||
PgDatabaseObject.cpp \
|
||||
PgServerObject.cpp
|
||||
PgServerObject.cpp \
|
||||
PgOwnedObject.cpp \
|
||||
PgNamespaceObject.cpp
|
||||
|
||||
HEADERS += \
|
||||
Pglablib.h \
|
||||
|
|
@ -121,9 +122,10 @@ codebuilder/StructureTemplate.h \
|
|||
PgTriggerContainer.h \
|
||||
PgProc.h \
|
||||
PgProcContainer.h \
|
||||
PgSchemaObject.h \
|
||||
PgDatabaseObject.h \
|
||||
PgServerObject.h
|
||||
PgServerObject.h \
|
||||
PgOwnedObject.h \
|
||||
PgNamespaceObject.h
|
||||
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue