-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableutils.cpp
More file actions
268 lines (231 loc) · 7.33 KB
/
tableutils.cpp
File metadata and controls
268 lines (231 loc) · 7.33 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
#include "tableutils.h"
//Table extractTemplate(Table table){
Table TableUtils::extractTemplate(Table table){
Table result;
result.setName(table.getName());
result.setPrimaryKey(table.getPrimaryKey());
result.setColumnProperties(table.getColumnProperties());
return result;
}
bool existOnTable(Row row, Table table){
bool result = false;
int keyIndex = table.getPrimaryKeyIndex();
for(const Row &tableRow : table.getRows()){
if(tableRow.getContents()[keyIndex] == row.getContents()[keyIndex]){
result = true;
break;
}
}
return result;
}
Table TableUtils::tableUnion(Table table1, Table table2)
{
Table result = extractTemplate(table1);
int keyIndex = table1.getPrimaryKeyIndex();
result.setRows(table1.getRows());
for(const Row &row : table2.getRows() ){
if(!existOnTable(row, result)){
result.insertRow(row);
}
}
return result;
}
Table TableUtils::tableIntersection(Table table1, Table table2)
{
Table result = extractTemplate(table1);
int keyIndex = table1.getPrimaryKeyIndex();
for(const Row &row : table1.getRows() ){
if(existOnTable(row, table2)){
result.insertRow(row);
}
}
return result;
}
Table TableUtils::equalTo(Table table, std::string columnName, std::string value)
{
Table result = extractTemplate(table);
int index = result.getColumnIndex(columnName);
if(index != -1){
for(const Row &row: table.getRows()){
if(row.getContents()[index]==value){
result.insertRow(row);
}
}
}
return result;
}
Table lessThanInt(Table table, std::string columnName, std::string value)
{
int valueI = std::stoi(value);
Table result = TableUtils::extractTemplate(table);
int index = result.getColumnIndex(columnName);
if(index != -1){
for(const Row &row: table.getRows()){
if( std::stoi(row.getContents()[index]) < valueI ){
result.insertRow(row);
}
}
}
return result;
}
Table lessThanDouble(Table table, std::string columnName, std::string value)
{
double valueD = std::stoi(value);
Table result = TableUtils::extractTemplate(table);
int index = result.getColumnIndex(columnName);
if(index != -1){
for(const Row &row: table.getRows()){
if( std::stod(row.getContents()[index]) < valueD ){
result.insertRow(row);
}
}
}
return result;
}
Table lessThanString(Table table, std::string columnName, std::string value)
{
Table result = TableUtils::extractTemplate(table);
int index = result.getColumnIndex(columnName);
if(index != -1){
for(const Row &row: table.getRows()){
if( row.getContents()[index].compare(value) < 0 ){
result.insertRow(row);
}
}
}
return result;
}
Table TableUtils::lessThan(Table table, std::string columnName, std::string value)
{
Table result;
int index = table.getColumnIndex(columnName);
if(index != -1){
int type = table.getColumnProperties()[index].getType();
if(type == INT)
result = lessThanInt( table, columnName, value);
else if (type == DOUBLE)
result = lessThanDouble( table, columnName, value);
else if (type == STRING)
result = lessThanString( table, columnName, value);
}
return result;
}
Table greaterThanInt(Table table, std::string columnName, std::string value)
{
int valueI= std::stoi(value);
Table result = TableUtils::extractTemplate(table);
int index = result.getColumnIndex(columnName);
if(index != -1){
for(const Row &row: table.getRows()){
if( std::stoi(row.getContents()[index]) > valueI ){
result.insertRow(row);
}
}
}
return result;
}
Table greaterThanDouble(Table table, std::string columnName, std::string value)
{
double valueD =std::stod(value);
Table result = TableUtils::extractTemplate(table);
int index = result.getColumnIndex(columnName);
if(index != -1){
for(const Row &row: table.getRows()){
if( std::stod(row.getContents()[index]) > valueD ){
result.insertRow(row);
}
}
}
return result;
}
Table greaterThanString(Table table, std::string columnName, std::string value)
{
Table result = TableUtils::extractTemplate(table);
int index = result.getColumnIndex(columnName);
if(index != -1){
for(const Row &row: table.getRows()){
if( row.getContents()[index].compare(value) > 0 ){
result.insertRow(row);
}
}
}
return result;
}
Table TableUtils::greaterThan(Table table, std::string columnName, std::string value)
{
Table result;
int index = table.getColumnIndex(columnName);
if(index != -1){
int type = table.getColumnProperties()[index].getType();
if(type == INT)
result = greaterThanInt( table, columnName, value);
else if (type == DOUBLE)
result = greaterThanDouble( table, columnName, value);
else if (type == STRING)
result = greaterThanString( table, columnName, value);
}
return result;
}
Table TableUtils::subTable(Table table, std::vector<std::string> columnsNames, std::vector<int> rowsIndexes)
{
Table result = extractTemplate(table);
if(!rowsIndexes.empty()){
result.setRows(table.getRows(rowsIndexes));
}
else{
result.setRows(table.getRows());
}
if(!(columnsNames.empty() or columnsNames[0]=="*" )){
for(const ColumnProperties &columnProperty : result.getColumnProperties()){
bool exists= false;
for(const std::string &columnName: columnsNames){
if(columnProperty.getName()==columnName){
exists=true;
break;
}
}
if(!exists){
result.removeColumn(columnProperty.getName());
}
}
}
return result;
}
Table TableUtils::tableDifference(Table tableMain, Table tableToRemove)
{
Table result = extractTemplate(tableMain);
for(const Row &row:tableToRemove.getRows()){
if(!existOnTable(row, tableToRemove)){
result.insertRow(row);
}
}
return result;
}
Table TableUtils::updateRows(Table tableToUpdate, Table newData)
{
int index = tableToUpdate.getPrimaryKeyIndex();
///Table result = extractTemplate(table);
if(index != -1){
for(int i = 0; i < tableToUpdate.getRows().size(); ++i){
bool updated=false;
for(const Row &row:newData.getRows()){
if(tableToUpdate.getRows()[i].getContents()[index]==row.getContents()[index]){
Row newrow = row;
newrow.setIndex(i);
tableToUpdate.updateRow(newrow);
//updated=true;
//break;
}
}
}
}
return tableToUpdate;
}
Table TableUtils::updateColumns(Table table, std::vector<std::string> contents, std::vector<std::string> columnsNames)
{
//std::cout<<"cont size"<<contents.size();
for (int i = 0; i < contents.size(); ++i) {
table.updateColumn(columnsNames[i],contents[i]);
}
return table;
}