9#include "TSQLStatement.h"
16ElogPerson::ElogPerson(
ElogConnection* db_connect, TString person_id, TString person_name,
bool is_active, TString* email)
18 connectionDB = db_connect;
20 str_person_id = person_id;
21 str_person_name = person_name;
22 b_is_active = is_active;
39 if (connDb ==
nullptr)
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);
48 stmt->NextIteration();
49 stmt->SetString(0, person_name);
50 stmt->SetInt(1, is_active);
54 stmt->SetString(2, *email);
57 if (!stmt->Process()) {
58 cout <<
"ERROR: inserting new person to the Database has been failed" << endl;
68 TSQLStatement* stmt_last = db_server->Statement(
"SELECT currval(pg_get_serial_sequence('person_','person_id'))");
71 if (stmt_last->Process()) {
73 stmt_last->StoreResult();
76 if (!stmt_last->NextResultRow()) {
77 cout <<
"ERROR: no last ID in DB!" << endl;
81 person_id = stmt_last->GetString(0);
85 cout <<
"ERROR: getting last ID has been failed!" << endl;
90 TString tmp_person_id;
91 tmp_person_id = person_id;
92 TString tmp_person_name;
93 tmp_person_name = person_name;
95 tmp_is_active = is_active;
100 tmp_email =
new TString(*email);
102 return new ElogPerson(connDb, tmp_person_id, tmp_person_name, tmp_is_active, tmp_email);
109 if (connDb ==
nullptr)
114 TString sql = TString::Format(
"select person_id, person_name, is_active, email "
116 "where person_id = %s",
118 TSQLStatement* stmt = db_server->Statement(sql);
121 if (!stmt->Process()) {
122 cout <<
"ERROR: getting person from the database has been failed" << endl;
133 if (!stmt->NextResultRow()) {
134 cout <<
"ERROR: person was not found in the database" << endl;
141 TString tmp_person_id;
142 tmp_person_id = stmt->GetString(0);
143 TString tmp_person_name;
144 tmp_person_name = stmt->GetString(1);
146 tmp_is_active = stmt->GetInt(2);
151 tmp_email =
new TString(stmt->GetString(3));
155 return new ElogPerson(connDb, tmp_person_id, tmp_person_name, tmp_is_active, tmp_email);
162 if (connDb ==
nullptr)
167 TString sql = TString::Format(
"select 1 "
169 "where person_id = %s",
171 TSQLStatement* stmt = db_server->Statement(sql);
174 if (!stmt->Process()) {
175 cout <<
"ERROR: getting person from the database has been failed" << endl;
186 if (!stmt->NextResultRow()) {
202 if (connDb ==
nullptr)
207 TString sql = TString::Format(
"delete from person_ "
208 "where person_id = $1");
209 TSQLStatement* stmt = db_server->Statement(sql);
211 stmt->NextIteration();
212 stmt->SetString(0, person_id);
215 if (!stmt->Process()) {
216 cout <<
"ERROR: deleting person from the dataBase has been failed" << endl;
232 if (connDb ==
nullptr)
237 TString sql = TString::Format(
"select person_id, person_name, is_active, email "
239 TSQLStatement* stmt = db_server->Statement(sql);
242 if (!stmt->Process()) {
243 cout <<
"ERROR: getting all 'persons' from the dataBase has been failed" << endl;
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));
266 cout << stmt->GetString(3);
280 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
286 TString sql = TString::Format(
"update person_ "
287 "set person_name = $1 "
288 "where person_id = $2");
289 TSQLStatement* stmt = db_server->Statement(sql);
291 stmt->NextIteration();
292 stmt->SetString(0, person_name);
293 stmt->SetString(1, str_person_id);
296 if (!stmt->Process()) {
297 cout <<
"ERROR: updating information about person has been failed" << endl;
303 str_person_name = person_name;
312 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
318 TString sql = TString::Format(
"update person_ "
319 "set is_active = $1 "
320 "where person_id = $2");
321 TSQLStatement* stmt = db_server->Statement(sql);
323 stmt->NextIteration();
324 stmt->SetInt(0, is_active);
325 stmt->SetString(1, str_person_id);
328 if (!stmt->Process()) {
329 cout <<
"ERROR: updating information about person has been failed" << endl;
335 b_is_active = is_active;
344 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
350 TString sql = TString::Format(
"update person_ "
352 "where person_id = $2");
353 TSQLStatement* stmt = db_server->Statement(sql);
355 stmt->NextIteration();
356 if (email ==
nullptr)
359 stmt->SetString(0, *email);
360 stmt->SetString(1, str_person_id);
363 if (!stmt->Process()) {
364 cout <<
"ERROR: updating information about person has been failed" << endl;
372 if (email ==
nullptr)
375 str_email =
new TString(*email);
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;
395 TObjArray* arrayResult =
nullptr;
398 if (connDb ==
nullptr)
400 cout <<
"ERROR: connection to the eLog Database was failed" << endl;
406 TString sql = TString::Format(
"select person_id, person_name, is_active, email "
408 "where lower(person_name) like lower('%s')",
409 person_name_part.Data());
410 TSQLStatement* stmt = db_server->Statement(sql);
413 if (!stmt->Process()) {
414 cout <<
"ERROR: getting person from the database has been failed" << endl;
425 arrayResult =
new TObjArray();
426 arrayResult->SetOwner(kTRUE);
427 while (stmt->NextResultRow()) {
429 if (connRecord ==
nullptr) {
430 cout <<
"ERROR: connection to the eLog database for a record was failed" << endl;
434 TString tmp_person_id;
435 tmp_person_id = stmt->GetString(0);
436 TString tmp_person_name;
437 tmp_person_name = stmt->GetString(1);
439 tmp_is_active = stmt->GetInt(2);
444 tmp_email =
new TString(stmt->GetString(3));
447 connDb, tmp_person_id, tmp_person_name, tmp_is_active, tmp_email));
460 if (connDb ==
nullptr)
465 TString sql = TString::Format(
"select count(person_id) "
467 "where lower(person_name) like lower('%s')",
468 person_name_part.Data());
469 TSQLStatement* stmt = db_server->Statement(sql);
472 if (!stmt->Process()) {
473 cout <<
"ERROR: checking persons in the database has been failed" << endl;
484 if (!stmt->NextResultRow()) {
490 int person_count = stmt->GetInt(0);
503 if (connDb ==
nullptr)
508 TString sql = TString::Format(
"delete from person_ "
509 "where lower(person_name) like lower($1)");
510 TSQLStatement* stmt = db_server->Statement(sql);
512 stmt->NextIteration();
513 stmt->SetString(0, person_name_part);
516 if (!stmt->Process()) {
517 cout <<
"ERROR: deleting persons from the database has been failed" << endl;
525 Int_t deleted_count = stmt->GetNumAffectedRows();
530 return deleted_count;
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
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