BmnRoot
Loading...
Searching...
No Matches
ElogContact.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// ElogContact cxx file
3// Generated 13-04-2026
4// ----------------------------------------------------------------------
5
6#include "ElogContact.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 -----------------------
16ElogContact::ElogContact(ElogConnection* db_connect,
17 int contact_id,
18 TString* contact_name,
19 TString contact_desc,
20 TString* phones,
21 TString* email)
22{
23 connectionDB = db_connect;
24
25 i_contact_id = contact_id;
26 str_contact_name = contact_name;
27 str_contact_desc = contact_desc;
28 str_phones = phones;
29 str_email = email;
30}
31
32// ----- Destructor -------------------------------------------------
34{
35 if (connectionDB)
36 delete connectionDB;
37 if (str_contact_name)
38 delete str_contact_name;
39 if (str_phones)
40 delete str_phones;
41 if (str_email)
42 delete str_email;
43}
44
45// ----- Creating new contact in the database ---------------------------
46ElogContact* ElogContact::CreateContact(TString* contact_name, TString contact_desc, TString* phones, TString* email)
47{
49 if (connDb == nullptr)
50 return nullptr;
51
52 TSQLServer* db_server = connDb->GetSQLServer();
53
54 TString sql = TString::Format("insert into contact_(contact_name, contact_desc, phones, email) "
55 "values ($1, $2, $3, $4)");
56 TSQLStatement* stmt = db_server->Statement(sql);
57
58 stmt->NextIteration();
59 if (contact_name == nullptr)
60 stmt->SetNull(0);
61 else
62 stmt->SetString(0, *contact_name);
63 stmt->SetString(1, contact_desc);
64 if (phones == nullptr)
65 stmt->SetNull(2);
66 else
67 stmt->SetString(2, *phones);
68 if (email == nullptr)
69 stmt->SetNull(3);
70 else
71 stmt->SetString(3, *email);
72
73 // inserting new contact to the Database
74 if (!stmt->Process()) {
75 cout << "ERROR: inserting new contact to the Database has been failed" << endl;
76 delete stmt;
77 delete connDb;
78 return nullptr;
79 }
80
81 delete stmt;
82
83 // getting last inserted ID
84 int contact_id;
85 TSQLStatement* stmt_last = db_server->Statement("SELECT currval(pg_get_serial_sequence('contact_','contact_id'))");
86
87 // process getting last id
88 if (stmt_last->Process()) {
89 // store result of statement in buffer
90 stmt_last->StoreResult();
91
92 // if there is no last id then exit with error
93 if (!stmt_last->NextResultRow()) {
94 cout << "ERROR: no last ID in DB!" << endl;
95 delete stmt_last;
96 return nullptr;
97 } else {
98 contact_id = stmt_last->GetInt(0);
99 delete stmt_last;
100 }
101 } else {
102 cout << "ERROR: getting last ID has been failed!" << endl;
103 delete stmt_last;
104 return nullptr;
105 }
106
107 int tmp_contact_id;
108 tmp_contact_id = contact_id;
109 TString* tmp_contact_name;
110 if (contact_name == nullptr)
111 tmp_contact_name = nullptr;
112 else
113 tmp_contact_name = new TString(*contact_name);
114 TString tmp_contact_desc;
115 tmp_contact_desc = contact_desc;
116 TString* tmp_phones;
117 if (phones == nullptr)
118 tmp_phones = nullptr;
119 else
120 tmp_phones = new TString(*phones);
121 TString* tmp_email;
122 if (email == nullptr)
123 tmp_email = nullptr;
124 else
125 tmp_email = new TString(*email);
126
127 return new ElogContact(connDb, tmp_contact_id, tmp_contact_name, tmp_contact_desc, tmp_phones, tmp_email);
128}
129
130// ----- Get contact from the database ---------------------------
132{
134 if (connDb == nullptr)
135 return nullptr;
136
137 TSQLServer* db_server = connDb->GetSQLServer();
138
139 TString sql = TString::Format("select contact_id, contact_name, contact_desc, phones, email "
140 "from contact_ "
141 "where contact_id = %d",
142 contact_id);
143 TSQLStatement* stmt = db_server->Statement(sql);
144
145 // get contact from the database
146 if (!stmt->Process()) {
147 cout << "ERROR: getting contact from the database has been failed" << endl;
148
149 delete stmt;
150 delete connDb;
151 return nullptr;
152 }
153
154 // store result of statement in buffer
155 stmt->StoreResult();
156
157 // extract row
158 if (!stmt->NextResultRow()) {
159 cout << "ERROR: contact was not found in the database" << endl;
160
161 delete stmt;
162 delete connDb;
163 return nullptr;
164 }
165
166 int tmp_contact_id;
167 tmp_contact_id = stmt->GetInt(0);
168 TString* tmp_contact_name;
169 if (stmt->IsNull(1))
170 tmp_contact_name = nullptr;
171 else
172 tmp_contact_name = new TString(stmt->GetString(1));
173 TString tmp_contact_desc;
174 tmp_contact_desc = stmt->GetString(2);
175 TString* tmp_phones;
176 if (stmt->IsNull(3))
177 tmp_phones = nullptr;
178 else
179 tmp_phones = new TString(stmt->GetString(3));
180 TString* tmp_email;
181 if (stmt->IsNull(4))
182 tmp_email = nullptr;
183 else
184 tmp_email = new TString(stmt->GetString(4));
185
186 delete stmt;
187
188 return new ElogContact(connDb, tmp_contact_id, tmp_contact_name, tmp_contact_desc, tmp_phones, tmp_email);
189}
190
191// ----- Check contact exists in the database ---------------------------
193{
195 if (connDb == nullptr)
196 return -1;
197
198 TSQLServer* db_server = connDb->GetSQLServer();
199
200 TString sql = TString::Format("select 1 "
201 "from contact_ "
202 "where contact_id = %d",
203 contact_id);
204 TSQLStatement* stmt = db_server->Statement(sql);
205
206 // get contact from the database
207 if (!stmt->Process()) {
208 cout << "ERROR: getting contact from the database has been failed" << endl;
209
210 delete stmt;
211 delete connDb;
212 return -2;
213 }
214
215 // store result of statement in buffer
216 stmt->StoreResult();
217
218 // extract row
219 if (!stmt->NextResultRow()) {
220 delete stmt;
221 delete connDb;
222 return 0;
223 }
224
225 delete stmt;
226 delete connDb;
227
228 return 1;
229}
230
231// ----- Delete contact from the database ---------------------------
232int ElogContact::DeleteContact(int contact_id)
233{
235 if (connDb == nullptr)
236 return -1;
237
238 TSQLServer* db_server = connDb->GetSQLServer();
239
240 TString sql = TString::Format("delete from contact_ "
241 "where contact_id = $1");
242 TSQLStatement* stmt = db_server->Statement(sql);
243
244 stmt->NextIteration();
245 stmt->SetInt(0, contact_id);
246
247 // delete contact from the dataBase
248 if (!stmt->Process()) {
249 cout << "ERROR: deleting contact from the dataBase has been failed" << endl;
250
251 delete stmt;
252 delete connDb;
253 return -2;
254 }
255
256 delete stmt;
257 delete connDb;
258 return 0;
259}
260
261// ----- Print all 'contacts' ---------------------------------
263{
265 if (connDb == nullptr)
266 return -1;
267
268 TSQLServer* db_server = connDb->GetSQLServer();
269
270 TString sql = TString::Format("select contact_id, contact_name, contact_desc, phones, email "
271 "from contact_");
272 TSQLStatement* stmt = db_server->Statement(sql);
273
274 // get all 'contacts' from the database
275 if (!stmt->Process()) {
276 cout << "ERROR: getting all 'contacts' from the dataBase has been failed" << endl;
277
278 delete stmt;
279 delete connDb;
280 return -2;
281 }
282
283 // store result of statement in buffer
284 stmt->StoreResult();
285
286 // print rows
287 cout << "Table 'contact_':" << endl;
288 while (stmt->NextResultRow()) {
289 cout << "contact_id: ";
290 cout << (stmt->GetInt(0));
291 cout << ", contact_name: ";
292 if (stmt->IsNull(1))
293 cout << "nullptr";
294 else
295 cout << stmt->GetString(1);
296 cout << ", contact_desc: ";
297 cout << (stmt->GetString(2));
298 cout << ", phones: ";
299 if (stmt->IsNull(3))
300 cout << "nullptr";
301 else
302 cout << stmt->GetString(3);
303 cout << ", email: ";
304 if (stmt->IsNull(4))
305 cout << "nullptr";
306 else
307 cout << stmt->GetString(4);
308 cout << "." << endl;
309 }
310
311 delete stmt;
312 delete connDb;
313
314 return 0;
315}
316
317// Setters functions
318int ElogContact::SetContactName(TString* contact_name)
319{
320 if (!connectionDB) {
321 cout << "CRITICAL ERROR: Connection object is null" << endl;
322 return -1;
323 }
324
325 TSQLServer* db_server = connectionDB->GetSQLServer();
326
327 TString sql = TString::Format("update contact_ "
328 "set contact_name = $1 "
329 "where contact_id = $2");
330 TSQLStatement* stmt = db_server->Statement(sql);
331
332 stmt->NextIteration();
333 if (contact_name == nullptr)
334 stmt->SetNull(0);
335 else
336 stmt->SetString(0, *contact_name);
337 stmt->SetInt(1, i_contact_id);
338
339 // write new value to the database
340 if (!stmt->Process()) {
341 cout << "ERROR: updating information about contact has been failed" << endl;
342
343 delete stmt;
344 return -2;
345 }
346
347 if (str_contact_name)
348 delete str_contact_name;
349 if (contact_name == nullptr)
350 str_contact_name = nullptr;
351 else
352 str_contact_name = new TString(*contact_name);
353
354 delete stmt;
355 return 0;
356}
357
358int ElogContact::SetContactDesc(TString contact_desc)
359{
360 if (!connectionDB) {
361 cout << "CRITICAL ERROR: Connection object is null" << endl;
362 return -1;
363 }
364
365 TSQLServer* db_server = connectionDB->GetSQLServer();
366
367 TString sql = TString::Format("update contact_ "
368 "set contact_desc = $1 "
369 "where contact_id = $2");
370 TSQLStatement* stmt = db_server->Statement(sql);
371
372 stmt->NextIteration();
373 stmt->SetString(0, contact_desc);
374 stmt->SetInt(1, i_contact_id);
375
376 // write new value to the database
377 if (!stmt->Process()) {
378 cout << "ERROR: updating information about contact has been failed" << endl;
379
380 delete stmt;
381 return -2;
382 }
383
384 str_contact_desc = contact_desc;
385
386 delete stmt;
387 return 0;
388}
389
390int ElogContact::SetPhones(TString* phones)
391{
392 if (!connectionDB) {
393 cout << "CRITICAL ERROR: Connection object is null" << endl;
394 return -1;
395 }
396
397 TSQLServer* db_server = connectionDB->GetSQLServer();
398
399 TString sql = TString::Format("update contact_ "
400 "set phones = $1 "
401 "where contact_id = $2");
402 TSQLStatement* stmt = db_server->Statement(sql);
403
404 stmt->NextIteration();
405 if (phones == nullptr)
406 stmt->SetNull(0);
407 else
408 stmt->SetString(0, *phones);
409 stmt->SetInt(1, i_contact_id);
410
411 // write new value to the database
412 if (!stmt->Process()) {
413 cout << "ERROR: updating information about contact has been failed" << endl;
414
415 delete stmt;
416 return -2;
417 }
418
419 if (str_phones)
420 delete str_phones;
421 if (phones == nullptr)
422 str_phones = nullptr;
423 else
424 str_phones = new TString(*phones);
425
426 delete stmt;
427 return 0;
428}
429
430int ElogContact::SetEmail(TString* email)
431{
432 if (!connectionDB) {
433 cout << "CRITICAL ERROR: Connection object is null" << endl;
434 return -1;
435 }
436
437 TSQLServer* db_server = connectionDB->GetSQLServer();
438
439 TString sql = TString::Format("update contact_ "
440 "set email = $1 "
441 "where contact_id = $2");
442 TSQLStatement* stmt = db_server->Statement(sql);
443
444 stmt->NextIteration();
445 if (email == nullptr)
446 stmt->SetNull(0);
447 else
448 stmt->SetString(0, *email);
449 stmt->SetInt(1, i_contact_id);
450
451 // write new value to the database
452 if (!stmt->Process()) {
453 cout << "ERROR: updating information about contact has been failed" << endl;
454
455 delete stmt;
456 return -2;
457 }
458
459 if (str_email)
460 delete str_email;
461 if (email == nullptr)
462 str_email = nullptr;
463 else
464 str_email = new TString(*email);
465
466 delete stmt;
467 return 0;
468}
469
470// ----- Print current contact ---------------------------------------
472{
473 cout << "Table 'contact_'";
474 cout << ". contact_id: " << i_contact_id
475 << ". contact_name: " << (str_contact_name == nullptr ? "nullptr" : *str_contact_name)
476 << ". contact_desc: " << str_contact_desc << ". phones: " << (str_phones == nullptr ? "nullptr" : *str_phones)
477 << ". email: " << (str_email == nullptr ? "nullptr" : *str_email) << endl;
478
479 return;
480}
481/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
static ElogConnection * Open()
TSQLServer * GetSQLServer()
int SetContactName(TString *contact_name)
set contact name of the current contact
static int PrintAll()
print all contacts
static ElogContact * CreateContact(TString *contact_name, TString contact_desc, TString *phones, TString *email)
add new contact to the database
static ElogContact * GetContact(int contact_id)
get contact from the database
int SetPhones(TString *phones)
set phones of the current contact
int SetContactDesc(TString contact_desc)
set contact desc of the current contact
int SetEmail(TString *email)
set email of the current contact
static int CheckContactExists(int contact_id)
check contact exists in the database: 1- true, 0 - false, <0 - database operation error
static int DeleteContact(int contact_id)
delete contact from the database
void Print()
print information about current contact
virtual ~ElogContact()
STL namespace.