Started with classes for keeping track of the database schema.

Class to represent the contents of pg_type catalog table.
This commit is contained in:
Eelke Klein 2017-01-25 06:52:02 +01:00
parent 202e6e431a
commit 424cbc9e2e
6 changed files with 192 additions and 0 deletions

View file

@ -0,0 +1,6 @@
#include "pgsqldatabasecatalogue.h"
PgsqlDatabaseCatalogue::PgsqlDatabaseCatalogue()
{
}

16
pgsqldatabasecatalogue.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef PGSQLDATABASECATALOGUE_H
#define PGSQLDATABASECATALOGUE_H
#include <vector>
class PgTypeContainer;
class PgsqlDatabaseCatalogue {
public:
PgsqlDatabaseCatalogue();
private:
PgTypeContainer *m_types;
};
#endif // PGSQLDATABASECATALOGUE_H

5
pgtype.cpp Normal file
View file

@ -0,0 +1,5 @@
#include "pgtype.h"
#include "PgsqlConn.h"
PgType::PgType() = default;

49
pgtype.h Normal file
View file

@ -0,0 +1,49 @@
#ifndef PGTYPE_H
#define PGTYPE_H
#include <QString>
#include <pgsql/libpq-fe.h>
class PgType {
public:
PgType();
Oid oid = InvalidOid;
QString typname;//"name";"NO"
Oid typnamespace = InvalidOid;//"oid";"NO"
Oid typowner = InvalidOid;//"oid";"NO"
short typlen = -1;//"smallint";"NO"
bool typbyval = false;//"boolean";"NO"
QString typtype;//""char"";"NO"
QString typcategory;//""char"";"NO"
bool typispreferred = false;//"boolean";"NO"
bool typisdefined = false;//"boolean";"NO"
QString typdelim;//""char"";"NO"
Oid typrelid = InvalidOid;//"oid";"NO"
Oid typelem = InvalidOid;//"oid";"NO"
Oid typarray = InvalidOid;//"oid";"NO"
QString typinput;//regproc";"NO"
QString typoutput;//"regproc";"NO"
QString typreceive;//"regproc";"NO"
QString typsend;//"regproc";"NO"
QString typmodin;//"regproc";"NO"
QString typmodout;//"regproc";"NO"
QString typanalyze;//"regproc";"NO"
QString typalign;//""char"";"NO"
QString typstorage;//""char"";"NO"
bool typnotnull = false;//"boolean";"NO"
Oid typbasetype = InvalidOid;//"oid";"NO"
int typtypmod = -1;//"integer";"NO"
int typndims = 0;//"integer";"NO"
Oid typcollation = InvalidOid;//"oid";"NO"
QString typdefaultbin;//"pg_node_tree";"YES"
QString typdefault;//"text";"YES"
QString typacl;//"ARRAY";"YES"
bool operator==(Oid _oid) const { return oid == _oid; }
bool operator==(const QString &n) const { return typname == n; }
bool operator<(Oid _oid) const { return oid < _oid; }
bool operator<(const PgType &rhs) const { return oid < rhs.oid; }
};
#endif // PGTYPE_H

79
pgtypecontainer.cpp Normal file
View file

@ -0,0 +1,79 @@
#include "pgtypecontainer.h"
#include "PgsqlConn.h"
#include <algorithm>
PgTypeContainer::PgTypeContainer() = default;
const PgType& PgTypeContainer::getTypeByOid(Oid oid) const
{
auto lb_result = std::lower_bound(m_types.begin(), m_types.end(), oid);
if (lb_result != m_types.end() && lb_result->oid == oid)
return *lb_result;
return m_invalidType;
}
const PgType& PgTypeContainer::getTypeByName(const QString &name) const
{
auto find_res = std::find(m_types.begin(), m_types.end(), name);
if (find_res != m_types.end())
return *find_res;
return m_invalidType;
}
std::string PgTypeContainer::getLoadQuery()
{
return
"SELECT oid, typname, typnamespace, typowner, typlen, typbyval, typtype, typcategory, \n"
" typispreferred, typisdefined, typdelim, typrelid, typelem, typarray, typinput, typoutput, \n"
" typreceive, typsend, typmodin, typmodout, typanalyze, typalign, typstorage, typnotnull, \n"
" typbasetype, typtypmod, typndims, typcollation, typdefaultbin, typdefault, typacl \n"
"FROM pg_type";
}
void PgTypeContainer::load(const Pgsql::Result &res)
{
const int n_rows = res.getRows();
m_types.clear();
m_types.reserve(n_rows);
for (auto row : res) {
PgType v;
v.oid = row.get(0); // InvalidOid;
v.typname = row.get(1); //.asQString(); //. operator QString(); // "name";"NO"
v.typnamespace = row.get(2); // InvalidOid;//"oid";"NO"
v.typowner = row.get(3); // InvalidOid;//"oid";"NO"
v.typlen = row.get(4); // -1;//"smallint";"NO"
v.typbyval = row.get(5); // false;//"boolean";"NO"
v.typtype = row.get(6);//""char"";"NO"
v.typcategory = row.get(7);//""char"";"NO"
v.typispreferred = row.get(8); //false;//"boolean";"NO"
v.typisdefined = row.get(9); //false;//"boolean";"NO"
v.typdelim = row.get(10); //""char"";"NO"
v.typrelid = row.get(11); // InvalidOid;//"oid";"NO"
v.typelem = row.get(12); // InvalidOid;//"oid";"NO"
v.typarray = row.get(13); // InvalidOid;//"oid";"NO"
v.typinput = row.get(14).asQString();//regproc";"NO"
v.typoutput = row.get(15).asQString();//"regproc";"NO"
v.typreceive = row.get(16).asQString();//"regproc";"NO"
v.typsend = row.get(17).asQString();//"regproc";"NO"
v.typmodin = row.get(18).asQString();//"regproc";"NO"
v.typmodout = row.get(19).asQString();//"regproc";"NO"
v.typanalyze = row.get(20).asQString();//"regproc";"NO"
v.typalign = row.get(21); // //""char"";"NO"
v.typstorage = row.get(22); //""char"";"NO"
v.typnotnull = row.get(23); //"boolean";"NO"
v.typbasetype = row.get(24); //"oid";"NO"
v.typtypmod = row.get(25); //-1;//"integer";"NO"
v.typndims = row.get(26); //"integer";"NO"
v.typcollation = row.get(27); //InvalidOid;//"oid";"NO"
v.typdefaultbin = row.get(28).asQString();//"pg_node_tree";"YES"
v.typdefault = row.get(29).asQString();//"text";"YES"
v.typacl = row.get(30).asQString();//"ARRAY";"YES"
m_types.push_back(v);
}
std::sort(m_types.begin(), m_types.end());
}

37
pgtypecontainer.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef PGTYPECONTAINER_H
#define PGTYPECONTAINER_H
#include <vector>
#include "pgtype.h"
namespace Pgsql {
class Result;
}
class PgTypeContainer {
public:
using t_Types = std::vector<PgType>; ///< Do not assume it will stay a vector only expect bidirectional access
PgTypeContainer();
t_Types::const_iterator begin() const { return m_types.begin(); }
t_Types::const_iterator end() const { return m_types.end(); }
void clear();
std::string getLoadQuery();
void load(const Pgsql::Result &res);
/** Searches for the type matching the specified oid.
*
* \return Returns the matching type or if it is not found a default constructed PgType (oid == InvalidOid).
*/
const PgType& getTypeByOid(Oid oid) const;
const PgType& getTypeByName(const QString &name) const;
private:
PgType m_invalidType; ///< default constructed object for when a non existent type is being retrieved.
t_Types m_types; // Keep sorted by Oid
};
#endif // PGTYPECONTAINER_H