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:
parent
287073afdc
commit
be0064f730
8 changed files with 34 additions and 20 deletions
|
|
@ -20,8 +20,10 @@ CodeGenerator::~CodeGenerator()
|
|||
delete ui;
|
||||
}
|
||||
|
||||
void CodeGenerator::Init(QString query, std::shared_ptr<const Pgsql::Result> dbres)
|
||||
void CodeGenerator::Init(std::shared_ptr<PgDatabaseCatalog> catalog, QString query,
|
||||
std::shared_ptr<const Pgsql::Result> dbres)
|
||||
{
|
||||
m_catalog = catalog;
|
||||
m_query = query;
|
||||
m_dbres = dbres;
|
||||
generateCode();
|
||||
|
|
@ -42,7 +44,7 @@ void CodeGenerator::generateCode()
|
|||
QString buffer;
|
||||
QTextStream stream(&buffer);
|
||||
CodeBuilder builder;
|
||||
builder.setLanguageConfig(getPglabCppLanguageConfig());
|
||||
builder.setLanguageConfig(getPglabCppLanguageConfig(m_catalog));
|
||||
builder.GenCodeForExecutingQuery(stream, m_query, *m_dbres, struct_name);
|
||||
stream.flush();
|
||||
ui->generatedCodeEditor->setPlainText(buffer);
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ namespace Ui {
|
|||
class CodeGenerator;
|
||||
}
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
|
||||
class CodeGenerator : public PlgPage
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -17,13 +19,14 @@ public:
|
|||
explicit CodeGenerator(QWidget *parent = nullptr);
|
||||
~CodeGenerator();
|
||||
|
||||
void Init(QString query, std::shared_ptr<const Pgsql::Result> dbres);
|
||||
void Init(std::shared_ptr<PgDatabaseCatalog> catalog, QString query, std::shared_ptr<const Pgsql::Result> dbres);
|
||||
private slots:
|
||||
void on_updateCodeButton_clicked();
|
||||
|
||||
private:
|
||||
Ui::CodeGenerator *ui;
|
||||
|
||||
std::shared_ptr<PgDatabaseCatalog> m_catalog;
|
||||
QString m_query;
|
||||
std::shared_ptr<const Pgsql::Result> m_dbres;
|
||||
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ void MainWindow::newCrudPage(const PgClass &table)
|
|||
void MainWindow::newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres)
|
||||
{
|
||||
auto cgtab = new CodeGenerator(this);
|
||||
cgtab->Init(query, dbres);
|
||||
cgtab->Init(m_database->catalogue(), query, dbres);
|
||||
addPage(cgtab, "Codegen");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ protected:
|
|||
def_str_type = "std::string";
|
||||
int4str = "int";
|
||||
int8str = "long long";
|
||||
tm = TypeMappings( {
|
||||
tm = TypeMappings(nullptr, {
|
||||
{ Pgsql::int4_oid, int4str },
|
||||
{ Pgsql::int8_oid, int8str }
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue