9#include "TSQLStatement.h"
16ElogTrigger::ElogTrigger(
ElogConnection* db_connect,
int trigger_id, TString trigger_info,
bool is_active)
18 connectionDB = db_connect;
20 i_trigger_id = trigger_id;
21 str_trigger_info = trigger_info;
22 b_is_active = is_active;
36 if (connDb ==
nullptr)
41 TString sql = TString::Format(
"insert into trigger_(trigger_info, is_active) "
43 TSQLStatement* stmt = db_server->Statement(sql);
45 stmt->NextIteration();
46 stmt->SetString(0, trigger_info);
47 stmt->SetInt(1, is_active);
50 if (!stmt->Process()) {
51 cout <<
"ERROR: inserting new trigger to the Database has been failed" << endl;
61 TSQLStatement* stmt_last = db_server->Statement(
"SELECT currval(pg_get_serial_sequence('trigger_','trigger_id'))");
64 if (stmt_last->Process()) {
66 stmt_last->StoreResult();
69 if (!stmt_last->NextResultRow()) {
70 cout <<
"ERROR: no last ID in DB!" << endl;
74 trigger_id = stmt_last->GetInt(0);
78 cout <<
"ERROR: getting last ID has been failed!" << endl;
84 tmp_trigger_id = trigger_id;
85 TString tmp_trigger_info;
86 tmp_trigger_info = trigger_info;
88 tmp_is_active = is_active;
90 return new ElogTrigger(connDb, tmp_trigger_id, tmp_trigger_info, tmp_is_active);
97 if (connDb ==
nullptr)
102 TString sql = TString::Format(
"select trigger_id, trigger_info, is_active "
104 "where trigger_id = %d",
106 TSQLStatement* stmt = db_server->Statement(sql);
109 if (!stmt->Process()) {
110 cout <<
"ERROR: getting trigger from the database has been failed" << endl;
121 if (!stmt->NextResultRow()) {
122 cout <<
"ERROR: trigger was not found in the database" << endl;
130 tmp_trigger_id = stmt->GetInt(0);
131 TString tmp_trigger_info;
132 tmp_trigger_info = stmt->GetString(1);
134 tmp_is_active = stmt->GetInt(2);
138 return new ElogTrigger(connDb, tmp_trigger_id, tmp_trigger_info, tmp_is_active);
145 if (connDb ==
nullptr)
150 TString sql = TString::Format(
"select trigger_id, trigger_info, is_active "
152 "where lower(trigger_info) = lower('%s')",
153 trigger_info.Data());
154 TSQLStatement* stmt = db_server->Statement(sql);
157 if (!stmt->Process()) {
158 cout <<
"ERROR: getting trigger from the database has been failed" << endl;
169 if (!stmt->NextResultRow()) {
170 cout <<
"ERROR: trigger was not found in the database" << endl;
178 tmp_trigger_id = stmt->GetInt(0);
179 TString tmp_trigger_info;
180 tmp_trigger_info = stmt->GetString(1);
182 tmp_is_active = stmt->GetInt(2);
186 return new ElogTrigger(connDb, tmp_trigger_id, tmp_trigger_info, tmp_is_active);
193 if (connDb ==
nullptr)
198 TString sql = TString::Format(
"select 1 "
200 "where trigger_id = %d",
202 TSQLStatement* stmt = db_server->Statement(sql);
205 if (!stmt->Process()) {
206 cout <<
"ERROR: getting trigger from the database has been failed" << endl;
217 if (!stmt->NextResultRow()) {
233 if (connDb ==
nullptr)
238 TString sql = TString::Format(
"select 1 "
240 "where lower(trigger_info) = lower('%s')",
241 trigger_info.Data());
242 TSQLStatement* stmt = db_server->Statement(sql);
245 if (!stmt->Process()) {
246 cout <<
"ERROR: getting trigger from the database has been failed" << endl;
257 if (!stmt->NextResultRow()) {
273 if (connDb ==
nullptr)
278 TString sql = TString::Format(
"delete from trigger_ "
279 "where trigger_id = $1");
280 TSQLStatement* stmt = db_server->Statement(sql);
282 stmt->NextIteration();
283 stmt->SetInt(0, trigger_id);
286 if (!stmt->Process()) {
287 cout <<
"ERROR: deleting trigger from the dataBase has been failed" << endl;
303 if (connDb ==
nullptr)
308 TString sql = TString::Format(
"delete from trigger_ "
309 "where lower(trigger_info) = lower($1)");
310 TSQLStatement* stmt = db_server->Statement(sql);
312 stmt->NextIteration();
313 stmt->SetString(0, trigger_info);
316 if (!stmt->Process()) {
317 cout <<
"ERROR: deleting trigger from the DataBase has been failed" << endl;
333 if (connDb ==
nullptr)
338 TString sql = TString::Format(
"select trigger_id, trigger_info, is_active "
340 TSQLStatement* stmt = db_server->Statement(sql);
343 if (!stmt->Process()) {
344 cout <<
"ERROR: getting all 'triggers' from the dataBase has been failed" << endl;
355 cout <<
"Table 'trigger_':" << endl;
356 while (stmt->NextResultRow()) {
357 cout <<
"trigger_id: ";
358 cout << (stmt->GetInt(0));
359 cout <<
", trigger_info: ";
360 cout << (stmt->GetString(1));
361 cout <<
", is_active: ";
362 cout << (stmt->GetInt(2));
376 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
382 TString sql = TString::Format(
"update trigger_ "
383 "set trigger_info = $1 "
384 "where trigger_id = $2");
385 TSQLStatement* stmt = db_server->Statement(sql);
387 stmt->NextIteration();
388 stmt->SetString(0, trigger_info);
389 stmt->SetInt(1, i_trigger_id);
392 if (!stmt->Process()) {
393 cout <<
"ERROR: updating information about trigger has been failed" << endl;
399 str_trigger_info = trigger_info;
408 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
414 TString sql = TString::Format(
"update trigger_ "
415 "set is_active = $1 "
416 "where trigger_id = $2");
417 TSQLStatement* stmt = db_server->Statement(sql);
419 stmt->NextIteration();
420 stmt->SetInt(0, is_active);
421 stmt->SetInt(1, i_trigger_id);
424 if (!stmt->Process()) {
425 cout <<
"ERROR: updating information about trigger has been failed" << endl;
431 b_is_active = is_active;
440 cout <<
"Table 'trigger_'";
441 cout <<
". trigger_id: " << i_trigger_id <<
". trigger_info: " << str_trigger_info <<
". is_active: " << b_is_active
static ElogConnection * Open()
TSQLServer * GetSQLServer()
static int PrintAll()
print all triggers
int SetTriggerInfo(TString trigger_info)
set trigger info of the current trigger
static int CheckTriggerExists(int trigger_id)
check trigger exists in the database: 1- true, 0 - false, <0 - database operation error
static ElogTrigger * GetTrigger(int trigger_id)
get trigger from the database
static int DeleteTrigger(int trigger_id)
delete trigger from the database
void Print()
print information about current trigger
static ElogTrigger * CreateTrigger(TString trigger_info, bool is_active)
add new trigger to the database
int SetIsActive(bool is_active)
set is active of the current trigger