9#include "TSQLStatement.h"
18 TString* contact_name,
23 connectionDB = db_connect;
25 i_contact_id = contact_id;
26 str_contact_name = contact_name;
27 str_contact_desc = contact_desc;
38 delete str_contact_name;
49 if (connDb ==
nullptr)
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);
58 stmt->NextIteration();
59 if (contact_name ==
nullptr)
62 stmt->SetString(0, *contact_name);
63 stmt->SetString(1, contact_desc);
64 if (phones ==
nullptr)
67 stmt->SetString(2, *phones);
71 stmt->SetString(3, *email);
74 if (!stmt->Process()) {
75 cout <<
"ERROR: inserting new contact to the Database has been failed" << endl;
85 TSQLStatement* stmt_last = db_server->Statement(
"SELECT currval(pg_get_serial_sequence('contact_','contact_id'))");
88 if (stmt_last->Process()) {
90 stmt_last->StoreResult();
93 if (!stmt_last->NextResultRow()) {
94 cout <<
"ERROR: no last ID in DB!" << endl;
98 contact_id = stmt_last->GetInt(0);
102 cout <<
"ERROR: getting last ID has been failed!" << endl;
108 tmp_contact_id = contact_id;
109 TString* tmp_contact_name;
110 if (contact_name ==
nullptr)
111 tmp_contact_name =
nullptr;
113 tmp_contact_name =
new TString(*contact_name);
114 TString tmp_contact_desc;
115 tmp_contact_desc = contact_desc;
117 if (phones ==
nullptr)
118 tmp_phones =
nullptr;
120 tmp_phones =
new TString(*phones);
122 if (email ==
nullptr)
125 tmp_email =
new TString(*email);
127 return new ElogContact(connDb, tmp_contact_id, tmp_contact_name, tmp_contact_desc, tmp_phones, tmp_email);
134 if (connDb ==
nullptr)
139 TString sql = TString::Format(
"select contact_id, contact_name, contact_desc, phones, email "
141 "where contact_id = %d",
143 TSQLStatement* stmt = db_server->Statement(sql);
146 if (!stmt->Process()) {
147 cout <<
"ERROR: getting contact from the database has been failed" << endl;
158 if (!stmt->NextResultRow()) {
159 cout <<
"ERROR: contact was not found in the database" << endl;
167 tmp_contact_id = stmt->GetInt(0);
168 TString* tmp_contact_name;
170 tmp_contact_name =
nullptr;
172 tmp_contact_name =
new TString(stmt->GetString(1));
173 TString tmp_contact_desc;
174 tmp_contact_desc = stmt->GetString(2);
177 tmp_phones =
nullptr;
179 tmp_phones =
new TString(stmt->GetString(3));
184 tmp_email =
new TString(stmt->GetString(4));
188 return new ElogContact(connDb, tmp_contact_id, tmp_contact_name, tmp_contact_desc, tmp_phones, tmp_email);
195 if (connDb ==
nullptr)
200 TString sql = TString::Format(
"select 1 "
202 "where contact_id = %d",
204 TSQLStatement* stmt = db_server->Statement(sql);
207 if (!stmt->Process()) {
208 cout <<
"ERROR: getting contact from the database has been failed" << endl;
219 if (!stmt->NextResultRow()) {
235 if (connDb ==
nullptr)
240 TString sql = TString::Format(
"delete from contact_ "
241 "where contact_id = $1");
242 TSQLStatement* stmt = db_server->Statement(sql);
244 stmt->NextIteration();
245 stmt->SetInt(0, contact_id);
248 if (!stmt->Process()) {
249 cout <<
"ERROR: deleting contact from the dataBase has been failed" << endl;
265 if (connDb ==
nullptr)
270 TString sql = TString::Format(
"select contact_id, contact_name, contact_desc, phones, email "
272 TSQLStatement* stmt = db_server->Statement(sql);
275 if (!stmt->Process()) {
276 cout <<
"ERROR: getting all 'contacts' from the dataBase has been failed" << endl;
287 cout <<
"Table 'contact_':" << endl;
288 while (stmt->NextResultRow()) {
289 cout <<
"contact_id: ";
290 cout << (stmt->GetInt(0));
291 cout <<
", contact_name: ";
295 cout << stmt->GetString(1);
296 cout <<
", contact_desc: ";
297 cout << (stmt->GetString(2));
298 cout <<
", phones: ";
302 cout << stmt->GetString(3);
307 cout << stmt->GetString(4);
321 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
327 TString sql = TString::Format(
"update contact_ "
328 "set contact_name = $1 "
329 "where contact_id = $2");
330 TSQLStatement* stmt = db_server->Statement(sql);
332 stmt->NextIteration();
333 if (contact_name ==
nullptr)
336 stmt->SetString(0, *contact_name);
337 stmt->SetInt(1, i_contact_id);
340 if (!stmt->Process()) {
341 cout <<
"ERROR: updating information about contact has been failed" << endl;
347 if (str_contact_name)
348 delete str_contact_name;
349 if (contact_name ==
nullptr)
350 str_contact_name =
nullptr;
352 str_contact_name =
new TString(*contact_name);
361 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
367 TString sql = TString::Format(
"update contact_ "
368 "set contact_desc = $1 "
369 "where contact_id = $2");
370 TSQLStatement* stmt = db_server->Statement(sql);
372 stmt->NextIteration();
373 stmt->SetString(0, contact_desc);
374 stmt->SetInt(1, i_contact_id);
377 if (!stmt->Process()) {
378 cout <<
"ERROR: updating information about contact has been failed" << endl;
384 str_contact_desc = contact_desc;
393 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
399 TString sql = TString::Format(
"update contact_ "
401 "where contact_id = $2");
402 TSQLStatement* stmt = db_server->Statement(sql);
404 stmt->NextIteration();
405 if (phones ==
nullptr)
408 stmt->SetString(0, *phones);
409 stmt->SetInt(1, i_contact_id);
412 if (!stmt->Process()) {
413 cout <<
"ERROR: updating information about contact has been failed" << endl;
421 if (phones ==
nullptr)
422 str_phones =
nullptr;
424 str_phones =
new TString(*phones);
433 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
439 TString sql = TString::Format(
"update contact_ "
441 "where contact_id = $2");
442 TSQLStatement* stmt = db_server->Statement(sql);
444 stmt->NextIteration();
445 if (email ==
nullptr)
448 stmt->SetString(0, *email);
449 stmt->SetInt(1, i_contact_id);
452 if (!stmt->Process()) {
453 cout <<
"ERROR: updating information about contact has been failed" << endl;
461 if (email ==
nullptr)
464 str_email =
new TString(*email);
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;
static ElogConnection * Open()
TSQLServer * GetSQLServer()