-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqtlgen.cpp
139 lines (127 loc) · 4.27 KB
/
qtlgen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "stdafx.h"
#include <boost/filesystem.hpp>
#include "qtlgen.h"
#include "mysqlconfig.h"
#include "sqliteconfig.h"
#include "postgresconfig.h"
#include <leech/model.hpp>
#include <leech/json.hpp>
#include <inja/inja.hpp>
using namespace std;
#ifdef STRUCT_MODEL_FIELD_PREFIX
#undef STRUCT_MODEL_FIELD_PREFIX
#endif //STRUCT_MODEL_FIELD_PREFIX
#define STRUCT_MODEL_FIELD_PREFIX _
STRUCT_MODEL(connection_config, database)
STRUCT_MODEL(qtlstmt, type, text, classname, fieldlist, functions, params)
STRUCT_MODEL(qtlfunctions, for_each, get, insert, update, erase)
STRUCT_MODEL(qtlfield, name, type, primary, length, is_carray, auto_increment)
STRUCT_MODEL(qtlconfig, connection, filename, namespace, generate_pool, query_list)
STRUCT_MODEL_INHERIT(mysql_connection, (connection_config), host, port, user, password)
STRUCT_MODEL_INHERIT(sqlite_connection, (connection_config), filename)
STRUCT_MODEL_INHERIT(postgres_connection, (connection_config), host, port, user, password, schema, classes)
STRUCT_MODEL(pgclass, id, name, fields)
STRUCT_MODEL(pgfield, name, type)
namespace leech
{
}
namespace nlohmann {
template <>
struct adl_serializer<std::unique_ptr<connection_config>> {
static void to_json(json& j, const std::unique_ptr<connection_config>& config) {
if (config == nullptr) {
j = nullptr;
}
else {
if (dynamic_cast<mysql_connection*>(config.get())) {
j=move(leech::put(leech::json::document(), dynamic_cast<mysql_connection&>(*config)).root());
j["type"] = "mysql";
}
else if (dynamic_cast<sqlite_connection*>(config.get())) {
j = move(leech::put(leech::json::document(), dynamic_cast<sqlite_connection&>(*config)).root());
j["type"] = "sqlite";
}
else if (dynamic_cast<postgres_connection*>(config.get())) {
j = move(leech::put(leech::json::document(), dynamic_cast<postgres_connection&>(*config)).root());
j["type"] = "postgres";
}
}
}
};
template <>
struct adl_serializer<std::unique_ptr<qtlstmt>> {
static void to_json(json& j, const std::unique_ptr<qtlstmt>& stmt) {
if (stmt == nullptr) {
j = nullptr;
}
else {
j=move(leech::put(leech::json::document(), *stmt).root());
}
}
};
template <>
struct adl_serializer<params_type::value_type> {
static void to_json(json& j, const params_type::value_type& param) {
j["name"] = param.first;
j["type"] = param.second;
}
};
}
STRUCT_TO_JSON(qtlfield)
STRUCT_TO_JSON(pgclass)
STRUCT_TO_JSON(pgfield)
void generate(const qtlconfig& config, const std::string& template_path, const std::string& output_path)
{
leech::json::document doc;
leech::put(doc, config);
nlohmann::json::value_type& data=doc.root();
string str = data.dump(4);
boost::filesystem::path output_file;
output_file = data["filename"].get<string>();
output_file.replace_extension(".h");
inja::Environment env(template_path, output_path);
env.set_trim_blocks(true);
env.set_lstrip_blocks(true);
env.add_callback("primary", 1, [](inja::Arguments& args) {
const nlohmann::json* fieldlist = args.at(0);
nlohmann::json result;
for (const nlohmann::json& field : *fieldlist) {
if (field["primary"].get<bool>())
result.push_back(field);
}
return result;
});
env.add_callback("not_primary", 1, [](inja::Arguments& args) {
const nlohmann::json* fieldlist = args.at(0);
nlohmann::json result;
for (const nlohmann::json& field : *fieldlist) {
if (!field["primary"].get<bool>())
result.push_back(field);
}
return result;
});
env.add_callback("not_id", 1, [](inja::Arguments& args) {
const nlohmann::json* fieldlist = args.at(0);
nlohmann::json result;
for (const nlohmann::json& field : *fieldlist) {
if (!field["auto_increment"].get<bool>())
result.push_back(field);
}
return result;
});
env.add_callback("id_name", 1, [](inja::Arguments& args) {
const nlohmann::json* fieldlist = args.at(0);
nlohmann::json result;
for (const nlohmann::json& field : *fieldlist) {
if (field["auto_increment"].get<bool>())
return field["name"];
}
return result;
});
env.add_callback("escape_token", 1, [&config](inja::Arguments& args) {
const nlohmann::json* j = args.at(0);
nlohmann::json result = config._connection->escape_token(j->get<string>());
return result;
});
env.write(config._connection->template_file(), data, output_file.string());
}