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