-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperationManager.cpp
More file actions
356 lines (293 loc) · 11.1 KB
/
operationManager.cpp
File metadata and controls
356 lines (293 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// Created by jimena on 16/06/17.
//
#include "operationManager.h"
#include "where.h"
#include "join.h"
bool isInteger(const std::string & s)
{
if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;
char * p ;
strtol(s.c_str(), &p, 10) ;
return (*p == 0) ;
}
bool isDouble(const std::string& s)
{
int nb_point=0;
for (int i=0; i<s.length();i++)
{
if (s[i]=='.')
{
nb_point++;
}
else if (!isdigit(s[i])) {
return false;
}
}
if (nb_point==1)
{
return true;
}
else
{
return false;
}
}
OperationManager::OperationManager(TableManager *tableManager) {
this->tables = tableManager;
}
Table OperationManager::selectAux(std::string tableName, std::vector<std::string> columns, Where whereObject, Join joinObject){
}
/*
{
"command": "execute",
"type": "select",
"what": ["id ", "cedula", "*"],
"from": "estudiantes",
"where": {
"cmd": "AND",
"comparators": [{
"table": "estudiantes",
"column": "id",
"operator": "=",
"value": 5
}, {
"table": "estudiantes",
"column": "cedula",
"operator": "=",
"value": 2000222
}]
},
"join": {
"externalTable": "cursos",
"externalColumn": "idest",
"internalColumn": "id"
}
}
*/
Table applyComparator(Table table, Comparator comparator ){
Table result;
if(comparator.getOperatorType() == "="){
result = TableUtils::equalTo(table,comparator.getColumnName(), comparator.getValue());
}
else if (comparator.getOperatorType()== "<") {
result = TableUtils::lessThan(table,comparator.getColumnName(), comparator.getValue());
}
else if (comparator.getOperatorType() == ">") {
result = TableUtils::greaterThan(table,comparator.getColumnName(), comparator.getValue());
}
//std::cout<<"operator "<< comparator.getOperatorType()<< " "<<comparator.getColumnName() << "\n___________________________\n"<<result.toString()<<"_______________________________________\n";
return result;
}
Table applyWhere(Table table, Where where){
Table result;
if(where.getCondition()=="AND"){
result = table;
for(const Comparator &comparator : where.getComparators()){
result = applyComparator(result, comparator);
//result = TableUtils::AND(result, subResult);
}
}
else if(where.getCondition()=="OR"){
result = TableUtils::extractTemplate(table);
for(const Comparator &comparator : where.getComparators()){
Table subResult = applyComparator(table, comparator);
result = TableUtils::tableUnion(result, subResult);
//std::cout<<"union \n____________________________________\n"<<result.toString()<<"____________________________________\n";
}
}
else{
if(where.getComparators().size()>0){
result = applyComparator(table, where.getComparators()[0]);
}
}
// std::cout<<"where "<< where.getCondition() << "___________________________\n"<<result.toString();
return result;
}
//where sin join
Table OperationManager::selectAux(std::string tableName, std::vector<std::string> columns, Where whereObject){
Table table = tables->getTable(tableName);
//Table result = TableUtils::extractTemplate(table);
Table result = applyWhere(table, whereObject);
result = TableUtils::subTable(result, columns);
//whereObject.
return result;
}
//join sin where
Table OperationManager::selectAux(std::string tableName, std::vector<std::string> columns, Join joinObject){
}
//columnas
Table OperationManager::selectAux(std::string tableName, std::vector<std::string> columns){
Table table = tables->getTable(tableName);
Table result = TableUtils::subTable(table, columns);
return result;
/*if(columns.size()>0 && columns[0]=="*"){
result = table;
}
else{
Table result = TableUtils::subTable(table, columns);
}*/
}
resultCode OperationManager::insert(json j){
Table workingTable;
if(tables->exists(j["name"])){
workingTable = tables->getTable(j["name"]);
std::vector<std::string> toAdd;
for (int i = 0; i < workingTable.getTotalColumns(); i++){
bool found = false;
for (int k = 0; k < j["column_names"].size(); k++){
if (workingTable.getColumnProperties()[i].getName() == j["column_names"][k]){
found = true;
if (workingTable.getColumnProperties()[i].getType() == 0){
toAdd.push_back(j["values"][k]);
break;
}
else if (workingTable.getColumnProperties()[i].getType() == 1){
if (isInteger(j["values"][k])){
toAdd.push_back(j["values"][k]);
break;
}
else{
return resultCode(69,0,"Hay un valor inválido!");
}
}
else if (workingTable.getColumnProperties()[i].getType() == 2){
if (isDouble(j["values"][k])){
toAdd.push_back(j["values"][k]);
break;
}
else{
return resultCode(69,0,"Hay un valor inválido!");
}
}
}
}
if (!found){
toAdd.push_back("");
}
}
workingTable.insertRow(toAdd);
for (int i = 0; i < tables->tableList.size(); i++){
if (tables->tableList[i].getName() == workingTable.getName()){
tables->tableList[i] = workingTable;
break;
}
}
return resultCode(1,1,"Operación exitosa!");
}
return resultCode(404,0,"Table not found!!!");
}
resultCode OperationManager::drop(json j) {
std::cout<<"se esta haciendo drop\n";
int affectedRegisters = tables->deleteTable(j["name"]);
if (affectedRegisters > 1){
return resultCode(1, affectedRegisters, "Operación exitosa!");
}
return resultCode(404, 0, "Tabla no existe");
}
Table OperationManager::select(json inputJson){
Table result;
//columnas
if(inputJson["where"] == "" && inputJson["join"] == ""){
result = selectAux(inputJson["from"], inputJson["what"]);
}
//join sin where
else if(inputJson["where"] == "" && inputJson["join"] != "") {
Join joinObject = JSONutils::jsonToJoin(inputJson["join"]);
result = selectAux(inputJson["from"], inputJson["what"], joinObject);
}
//where sin join
else if(inputJson["where"] != "" && inputJson["join"] == ""){
Where whereObject = JSONutils::jsonToWhere(inputJson["where"]);
result = selectAux(inputJson["from"], inputJson["what"], whereObject);
}
//where con join
else{
Where whereObject = JSONutils::jsonToWhere(inputJson["where"]);
Join joinObject = JSONutils::jsonToJoin(inputJson["join"]);
// result = selectAux(inputJson["from"], inputJson["what"], whereObject, joinObject);
}
std::cout<<"\nSELECT \n____________________________________\n"<<result.toString()<<"____________________________________\n";
return result;
}
resultCode OperationManager::deleteT(json j){
resultCode result(404,0,"Table not found");
if(tables->exists(j["name"])) {
if(j["where"] != "") {
Where whereObject = JSONutils::jsonToWhere(j["where"]);
result = deleteAux(j["name"], whereObject);
}
else{
result = drop(j);
}
}
return result;
}
resultCode OperationManager::update(json j) {
resultCode result(404,0,"Table not found");
if(tables->exists(j["from"])) {
if(j["where"] != "") {
Where whereObject = JSONutils::jsonToWhere(j["where"]);
result = updateAux(j["from"], j["columns"], j["values"], whereObject);
}
else{
result = updateAux(j["from"], j["columns"], j["values"]);
}
}
return result;
}
resultCode
OperationManager::updateAux(std::string tableName, std::vector<std::string> columns, std::vector<std::string> values) {
Table workingTable = tables->getTable(tableName);
for (int i = 0; i < columns.size(); ++i) {
std::cout<<" "<<columns[i];
}
for (int i = 0; i < values.size(); ++i) {
std::cout<<" "<<values[i];
}
workingTable = TableUtils::updateColumns(workingTable, values, columns);
for (int i = 0; i < tables->tableList.size(); i++){
if (tables->tableList[i].getName() == workingTable.getName()){
tables->tableList[i] = workingTable;
break;
}
}
std::cout<<"\nUPDATE \n____________________________________\n"<<workingTable.toString()<<"____________________________________\n";
return resultCode(1, workingTable.getTotalRows(), "Operacion Exitosa!");
}
resultCode
OperationManager::updateAux(std::string tableName, std::vector<std::string> columns, std::vector<std::string> values,
Where whereObject) {
Table workingTable = tables->getTable(tableName);
Table subTable = applyWhere(workingTable, whereObject);
int affectedRegisters = subTable.getTotalRows();
subTable = TableUtils::updateColumns(subTable, values, columns);
//std::cout<<"subtable \n____________________________________\n"<<subTable.toString()<<"____________________________________\n";
Table tableToSend = TableUtils::updateRows(workingTable, subTable);
//std::cout<<"tableto \n____________________________________\n"<<tableToSend.toString()<<"____________________________________\n";
for (int i = 0; i < tables->tableList.size(); i++){
if (tables->tableList[i].getName() == tableToSend.getName()){
tables->tableList[i] = tableToSend;
break;
}
}
std::cout<<"\nUPDATE \n____________________________________\n"<<tableToSend.toString()<<"____________________________________\n";
return resultCode(1, affectedRegisters, "Operacion Exitosa!");
}
resultCode OperationManager::deleteAux(std::string tableName, Where whereObject)
{
Table workingTable = tables->getTable(tableName);
Table subTable = applyWhere(workingTable, whereObject);
Table resultTable = TableUtils::tableDifference(workingTable, subTable);
int affectedRegisters = subTable.getTotalRows();
//std::cout << resultTable.toString()<<std::endl;
for (int i = 0; i < tables->tableList.size(); i++){
if (tables->tableList[i].getName() == resultTable.getName()){
tables->tableList[i] = resultTable;
break;
}
}
std::cout<<"\nDELETE \n____________________________________\n"<<resultTable.toString()<<"____________________________________\n";
std::cout<< resultTable.toString() <<"\n";
return resultCode(1, affectedRegisters, "Operacion Exitosa!");
}