BmnRoot
Loading...
Searching...
No Matches
ElogPerson.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// ElogPerson cxx file
3// Generated 27-11-2017
4// ----------------------------------------------------------------------
5
6#include "ElogPerson.h"
7
8#include "TSQLServer.h"
9#include "TSQLStatement.h"
10
11#include <iostream>
12using namespace std;
13
14/* GENERATED CLASS MEMBERS (SHOULD NOT BE CHANGED MANUALLY) */
15// ----- Constructor with database connection -----------------------
16ElogPerson::ElogPerson(ElogConnection* db_connect, TString person_id, TString person_name, bool is_active, TString* email)
17{
18 connectionDB = db_connect;
19
20 str_person_id = person_id;
21 str_person_name = person_name;
22 b_is_active = is_active;
23 str_email = email;
24}
25
26// ----- Destructor -------------------------------------------------
28{
29 if (connectionDB)
30 delete connectionDB;
31 if (str_email)
32 delete str_email;
33}
34
35// ----- Creating new person in the database ---------------------------
36ElogPerson* ElogPerson::CreatePerson(TString person_name, bool is_active, TString* email)
37{
39 if (connDb == nullptr)
40 return nullptr;
41
42 TSQLServer* db_server = connDb->GetSQLServer();
43
44 TString sql = TString::Format("insert into person_(person_name, is_active, email) "
45 "values ($1, $2, $3)");
46 TSQLStatement* stmt = db_server->Statement(sql);
47
48 stmt->NextIteration();
49 stmt->SetString(0, person_name);
50 stmt->SetInt(1, is_active);
51 if (email == nullptr)
52 stmt->SetNull(2);
53 else
54 stmt->SetString(2, *email);
55
56 // inserting new person to the Database
57 if (!stmt->Process()) {
58 cout << "ERROR: inserting new person to the Database has been failed" << endl;
59 delete stmt;
60 delete connDb;
61 return nullptr;
62 }
63
64 delete stmt;
65
66 // getting last inserted ID
67 TString person_id;
68 TSQLStatement* stmt_last = db_server->Statement("SELECT currval(pg_get_serial_sequence('person_','person_id'))");
69
70 // process getting last id
71 if (stmt_last->Process()) {
72 // store result of statement in buffer
73 stmt_last->StoreResult();
74
75 // if there is no last id then exit with error
76 if (!stmt_last->NextResultRow()) {
77 cout << "ERROR: no last ID in DB!" << endl;
78 delete stmt_last;
79 return nullptr;
80 } else {
81 person_id = stmt_last->GetString(0);
82 delete stmt_last;
83 }
84 } else {
85 cout << "ERROR: getting last ID has been failed!" << endl;
86 delete stmt_last;
87 return nullptr;
88 }
89
90 TString tmp_person_id;
91 tmp_person_id = person_id;
92 TString tmp_person_name;
93 tmp_person_name = person_name;
94 bool tmp_is_active;
95 tmp_is_active = is_active;
96 TString* tmp_email;
97 if (email == nullptr)
98 tmp_email = nullptr;
99 else
100 tmp_email = new TString(*email);
101
102 return new ElogPerson(connDb, tmp_person_id, tmp_person_name, tmp_is_active, tmp_email);
103}
104
105// ----- Get person from the database ---------------------------
107{
109 if (connDb == nullptr)
110 return nullptr;
111
112 TSQLServer* db_server = connDb->GetSQLServer();
113
114 TString sql = TString::Format("select person_id, person_name, is_active, email "
115 "from person_ "
116 "where person_id = %s",
117 person_id.Data());
118 TSQLStatement* stmt = db_server->Statement(sql);
119
120 // get person from the database
121 if (!stmt->Process()) {
122 cout << "ERROR: getting person from the database has been failed" << endl;
123
124 delete stmt;
125 delete connDb;
126 return nullptr;
127 }
128
129 // store result of statement in buffer
130 stmt->StoreResult();
131
132 // extract row
133 if (!stmt->NextResultRow()) {
134 cout << "ERROR: person was not found in the database" << endl;
135
136 delete stmt;
137 delete connDb;
138 return nullptr;
139 }
140
141 TString tmp_person_id;
142 tmp_person_id = stmt->GetString(0);
143 TString tmp_person_name;
144 tmp_person_name = stmt->GetString(1);
145 bool tmp_is_active;
146 tmp_is_active = stmt->GetInt(2);
147 TString* tmp_email;
148 if (stmt->IsNull(3))
149 tmp_email = nullptr;
150 else
151 tmp_email = new TString(stmt->GetString(3));
152
153 delete stmt;
154
155 return new ElogPerson(connDb, tmp_person_id, tmp_person_name, tmp_is_active, tmp_email);
156}
157
158// ----- Check person exists in the database ---------------------------
159int ElogPerson::CheckPersonExists(TString person_id)
160{
162 if (connDb == nullptr)
163 return -1;
164
165 TSQLServer* db_server = connDb->GetSQLServer();
166
167 TString sql = TString::Format("select 1 "
168 "from person_ "
169 "where person_id = %s",
170 person_id.Data());
171 TSQLStatement* stmt = db_server->Statement(sql);
172
173 // get person from the database
174 if (!stmt->Process()) {
175 cout << "ERROR: getting person from the database has been failed" << endl;
176
177 delete stmt;
178 delete connDb;
179 return -2;
180 }
181
182 // store result of statement in buffer
183 stmt->StoreResult();
184
185 // extract row
186 if (!stmt->NextResultRow()) {
187 delete stmt;
188 delete connDb;
189 return 0;
190 }
191
192 delete stmt;
193 delete connDb;
194
195 return 1;
196}
197
198// ----- Delete person from the database ---------------------------
199int ElogPerson::DeletePerson(TString person_id)
200{
202 if (connDb == nullptr)
203 return -1;
204
205 TSQLServer* db_server = connDb->GetSQLServer();
206
207 TString sql = TString::Format("delete from person_ "
208 "where person_id = $1");
209 TSQLStatement* stmt = db_server->Statement(sql);
210
211 stmt->NextIteration();
212 stmt->SetString(0, person_id);
213
214 // delete person from the dataBase
215 if (!stmt->Process()) {
216 cout << "ERROR: deleting person from the dataBase has been failed" << endl;
217
218 delete stmt;
219 delete connDb;
220 return -2;
221 }
222
223 delete stmt;
224 delete connDb;
225 return 0;
226}
227
228// ----- Print all 'persons' ---------------------------------
230{
232 if (connDb == nullptr)
233 return -1;
234
235 TSQLServer* db_server = connDb->GetSQLServer();
236
237 TString sql = TString::Format("select person_id, person_name, is_active, email "
238 "from person_");
239 TSQLStatement* stmt = db_server->Statement(sql);
240
241 // get all 'persons' from the database
242 if (!stmt->Process()) {
243 cout << "ERROR: getting all 'persons' from the dataBase has been failed" << endl;
244
245 delete stmt;
246 delete connDb;
247 return -2;
248 }
249
250 // store result of statement in buffer
251 stmt->StoreResult();
252
253 // print rows
254 cout << "Table 'person_':" << endl;
255 while (stmt->NextResultRow()) {
256 cout << "person_id: ";
257 cout << (stmt->GetString(0));
258 cout << ", person_name: ";
259 cout << (stmt->GetString(1));
260 cout << ", is_active: ";
261 cout << (stmt->GetInt(2));
262 cout << ", email: ";
263 if (stmt->IsNull(3))
264 cout << "nullptr";
265 else
266 cout << stmt->GetString(3);
267 cout << "." << endl;
268 }
269
270 delete stmt;
271 delete connDb;
272
273 return 0;
274}
275
276// Setters functions
277int ElogPerson::SetPersonName(TString person_name)
278{
279 if (!connectionDB) {
280 cout << "CRITICAL ERROR: Connection object is null" << endl;
281 return -1;
282 }
283
284 TSQLServer* db_server = connectionDB->GetSQLServer();
285
286 TString sql = TString::Format("update person_ "
287 "set person_name = $1 "
288 "where person_id = $2");
289 TSQLStatement* stmt = db_server->Statement(sql);
290
291 stmt->NextIteration();
292 stmt->SetString(0, person_name);
293 stmt->SetString(1, str_person_id);
294
295 // write new value to the database
296 if (!stmt->Process()) {
297 cout << "ERROR: updating information about person has been failed" << endl;
298
299 delete stmt;
300 return -2;
301 }
302
303 str_person_name = person_name;
304
305 delete stmt;
306 return 0;
307}
308
309int ElogPerson::SetIsActive(bool is_active)
310{
311 if (!connectionDB) {
312 cout << "CRITICAL ERROR: Connection object is null" << endl;
313 return -1;
314 }
315
316 TSQLServer* db_server = connectionDB->GetSQLServer();
317
318 TString sql = TString::Format("update person_ "
319 "set is_active = $1 "
320 "where person_id = $2");
321 TSQLStatement* stmt = db_server->Statement(sql);
322
323 stmt->NextIteration();
324 stmt->SetInt(0, is_active);
325 stmt->SetString(1, str_person_id);
326
327 // write new value to the database
328 if (!stmt->Process()) {
329 cout << "ERROR: updating information about person has been failed" << endl;
330
331 delete stmt;
332 return -2;
333 }
334
335 b_is_active = is_active;
336
337 delete stmt;
338 return 0;
339}
340
341int ElogPerson::SetEmail(TString* email)
342{
343 if (!connectionDB) {
344 cout << "CRITICAL ERROR: Connection object is null" << endl;
345 return -1;
346 }
347
348 TSQLServer* db_server = connectionDB->GetSQLServer();
349
350 TString sql = TString::Format("update person_ "
351 "set email = $1 "
352 "where person_id = $2");
353 TSQLStatement* stmt = db_server->Statement(sql);
354
355 stmt->NextIteration();
356 if (email == nullptr)
357 stmt->SetNull(0);
358 else
359 stmt->SetString(0, *email);
360 stmt->SetString(1, str_person_id);
361
362 // write new value to the database
363 if (!stmt->Process()) {
364 cout << "ERROR: updating information about person has been failed" << endl;
365
366 delete stmt;
367 return -2;
368 }
369
370 if (str_email)
371 delete str_email;
372 if (email == nullptr)
373 str_email = nullptr;
374 else
375 str_email = new TString(*email);
376
377 delete stmt;
378 return 0;
379}
380
381// ----- Print current person ---------------------------------------
383{
384 cout << "Table 'person_'";
385 cout << ". person_id: " << str_person_id << ". person_name: " << str_person_name << ". is_active: " << b_is_active
386 << ". email: " << (str_email == nullptr ? "nullptr" : *str_email) << endl;
387
388 return;
389}
390/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
391
392// ----- Get persons from the database by name or its part -----
393TObjArray* ElogPerson::GetPersonByName(TString person_name_part)
394{
395 TObjArray* arrayResult = nullptr;
396
398 if (connDb == nullptr)
399 {
400 cout << "ERROR: connection to the eLog Database was failed" << endl;
401 return arrayResult;
402 }
403
404 TSQLServer* db_server = connDb->GetSQLServer();
405
406 TString sql = TString::Format("select person_id, person_name, is_active, email "
407 "from person_ "
408 "where lower(person_name) like lower('%s')",
409 person_name_part.Data());
410 TSQLStatement* stmt = db_server->Statement(sql);
411
412 // get persons from the database
413 if (!stmt->Process()) {
414 cout << "ERROR: getting person from the database has been failed" << endl;
415
416 delete stmt;
417 delete connDb;
418 return arrayResult;
419 }
420
421 // store result of statement in buffer
422 stmt->StoreResult();
423
424 // extract rows one after another
425 arrayResult = new TObjArray();
426 arrayResult->SetOwner(kTRUE);
427 while (stmt->NextResultRow()) {
428 ElogConnection* connRecord = ElogConnection::Open();
429 if (connRecord == nullptr) {
430 cout << "ERROR: connection to the eLog database for a record was failed" << endl;
431 return arrayResult;
432 }
433
434 TString tmp_person_id;
435 tmp_person_id = stmt->GetString(0);
436 TString tmp_person_name;
437 tmp_person_name = stmt->GetString(1);
438 bool tmp_is_active;
439 tmp_is_active = stmt->GetInt(2);
440 TString* tmp_email;
441 if (stmt->IsNull(3))
442 tmp_email = nullptr;
443 else
444 tmp_email = new TString(stmt->GetString(3));
445
446 arrayResult->Add((TObject*)new ElogPerson(
447 connDb, tmp_person_id, tmp_person_name, tmp_is_active, tmp_email));
448 }
449
450 delete stmt;
451
452 return arrayResult;
453}
454
455// ----- Check persons exists in the database by name or its part: ------------
456// ----- >=1 - person count, 0 - false, <0 - database operation error ---------
457int ElogPerson::CheckPersonExistsByName(TString person_name_part)
458{
460 if (connDb == nullptr)
461 return -1;
462
463 TSQLServer* db_server = connDb->GetSQLServer();
464
465 TString sql = TString::Format("select count(person_id) "
466 "from person_ "
467 "where lower(person_name) like lower('%s')",
468 person_name_part.Data());
469 TSQLStatement* stmt = db_server->Statement(sql);
470
471 // get person from the database
472 if (!stmt->Process()) {
473 cout << "ERROR: checking persons in the database has been failed" << endl;
474
475 delete stmt;
476 delete connDb;
477 return -2;
478 }
479
480 // store result of statement in buffer
481 stmt->StoreResult();
482
483 // extract row
484 if (!stmt->NextResultRow()) {
485 delete stmt;
486 delete connDb;
487 return 0;
488 }
489
490 int person_count = stmt->GetInt(0);
491
492 delete stmt;
493 delete connDb;
494
495 return person_count;
496}
497
498// ----- Delete persons from the database by name or its part -----------------
499// ----- >=1 - affected count, 0 - persons not found, <0 - database error -----
500int ElogPerson::DeletePersonByName(TString person_name_part)
501{
503 if (connDb == nullptr)
504 return -1;
505
506 TSQLServer* db_server = connDb->GetSQLServer();
507
508 TString sql = TString::Format("delete from person_ "
509 "where lower(person_name) like lower($1)");
510 TSQLStatement* stmt = db_server->Statement(sql);
511
512 stmt->NextIteration();
513 stmt->SetString(0, person_name_part);
514
515 // delete person from the dataBase
516 if (!stmt->Process()) {
517 cout << "ERROR: deleting persons from the database has been failed" << endl;
518
519 delete stmt;
520 delete connDb;
521 return -2;
522 }
523
524 // get affected rows after Process()
525 Int_t deleted_count = stmt->GetNumAffectedRows();
526
527 delete stmt;
528 delete connDb;
529
530 return deleted_count;
531}
static ElogConnection * Open()
TSQLServer * GetSQLServer()
static int CheckPersonExistsByName(TString person_name_part)
int SetIsActive(bool is_active)
set is active of the current person
static ElogPerson * CreatePerson(TString person_name, bool is_active, TString *email)
add new person to the database
static int PrintAll()
print all persons
static int DeletePersonByName(TString person_name_part)
static TObjArray * GetPersonByName(TString person_name_part)
get persons from the database by name or its part
static ElogPerson * GetPerson(TString person_id)
get person from the database
void Print()
print information about current person
int SetEmail(TString *email)
set email of the current person
virtual ~ElogPerson()
int SetPersonName(TString person_name)
set person name of the current person
static int CheckPersonExists(TString person_id)
check person exists in the database: 1 - true, 0 - false, <0 - database operation error
static int DeletePerson(TString person_id)
delete person from the database
STL namespace.