9#include "TSQLStatement.h"
16ElogStatus::ElogStatus(
ElogConnection* db_connect,
int status_id, TString status_text)
18 connectionDB = db_connect;
20 i_status_id = status_id;
21 str_status_text = status_text;
35 if (connDb ==
nullptr)
40 TString sql = TString::Format(
"insert into status_(status_text) "
42 TSQLStatement* stmt = db_server->Statement(sql);
44 stmt->NextIteration();
45 stmt->SetString(0, status_text);
48 if (!stmt->Process()) {
49 cout <<
"ERROR: inserting new status to the Database has been failed" << endl;
59 TSQLStatement* stmt_last = db_server->Statement(
"SELECT currval(pg_get_serial_sequence('status_','status_id'))");
62 if (stmt_last->Process()) {
64 stmt_last->StoreResult();
67 if (!stmt_last->NextResultRow()) {
68 cout <<
"ERROR: no last ID in DB!" << endl;
72 status_id = stmt_last->GetInt(0);
76 cout <<
"ERROR: getting last ID has been failed!" << endl;
82 tmp_status_id = status_id;
83 TString tmp_status_text;
84 tmp_status_text = status_text;
86 return new ElogStatus(connDb, tmp_status_id, tmp_status_text);
93 if (connDb ==
nullptr)
98 TString sql = TString::Format(
"select status_id, status_text "
100 "where status_id = %d",
102 TSQLStatement* stmt = db_server->Statement(sql);
105 if (!stmt->Process()) {
106 cout <<
"ERROR: getting status from the database has been failed" << endl;
117 if (!stmt->NextResultRow()) {
118 cout <<
"ERROR: status was not found in the database" << endl;
126 tmp_status_id = stmt->GetInt(0);
127 TString tmp_status_text;
128 tmp_status_text = stmt->GetString(1);
132 return new ElogStatus(connDb, tmp_status_id, tmp_status_text);
139 if (connDb ==
nullptr)
144 TString sql = TString::Format(
"select status_id, status_text "
146 "where lower(status_text) = lower('%s')",
148 TSQLStatement* stmt = db_server->Statement(sql);
151 if (!stmt->Process()) {
152 cout <<
"ERROR: getting status from the database has been failed" << endl;
163 if (!stmt->NextResultRow()) {
164 cout <<
"ERROR: status was not found in the database" << endl;
172 tmp_status_id = stmt->GetInt(0);
173 TString tmp_status_text;
174 tmp_status_text = stmt->GetString(1);
178 return new ElogStatus(connDb, tmp_status_id, tmp_status_text);
185 if (connDb ==
nullptr)
190 TString sql = TString::Format(
"select 1 "
192 "where status_id = %d",
194 TSQLStatement* stmt = db_server->Statement(sql);
197 if (!stmt->Process()) {
198 cout <<
"ERROR: getting status from the database has been failed" << endl;
209 if (!stmt->NextResultRow()) {
225 if (connDb ==
nullptr)
230 TString sql = TString::Format(
"select 1 "
232 "where lower(status_text) = lower('%s')",
234 TSQLStatement* stmt = db_server->Statement(sql);
237 if (!stmt->Process()) {
238 cout <<
"ERROR: getting status from the database has been failed" << endl;
249 if (!stmt->NextResultRow()) {
265 if (connDb ==
nullptr)
270 TString sql = TString::Format(
"delete from status_ "
271 "where status_id = $1");
272 TSQLStatement* stmt = db_server->Statement(sql);
274 stmt->NextIteration();
275 stmt->SetInt(0, status_id);
278 if (!stmt->Process()) {
279 cout <<
"ERROR: deleting status from the dataBase has been failed" << endl;
295 if (connDb ==
nullptr)
300 TString sql = TString::Format(
"delete from status_ "
301 "where lower(status_text) = lower($1)");
302 TSQLStatement* stmt = db_server->Statement(sql);
304 stmt->NextIteration();
305 stmt->SetString(0, status_text);
308 if (!stmt->Process()) {
309 cout <<
"ERROR: deleting status from the DataBase has been failed" << endl;
325 if (connDb ==
nullptr)
330 TString sql = TString::Format(
"select status_id, status_text "
332 TSQLStatement* stmt = db_server->Statement(sql);
335 if (!stmt->Process()) {
336 cout <<
"ERROR: getting all 'statuss' from the dataBase has been failed" << endl;
347 cout <<
"Table 'status_':" << endl;
348 while (stmt->NextResultRow()) {
349 cout <<
"status_id: ";
350 cout << (stmt->GetInt(0));
351 cout <<
", status_text: ";
352 cout << (stmt->GetString(1));
366 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
372 TString sql = TString::Format(
"update status_ "
373 "set status_text = $1 "
374 "where status_id = $2");
375 TSQLStatement* stmt = db_server->Statement(sql);
377 stmt->NextIteration();
378 stmt->SetString(0, status_text);
379 stmt->SetInt(1, i_status_id);
382 if (!stmt->Process()) {
383 cout <<
"ERROR: updating information about status has been failed" << endl;
389 str_status_text = status_text;
398 cout <<
"Table 'status_'";
399 cout <<
". status_id: " << i_status_id <<
". status_text: " << str_status_text << endl;
static ElogConnection * Open()
TSQLServer * GetSQLServer()
static int CheckStatusExists(int status_id)
check status exists in the database: 1- true, 0 - false, <0 - database operation error
void Print()
print information about current status
static int DeleteStatus(int status_id)
delete status from the database
static int PrintAll()
print all statuss
static ElogStatus * CreateStatus(TString status_text)
add new status to the database
int SetStatusText(TString status_text)
set status text of the current status
static ElogStatus * GetStatus(int status_id)
get status from the database