BmnRoot
Loading...
Searching...
No Matches
ElogTarget.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// ElogTarget cxx file
3// Generated 27-11-2017
4// ----------------------------------------------------------------------
5
6#include "ElogTarget.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 -----------------------
16ElogTarget::ElogTarget(ElogConnection* db_connect, TString target, bool is_active)
17{
18 connectionDB = db_connect;
19
20 str_target = target;
21 b_is_active = is_active;
22}
23
24// ----- Destructor -------------------------------------------------
26{
27 if (connectionDB)
28 delete connectionDB;
29}
30
31// ----- Creating new target in the database ---------------------------
32ElogTarget* ElogTarget::CreateTarget(TString target, bool is_active)
33{
35 if (connDb == nullptr)
36 return nullptr;
37
38 TSQLServer* db_server = connDb->GetSQLServer();
39
40 TString sql = TString::Format("insert into target_(target, is_active) "
41 "values ($1, $2)");
42 TSQLStatement* stmt = db_server->Statement(sql);
43
44 stmt->NextIteration();
45 stmt->SetString(0, target);
46 stmt->SetInt(1, is_active);
47
48 // inserting new target to the Database
49 if (!stmt->Process()) {
50 cout << "ERROR: inserting new target to the Database has been failed" << endl;
51 delete stmt;
52 delete connDb;
53 return nullptr;
54 }
55
56 delete stmt;
57
58 TString tmp_target;
59 tmp_target = target;
60 bool tmp_is_active;
61 tmp_is_active = is_active;
62
63 return new ElogTarget(connDb, tmp_target, tmp_is_active);
64}
65
66// ----- Get target from the database ---------------------------
68{
70 if (connDb == nullptr)
71 return nullptr;
72
73 TSQLServer* db_server = connDb->GetSQLServer();
74
75 TString sql = TString::Format("select target, is_active "
76 "from target_ "
77 "where lower(target) = lower('%s')",
78 target.Data());
79 TSQLStatement* stmt = db_server->Statement(sql);
80
81 // get target from the database
82 if (!stmt->Process()) {
83 cout << "ERROR: getting target from the database has been failed" << endl;
84
85 delete stmt;
86 delete connDb;
87 return nullptr;
88 }
89
90 // store result of statement in buffer
91 stmt->StoreResult();
92
93 // extract row
94 if (!stmt->NextResultRow()) {
95 cout << "ERROR: target was not found in the database" << endl;
96
97 delete stmt;
98 delete connDb;
99 return nullptr;
100 }
101
102 TString tmp_target;
103 tmp_target = stmt->GetString(0);
104 bool tmp_is_active;
105 tmp_is_active = stmt->GetInt(1);
106
107 delete stmt;
108
109 return new ElogTarget(connDb, tmp_target, tmp_is_active);
110}
111
112// ----- Check target exists in the database ---------------------------
114{
116 if (connDb == nullptr)
117 return -1;
118
119 TSQLServer* db_server = connDb->GetSQLServer();
120
121 TString sql = TString::Format("select 1 "
122 "from target_ "
123 "where lower(target) = lower('%s')",
124 target.Data());
125 TSQLStatement* stmt = db_server->Statement(sql);
126
127 // get target from the database
128 if (!stmt->Process()) {
129 cout << "ERROR: getting target from the database has been failed" << endl;
130
131 delete stmt;
132 delete connDb;
133 return -2;
134 }
135
136 // store result of statement in buffer
137 stmt->StoreResult();
138
139 // extract row
140 if (!stmt->NextResultRow()) {
141 delete stmt;
142 delete connDb;
143 return 0;
144 }
145
146 delete stmt;
147 delete connDb;
148
149 return 1;
150}
151
152// ----- Delete target from the database ---------------------------
153int ElogTarget::DeleteTarget(TString target)
154{
156 if (connDb == nullptr)
157 return -1;
158
159 TSQLServer* db_server = connDb->GetSQLServer();
160
161 TString sql = TString::Format("delete from target_ "
162 "where lower(target) = lower($1)");
163 TSQLStatement* stmt = db_server->Statement(sql);
164
165 stmt->NextIteration();
166 stmt->SetString(0, target);
167
168 // delete target from the dataBase
169 if (!stmt->Process()) {
170 cout << "ERROR: deleting target from the dataBase has been failed" << endl;
171
172 delete stmt;
173 delete connDb;
174 return -2;
175 }
176
177 delete stmt;
178 delete connDb;
179 return 0;
180}
181
182// ----- Print all 'targets' ---------------------------------
184{
186 if (connDb == nullptr)
187 return -1;
188
189 TSQLServer* db_server = connDb->GetSQLServer();
190
191 TString sql = TString::Format("select target, is_active "
192 "from target_");
193 TSQLStatement* stmt = db_server->Statement(sql);
194
195 // get all 'targets' from the database
196 if (!stmt->Process()) {
197 cout << "ERROR: getting all 'targets' from the dataBase has been failed" << endl;
198
199 delete stmt;
200 delete connDb;
201 return -2;
202 }
203
204 // store result of statement in buffer
205 stmt->StoreResult();
206
207 // print rows
208 cout << "Table 'target_':" << endl;
209 while (stmt->NextResultRow()) {
210 cout << "target: ";
211 cout << (stmt->GetString(0));
212 cout << ", is_active: ";
213 cout << (stmt->GetInt(1));
214 cout << "." << endl;
215 }
216
217 delete stmt;
218 delete connDb;
219
220 return 0;
221}
222
223// Setters functions
224int ElogTarget::SetTarget(TString target)
225{
226 if (!connectionDB) {
227 cout << "CRITICAL ERROR: Connection object is null" << endl;
228 return -1;
229 }
230
231 TSQLServer* db_server = connectionDB->GetSQLServer();
232
233 TString sql = TString::Format("update target_ "
234 "set target = $1 "
235 "where target = $2");
236 TSQLStatement* stmt = db_server->Statement(sql);
237
238 stmt->NextIteration();
239 stmt->SetString(0, target);
240 stmt->SetString(1, str_target);
241
242 // write new value to the database
243 if (!stmt->Process()) {
244 cout << "ERROR: updating information about target has been failed" << endl;
245
246 delete stmt;
247 return -2;
248 }
249
250 str_target = target;
251
252 delete stmt;
253 return 0;
254}
255
256int ElogTarget::SetIsActive(bool is_active)
257{
258 if (!connectionDB) {
259 cout << "CRITICAL ERROR: Connection object is null" << endl;
260 return -1;
261 }
262
263 TSQLServer* db_server = connectionDB->GetSQLServer();
264
265 TString sql = TString::Format("update target_ "
266 "set is_active = $1 "
267 "where target = $2");
268 TSQLStatement* stmt = db_server->Statement(sql);
269
270 stmt->NextIteration();
271 stmt->SetInt(0, is_active);
272 stmt->SetString(1, str_target);
273
274 // write new value to the database
275 if (!stmt->Process()) {
276 cout << "ERROR: updating information about target has been failed" << endl;
277
278 delete stmt;
279 return -2;
280 }
281
282 b_is_active = is_active;
283
284 delete stmt;
285 return 0;
286}
287
288// ----- Print current target ---------------------------------------
290{
291 cout << "Table 'target_'";
292 cout << ". target: " << str_target << ". is_active: " << b_is_active << endl;
293
294 return;
295}
296/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
static ElogConnection * Open()
TSQLServer * GetSQLServer()
TString GetTarget()
get target of the current target
Definition ElogTarget.h:51
int SetTarget(TString target)
set target of the current target
static int DeleteTarget(TString target)
delete target from the database
static int PrintAll()
print all targets
static int CheckTargetExists(TString target)
check target exists in the database: 1- true, 0 - false, <0 - database operation error
static ElogTarget * CreateTarget(TString target, bool is_active)
add new target to the database
int SetIsActive(bool is_active)
set is active of the current target
virtual ~ElogTarget()
void Print()
print information about current target
STL namespace.