-
Notifications
You must be signed in to change notification settings - Fork 2
/
qtlconfig.cpp
232 lines (204 loc) · 5.66 KB
/
qtlconfig.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "stdafx.h"
#include "qtlconfig.h"
#include "mysqlconfig.h"
#include "sqliteconfig.h"
#include "postgresconfig.h"
namespace leech
{
template<typename Document>
void get(const Document& ar, const typename Document::element_type& element, std::unique_ptr<connection_config>& v, const char* /*name*/ = nullptr);
}
#include <leech/model.hpp>
#include <leech/yaml.hpp>
#include <functional>
#include <iostream>
#include <fmt/printf.h>
#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, classname, params, fields, functions)
STRUCT_MODEL(qtlfunctions, for_each, get, insert, update, erase)
STRUCT_MODEL(qtlconfig, connection, filename, namespace, all_tables, 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)
using namespace std;
std::function<qtlstmt*()> create_query;
namespace leech
{
template<typename Document>
void get(const Document& ar, const typename Document::element_type& element, std::unique_ptr<connection_config>& v, const char* /*name*/)
{
std::string type;
ar.get(element["type"], type);
v = nullptr;
if (type == "mysql")
{
mysql_connection* connection = new mysql_connection;
leech::get(leech::yaml::document(element), *connection);
v.reset(connection);
create_query = [connection=v.get()]() {
return new mysql_stmt(dynamic_cast<mysql_connection&>(*connection));
};
}
else if (type == "sqlite")
{
sqlite_connection* connection = new sqlite_connection;
leech::get(leech::yaml::document(element), *connection);
v.reset(connection);
create_query = [connection = v.get()]() {
return new sqlite_stmt(dynamic_cast<sqlite_connection&>(*connection));
};
}
else if (type == "postgres")
{
postgres_connection* connection = new postgres_connection;
leech::get(leech::yaml::document(element), *connection);
v.reset(connection);
create_query = [connection = v.get()]() {
return new postgres_stmt(dynamic_cast<postgres_connection&>(*connection));
};
}
}
}
namespace YAML
{
template <>
struct as_if<std::unique_ptr<qtlstmt>, void>
{
explicit as_if(const Node& node_) : node(node_) {}
const Node& node;
std::unique_ptr<qtlstmt> operator()() const
{
std::unique_ptr<qtlstmt> query = nullptr;
if (node.Type() != NodeType::Map)
throw TypedBadConversion<std::string>(node.Mark());
query.reset(create_query());
std::string type;
if (node["table"].IsDefined())
{
type = "table";
query->as_table();
}
else if (node["query"].IsDefined())
{
type = "query";
query->as_query();
}
leech::get(leech::yaml::document(node[type]), query->_text);
leech::get(leech::yaml::document(node), *query);
return query;
}
};
template <>
struct as_if<params_type, void>
{
explicit as_if(const Node& node_) : node(node_) {}
const Node& node;
params_type operator()() const
{
params_type v;
if (node.Type() != NodeType::Map)
throw TypedBadConversion<std::string>(node.Mark());
for (auto i : node)
{
v.emplace_back(i.first.as<string>(), i.second.as<string>());
}
return v;
}
};
template <>
struct as_if<fields_type, void>
{
explicit as_if(const Node& node_) : node(node_) {}
const Node& node;
fields_type operator()() const
{
fields_type v;
if (node.Type() != NodeType::Map)
throw TypedBadConversion<std::string>(node.Mark());
for (auto i : node)
{
v.emplace(i.first.as<string>(), i.second.as<string>());
}
return v;
}
};
}
bool connection_config::find_table(const std::vector<std::unique_ptr<qtlstmt>>& queries, const std::string& name)
{
for (const auto& query : queries)
{
if (query->_type == qtlstmt::table && query->_text == name)
return true;
}
return false;
}
void qtlconfig::load(const char* filename)
{
STRUCT_MODEL_SET_OPTIONAL(qtlconfig, all_tables);
STRUCT_MODEL_SET_OPTIONAL(qtlconfig, generate_pool);
STRUCT_MODEL_SET_OPTIONAL(qtlconfig, query_list);
STRUCT_MODEL_SET_OPTIONAL(qtlstmt, params);
STRUCT_MODEL_SET_OPTIONAL(qtlstmt, fields);
STRUCT_MODEL_SET_OPTIONAL(qtlfunctions, for_each);
STRUCT_MODEL_SET_OPTIONAL(qtlfunctions, get);
STRUCT_MODEL_SET_OPTIONAL(qtlfunctions, insert);
STRUCT_MODEL_SET_OPTIONAL(qtlfunctions, update);
STRUCT_MODEL_SET_OPTIONAL(qtlfunctions, erase);
STRUCT_MODEL_SET_OPTIONAL(mysql_connection, port);
leech::yaml::document doc = leech::yaml::load_file(filename);
leech::get(doc, *this);
}
bool qtlconfig::prepare()
{
assert(_connection);
if (!_connection->open()) {
cerr << "Can't connect to database: " << _connection->_database << endl;
return false;
}
if (_all_tables)
{
_connection->scan_tables(_query_list);
}
for (auto it= _query_list.begin(); it!=_query_list.end();)
{
auto& query = *it;
query->init_fields();
query->init_primaries();
if (query->_fieldlist.empty())
it = _query_list.erase(it);
else
++it;
for (auto& field : query->_fieldlist)
{
if (field._type == "array" && field._length > 0)
{
field._type = fmt::sprintf("std::array<char, %d>", field._length);
}
else if (field._type == "char")
field._is_carray = true;
}
}
return true;
}
void qtlstmt::as_table()
{
_type = qtlstmt::table;
_functions._for_each = true;
_functions._get = true;
_functions._insert = true;
_functions._update = true;
_functions._erase = true;
}
void qtlstmt::as_query()
{
_type = qtlstmt::query;
_functions._for_each = true;
_functions._get = false;
_functions._insert = false;
_functions._update = false;
_functions._erase = false;
}