-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepartment.cc
More file actions
504 lines (485 loc) · 13 KB
/
Department.cc
File metadata and controls
504 lines (485 loc) · 13 KB
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
/**
*
* Department.cc
* DO NOT EDIT. This file is generated by drogon_ctl
*
*/
#include "Department.h"
#include "Person.h"
#include <drogon/utils/Utilities.h>
#include <string>
using namespace drogon;
using namespace drogon::orm;
using namespace drogon_model::org_chart;
const std::string Department::Cols::_id = "id";
const std::string Department::Cols::_name = "name";
const std::string Department::primaryKeyName = "id";
const bool Department::hasPrimaryKey = true;
const std::string Department::tableName = "department";
const std::vector<typename Department::MetaData> Department::metaData_={
{"id","int32_t","integer",4,1,1,1},
{"name","std::string","character varying",50,0,0,1}
};
const std::string &Department::getColumnName(size_t index) noexcept(false)
{
assert(index < metaData_.size());
return metaData_[index].colName_;
}
Department::Department(const Row &r, const ssize_t indexOffset) noexcept
{
if(indexOffset < 0)
{
if(!r["id"].isNull())
{
id_=std::make_shared<int32_t>(r["id"].as<int32_t>());
}
if(!r["name"].isNull())
{
name_=std::make_shared<std::string>(r["name"].as<std::string>());
}
}
else
{
size_t offset = (size_t)indexOffset;
if(offset + 2 > r.size())
{
LOG_FATAL << "Invalid SQL result for this model";
return;
}
size_t index;
index = offset + 0;
if(!r[index].isNull())
{
id_=std::make_shared<int32_t>(r[index].as<int32_t>());
}
index = offset + 1;
if(!r[index].isNull())
{
name_=std::make_shared<std::string>(r[index].as<std::string>());
}
}
}
Department::Department(const Json::Value &pJson, const std::vector<std::string> &pMasqueradingVector) noexcept(false)
{
if(pMasqueradingVector.size() != 2)
{
LOG_ERROR << "Bad masquerading vector";
return;
}
if(!pMasqueradingVector[0].empty() && pJson.isMember(pMasqueradingVector[0]))
{
dirtyFlag_[0] = true;
if(!pJson[pMasqueradingVector[0]].isNull())
{
id_=std::make_shared<int32_t>((int32_t)pJson[pMasqueradingVector[0]].asInt64());
}
}
if(!pMasqueradingVector[1].empty() && pJson.isMember(pMasqueradingVector[1]))
{
dirtyFlag_[1] = true;
if(!pJson[pMasqueradingVector[1]].isNull())
{
name_=std::make_shared<std::string>(pJson[pMasqueradingVector[1]].asString());
}
}
}
Department::Department(const Json::Value &pJson) noexcept(false)
{
if(pJson.isMember("id"))
{
dirtyFlag_[0]=true;
if(!pJson["id"].isNull())
{
id_=std::make_shared<int32_t>((int32_t)pJson["id"].asInt64());
}
}
if(pJson.isMember("name"))
{
dirtyFlag_[1]=true;
if(!pJson["name"].isNull())
{
name_=std::make_shared<std::string>(pJson["name"].asString());
}
}
}
void Department::updateByMasqueradedJson(const Json::Value &pJson,
const std::vector<std::string> &pMasqueradingVector) noexcept(false)
{
if(pMasqueradingVector.size() != 2)
{
LOG_ERROR << "Bad masquerading vector";
return;
}
if(!pMasqueradingVector[0].empty() && pJson.isMember(pMasqueradingVector[0]))
{
if(!pJson[pMasqueradingVector[0]].isNull())
{
id_=std::make_shared<int32_t>((int32_t)pJson[pMasqueradingVector[0]].asInt64());
}
}
if(!pMasqueradingVector[1].empty() && pJson.isMember(pMasqueradingVector[1]))
{
dirtyFlag_[1] = true;
if(!pJson[pMasqueradingVector[1]].isNull())
{
name_=std::make_shared<std::string>(pJson[pMasqueradingVector[1]].asString());
}
}
}
void Department::updateByJson(const Json::Value &pJson) noexcept(false)
{
if(pJson.isMember("id"))
{
if(!pJson["id"].isNull())
{
id_=std::make_shared<int32_t>((int32_t)pJson["id"].asInt64());
}
}
if(pJson.isMember("name"))
{
dirtyFlag_[1] = true;
if(!pJson["name"].isNull())
{
name_=std::make_shared<std::string>(pJson["name"].asString());
}
}
}
const int32_t &Department::getValueOfId() const noexcept
{
const static int32_t defaultValue = int32_t();
if(id_)
return *id_;
return defaultValue;
}
const std::shared_ptr<int32_t> &Department::getId() const noexcept
{
return id_;
}
void Department::setId(const int32_t &pId) noexcept
{
id_ = std::make_shared<int32_t>(pId);
dirtyFlag_[0] = true;
}
const typename Department::PrimaryKeyType & Department::getPrimaryKey() const
{
assert(id_);
return *id_;
}
const std::string &Department::getValueOfName() const noexcept
{
const static std::string defaultValue = std::string();
if(name_)
return *name_;
return defaultValue;
}
const std::shared_ptr<std::string> &Department::getName() const noexcept
{
return name_;
}
void Department::setName(const std::string &pName) noexcept
{
name_ = std::make_shared<std::string>(pName);
dirtyFlag_[1] = true;
}
void Department::setName(std::string &&pName) noexcept
{
name_ = std::make_shared<std::string>(std::move(pName));
dirtyFlag_[1] = true;
}
void Department::updateId(const uint64_t id)
{
}
const std::vector<std::string> &Department::insertColumns() noexcept
{
static const std::vector<std::string> inCols={
"name"
};
return inCols;
}
void Department::outputArgs(drogon::orm::internal::SqlBinder &binder) const
{
if(dirtyFlag_[1])
{
if(getName())
{
binder << getValueOfName();
}
else
{
binder << nullptr;
}
}
}
const std::vector<std::string> Department::updateColumns() const
{
std::vector<std::string> ret;
if(dirtyFlag_[1])
{
ret.push_back(getColumnName(1));
}
return ret;
}
void Department::updateArgs(drogon::orm::internal::SqlBinder &binder) const
{
if(dirtyFlag_[1])
{
if(getName())
{
binder << getValueOfName();
}
else
{
binder << nullptr;
}
}
}
Json::Value Department::toJson() const
{
Json::Value ret;
if(getId())
{
ret["id"]=getValueOfId();
}
else
{
ret["id"]=Json::Value();
}
if(getName())
{
ret["name"]=getValueOfName();
}
else
{
ret["name"]=Json::Value();
}
return ret;
}
Json::Value Department::toMasqueradedJson(
const std::vector<std::string> &pMasqueradingVector) const
{
Json::Value ret;
if(pMasqueradingVector.size() == 2)
{
if(!pMasqueradingVector[0].empty())
{
if(getId())
{
ret[pMasqueradingVector[0]]=getValueOfId();
}
else
{
ret[pMasqueradingVector[0]]=Json::Value();
}
}
if(!pMasqueradingVector[1].empty())
{
if(getName())
{
ret[pMasqueradingVector[1]]=getValueOfName();
}
else
{
ret[pMasqueradingVector[1]]=Json::Value();
}
}
return ret;
}
LOG_ERROR << "Masquerade failed";
if(getId())
{
ret["id"]=getValueOfId();
}
else
{
ret["id"]=Json::Value();
}
if(getName())
{
ret["name"]=getValueOfName();
}
else
{
ret["name"]=Json::Value();
}
return ret;
}
bool Department::validateJsonForCreation(const Json::Value &pJson, std::string &err)
{
if(pJson.isMember("id"))
{
if(!validJsonOfField(0, "id", pJson["id"], err, true))
return false;
}
if(pJson.isMember("name"))
{
if(!validJsonOfField(1, "name", pJson["name"], err, true))
return false;
}
else
{
err="The name column cannot be null";
return false;
}
return true;
}
bool Department::validateMasqueradedJsonForCreation(const Json::Value &pJson,
const std::vector<std::string> &pMasqueradingVector,
std::string &err)
{
if(pMasqueradingVector.size() != 2)
{
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;
}
}
if(!pMasqueradingVector[1].empty())
{
if(pJson.isMember(pMasqueradingVector[1]))
{
if(!validJsonOfField(1, pMasqueradingVector[1], pJson[pMasqueradingVector[1]], err, true))
return false;
}
else
{
err="The " + pMasqueradingVector[1] + " column cannot be null";
return false;
}
}
}
catch(const Json::LogicError &e)
{
err = e.what();
return false;
}
return true;
}
bool Department::validateJsonForUpdate(const Json::Value &pJson, std::string &err)
{
if(pJson.isMember("id"))
{
if(!validJsonOfField(0, "id", pJson["id"], err, false))
return false;
}
else
{
err = "The value of primary key must be set in the json object for update";
return false;
}
if(pJson.isMember("name"))
{
if(!validJsonOfField(1, "name", pJson["name"], err, false))
return false;
}
return true;
}
bool Department::validateMasqueradedJsonForUpdate(const Json::Value &pJson,
const std::vector<std::string> &pMasqueradingVector,
std::string &err)
{
if(pMasqueradingVector.size() != 2)
{
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;
}
else
{
err = "The value of primary key must be set in the json object for update";
return false;
}
if(!pMasqueradingVector[1].empty() && pJson.isMember(pMasqueradingVector[1]))
{
if(!validJsonOfField(1, pMasqueradingVector[1], pJson[pMasqueradingVector[1]], err, false))
return false;
}
}
catch(const Json::LogicError &e)
{
err = e.what();
return false;
}
return true;
}
bool Department::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(isForCreation)
{
err="The automatic primary key cannot be set";
return false;
}
if(!pJson.isInt())
{
err="Type error in the "+fieldName+" field";
return false;
}
break;
case 1:
if(pJson.isNull())
{
err="The " + fieldName + " column cannot be null";
return false;
}
if(!pJson.isString())
{
err="Type error in the "+fieldName+" field";
return false;
}
// asString().length() creates a string object, is there any better way to validate the length?
if(pJson.isString() && pJson.asString().length() > 50)
{
err="String length exceeds limit for the " +
fieldName +
" field (the maximum value is 50)";
return false;
}
break;
default:
err="Internal error in the server";
return false;
break;
}
return true;
}
void Department::getPersons(const DbClientPtr &clientPtr,
const std::function<void(std::vector<Person>)> &rcb,
const ExceptionCallback &ecb) const
{
const static std::string sql = "select * from person where department_id = $1";
*clientPtr << sql
<< *id_
>> [rcb = std::move(rcb)](const Result &r){
std::vector<Person> ret;
ret.reserve(r.size());
for (auto const &row : r)
{
ret.emplace_back(Person(row));
}
rcb(ret);
}
>> ecb;
}