PgType now inherits from PgNamespaceObject and PgOwnedObject

This commit is contained in:
eelke 2018-12-16 09:24:27 +01:00
parent 742fd0a4d3
commit 44358d198a
10 changed files with 37 additions and 54 deletions

View file

@ -24,7 +24,10 @@ Pgsql::Params QueryParamListController::params() const
Pgsql::Params params;
auto types = m_openDatabase->catalog()->types();
for (auto e : m_paramList.GetParams()) {
Oid oid = types->getByName(e.type)->oid;
// some types have two names that are in seperate fields
// this function only checks one field currently :(
// for example integer vs int4, bigint vs int8
Oid oid = types->getByName(e.type)->oid();
params.add(e.value, oid);
}
return params;

View file

@ -32,7 +32,7 @@ void SqlSyntaxHighlighter::setTypes(const PgTypeContainer& types)
{
m_typeNames.clear();
for (auto&& e : types) {
m_typeNames.insert(e.name);
m_typeNames.insert(e.objectName());
}
rehighlight();
}

View file

@ -14,7 +14,7 @@ QString PgAttribute::columnDefinition(const PgDatabaseCatalog &cat) const
// constraints NULL/NOT NULL, DEFAULT, GENERATED other constraints will be ignored here a
auto&& type = cat.types()->getByKey(typid);
QString sql = quoteIdent(name) % " " % type->name;
QString sql = quoteIdent(name) % " " % type->objectName();
if (collation != InvalidOid) {
auto&& col = cat.collations()->getByKey(collation);
QString oname = col->objectName();

View file

@ -98,7 +98,7 @@ QString getTypeDisplayString(const PgDatabaseCatalog &cat, Oid oid, int32_t typm
s += "[]";
}
else {
s = t->name;
s = t->objectName();
switch (oid) {
case varchar_oid:
case char_oid:

View file

@ -146,7 +146,7 @@ QString PgProc::argListWithNames(bool multiline) const
if (!arg.isEmpty())
arg += " ";
arg += types->getByKey(arg_elem.type)->name;
arg += types->getByKey(arg_elem.type)->objectName();
if (!arg_elem.def.isEmpty())
arg += " DEFAULT " + arg_elem.def;
@ -179,7 +179,7 @@ QString PgProc::argSigList(const bool forScript) const
args += ", ";
}
QString typname = types->getByKey(arg_elem.type)->name;
QString typname = types->getByKey(arg_elem.type)->objectName();
if (forScript)
args += " <" + typname + ">";
else
@ -201,7 +201,7 @@ QString PgProc::createSql() const
QString quoted_sig = QString("%1(%2)").arg(fullyQualifiedQuotedObjectName(), argSigList());
auto&& types = catalog().types();
QString return_type = types->getByKey(rettype)->name;
QString return_type = types->getByKey(rettype)->objectName();
sql = QString("-- Function: %1\n\n"

View file

@ -50,7 +50,3 @@ void operator<<(TypCategory &s, const Pgsql::Value &v)
break;
}
}
PgType::PgType() = default;

View file

@ -1,6 +1,8 @@
#ifndef PGTYPE_H
#define PGTYPE_H
#include "PgNamespaceObject.h"
#include "PgOwnedObject.h"
#include <QString>
#include <libpq-fe.h>
#include "Pgsql_Value.h"
@ -26,15 +28,13 @@ enum class TypCategory {
void operator<<(TypCategory &s, const Pgsql::Value &v);
class PgType {
class PgType: public PgNamespaceObject, public PgOwnedObject {
public:
PgType();
Oid oid = InvalidOid;
QString name; // formatted name as per database, arrays
// Oid oid = InvalidOid;
// QString name; // formatted name as per database, arrays
QString typname; //"name";"NO"
Oid typnamespace = InvalidOid;//"oid";"NO"
Oid owner = InvalidOid;//"oid";"NO"
// Oid typnamespace = InvalidOid;//"oid";"NO"
// Oid owner = InvalidOid;//"oid";"NO"
short len = -1;//"smallint";"NO"
bool byval = false;//"boolean";"NO"
QString type;//""char"";"NO"
@ -63,10 +63,12 @@ public:
QString typdefault;//"text";"YES"
QString acl;//"ARRAY";"YES"
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 PgType &rhs) const { return oid < rhs.oid; }
using PgNamespaceObject::PgNamespaceObject;
// 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 PgType &rhs) const { return oid < rhs.oid; }
};
#endif // PGTYPE_H

View file

@ -3,34 +3,11 @@
#include "Pgsql_Col.h"
#include <algorithm>
//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;
//}
//const PgType& PgTypeContainer::getTypeByIdx(int idx) const
//{
// return m_types.at(idx);
//}
std::string PgTypeContainer::getLoadQuery() const
{
return
"SELECT oid, format_type(oid, NULL) AS name, typname, typnamespace, typowner, typlen, typbyval, typtype, typcategory, \n"
"SELECT oid, format_type(oid, NULL) AS name, typnamespace, typowner, typname, 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"
@ -40,10 +17,15 @@ std::string PgTypeContainer::getLoadQuery() const
PgType PgTypeContainer::loadElem(const Pgsql::Row &row)
{
Pgsql::Col col(row);
PgType v;
col >> v.oid >> v.name >> v.typname >> v.typnamespace >> v.owner >> v.len >> v.byval >> v.type >> v.category
Oid type_oid = col.nextValue();
QString name = col.nextValue();
Oid ns_oid = col.nextValue();
Oid owner = col.nextValue();
PgType v(m_catalog, type_oid, name, ns_oid);
col >> v.typname >> v.len >> v.byval >> v.type >> v.category
>> v.ispreferred >> v.isdefined >> v.delim >> v.relid >> v.elem >> v.array >> v.input >> v.output
>> v.receive >> v.send >> v.modin >> v.modout >> v.analyze >> v.align >> v.storage >> v.notnull
>> v.basetype >> v.typmod >> v.ndims >> v.collation >> v.defaultbin >> v.typdefault >> v.acl;
v.setOwnerOid(m_catalog, owner);
return v;
}

View file

@ -49,7 +49,7 @@ void TypeSelectionItemModel::setTypeList(std::shared_ptr<const PgTypeContainer>
m_types.clear();
for (const auto &e : *types) {
if (e.category != TypCategory::Array && e.type != "c") {
m_types.push_back(e.name);
m_types.push_back(e.objectName());
}
}
std::sort(m_types.begin(), m_types.end());
@ -87,8 +87,8 @@ QVariant TypeModel::data(const QModelIndex &index, int role) const
//const PgType &tp = m_types->getByIdx(row);
auto elem = m_types->getByIdx(row);
switch (column) {
case OidCol: return elem.oid;
case NameCol: return elem.name;
case OidCol: return elem.oid();
case NameCol: return elem.objectName();
}
}
}

View file

@ -28,7 +28,7 @@ TypeMappingResult TypeMappings::getTypeForOid(Oid oid) const
if (m_types) {
const PgType* type = m_types->getByKey(oid);
if (type)
dbtypename = type->name;
dbtypename = type->objectName();
}
return { res->second, dbtypename };
@ -52,11 +52,11 @@ TypeMappingResult TypeMappings::getTypeForOid(Oid oid) const
}
return {
QString(m_defaultContainerType).arg(type_string),
elem_type->name + "[]"
elem_type->objectName() + "[]"
};
}
else {
return { m_defaultStringType, type->name };
return { m_defaultStringType, type->objectName() };
}
}
// We shouldn't get here unless m_types is empty