BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
work_binary_object.C
Go to the documentation of this file.
2{
3 TStopwatch timer;
4 timer.Start();
5 gDebug = 0;
6
8 if (connectionUniDb == nullptr) {
9 cout << "ERROR: connection to the database can not be established" << endl;
10 return -1;
11 }
12
13 TSQLServer* db_server = connectionUniDb->GetSQLServer();
14
15 cout << "DB: " << uni_db->GetDB() << ". Type: " << uni_db->GetDBMS() << "." << endl;
16
17 TSQLStatement* stmt = uni_db->Statement("insert into test(d) "
18 "values ($1)");
19
20 string file_name("input.txt");
21 FILE* input_file = fopen(file_name.c_str(), "rb");
22 if (input_file == nullptr)
23 return;
24 fseek(input_file, 0L, SEEK_END);
25 size_t file_size = ftell(input_file);
26 unsigned char* in_array = new unsigned char[file_size];
27 fseek(input_file, 0L, SEEK_SET);
28 int bytes_read = fread(in_array, sizeof(unsigned char), file_size, input_file);
29 if (bytes_read != file_size)
30 return;
31 fclose(input_file);
32
33 stmt->NextIteration();
34 stmt->SetBinary(0, in_array, file_size);
35
36 if (!stmt->Process()) {
37 cout << "ERROR: inserting new record to DB has been failed" << endl;
38 delete[] in_array;
39 delete stmt;
40 delete connectionUniDb;
41 return -11;
42 }
43 delete[] in_array;
44 delete stmt;
45 delete connectionUniDb;
46
47 // print all values
48 connDb = UniConnection::Open();
49 if (connectionUniDb == nullptr) {
50 cout << "ERROR: connection to the database can't be established" << endl;
51 return -2;
52 }
53
54 uni_db = connectionUniDb->GetSQLServer();
55
56 stmt = uni_db->Statement("select d from test");
57 if (!stmt->Process()) {
58 cout << "ERROR: reading all record from DB has been failed" << endl;
59 delete stmt;
60 delete connectionUniDb;
61 return -12;
62 }
63
64 // store result of statement in buffer
65 stmt->StoreResult();
66
67 // if there is no last id then exit with error
68 while (stmt->NextResultRow()) {
69 void* mem = 0;
70 Long_t size_mem = 0;
71 stmt->GetBinary(0, mem, size_mem);
72
73 string file_name_out("output.txt");
74 FILE* output_file = fopen(file_name_out.c_str(), "wb");
75 int bytes_written = fwrite(mem, sizeof(unsigned char), size_mem, output_file);
76 fclose(output_file);
77 }
78
79 delete stmt;
80 delete connectionUniDb;
81
82 timer.Stop();
83 Double_t rtime = timer.RealTime(), ctime = timer.CpuTime();
84 printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime);
85
86 cout << "Test was successfull" << endl;
87}
Int_t gDebug
long Long_t
double Double_t
const char * GetDB() const
virtual Bool_t SetBinary(Int_t, void *, Long_t, Long_t=0x1000)
virtual Bool_t NextIteration()=0
virtual Bool_t NextResultRow()=0
virtual Bool_t Process()=0
virtual Bool_t StoreResult()=0
virtual Bool_t GetBinary(Int_t, void *&, Long_t &)
Double_t RealTime()
void Start(Bool_t reset=kTRUE)
Double_t CpuTime()
void Stop()
static UniConnection * Open()
void work_binary_object()