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