BmnRoot
Loading...
Searching...
No Matches
UniParameterType.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// UniParameterType cxx file
3// Generated 05-06-2023
4// ----------------------------------------------------------------------
5
6#include "UniParameterType.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 -----------------------
16UniParameterType::UniParameterType(UniConnection* db_connect, int type_id, TString type_name)
17{
18 connectionDB = db_connect;
19
20 i_type_id = type_id;
21 str_type_name = type_name;
22}
23
24// ----- Destructor -------------------------------------------------
26{
27 if (connectionDB)
28 delete connectionDB;
29}
30
31// ----- Creating new parameter type in the database ---------------------------
33{
35 if (connDb == nullptr)
36 return nullptr;
37
38 TSQLServer* db_server = connDb->GetSQLServer();
39
40 TString sql = TString::Format("insert into parameter_type(type_name) "
41 "values ($1)");
42 TSQLStatement* stmt = db_server->Statement(sql);
43
44 stmt->NextIteration();
45 stmt->SetString(0, type_name);
46
47 // inserting new parameter type to the Database
48 if (!stmt->Process()) {
49 cout << "ERROR: inserting new parameter type to the Database has been failed" << endl;
50 delete stmt;
51 delete connDb;
52 return nullptr;
53 }
54
55 delete stmt;
56
57 // getting last inserted ID
58 int type_id;
59 TSQLStatement* stmt_last =
60 db_server->Statement("SELECT currval(pg_get_serial_sequence('parameter_type','type_id'))");
61
62 // process getting last id
63 if (stmt_last->Process()) {
64 // store result of statement in buffer
65 stmt_last->StoreResult();
66
67 // if there is no last id then exit with error
68 if (!stmt_last->NextResultRow()) {
69 cout << "ERROR: no last ID in DB!" << endl;
70 delete stmt_last;
71 return nullptr;
72 } else {
73 type_id = stmt_last->GetInt(0);
74 delete stmt_last;
75 }
76 } else {
77 cout << "ERROR: getting last ID has been failed!" << endl;
78 delete stmt_last;
79 return nullptr;
80 }
81
82 int tmp_type_id;
83 tmp_type_id = type_id;
84 TString tmp_type_name;
85 tmp_type_name = type_name;
86
87 return new UniParameterType(connDb, tmp_type_id, tmp_type_name);
88}
89
90// ----- Get parameter type from the database ---------------------------
92{
94 if (connDb == nullptr)
95 return nullptr;
96
97 TSQLServer* db_server = connDb->GetSQLServer();
98
99 TString sql = TString::Format("select type_id, type_name "
100 "from parameter_type "
101 "where type_id = %d",
102 type_id);
103 TSQLStatement* stmt = db_server->Statement(sql);
104
105 // get parameter type from the database
106 if (!stmt->Process()) {
107 cout << "ERROR: getting parameter type from the database has been failed" << endl;
108
109 delete stmt;
110 delete connDb;
111 return nullptr;
112 }
113
114 // store result of statement in buffer
115 stmt->StoreResult();
116
117 // extract row
118 if (!stmt->NextResultRow()) {
119 cout << "ERROR: parameter type was not found in the database" << endl;
120
121 delete stmt;
122 delete connDb;
123 return nullptr;
124 }
125
126 int tmp_type_id;
127 tmp_type_id = stmt->GetInt(0);
128 TString tmp_type_name;
129 tmp_type_name = stmt->GetString(1);
130
131 delete stmt;
132
133 return new UniParameterType(connDb, tmp_type_id, tmp_type_name);
134}
135
136// ----- Check parameter type exists in the database ---------------------------
138{
140 if (connDb == nullptr)
141 return -1;
142
143 TSQLServer* db_server = connDb->GetSQLServer();
144
145 TString sql = TString::Format("select 1 "
146 "from parameter_type "
147 "where type_id = %d",
148 type_id);
149 TSQLStatement* stmt = db_server->Statement(sql);
150
151 // get parameter type from the database
152 if (!stmt->Process()) {
153 cout << "ERROR: getting parameter type from the database has been failed" << endl;
154
155 delete stmt;
156 delete connDb;
157 return -2;
158 }
159
160 // store result of statement in buffer
161 stmt->StoreResult();
162
163 // extract row
164 if (!stmt->NextResultRow()) {
165 delete stmt;
166 delete connDb;
167 return 0;
168 }
169
170 delete stmt;
171 delete connDb;
172
173 return 1;
174}
175
176// ----- Delete parameter type from the database ---------------------------
178{
180 if (connDb == nullptr)
181 return -1;
182
183 TSQLServer* db_server = connDb->GetSQLServer();
184
185 TString sql = TString::Format("delete from parameter_type "
186 "where type_id = $1");
187 TSQLStatement* stmt = db_server->Statement(sql);
188
189 stmt->NextIteration();
190 stmt->SetInt(0, type_id);
191
192 // delete parameter type from the dataBase
193 if (!stmt->Process()) {
194 cout << "ERROR: deleting parameter type from the dataBase has been failed" << endl;
195
196 delete stmt;
197 delete connDb;
198 return -2;
199 }
200
201 delete stmt;
202 delete connDb;
203 return 0;
204}
205
206// ----- Print all 'parameter types' ---------------------------------
208{
210 if (connDb == nullptr)
211 return -1;
212
213 TSQLServer* db_server = connDb->GetSQLServer();
214
215 TString sql = TString::Format("select type_id, type_name "
216 "from parameter_type");
217 TSQLStatement* stmt = db_server->Statement(sql);
218
219 // get all 'parameter types' from the database
220 if (!stmt->Process()) {
221 cout << "ERROR: getting all 'parameter types' from the dataBase has been failed" << endl;
222
223 delete stmt;
224 delete connDb;
225 return -2;
226 }
227
228 // store result of statement in buffer
229 stmt->StoreResult();
230
231 // print rows
232 cout << "Table 'parameter_type':" << endl;
233 while (stmt->NextResultRow()) {
234 cout << "type_id: ";
235 cout << (stmt->GetInt(0));
236 cout << ", type_name: ";
237 cout << (stmt->GetString(1));
238 cout << "." << endl;
239 }
240
241 delete stmt;
242 delete connDb;
243
244 return 0;
245}
246
247// Setters functions
248int UniParameterType::SetTypeName(TString type_name)
249{
250 if (!connectionDB) {
251 cout << "CRITICAL ERROR: Connection object is null" << endl;
252 return -1;
253 }
254
255 TSQLServer* db_server = connectionDB->GetSQLServer();
256
257 TString sql = TString::Format("update parameter_type "
258 "set type_name = $1 "
259 "where type_id = $2");
260 TSQLStatement* stmt = db_server->Statement(sql);
261
262 stmt->NextIteration();
263 stmt->SetString(0, type_name);
264 stmt->SetInt(1, i_type_id);
265
266 // write new value to the database
267 if (!stmt->Process()) {
268 cout << "ERROR: updating information about parameter type has been failed" << endl;
269
270 delete stmt;
271 return -2;
272 }
273
274 str_type_name = type_name;
275
276 delete stmt;
277 return 0;
278}
279
280// ----- Print current parameter type ---------------------------------------
282{
283 cout << "Table 'parameter_type'";
284 cout << ". type_id: " << i_type_id << ". type_name: " << str_type_name << endl;
285
286 return;
287}
288/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
static UniConnection * Open()
TSQLServer * GetSQLServer()
static UniParameterType * GetParameterType(int type_id)
get parameter type from the database
static int DeleteParameterType(int type_id)
delete parameter type from the database
int SetTypeName(TString type_name)
set type name of the current parameter type
static UniParameterType * CreateParameterType(TString type_name)
add new parameter type to the database
static int PrintAll()
print all parameter types
void Print()
print information about current parameter type
static int CheckParameterTypeExists(int type_id)
check parameter type exists in the database: 1- true, 0 - false, <0 - database operation error
STL namespace.