9#include "TSQLStatement.h"
16ElogAttachment::ElogAttachment(
ElogConnection* db_connect,
int record_id,
int attachment_number, TString file_path)
18 connectionDB = db_connect;
20 i_record_id = record_id;
21 i_attachment_number = attachment_number;
22 str_file_path = file_path;
36 if (connDb ==
nullptr)
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);
45 stmt->NextIteration();
46 stmt->SetInt(0, record_id);
47 stmt->SetInt(1, attachment_number);
48 stmt->SetString(2, file_path);
51 if (!stmt->Process()) {
52 cout <<
"ERROR: inserting new attachment to the Database has been failed" << endl;
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;
67 return new ElogAttachment(connDb, tmp_record_id, tmp_attachment_number, tmp_file_path);
74 if (connDb ==
nullptr)
79 TString sql = TString::Format(
"select record_id, attachment_number, file_path "
81 "where record_id = %d and attachment_number = %d",
82 record_id, attachment_number);
83 TSQLStatement* stmt = db_server->Statement(sql);
86 if (!stmt->Process()) {
87 cout <<
"ERROR: getting attachment from the database has been failed" << endl;
98 if (!stmt->NextResultRow()) {
99 cout <<
"ERROR: attachment was not found in the database" << endl;
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);
115 return new ElogAttachment(connDb, tmp_record_id, tmp_attachment_number, tmp_file_path);
122 if (connDb ==
nullptr)
127 TString sql = TString::Format(
"select record_id, attachment_number, file_path "
129 "where lower(file_path) = lower('%s')",
131 TSQLStatement* stmt = db_server->Statement(sql);
134 if (!stmt->Process()) {
135 cout <<
"ERROR: getting attachment from the database has been failed" << endl;
146 if (!stmt->NextResultRow()) {
147 cout <<
"ERROR: attachment was not found in the database" << endl;
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);
163 return new ElogAttachment(connDb, tmp_record_id, tmp_attachment_number, tmp_file_path);
170 if (connDb ==
nullptr)
175 TString sql = TString::Format(
"select 1 "
177 "where record_id = %d and attachment_number = %d",
178 record_id, attachment_number);
179 TSQLStatement* stmt = db_server->Statement(sql);
182 if (!stmt->Process()) {
183 cout <<
"ERROR: getting attachment from the database has been failed" << endl;
194 if (!stmt->NextResultRow()) {
210 if (connDb ==
nullptr)
215 TString sql = TString::Format(
"select 1 "
217 "where lower(file_path) = lower('%s')",
219 TSQLStatement* stmt = db_server->Statement(sql);
222 if (!stmt->Process()) {
223 cout <<
"ERROR: getting attachment from the database has been failed" << endl;
234 if (!stmt->NextResultRow()) {
250 if (connDb ==
nullptr)
255 TString sql = TString::Format(
"delete from attachment_ "
256 "where record_id = $1 and attachment_number = $2");
257 TSQLStatement* stmt = db_server->Statement(sql);
259 stmt->NextIteration();
260 stmt->SetInt(0, record_id);
261 stmt->SetInt(1, attachment_number);
264 if (!stmt->Process()) {
265 cout <<
"ERROR: deleting attachment from the dataBase has been failed" << endl;
281 if (connDb ==
nullptr)
286 TString sql = TString::Format(
"delete from attachment_ "
287 "where lower(file_path) = lower($1)");
288 TSQLStatement* stmt = db_server->Statement(sql);
290 stmt->NextIteration();
291 stmt->SetString(0, file_path);
294 if (!stmt->Process()) {
295 cout <<
"ERROR: deleting attachment from the DataBase has been failed" << endl;
311 if (connDb ==
nullptr)
316 TString sql = TString::Format(
"select record_id, attachment_number, file_path "
318 TSQLStatement* stmt = db_server->Statement(sql);
321 if (!stmt->Process()) {
322 cout <<
"ERROR: getting all 'attachments' from the dataBase has been failed" << endl;
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));
354 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
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);
365 stmt->NextIteration();
366 stmt->SetInt(0, record_id);
367 stmt->SetInt(1, i_record_id);
368 stmt->SetInt(2, i_attachment_number);
371 if (!stmt->Process()) {
372 cout <<
"ERROR: updating information about attachment has been failed" << endl;
378 i_record_id = record_id;
387 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
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);
398 stmt->NextIteration();
399 stmt->SetInt(0, attachment_number);
400 stmt->SetInt(1, i_record_id);
401 stmt->SetInt(2, i_attachment_number);
404 if (!stmt->Process()) {
405 cout <<
"ERROR: updating information about attachment has been failed" << endl;
411 i_attachment_number = attachment_number;
420 cout <<
"CRITICAL ERROR: Connection object is null" << endl;
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);
431 stmt->NextIteration();
432 stmt->SetString(0, file_path);
433 stmt->SetInt(1, i_record_id);
434 stmt->SetInt(2, i_attachment_number);
437 if (!stmt->Process()) {
438 cout <<
"ERROR: updating information about attachment has been failed" << endl;
444 str_file_path = file_path;
453 cout <<
"Table 'attachment_'";
454 cout <<
". record_id: " << i_record_id <<
". attachment_number: " << i_attachment_number
455 <<
". file_path: " << str_file_path << endl;
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()