The codegen now can properly lookup array types.

Array type lookup failed previously because the typemapping class was
not yet receiving the list of types from the database. Fix was to pass
this data
This commit is contained in:
eelke 2018-11-17 09:47:50 +01:00
parent 287073afdc
commit be0064f730
8 changed files with 34 additions and 20 deletions

View file

@ -6,13 +6,16 @@
#include "StructureTemplate.h"
#include "TypeMappings.h"
#include "Pgsql_oids.h"
#include "PgDatabaseCatalog.h"
using namespace Pgsql;
std::shared_ptr<const TypeMappings> GetPglabCppTypeMappings()
class PgTypeContainer;
std::shared_ptr<const TypeMappings> GetPglabCppTypeMappings(std::shared_ptr<const PgTypeContainer> types)
{
auto tm = std::make_shared<TypeMappings>();
*tm = {
auto tm = std::make_shared<TypeMappings>(types, std::initializer_list<TypeMappings::Mapping>
{
{ bool_oid, "bool" },
{ char_oid, "char" },
{ name_oid, "std::string" },
@ -23,7 +26,7 @@ std::shared_ptr<const TypeMappings> GetPglabCppTypeMappings()
{ oid_oid, "Oid" },
{ float4_oid, "float" },
{ float8_oid, "double" }
};
});
tm->setDefaultStringType("std::string");
tm->setDefaultContainerType("std::vector<%1>");
return tm;
@ -66,10 +69,10 @@ for (auto row: result) {
}
std::shared_ptr<LanguageConfig> buildPglabCppLanguageConfig()
std::shared_ptr<LanguageConfig> buildPglabCppLanguageConfig(std::shared_ptr<PgDatabaseCatalog> catalog)
{
auto config = std::make_shared<LanguageConfig>();
config->setTypeMappings(GetPglabCppTypeMappings());
config->setTypeMappings(GetPglabCppTypeMappings(catalog->types()));
config->setNameManglingRules(std::make_shared<NameManglingRules>());
config->setStructureTemplate(buildPglabStructureTemplate());
config->setIndentationConfig(std::make_shared<IndentationConfig>());
@ -78,9 +81,9 @@ std::shared_ptr<LanguageConfig> buildPglabCppLanguageConfig()
}
std::shared_ptr<const LanguageConfig> getPglabCppLanguageConfig()
std::shared_ptr<const LanguageConfig> getPglabCppLanguageConfig(std::shared_ptr<PgDatabaseCatalog> catalog)
{
static auto config = buildPglabCppLanguageConfig();
static auto config = buildPglabCppLanguageConfig(catalog);
return config;
}

View file

@ -4,8 +4,9 @@
#include <memory>
class LanguageConfig;
class PgDatabaseCatalog;
//std::shared_ptr<const LanguageConfig> getDefaultCppLanguageConfig();
std::shared_ptr<const LanguageConfig> getPglabCppLanguageConfig();
std::shared_ptr<const LanguageConfig> getPglabCppLanguageConfig(std::shared_ptr<PgDatabaseCatalog> catalog);
#endif // DEFAULTCONFIGS_H

View file

@ -10,17 +10,18 @@
// };
//}
TypeMappings::TypeMappings() = default;
TypeMappings::TypeMappings(std::shared_ptr<const PgTypeContainer> types)
: m_types(types)
{}
TypeMappings::TypeMappings(std::initializer_list<Mapping> mappings)
TypeMappings::TypeMappings(std::shared_ptr<const PgTypeContainer> types, std::initializer_list<Mapping> mappings)
: TypeMappings(types)
{
m_typeMap.insert(mappings.begin(), mappings.end());
}
QString TypeMappings::getTypeForOid(Oid oid) const
{
// TODO use the catalog to determine if someting is an array or vector type.
// If it is lookup its element type and use the std container
auto res = m_typeMap.find(oid);
if (res != m_typeMap.end()) {
return res->second;
@ -28,7 +29,11 @@ QString TypeMappings::getTypeForOid(Oid oid) const
if (m_types) {
PgType type = m_types->getByKey(oid);
// Found a valid type? elem is set? then it is array type
if (type.oid != InvalidOid && type.elem != InvalidOid) {
// Lookup what the element type is and wrap the mapping for that in the standard container type
// for the language config. If that isn't right the end user should create a specific mapping for
// that array type.
res = m_typeMap.find(type.elem);
QString type_string;
if (res == m_typeMap.end()) {

View file

@ -13,9 +13,9 @@ public:
using TypeMap = std::unordered_map<Oid, QString>;
using Mapping = std::pair<Oid, QString>;
TypeMappings();
TypeMappings(std::shared_ptr<const PgTypeContainer> types = nullptr);
TypeMappings(std::initializer_list<Mapping> mappings);
TypeMappings(std::shared_ptr<const PgTypeContainer> types, std::initializer_list<Mapping> mappings);
QString getTypeForOid(Oid oid) const;