BmnRoot
Loading...
Searching...
No Matches
ElogAttachment.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// ElogAttachment cxx file
3// Generated 27-11-2017
4// ----------------------------------------------------------------------
5
6#include "ElogAttachment.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 -----------------------
16ElogAttachment::ElogAttachment(ElogConnection* db_connect, int record_id, int attachment_number, TString file_path)
17{
18 connectionDB = db_connect;
19
20 i_record_id = record_id;
21 i_attachment_number = attachment_number;
22 str_file_path = file_path;
23}
24
25// ----- Destructor -------------------------------------------------
27{
28 if (connectionDB)
29 delete connectionDB;
30}
31
32// ----- Creating new attachment in the database ---------------------------
33ElogAttachment* ElogAttachment::CreateAttachment(int record_id, int attachment_number, TString file_path)
34{
36 if (connDb == nullptr)
37 return nullptr;
38
39 TSQLServer* db_server = connDb->GetSQLServer();
40
41 TString sql = TString::Format("insert into attachment_(record_id, attachment_number, file_path) "
42 "values ($1, $2, $3)");
43 TSQLStatement* stmt = db_server->Statement(sql);
44
45 stmt->NextIteration();
46 stmt->SetInt(0, record_id);
47 stmt->SetInt(1, attachment_number);
48 stmt->SetString(2, file_path);
49
50 // inserting new attachment to the Database
51 if (!stmt->Process()) {
52 cout << "ERROR: inserting new attachment to the Database has been failed" << endl;
53 delete stmt;
54 delete connDb;
55 return nullptr;
56 }
57
58 delete stmt;
59
60 int tmp_record_id;
61 tmp_record_id = record_id;
62 int tmp_attachment_number;
63 tmp_attachment_number = attachment_number;
64 TString tmp_file_path;
65 tmp_file_path = file_path;
66
67 return new ElogAttachment(connDb, tmp_record_id, tmp_attachment_number, tmp_file_path);
68}
69
70// ----- Get attachment from the database ---------------------------
71ElogAttachment* ElogAttachment::GetAttachment(int record_id, int attachment_number)
72{
74 if (connDb == nullptr)
75 return nullptr;
76
77 TSQLServer* db_server = connDb->GetSQLServer();
78
79 TString sql = TString::Format("select record_id, attachment_number, file_path "
80 "from attachment_ "
81 "where record_id = %d and attachment_number = %d",
82 record_id, attachment_number);
83 TSQLStatement* stmt = db_server->Statement(sql);
84
85 // get attachment from the database
86 if (!stmt->Process()) {
87 cout << "ERROR: getting attachment from the database has been failed" << endl;
88
89 delete stmt;
90 delete connDb;
91 return nullptr;
92 }
93
94 // store result of statement in buffer
95 stmt->StoreResult();
96
97 // extract row
98 if (!stmt->NextResultRow()) {
99 cout << "ERROR: attachment was not found in the database" << endl;
100
101 delete stmt;
102 delete connDb;
103 return nullptr;
104 }
105
106 int tmp_record_id;
107 tmp_record_id = stmt->GetInt(0);
108 int tmp_attachment_number;
109 tmp_attachment_number = stmt->GetInt(1);
110 TString tmp_file_path;
111 tmp_file_path = stmt->GetString(2);
112
113 delete stmt;
114
115 return new ElogAttachment(connDb, tmp_record_id, tmp_attachment_number, tmp_file_path);
116}
117
118// ----- Get attachment from the database by unique key --------------
120{
122 if (connDb == nullptr)
123 return nullptr;
124
125 TSQLServer* db_server = connDb->GetSQLServer();
126
127 TString sql = TString::Format("select record_id, attachment_number, file_path "
128 "from attachment_ "
129 "where lower(file_path) = lower('%s')",
130 file_path.Data());
131 TSQLStatement* stmt = db_server->Statement(sql);
132
133 // get attachment from the database
134 if (!stmt->Process()) {
135 cout << "ERROR: getting attachment from the database has been failed" << endl;
136
137 delete stmt;
138 delete connDb;
139 return nullptr;
140 }
141
142 // store result of statement in buffer
143 stmt->StoreResult();
144
145 // extract row
146 if (!stmt->NextResultRow()) {
147 cout << "ERROR: attachment was not found in the database" << endl;
148
149 delete stmt;
150 delete connDb;
151 return nullptr;
152 }
153
154 int tmp_record_id;
155 tmp_record_id = stmt->GetInt(0);
156 int tmp_attachment_number;
157 tmp_attachment_number = stmt->GetInt(1);
158 TString tmp_file_path;
159 tmp_file_path = stmt->GetString(2);
160
161 delete stmt;
162
163 return new ElogAttachment(connDb, tmp_record_id, tmp_attachment_number, tmp_file_path);
164}
165
166// ----- Check attachment exists in the database ---------------------------
167int ElogAttachment::CheckAttachmentExists(int record_id, int attachment_number)
168{
170 if (connDb == nullptr)
171 return -1;
172
173 TSQLServer* db_server = connDb->GetSQLServer();
174
175 TString sql = TString::Format("select 1 "
176 "from attachment_ "
177 "where record_id = %d and attachment_number = %d",
178 record_id, attachment_number);
179 TSQLStatement* stmt = db_server->Statement(sql);
180
181 // get attachment from the database
182 if (!stmt->Process()) {
183 cout << "ERROR: getting attachment from the database has been failed" << endl;
184
185 delete stmt;
186 delete connDb;
187 return -2;
188 }
189
190 // store result of statement in buffer
191 stmt->StoreResult();
192
193 // extract row
194 if (!stmt->NextResultRow()) {
195 delete stmt;
196 delete connDb;
197 return 0;
198 }
199
200 delete stmt;
201 delete connDb;
202
203 return 1;
204}
205
206// ----- Check attachment exists in the database by unique key --------------
208{
210 if (connDb == nullptr)
211 return -1;
212
213 TSQLServer* db_server = connDb->GetSQLServer();
214
215 TString sql = TString::Format("select 1 "
216 "from attachment_ "
217 "where lower(file_path) = lower('%s')",
218 file_path.Data());
219 TSQLStatement* stmt = db_server->Statement(sql);
220
221 // get attachment from the database
222 if (!stmt->Process()) {
223 cout << "ERROR: getting attachment from the database has been failed" << endl;
224
225 delete stmt;
226 delete connDb;
227 return -2;
228 }
229
230 // store result of statement in buffer
231 stmt->StoreResult();
232
233 // extract row
234 if (!stmt->NextResultRow()) {
235 delete stmt;
236 delete connDb;
237 return 0;
238 }
239
240 delete stmt;
241 delete connDb;
242
243 return 1;
244}
245
246// ----- Delete attachment from the database ---------------------------
247int ElogAttachment::DeleteAttachment(int record_id, int attachment_number)
248{
250 if (connDb == nullptr)
251 return -1;
252
253 TSQLServer* db_server = connDb->GetSQLServer();
254
255 TString sql = TString::Format("delete from attachment_ "
256 "where record_id = $1 and attachment_number = $2");
257 TSQLStatement* stmt = db_server->Statement(sql);
258
259 stmt->NextIteration();
260 stmt->SetInt(0, record_id);
261 stmt->SetInt(1, attachment_number);
262
263 // delete attachment from the dataBase
264 if (!stmt->Process()) {
265 cout << "ERROR: deleting attachment from the dataBase has been failed" << endl;
266
267 delete stmt;
268 delete connDb;
269 return -2;
270 }
271
272 delete stmt;
273 delete connDb;
274 return 0;
275}
276
277// ----- Delete attachment from the database by unique key --------------
279{
281 if (connDb == nullptr)
282 return -1;
283
284 TSQLServer* db_server = connDb->GetSQLServer();
285
286 TString sql = TString::Format("delete from attachment_ "
287 "where lower(file_path) = lower($1)");
288 TSQLStatement* stmt = db_server->Statement(sql);
289
290 stmt->NextIteration();
291 stmt->SetString(0, file_path);
292
293 // delete attachment from the dataBase
294 if (!stmt->Process()) {
295 cout << "ERROR: deleting attachment from the DataBase has been failed" << endl;
296
297 delete stmt;
298 delete connDb;
299 return -2;
300 }
301
302 delete stmt;
303 delete connDb;
304 return 0;
305}
306
307// ----- Print all 'attachments' ---------------------------------
309{
311 if (connDb == nullptr)
312 return -1;
313
314 TSQLServer* db_server = connDb->GetSQLServer();
315
316 TString sql = TString::Format("select record_id, attachment_number, file_path "
317 "from attachment_");
318 TSQLStatement* stmt = db_server->Statement(sql);
319
320 // get all 'attachments' from the database
321 if (!stmt->Process()) {
322 cout << "ERROR: getting all 'attachments' from the dataBase has been failed" << endl;
323
324 delete stmt;
325 delete connDb;
326 return -2;
327 }
328
329 // store result of statement in buffer
330 stmt->StoreResult();
331
332 // print rows
333 cout << "Table 'attachment_':" << endl;
334 while (stmt->NextResultRow()) {
335 cout << "record_id: ";
336 cout << (stmt->GetInt(0));
337 cout << ", attachment_number: ";
338 cout << (stmt->GetInt(1));
339 cout << ", file_path: ";
340 cout << (stmt->GetString(2));
341 cout << "." << endl;
342 }
343
344 delete stmt;
345 delete connDb;
346
347 return 0;
348}
349
350// Setters functions
352{
353 if (!connectionDB) {
354 cout << "CRITICAL ERROR: Connection object is null" << endl;
355 return -1;
356 }
357
358 TSQLServer* db_server = connectionDB->GetSQLServer();
359
360 TString sql = TString::Format("update attachment_ "
361 "set record_id = $1 "
362 "where record_id = $2 and attachment_number = $3");
363 TSQLStatement* stmt = db_server->Statement(sql);
364
365 stmt->NextIteration();
366 stmt->SetInt(0, record_id);
367 stmt->SetInt(1, i_record_id);
368 stmt->SetInt(2, i_attachment_number);
369
370 // write new value to the database
371 if (!stmt->Process()) {
372 cout << "ERROR: updating information about attachment has been failed" << endl;
373
374 delete stmt;
375 return -2;
376 }
377
378 i_record_id = record_id;
379
380 delete stmt;
381 return 0;
382}
383
384int ElogAttachment::SetAttachmentNumber(int attachment_number)
385{
386 if (!connectionDB) {
387 cout << "CRITICAL ERROR: Connection object is null" << endl;
388 return -1;
389 }
390
391 TSQLServer* db_server = connectionDB->GetSQLServer();
392
393 TString sql = TString::Format("update attachment_ "
394 "set attachment_number = $1 "
395 "where record_id = $2 and attachment_number = $3");
396 TSQLStatement* stmt = db_server->Statement(sql);
397
398 stmt->NextIteration();
399 stmt->SetInt(0, attachment_number);
400 stmt->SetInt(1, i_record_id);
401 stmt->SetInt(2, i_attachment_number);
402
403 // write new value to the database
404 if (!stmt->Process()) {
405 cout << "ERROR: updating information about attachment has been failed" << endl;
406
407 delete stmt;
408 return -2;
409 }
410
411 i_attachment_number = attachment_number;
412
413 delete stmt;
414 return 0;
415}
416
417int ElogAttachment::SetFilePath(TString file_path)
418{
419 if (!connectionDB) {
420 cout << "CRITICAL ERROR: Connection object is null" << endl;
421 return -1;
422 }
423
424 TSQLServer* db_server = connectionDB->GetSQLServer();
425
426 TString sql = TString::Format("update attachment_ "
427 "set file_path = $1 "
428 "where record_id = $2 and attachment_number = $3");
429 TSQLStatement* stmt = db_server->Statement(sql);
430
431 stmt->NextIteration();
432 stmt->SetString(0, file_path);
433 stmt->SetInt(1, i_record_id);
434 stmt->SetInt(2, i_attachment_number);
435
436 // write new value to the database
437 if (!stmt->Process()) {
438 cout << "ERROR: updating information about attachment has been failed" << endl;
439
440 delete stmt;
441 return -2;
442 }
443
444 str_file_path = file_path;
445
446 delete stmt;
447 return 0;
448}
449
450// ----- Print current attachment ---------------------------------------
452{
453 cout << "Table 'attachment_'";
454 cout << ". record_id: " << i_record_id << ". attachment_number: " << i_attachment_number
455 << ". file_path: " << str_file_path << endl;
456
457 return;
458}
459/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
static ElogAttachment * GetAttachment(int record_id, int attachment_number)
get attachment from the database
void Print()
print information about current attachment
static int PrintAll()
print all attachments
static int DeleteAttachment(int record_id, int attachment_number)
delete attachment from the database
static ElogAttachment * CreateAttachment(int record_id, int attachment_number, TString file_path)
add new attachment to the database
int SetRecordId(int record_id)
set record id of the current attachment
virtual ~ElogAttachment()
int SetFilePath(TString file_path)
set file path of the current attachment
int SetAttachmentNumber(int attachment_number)
set attachment number of the current attachment
static int CheckAttachmentExists(int record_id, int attachment_number)
check attachment exists in the database: 1- true, 0 - false, <0 - database operation error
static ElogConnection * Open()
TSQLServer * GetSQLServer()
STL namespace.