-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatuses.cc
610 lines (592 loc) · 19.9 KB
/
Statuses.cc
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/**
*
* Statuses.cc
* DO NOT EDIT. This file is generated by drogon_ctl
*
*/
#include "Statuses.h"
#include <drogon/utils/Utilities.h>
#include <string>
using namespace drogon;
using namespace drogon::orm;
using namespace drogon_model::cavoke_orm;
const std::string Statuses::Cols::_session_id = "session_id";
const std::string Statuses::Cols::_status = "status";
const std::string Statuses::Cols::_saved_on = "saved_on";
const std::string Statuses::primaryKeyName = "";
const bool Statuses::hasPrimaryKey = false;
const std::string Statuses::tableName = "statuses";
const std::vector<typename Statuses::MetaData> Statuses::metaData_ = {
{"session_id", "std::string", "uuid", 0, 0, 0, 1},
{"status", "int32_t", "integer", 4, 0, 0, 0},
{"saved_on", "::trantor::Date", "timestamp without time zone", 0, 0, 0, 0}};
const std::string &Statuses::getColumnName(size_t index) noexcept(false) {
assert(index < metaData_.size());
return metaData_[index].colName_;
}
Statuses::Statuses(const Row &r, const ssize_t indexOffset) noexcept {
if (indexOffset < 0) {
if (!r["session_id"].isNull()) {
sessionId_ = std::make_shared<std::string>(
r["session_id"].as<std::string>());
}
if (!r["status"].isNull()) {
status_ = std::make_shared<int32_t>(r["status"].as<int32_t>());
}
if (!r["saved_on"].isNull()) {
auto timeStr = r["saved_on"].as<std::string>();
struct tm stm;
memset(&stm, 0, sizeof(stm));
auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm);
time_t t = mktime(&stm);
size_t decimalNum = 0;
if (p) {
if (*p == '.') {
std::string decimals(p + 1, &timeStr[timeStr.length()]);
while (decimals.length() < 6) {
decimals += "0";
}
decimalNum = (size_t)atol(decimals.c_str());
}
savedOn_ =
std::make_shared<::trantor::Date>(t * 1000000 + decimalNum);
}
}
} else {
size_t offset = (size_t)indexOffset;
if (offset + 3 > r.size()) {
LOG_FATAL << "Invalid SQL result for this model";
return;
}
size_t index;
index = offset + 0;
if (!r[index].isNull()) {
sessionId_ =
std::make_shared<std::string>(r[index].as<std::string>());
}
index = offset + 1;
if (!r[index].isNull()) {
status_ = std::make_shared<int32_t>(r[index].as<int32_t>());
}
index = offset + 2;
if (!r[index].isNull()) {
auto timeStr = r[index].as<std::string>();
struct tm stm;
memset(&stm, 0, sizeof(stm));
auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm);
time_t t = mktime(&stm);
size_t decimalNum = 0;
if (p) {
if (*p == '.') {
std::string decimals(p + 1, &timeStr[timeStr.length()]);
while (decimals.length() < 6) {
decimals += "0";
}
decimalNum = (size_t)atol(decimals.c_str());
}
savedOn_ =
std::make_shared<::trantor::Date>(t * 1000000 + decimalNum);
}
}
}
}
Statuses::Statuses(
const Json::Value &pJson,
const std::vector<std::string> &pMasqueradingVector) noexcept(false) {
if (pMasqueradingVector.size() != 3) {
LOG_ERROR << "Bad masquerading vector";
return;
}
if (!pMasqueradingVector[0].empty() &&
pJson.isMember(pMasqueradingVector[0])) {
dirtyFlag_[0] = true;
if (!pJson[pMasqueradingVector[0]].isNull()) {
sessionId_ = std::make_shared<std::string>(
pJson[pMasqueradingVector[0]].asString());
}
}
if (!pMasqueradingVector[1].empty() &&
pJson.isMember(pMasqueradingVector[1])) {
dirtyFlag_[1] = true;
if (!pJson[pMasqueradingVector[1]].isNull()) {
status_ = std::make_shared<int32_t>(
(int32_t)pJson[pMasqueradingVector[1]].asInt64());
}
}
if (!pMasqueradingVector[2].empty() &&
pJson.isMember(pMasqueradingVector[2])) {
dirtyFlag_[2] = true;
if (!pJson[pMasqueradingVector[2]].isNull()) {
auto timeStr = pJson[pMasqueradingVector[2]].asString();
struct tm stm;
memset(&stm, 0, sizeof(stm));
auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm);
time_t t = mktime(&stm);
size_t decimalNum = 0;
if (p) {
if (*p == '.') {
std::string decimals(p + 1, &timeStr[timeStr.length()]);
while (decimals.length() < 6) {
decimals += "0";
}
decimalNum = (size_t)atol(decimals.c_str());
}
savedOn_ =
std::make_shared<::trantor::Date>(t * 1000000 + decimalNum);
}
}
}
}
Statuses::Statuses(const Json::Value &pJson) noexcept(false) {
if (pJson.isMember("session_id")) {
dirtyFlag_[0] = true;
if (!pJson["session_id"].isNull()) {
sessionId_ =
std::make_shared<std::string>(pJson["session_id"].asString());
}
}
if (pJson.isMember("status")) {
dirtyFlag_[1] = true;
if (!pJson["status"].isNull()) {
status_ =
std::make_shared<int32_t>((int32_t)pJson["status"].asInt64());
}
}
if (pJson.isMember("saved_on")) {
dirtyFlag_[2] = true;
if (!pJson["saved_on"].isNull()) {
auto timeStr = pJson["saved_on"].asString();
struct tm stm;
memset(&stm, 0, sizeof(stm));
auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm);
time_t t = mktime(&stm);
size_t decimalNum = 0;
if (p) {
if (*p == '.') {
std::string decimals(p + 1, &timeStr[timeStr.length()]);
while (decimals.length() < 6) {
decimals += "0";
}
decimalNum = (size_t)atol(decimals.c_str());
}
savedOn_ =
std::make_shared<::trantor::Date>(t * 1000000 + decimalNum);
}
}
}
}
void Statuses::updateByMasqueradedJson(
const Json::Value &pJson,
const std::vector<std::string> &pMasqueradingVector) noexcept(false) {
if (pMasqueradingVector.size() != 3) {
LOG_ERROR << "Bad masquerading vector";
return;
}
if (!pMasqueradingVector[0].empty() &&
pJson.isMember(pMasqueradingVector[0])) {
dirtyFlag_[0] = true;
if (!pJson[pMasqueradingVector[0]].isNull()) {
sessionId_ = std::make_shared<std::string>(
pJson[pMasqueradingVector[0]].asString());
}
}
if (!pMasqueradingVector[1].empty() &&
pJson.isMember(pMasqueradingVector[1])) {
dirtyFlag_[1] = true;
if (!pJson[pMasqueradingVector[1]].isNull()) {
status_ = std::make_shared<int32_t>(
(int32_t)pJson[pMasqueradingVector[1]].asInt64());
}
}
if (!pMasqueradingVector[2].empty() &&
pJson.isMember(pMasqueradingVector[2])) {
dirtyFlag_[2] = true;
if (!pJson[pMasqueradingVector[2]].isNull()) {
auto timeStr = pJson[pMasqueradingVector[2]].asString();
struct tm stm;
memset(&stm, 0, sizeof(stm));
auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm);
time_t t = mktime(&stm);
size_t decimalNum = 0;
if (p) {
if (*p == '.') {
std::string decimals(p + 1, &timeStr[timeStr.length()]);
while (decimals.length() < 6) {
decimals += "0";
}
decimalNum = (size_t)atol(decimals.c_str());
}
savedOn_ =
std::make_shared<::trantor::Date>(t * 1000000 + decimalNum);
}
}
}
}
void Statuses::updateByJson(const Json::Value &pJson) noexcept(false) {
if (pJson.isMember("session_id")) {
dirtyFlag_[0] = true;
if (!pJson["session_id"].isNull()) {
sessionId_ =
std::make_shared<std::string>(pJson["session_id"].asString());
}
}
if (pJson.isMember("status")) {
dirtyFlag_[1] = true;
if (!pJson["status"].isNull()) {
status_ =
std::make_shared<int32_t>((int32_t)pJson["status"].asInt64());
}
}
if (pJson.isMember("saved_on")) {
dirtyFlag_[2] = true;
if (!pJson["saved_on"].isNull()) {
auto timeStr = pJson["saved_on"].asString();
struct tm stm;
memset(&stm, 0, sizeof(stm));
auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm);
time_t t = mktime(&stm);
size_t decimalNum = 0;
if (p) {
if (*p == '.') {
std::string decimals(p + 1, &timeStr[timeStr.length()]);
while (decimals.length() < 6) {
decimals += "0";
}
decimalNum = (size_t)atol(decimals.c_str());
}
savedOn_ =
std::make_shared<::trantor::Date>(t * 1000000 + decimalNum);
}
}
}
}
const std::string &Statuses::getValueOfSessionId() const noexcept {
const static std::string defaultValue = std::string();
if (sessionId_)
return *sessionId_;
return defaultValue;
}
const std::shared_ptr<std::string> &Statuses::getSessionId() const noexcept {
return sessionId_;
}
void Statuses::setSessionId(const std::string &pSessionId) noexcept {
sessionId_ = std::make_shared<std::string>(pSessionId);
dirtyFlag_[0] = true;
}
void Statuses::setSessionId(std::string &&pSessionId) noexcept {
sessionId_ = std::make_shared<std::string>(std::move(pSessionId));
dirtyFlag_[0] = true;
}
const int32_t &Statuses::getValueOfStatus() const noexcept {
const static int32_t defaultValue = int32_t();
if (status_)
return *status_;
return defaultValue;
}
const std::shared_ptr<int32_t> &Statuses::getStatus() const noexcept {
return status_;
}
void Statuses::setStatus(const int32_t &pStatus) noexcept {
status_ = std::make_shared<int32_t>(pStatus);
dirtyFlag_[1] = true;
}
void Statuses::setStatusToNull() noexcept {
status_.reset();
dirtyFlag_[1] = true;
}
const ::trantor::Date &Statuses::getValueOfSavedOn() const noexcept {
const static ::trantor::Date defaultValue = ::trantor::Date();
if (savedOn_)
return *savedOn_;
return defaultValue;
}
const std::shared_ptr<::trantor::Date> &Statuses::getSavedOn() const noexcept {
return savedOn_;
}
void Statuses::setSavedOn(const ::trantor::Date &pSavedOn) noexcept {
savedOn_ = std::make_shared<::trantor::Date>(pSavedOn);
dirtyFlag_[2] = true;
}
void Statuses::setSavedOnToNull() noexcept {
savedOn_.reset();
dirtyFlag_[2] = true;
}
void Statuses::updateId(const uint64_t id) {
}
const std::vector<std::string> &Statuses::insertColumns() noexcept {
static const std::vector<std::string> inCols = {"session_id", "status",
"saved_on"};
return inCols;
}
void Statuses::outputArgs(drogon::orm::internal::SqlBinder &binder) const {
if (dirtyFlag_[0]) {
if (getSessionId()) {
binder << getValueOfSessionId();
} else {
binder << nullptr;
}
}
if (dirtyFlag_[1]) {
if (getStatus()) {
binder << getValueOfStatus();
} else {
binder << nullptr;
}
}
if (dirtyFlag_[2]) {
if (getSavedOn()) {
binder << getValueOfSavedOn();
} else {
binder << nullptr;
}
}
}
const std::vector<std::string> Statuses::updateColumns() const {
std::vector<std::string> ret;
if (dirtyFlag_[0]) {
ret.push_back(getColumnName(0));
}
if (dirtyFlag_[1]) {
ret.push_back(getColumnName(1));
}
if (dirtyFlag_[2]) {
ret.push_back(getColumnName(2));
}
return ret;
}
void Statuses::updateArgs(drogon::orm::internal::SqlBinder &binder) const {
if (dirtyFlag_[0]) {
if (getSessionId()) {
binder << getValueOfSessionId();
} else {
binder << nullptr;
}
}
if (dirtyFlag_[1]) {
if (getStatus()) {
binder << getValueOfStatus();
} else {
binder << nullptr;
}
}
if (dirtyFlag_[2]) {
if (getSavedOn()) {
binder << getValueOfSavedOn();
} else {
binder << nullptr;
}
}
}
Json::Value Statuses::toJson() const {
Json::Value ret;
if (getSessionId()) {
ret["session_id"] = getValueOfSessionId();
} else {
ret["session_id"] = Json::Value();
}
if (getStatus()) {
ret["status"] = getValueOfStatus();
} else {
ret["status"] = Json::Value();
}
if (getSavedOn()) {
ret["saved_on"] = getSavedOn()->toDbStringLocal();
} else {
ret["saved_on"] = Json::Value();
}
return ret;
}
Json::Value Statuses::toMasqueradedJson(
const std::vector<std::string> &pMasqueradingVector) const {
Json::Value ret;
if (pMasqueradingVector.size() == 3) {
if (!pMasqueradingVector[0].empty()) {
if (getSessionId()) {
ret[pMasqueradingVector[0]] = getValueOfSessionId();
} else {
ret[pMasqueradingVector[0]] = Json::Value();
}
}
if (!pMasqueradingVector[1].empty()) {
if (getStatus()) {
ret[pMasqueradingVector[1]] = getValueOfStatus();
} else {
ret[pMasqueradingVector[1]] = Json::Value();
}
}
if (!pMasqueradingVector[2].empty()) {
if (getSavedOn()) {
ret[pMasqueradingVector[2]] = getSavedOn()->toDbStringLocal();
} else {
ret[pMasqueradingVector[2]] = Json::Value();
}
}
return ret;
}
LOG_ERROR << "Masquerade failed";
if (getSessionId()) {
ret["session_id"] = getValueOfSessionId();
} else {
ret["session_id"] = Json::Value();
}
if (getStatus()) {
ret["status"] = getValueOfStatus();
} else {
ret["status"] = Json::Value();
}
if (getSavedOn()) {
ret["saved_on"] = getSavedOn()->toDbStringLocal();
} else {
ret["saved_on"] = Json::Value();
}
return ret;
}
bool Statuses::validateJsonForCreation(const Json::Value &pJson,
std::string &err) {
if (pJson.isMember("session_id")) {
if (!validJsonOfField(0, "session_id", pJson["session_id"], err, true))
return false;
} else {
err = "The session_id column cannot be null";
return false;
}
if (pJson.isMember("status")) {
if (!validJsonOfField(1, "status", pJson["status"], err, true))
return false;
}
if (pJson.isMember("saved_on")) {
if (!validJsonOfField(2, "saved_on", pJson["saved_on"], err, true))
return false;
}
return true;
}
bool Statuses::validateMasqueradedJsonForCreation(
const Json::Value &pJson,
const std::vector<std::string> &pMasqueradingVector,
std::string &err) {
if (pMasqueradingVector.size() != 3) {
err = "Bad masquerading vector";
return false;
}
try {
if (!pMasqueradingVector[0].empty()) {
if (pJson.isMember(pMasqueradingVector[0])) {
if (!validJsonOfField(0, pMasqueradingVector[0],
pJson[pMasqueradingVector[0]], err, true))
return false;
} else {
err =
"The " + pMasqueradingVector[0] + " column cannot be null";
return false;
}
}
if (!pMasqueradingVector[1].empty()) {
if (pJson.isMember(pMasqueradingVector[1])) {
if (!validJsonOfField(1, pMasqueradingVector[1],
pJson[pMasqueradingVector[1]], err, true))
return false;
}
}
if (!pMasqueradingVector[2].empty()) {
if (pJson.isMember(pMasqueradingVector[2])) {
if (!validJsonOfField(2, pMasqueradingVector[2],
pJson[pMasqueradingVector[2]], err, true))
return false;
}
}
} catch (const Json::LogicError &e) {
err = e.what();
return false;
}
return true;
}
bool Statuses::validateJsonForUpdate(const Json::Value &pJson,
std::string &err) {
if (pJson.isMember("session_id")) {
if (!validJsonOfField(0, "session_id", pJson["session_id"], err, false))
return false;
}
if (pJson.isMember("status")) {
if (!validJsonOfField(1, "status", pJson["status"], err, false))
return false;
}
if (pJson.isMember("saved_on")) {
if (!validJsonOfField(2, "saved_on", pJson["saved_on"], err, false))
return false;
}
return true;
}
bool Statuses::validateMasqueradedJsonForUpdate(
const Json::Value &pJson,
const std::vector<std::string> &pMasqueradingVector,
std::string &err) {
if (pMasqueradingVector.size() != 3) {
err = "Bad masquerading vector";
return false;
}
try {
if (!pMasqueradingVector[0].empty() &&
pJson.isMember(pMasqueradingVector[0])) {
if (!validJsonOfField(0, pMasqueradingVector[0],
pJson[pMasqueradingVector[0]], err, false))
return false;
}
if (!pMasqueradingVector[1].empty() &&
pJson.isMember(pMasqueradingVector[1])) {
if (!validJsonOfField(1, pMasqueradingVector[1],
pJson[pMasqueradingVector[1]], err, false))
return false;
}
if (!pMasqueradingVector[2].empty() &&
pJson.isMember(pMasqueradingVector[2])) {
if (!validJsonOfField(2, pMasqueradingVector[2],
pJson[pMasqueradingVector[2]], err, false))
return false;
}
} catch (const Json::LogicError &e) {
err = e.what();
return false;
}
return true;
}
bool Statuses::validJsonOfField(size_t index,
const std::string &fieldName,
const Json::Value &pJson,
std::string &err,
bool isForCreation) {
switch (index) {
case 0:
if (pJson.isNull()) {
err = "The " + fieldName + " column cannot be null";
return false;
}
if (!pJson.isString()) {
err = "Type error in the " + fieldName + " field";
return false;
}
break;
case 1:
if (pJson.isNull()) {
return true;
}
if (!pJson.isInt()) {
err = "Type error in the " + fieldName + " field";
return false;
}
break;
case 2:
if (pJson.isNull()) {
return true;
}
if (!pJson.isString()) {
err = "Type error in the " + fieldName + " field";
return false;
}
break;
default:
err = "Internal error in the server";
return false;
break;
}
return true;
}