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