-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonutils.cpp
More file actions
115 lines (99 loc) · 2.83 KB
/
jsonutils.cpp
File metadata and controls
115 lines (99 loc) · 2.83 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
#include "jsonutils.h"
json JSONutils::tableToJson(Table table)
{
json result;
result["name"] = table.getName();
json rows;
for(const Row &row : table.getRows()) {
json column;
for(const std::string content : row.getContents()){
column.push_back(content);
}
rows.push_back(column);
}
//json columnNames;
std::vector<std::string> columnNames;
std::vector<int> columnTypes;
for(const ColumnProperties &property : table.getColumnProperties()) {
//std::cout<<property.getName()<<"\n";
columnNames.push_back(property.getName());
columnTypes.push_back(property.getType());
}
result["columnNames"]= columnNames;
result["columnTypes"]= columnTypes;
result["rows"]= rows;
result["primaryKey"]= table.getPrimaryKey();
//std::cout<<"JSON: " <<result.dump()<<std::endl;
//json *result2 = new json;
//result2 = result;
return result;
}
Table JSONutils::jsonToTable(json inputJson)
{
Table result;
result.setName(inputJson["name"]);
result.setPrimaryKey(inputJson["primaryKey"]);
for (int i = 0; i < inputJson["columnNames"].size(); i++) {
result.insertColumn(inputJson["columnNames"][i],inputJson["columnTypes"][i] );
}
for (const auto &row : inputJson["rows"] ) {
std::vector<std::string> newRow = row;
result.insertRow(Row(newRow));
}
return result;
}
/*
{
"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"
}
}
*/
Where JSONutils::jsonToWhere(json inputJson)
{
Where result;
result.setCondition(inputJson["cmd"]);
std::vector<Comparator> compV;
for(const auto &comparator : inputJson["comparators"]){
compV.push_back(jsonToComparator(comparator));
}
result.setComparators(compV);
return result;
}
Join JSONutils::jsonToJoin(json inputJson)
{
Join result;
result.setExternalColumn(inputJson["externalColumn"]);
result.setExternalTable(inputJson["externalTable"]);
result.setInternalColumn(inputJson["internalColumn"]);
return result;
}
Comparator JSONutils::jsonToComparator(json inputJson)
{
Comparator result;
result.setValue(inputJson["value"]);
result.setColumnName(inputJson["column"]);
result.setOperatorType(inputJson["operator"]);
result.setTableName(inputJson["table"]);
return result;
}