Codegen now supports the database column type in the field template.
This allows for inserting it as a comment which is very useful while tweaking your typemappings as you can see what the input was.
This commit is contained in:
parent
be0064f730
commit
104ab5de1e
8 changed files with 49 additions and 19 deletions
|
|
@ -80,7 +80,7 @@ QString CodeBuilder::columnNameToVariableName(QString column_name) const
|
||||||
return m_configuration->columnNameToFieldName(column_name);
|
return m_configuration->columnNameToFieldName(column_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString CodeBuilder::getTypeName(Oid dbtype) const
|
TypeMappingResult CodeBuilder::getTypeName(Oid dbtype) const
|
||||||
{
|
{
|
||||||
return m_configuration->getTypeName(dbtype);
|
return m_configuration->getTypeName(dbtype);
|
||||||
}
|
}
|
||||||
|
|
@ -97,10 +97,11 @@ void CodeBuilder::genFieldRetrieval(QTextStream &q, const ColumnDataList &column
|
||||||
|
|
||||||
void CodeBuilder::genFieldDeclaration(QTextStream &q, const QString &format, const QString &field_name, Oid column_type) const
|
void CodeBuilder::genFieldDeclaration(QTextStream &q, const QString &format, const QString &field_name, Oid column_type) const
|
||||||
{
|
{
|
||||||
QString type_name = getTypeName(column_type);
|
TypeMappingResult type_name = getTypeName(column_type);
|
||||||
FormatToStream(q, format, [&] (QTextStream &out, QString var) {
|
FormatToStream(q, format, [&] (QTextStream &out, QString var) {
|
||||||
if (var == "typename") out << type_name;
|
if (var == "typename") out << type_name.codeType();
|
||||||
else if (var == "varname") out << field_name;
|
else if (var == "varname") out << field_name;
|
||||||
|
else if (var == "dbtype") out << type_name.dbType();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include "Pgsql_declare.h"
|
#include "Pgsql_declare.h"
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "TypeMappings.h"
|
||||||
|
|
||||||
class LanguageConfig;
|
class LanguageConfig;
|
||||||
class QTextStream;
|
class QTextStream;
|
||||||
|
|
@ -33,7 +34,7 @@ public:
|
||||||
// - Code for processing a single row
|
// - Code for processing a single row
|
||||||
// - Declaration of struct for holding single row result
|
// - Declaration of struct for holding single row result
|
||||||
QString columnNameToVariableName(QString column_name) const;
|
QString columnNameToVariableName(QString column_name) const;
|
||||||
QString getTypeName(Oid dbtype) const;
|
TypeMappingResult getTypeName(Oid dbtype) const;
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::shared_ptr<const LanguageConfig> m_configuration;
|
std::shared_ptr<const LanguageConfig> m_configuration;
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,11 @@ std::shared_ptr<const TypeMappings> GetPglabCppTypeMappings(std::shared_ptr<con
|
||||||
{
|
{
|
||||||
{ bool_oid, "bool" },
|
{ bool_oid, "bool" },
|
||||||
{ char_oid, "char" },
|
{ char_oid, "char" },
|
||||||
{ name_oid, "std::string" },
|
{ name_oid, "QString" },
|
||||||
{ int8_oid, "int64_t" },
|
{ int8_oid, "int64_t" },
|
||||||
{ int2_oid, "int16_t" },
|
{ int2_oid, "int16_t" },
|
||||||
{ int4_oid, "int32_t" },
|
{ int4_oid, "int32_t" },
|
||||||
{ text_oid, "std::string" },
|
{ text_oid, "QString" },
|
||||||
{ oid_oid, "Oid" },
|
{ oid_oid, "Oid" },
|
||||||
{ float4_oid, "float" },
|
{ float4_oid, "float" },
|
||||||
{ float8_oid, "double" }
|
{ float8_oid, "double" }
|
||||||
|
|
@ -43,7 +43,7 @@ public:
|
||||||
using /%structname%/Lst = std::vector</%structname%/>;
|
using /%structname%/Lst = std::vector</%structname%/>;
|
||||||
)__";
|
)__";
|
||||||
|
|
||||||
t->m_fieldTemplate = "/%typename%/ /%varname%/;";
|
t->m_fieldTemplate = "/%typename%/ /%varname%/; // /%dbtype%/";
|
||||||
//st_templ->m_fieldSeparator;
|
//st_templ->m_fieldSeparator;
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ QString LanguageConfig::columnNameToFieldName(const QString& column_name) const
|
||||||
return m_varNaming->transform(column_name);
|
return m_varNaming->transform(column_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LanguageConfig::getTypeName(Oid dbtype) const
|
TypeMappingResult LanguageConfig::getTypeName(Oid dbtype) const
|
||||||
{
|
{
|
||||||
return m_typeMappings->getTypeForOid(dbtype);
|
return m_typeMappings->getTypeForOid(dbtype);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include "Pgsql_oids.h"
|
#include "Pgsql_oids.h"
|
||||||
|
#include "TypeMappings.h"
|
||||||
|
|
||||||
class NameManglingRules;
|
class NameManglingRules;
|
||||||
class TypeMappings;
|
|
||||||
class StructureTemplate;
|
class StructureTemplate;
|
||||||
class IndentationConfig;
|
class IndentationConfig;
|
||||||
class ResultLoopTemplate;
|
class ResultLoopTemplate;
|
||||||
|
|
@ -17,7 +17,7 @@ public:
|
||||||
LanguageConfig();
|
LanguageConfig();
|
||||||
|
|
||||||
QString columnNameToFieldName(const QString& column_name) const;
|
QString columnNameToFieldName(const QString& column_name) const;
|
||||||
QString getTypeName(Oid dbtype) const;
|
TypeMappingResult getTypeName(Oid dbtype) const;
|
||||||
|
|
||||||
void setNameManglingRules(std::shared_ptr<const NameManglingRules> name_mangling_rules);
|
void setNameManglingRules(std::shared_ptr<const NameManglingRules> name_mangling_rules);
|
||||||
std::shared_ptr<const TypeMappings> typeMappings() const;
|
std::shared_ptr<const TypeMappings> typeMappings() const;
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,17 @@ TypeMappings::TypeMappings(std::shared_ptr<const PgTypeContainer> types, std::in
|
||||||
m_typeMap.insert(mappings.begin(), mappings.end());
|
m_typeMap.insert(mappings.begin(), mappings.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
QString TypeMappings::getTypeForOid(Oid oid) const
|
TypeMappingResult TypeMappings::getTypeForOid(Oid oid) const
|
||||||
{
|
{
|
||||||
auto res = m_typeMap.find(oid);
|
auto res = m_typeMap.find(oid);
|
||||||
if (res != m_typeMap.end()) {
|
if (res != m_typeMap.end()) {
|
||||||
return res->second;
|
QString dbtypename;
|
||||||
|
if (m_types) {
|
||||||
|
PgType type = m_types->getByKey(oid);
|
||||||
|
dbtypename = type.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { res->second, dbtypename };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_types) {
|
if (m_types) {
|
||||||
|
|
@ -35,6 +41,7 @@ QString TypeMappings::getTypeForOid(Oid oid) const
|
||||||
// for the language config. If that isn't right the end user should create a specific mapping for
|
// for the language config. If that isn't right the end user should create a specific mapping for
|
||||||
// that array type.
|
// that array type.
|
||||||
res = m_typeMap.find(type.elem);
|
res = m_typeMap.find(type.elem);
|
||||||
|
PgType elem_type = m_types->getByKey(type.elem);
|
||||||
QString type_string;
|
QString type_string;
|
||||||
if (res == m_typeMap.end()) {
|
if (res == m_typeMap.end()) {
|
||||||
type_string = m_defaultStringType;
|
type_string = m_defaultStringType;
|
||||||
|
|
@ -42,10 +49,17 @@ QString TypeMappings::getTypeForOid(Oid oid) const
|
||||||
else {
|
else {
|
||||||
type_string = res->second;
|
type_string = res->second;
|
||||||
}
|
}
|
||||||
return QString(m_defaultContainerType).arg(type_string);
|
return {
|
||||||
|
QString(m_defaultContainerType).arg(type_string),
|
||||||
|
elem_type.name + "[]"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return { m_defaultStringType, type.name };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return m_defaultStringType;
|
// We shouldn't get here unless m_types is empty
|
||||||
|
return { m_defaultStringType, QString() };
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeMappings::setTypes(std::shared_ptr<const PgTypeContainer> types)
|
void TypeMappings::setTypes(std::shared_ptr<const PgTypeContainer> types)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,20 @@
|
||||||
|
|
||||||
class PgTypeContainer;
|
class PgTypeContainer;
|
||||||
|
|
||||||
|
class TypeMappingResult {
|
||||||
|
public:
|
||||||
|
TypeMappingResult(QString codetype, QString dbtype)
|
||||||
|
: m_codeType(codetype)
|
||||||
|
, m_dbType(dbtype)
|
||||||
|
{}
|
||||||
|
|
||||||
|
const QString& codeType() const { return m_codeType; }
|
||||||
|
const QString& dbType() const { return m_dbType; }
|
||||||
|
private:
|
||||||
|
QString m_codeType;
|
||||||
|
QString m_dbType;
|
||||||
|
};
|
||||||
|
|
||||||
class TypeMappings {
|
class TypeMappings {
|
||||||
public:
|
public:
|
||||||
using TypeMap = std::unordered_map<Oid, QString>;
|
using TypeMap = std::unordered_map<Oid, QString>;
|
||||||
|
|
@ -17,7 +31,7 @@ public:
|
||||||
|
|
||||||
TypeMappings(std::shared_ptr<const PgTypeContainer> types, std::initializer_list<Mapping> mappings);
|
TypeMappings(std::shared_ptr<const PgTypeContainer> types, std::initializer_list<Mapping> mappings);
|
||||||
|
|
||||||
QString getTypeForOid(Oid oid) const;
|
TypeMappingResult getTypeForOid(Oid oid) const;
|
||||||
|
|
||||||
const TypeMap& typeMap() const { return m_typeMap; }
|
const TypeMap& typeMap() const { return m_typeMap; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,20 +36,20 @@ protected:
|
||||||
|
|
||||||
TEST_F(TypeMappingsTest, defStringType)
|
TEST_F(TypeMappingsTest, defStringType)
|
||||||
{
|
{
|
||||||
QString result = tm.getTypeForOid(Pgsql::float4_oid);
|
QString result = tm.getTypeForOid(Pgsql::float4_oid).codeType();
|
||||||
ASSERT_EQ(result, def_str_type);
|
ASSERT_EQ(result, def_str_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(TypeMappingsTest, int4Type)
|
TEST_F(TypeMappingsTest, int4Type)
|
||||||
{
|
{
|
||||||
QString result = tm.getTypeForOid(Pgsql::int4_oid);
|
QString result = tm.getTypeForOid(Pgsql::int4_oid).codeType();
|
||||||
ASSERT_EQ(result, int4str);
|
ASSERT_EQ(result, int4str);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(TypeMappingsTest, int4overideType)
|
TEST_F(TypeMappingsTest, int4overideType)
|
||||||
{
|
{
|
||||||
tm.set(Pgsql::int4_oid, "QString");
|
tm.set(Pgsql::int4_oid, "QString");
|
||||||
QString result = tm.getTypeForOid(Pgsql::int4_oid);
|
QString result = tm.getTypeForOid(Pgsql::int4_oid).codeType();
|
||||||
ASSERT_EQ(result, "QString");
|
ASSERT_EQ(result, "QString");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,7 +65,7 @@ TEST_F(TypeMappingsTest, int4arrayType)
|
||||||
|
|
||||||
tm.setTypes(types);
|
tm.setTypes(types);
|
||||||
|
|
||||||
QString result = tm.getTypeForOid(Pgsql::int4_array_oid);
|
QString result = tm.getTypeForOid(Pgsql::int4_array_oid).codeType();
|
||||||
ASSERT_EQ(result, "std::vector<int>");
|
ASSERT_EQ(result, "std::vector<int>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue