-
Notifications
You must be signed in to change notification settings - Fork 21
/
sweetqltest.cpp
360 lines (325 loc) · 10 KB
/
sweetqltest.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <string>
#include <sstream>
#include <fstream>
#include <boost/regex.hpp>
#include <utility>
#include <iostream>
#include <algorithm>
#include <sweetql.hpp>
#include <unit.hpp>
//#include <conv.hpp>
#include <benchmark.hpp>
#include <mysqlimpl.hpp>
//#include <Wt/Dbo/Dbo>
//#include <Wt/Dbo/backend/Sqlite3>
//#include <Wt/Dbo/Session>
//namespace dbo = Wt::Dbo;
class Person {
public:
Person() {} // dummy
Person(const std::string& f, const std::string& l, const std::string& c,
const std::string& a, const std::string& a2,
const std::string& ci, const std::string& s, int z,
const std::string& pw, const std::string& pp, const std::string& m,
const std::string& w
) : firstname(f), lastname(l), company(c),
address(a), county(a2), city(ci),
state(s), phoneWork(pw), phonePrivat(pp),
mail(m), www(w), zip(z) {
}
std::string firstname;
std::string lastname;
std::string company;
std::string address;
std::string county;
std::string city;
std::string state;
std::string phoneWork;
std::string phonePrivat;
std::string mail;
std::string www;
int64_t zip;
/*template<class Action>
void persist(Action& a) {
dbo::field(a, firstname, "firstname");
dbo::field(a, lastname, "lastname");
dbo::field(a, company, "company");
dbo::field(a, address, "address");
dbo::field(a, county, "county");
dbo::field(a, city, "city");
dbo::field(a, state, "state");
dbo::field(a, phoneWork, "phonework");
dbo::field(a, phonePrivat, "phoneprivate");
dbo::field(a, mail, "mail");
dbo::field(a, www, "www");
dbo::field(a, zip, "zip");
}*/
static SqlTable<Person>& table() {
static SqlTable<Person> tab = SqlTable<Person>::sqlTable("Person",
SqlColumn<Person>("Firstname", makeAttr(&Person::firstname,
SweetqlFlags::PrimaryKey)),
SqlColumn<Person>("Lastname", makeAttr(&Person::lastname,
SweetqlFlags::PrimaryKey)),
SqlColumn<Person>("Company", makeAttr(&Person::company,
SweetqlFlags::PrimaryKey)),
SqlColumn<Person>("Address", makeAttr(&Person::address,
SweetqlFlags::PrimaryKey)),
SqlColumn<Person>("County", makeAttr(&Person::county)),
SqlColumn<Person>("City", makeAttr(&Person::city)),
SqlColumn<Person>("State", makeAttr(&Person::state)),
SqlColumn<Person>("PhoneWork", makeAttr(&Person::phoneWork)),
SqlColumn<Person>("PhonePrivat",makeAttr(&Person::phonePrivat)),
SqlColumn<Person>("Mail", makeAttr(&Person::mail)),
SqlColumn<Person>("Www", makeAttr(&Person::www)),
SqlColumn<Person>("Zip", makeAttr(&Person::zip))
);
return tab;
}
};
typedef std::vector<Person> PersonVec;
class Reservation {
public:
std::string firstname;
std::string lastname;
std::string restaurant;
std::string location;
int64_t date;
int64_t tablemem;
inline Reservation() {}
static SqlTable<Reservation>& table() {
static SqlTable<Reservation> tab =
SqlTable<Reservation>::sqlTable("Reservation",
SqlColumn<Reservation>("Firstname", makeAttr(&Reservation::firstname,
SweetqlFlags::PrimaryKey)),
SqlColumn<Reservation>("Lastname", makeAttr(&Reservation::lastname,
SweetqlFlags::PrimaryKey)),
SqlColumn<Reservation>("Restaurant", makeAttr(&Reservation::restaurant,
SweetqlFlags::PrimaryKey)),
SqlColumn<Reservation>("Location", makeAttr(&Reservation::location,
SweetqlFlags::PrimaryKey)),
SqlColumn<Reservation>("Date", makeAttr(&Reservation::date,
SweetqlFlags::PrimaryKey)),
SqlColumn<Reservation>("Tablem", makeAttr(&Reservation::tablemem,
SweetqlFlags::PrimaryKey))
);
return tab;
}
};
class ReservationPerson {
public:
std::string firstname;
std::string lastname;
std::string company;
std::string address;
std::string county;
std::string city;
std::string state;
std::string phoneWork;
std::string phonePrivat;
std::string mail;
std::string www;
int64_t zip;
std::string restaurant;
std::string location;
int64_t date;
int64_t tablemem;
ReservationPerson() {}
static SqlTable<ReservationPerson>& table() {
static SqlTable<ReservationPerson> tab =
SqlTable<ReservationPerson>::sqlTable("ReservationPerson",
SqlColumn<ReservationPerson>("Firstname",
makeAttr(&ReservationPerson::firstname,
SweetqlFlags::PrimaryKey)),
SqlColumn<ReservationPerson>("Lastname",
makeAttr(&ReservationPerson::lastname,
SweetqlFlags::PrimaryKey)),
SqlColumn<ReservationPerson>("Company",
makeAttr(&ReservationPerson::company,
SweetqlFlags::PrimaryKey)),
SqlColumn<ReservationPerson>("Address",
makeAttr(&ReservationPerson::address,
SweetqlFlags::PrimaryKey)),
SqlColumn<ReservationPerson>("County",
makeAttr(&ReservationPerson::county)),
SqlColumn<ReservationPerson>("City",
makeAttr(&ReservationPerson::city)),
SqlColumn<ReservationPerson>("State",
makeAttr(&ReservationPerson::state)),
SqlColumn<ReservationPerson>("PhoneWork",
makeAttr(&ReservationPerson::phoneWork)),
SqlColumn<ReservationPerson>("PhonePrivat",
makeAttr(&ReservationPerson::phonePrivat)),
SqlColumn<ReservationPerson>("Mail",
makeAttr(&ReservationPerson::mail)),
SqlColumn<ReservationPerson>("Www",
makeAttr(&ReservationPerson::www)),
SqlColumn<ReservationPerson>("Zip",
makeAttr(&ReservationPerson::zip)),
SqlColumn<ReservationPerson>("Restaurant",
makeAttr(&ReservationPerson::restaurant)),
SqlColumn<ReservationPerson>("Location",
makeAttr(&ReservationPerson::location)),
SqlColumn<ReservationPerson>("Date",
makeAttr(&ReservationPerson::date)),
SqlColumn<ReservationPerson>("Tablem",
makeAttr(&ReservationPerson::tablemem))
);
return tab;
}
};
PersonVec parsePersonFile(const std::string& fn) {
PersonVec ret;
std::ifstream infile(fn);
std::vector<std::string> v;
std::string line;
boost::regex re("\"[^\"]+\"");
while(std::getline(infile, line)) {
boost::sregex_iterator reBe(line.begin(), line.end(), re);
boost::sregex_iterator reEn;
std::transform(reBe, reEn, std::back_inserter(v), [&v]
(const boost::smatch& it) {
return it.str().substr(1, it.str().size()-2);
}
);
ret.push_back(Person(v[0], v[1], v[2], v[3], v[4], v[5], v[6],
stoi(v[7]), v[8], v[9], v[10], v[11]));
v.clear();
}
return ret;
}
UNITTEST(double_create) {
Sqlite3 dbImpl(":memory:");
SweetQL<Sqlite3> db(dbImpl);
db.createTable<Person>();
db.createTable<Person>();
// any good way to check existance of a table? SELECT?
}
UNITTEST(drop_non_existing_table) {
Sqlite3 dbImpl(":memory:");
SweetQL<Sqlite3> db(dbImpl);
db.dropTable<Person>();
}
UNITTEST(sweetqltest) {
/*remove("testtable2.db");
Sqlite3 dbImpl("testtable2.db");
SweetQL<Sqlite3> db(dbImpl);
db.createTable<Person>();
db.createTable<Reservation>();
sweet::Bench in;
std::vector<Person> per = parsePersonFile("50000.csv");
in.stop();
std::cout<<"Reading the file took "<<in.milli()<<" msec"<<std::endl;
sweet::Bench insert;
db.insert<Person>(per.begin(), per.end());
insert.stop();
std::cout<<"Writting the persons to the db took "<<insert.milli()
<<" msec SWEETQL"<<std::endl;
Reservation r;
r.firstname = per.front().firstname;
r.lastname = per.front().lastname;
r.tablemem = 1338;
r.location = "China";
r.date = 3482938498;
db.insert(r);
sweet::Bench s;
Person toDel;
auto sel(db.select<Person>());
std::for_each(sel.first, sel.second, [&toDel](const Person& p) {
//std::cout<<p.firstname<<' ';
//std::cout<<p.lastname<<' ';
//std::cout<<p.company<<' ';
//std::cout<<p.address<<' ';
//std::cout<<p.county<<' ';
//std::cout<<p.zip<<' ';
//std::cout<<p.state<<' ';
//std::cout<<p.phoneWork<<' ';
//std::cout<<p.phonePrivat<<' ';
//std::cout<<p.mail<<' ';
//std::cout<<p.www;
//std::cout<<std::endl;
toDel = p;
});
s.stop();
std::cout<<"Iterating the persons of the db took "<<s.milli()
<<" msec SWEETQL"<<std::endl;
auto h = db.join<ReservationPerson,Person,Reservation>();
for(auto it = h.first; it != h.second; ++it) {
std::cout<<it->firstname<<' ';
std::cout<<it->lastname<<' ';
std::cout<<it->company<<' ';
std::cout<<it->address<<' ';
std::cout<<it->county<<' ';
std::cout<<it->zip<<' ';
std::cout<<it->state<<' ';
std::cout<<it->phoneWork<<' ';
std::cout<<it->phonePrivat<<' ';
std::cout<<it->mail<<' ';
std::cout<<it->www;
std::cout<<std::endl;
}
remove("testtable2_odb.db");
dbo::backend::Sqlite3 sqlite3("testtable2_odb.db");
dbo::Session session;
session.setConnection(sqlite3);
session.mapClass<Person>("person");
session.createTables();
sweet::Bench in1;
dbo::Transaction transaction(session);
for(auto& it : per) {
auto p(dbo::ptr<Person>(new Person(it)));
session.add(p);
}
transaction.commit();
in2.stop();
std::cout<<"Writting the persons to the db took "<<in2.milli()
<<" msec WT::DBO"<<std::endl;
sweet::Bench s2;
dbo::Transaction transaction2(session);
dbo::ptr<Person> toDel2;
typedef Wt::Dbo::collection< Wt::Dbo::ptr<Person> > Persons;
Persons ps = session.find<Person>();
for(auto& p : ps) {
//std::cout<<p.firstname<<' ';
//std::cout<<p.lastname<<' ';
//std::cout<<p.company<<' ';
//std::cout<<p.address<<' ';
//std::cout<<p.county<<' ';
//std::cout<<p.zip<<' ';
//std::cout<<p.state<<' ';
//std::cout<<p.phoneWork<<' ';
//std::cout<<p.phonePrivat<<' ';
//std::cout<<p.mail<<' ';
//std::cout<<p.www;
//std::cout<<std::endl;
toDel2 = p;
};
s2.stop();
std::cout<<"Iterating the persons of the db took "<<s2.milli()
<<" msec WT::DBO"<<std::endl;
*/
/*sel = db.select<Person>(
"Firstname=\"Danny\" and Lastname=\"Zeckzer\""
);
std::for_each(sel.first, sel.second, [](const Person& p) {
std::cout<<p.firstname<<' ';
std::cout<<p.lastname<<' ';
std::cout<<p.company<<' ';
std::cout<<p.address<<' ';
std::cout<<p.county<<' ';
std::cout<<p.zip<<' ';
std::cout<<p.state<<' ';
std::cout<<p.phoneWork<<' ';
std::cout<<p.phonePrivat<<' ';
std::cout<<p.mail<<' ';
std::cout<<p.www<<std::endl;
});*/
}
UNITTEST(mysqlinit) {
MySQL m("localhost", "burner", "h5n1x264r", "testdb", 0, "", 0);
}
#ifdef MAIN
int main(int argc, char *argv[])
{
return sweet::Unit::runTests();
}
#endif