BmnRoot
Loading...
Searching...
No Matches
ElogBeam.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// ElogBeam cxx file
3// Generated 27-11-2017
4// ----------------------------------------------------------------------
5
6#include "ElogBeam.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 -----------------------
16ElogBeam::ElogBeam(ElogConnection* db_connect, TString beam, bool is_active)
17{
18 connectionDB = db_connect;
19
20 str_beam = beam;
21 b_is_active = is_active;
22}
23
24// ----- Destructor -------------------------------------------------
26{
27 if (connectionDB)
28 delete connectionDB;
29}
30
31// ----- Creating new beam in the database ---------------------------
32ElogBeam* ElogBeam::CreateBeam(TString beam, 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 beam_(beam, is_active) "
41 "values ($1, $2)");
42 TSQLStatement* stmt = db_server->Statement(sql);
43
44 stmt->NextIteration();
45 stmt->SetString(0, beam);
46 stmt->SetInt(1, is_active);
47
48 // inserting new beam to the Database
49 if (!stmt->Process()) {
50 cout << "ERROR: inserting new beam 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_beam;
59 tmp_beam = beam;
60 bool tmp_is_active;
61 tmp_is_active = is_active;
62
63 return new ElogBeam(connDb, tmp_beam, tmp_is_active);
64}
65
66// ----- Get beam 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 beam, is_active "
76 "from beam_ "
77 "where lower(beam) = lower('%s')",
78 beam.Data());
79 TSQLStatement* stmt = db_server->Statement(sql);
80
81 // get beam from the database
82 if (!stmt->Process()) {
83 cout << "ERROR: getting beam 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: beam was not found in the database" << endl;
96
97 delete stmt;
98 delete connDb;
99 return nullptr;
100 }
101
102 TString tmp_beam;
103 tmp_beam = stmt->GetString(0);
104 bool tmp_is_active;
105 tmp_is_active = stmt->GetInt(1);
106
107 delete stmt;
108
109 return new ElogBeam(connDb, tmp_beam, tmp_is_active);
110}
111
112// ----- Check beam 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 beam_ "
123 "where lower(beam) = lower('%s')",
124 beam.Data());
125 TSQLStatement* stmt = db_server->Statement(sql);
126
127 // get beam from the database
128 if (!stmt->Process()) {
129 cout << "ERROR: getting beam 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 beam from the database ---------------------------
153int ElogBeam::DeleteBeam(TString beam)
154{
156 if (connDb == nullptr)
157 return -1;
158
159 TSQLServer* db_server = connDb->GetSQLServer();
160
161 TString sql = TString::Format("delete from beam_ "
162 "where lower(beam) = lower($1)");
163 TSQLStatement* stmt = db_server->Statement(sql);
164
165 stmt->NextIteration();
166 stmt->SetString(0, beam);
167
168 // delete beam from the dataBase
169 if (!stmt->Process()) {
170 cout << "ERROR: deleting beam 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 'beams' ---------------------------------
184{
186 if (connDb == nullptr)
187 return -1;
188
189 TSQLServer* db_server = connDb->GetSQLServer();
190
191 TString sql = TString::Format("select beam, is_active "
192 "from beam_");
193 TSQLStatement* stmt = db_server->Statement(sql);
194
195 // get all 'beams' from the database
196 if (!stmt->Process()) {
197 cout << "ERROR: getting all 'beams' 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 'beam_':" << endl;
209 while (stmt->NextResultRow()) {
210 cout << "beam: ";
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 ElogBeam::SetBeam(TString beam)
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 beam_ "
234 "set beam = $1 "
235 "where beam = $2");
236 TSQLStatement* stmt = db_server->Statement(sql);
237
238 stmt->NextIteration();
239 stmt->SetString(0, beam);
240 stmt->SetString(1, str_beam);
241
242 // write new value to the database
243 if (!stmt->Process()) {
244 cout << "ERROR: updating information about beam has been failed" << endl;
245
246 delete stmt;
247 return -2;
248 }
249
250 str_beam = beam;
251
252 delete stmt;
253 return 0;
254}
255
256int ElogBeam::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 beam_ "
266 "set is_active = $1 "
267 "where beam = $2");
268 TSQLStatement* stmt = db_server->Statement(sql);
269
270 stmt->NextIteration();
271 stmt->SetInt(0, is_active);
272 stmt->SetString(1, str_beam);
273
274 // write new value to the database
275 if (!stmt->Process()) {
276 cout << "ERROR: updating information about beam 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 beam ---------------------------------------
290{
291 cout << "Table 'beam_'";
292 cout << ". beam: " << str_beam << ". is_active: " << b_is_active << endl;
293
294 return;
295}
296/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
static int PrintAll()
print all beams
Definition ElogBeam.cxx:183
TString GetBeam()
get beam of the current beam
Definition ElogBeam.h:51
int SetBeam(TString beam)
set beam of the current beam
Definition ElogBeam.cxx:224
static ElogBeam * CreateBeam(TString beam, bool is_active)
add new beam to the database
Definition ElogBeam.cxx:32
static int CheckBeamExists(TString beam)
check beam exists in the database: 1- true, 0 - false, <0 - database operation error
Definition ElogBeam.cxx:113
static int DeleteBeam(TString beam)
delete beam from the database
Definition ElogBeam.cxx:153
void Print()
print information about current beam
Definition ElogBeam.cxx:289
int SetIsActive(bool is_active)
set is active of the current beam
Definition ElogBeam.cxx:256
virtual ~ElogBeam()
Definition ElogBeam.cxx:25
static ElogConnection * Open()
TSQLServer * GetSQLServer()
STL namespace.