-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.cpp
308 lines (228 loc) · 7.39 KB
/
Table.cpp
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
#include "Table.h"
db::Table::Table()
{
record_container = {};
attributes = {};
}
db::Table::Table(const Table& t)
{
attributes = t.attributes;
record_container = t.record_container;
_size = 0;
}
db::Table::Table(vector<string> value)
{
for (int iterator = 0; iterator < value.size(); iterator++){
Attribute a;
a.attribute = value.at(iterator);
a.value_attr = "";
attributes.push_back(a);
_size++;
}
attributes = {};
record_container = {};
}
db::Table::Table(int size)
{
_size = size;
}
void db::Table::add_attribute(std::string table_attribute)
{
Attribute a;
a.attribute = table_attribute;
a.value_attr = "";
attributes.push_back(a);
_size++;
}
void db::Table::delete_attribute(std::string table_attribute)
{
int attributes_index;
for (int attr_iterator = 0; attr_iterator < attributes.size(); attr_iterator++) {
if (attributes.at(attr_iterator).attribute == table_attribute) {
attributes_index = attr_iterator;
}
}
attributes.erase(attributes.begin() + attributes_index);
return;
}
std::vector<Attribute> db::Table::get_attribute_in_order() {
vector<string> attribute_values;
for (int i = 0; i < attributes.size(); i++) {
attribute_values.push_back(attributes.at(i).attribute);
}
return attribute_values;
}
//returns the number of records in the table
int db::Table::get_size()
{
return record_container.size();
}
//a way to iterate through the table to get a Record
db::Record db::Table::record_iterator(int index) {
return record_container.at(index);
}
void db::Table::specify_key(std::string key_attribute)
{
for (int i = 0; i < attributes.size(); i++) {
if (attributes.at(i).attribute == key_attribute) {
attributes.at(i).value_attr = "Key";
}
}
}
//function to implement cross join
db::Table db::Table::cross_join(Table a, Table b) {
Table t;
//for loops to iterate through vector or attributes
for (int i = 0; i < a.attributes.size(); i++) {
for (int j = 0; j < b.attributes.size(); j++) {
t.attributes.push_back(b.attributes.at(j));
}
t.attributes.push_back(a.attributes.at(i));
}
//for loops to iterate through vector of records
for (int i = 0; i < a.record_container.size(); i++) {
for (int j = 0; j < b.record_container.size(); j++) {
t.record_container.push_back(b.record_container.at(j));
}
t.record_container.push_back(a.record_container.at(i));
}
}
//function to implement natural join
db::Table db::Table::natural_join(Table a, Table b) {
Table t;
//values to identify the locations for matching attributes in tables
int a_attribute_location;
int b_attribute_location;
//for loop to iterate through attributes
for (int i = 0; i < a.attributes.size(); i++) {
for (int j = 0; j < b.attributes.size(); j++) {
//identifies if location in a matches location in b
if (a.attributes[i].attribute == b.attributes[j].attribute) {
//sets the current locations to the matching attribute varaibles
a_attribute_location = i;
b_attribute_location = j;
//pushback the matching attributes into new table
t.attributes.push_back(a.attributes[i]);
//pushbacks all attributes in table a (minus the matching)
for (int k = 0; k < a.attributes.size() && k != a_attribute_location; k++) {
t.attributes.push_back(a.attributes[k]);
}
//pushbacks all attributes in table b (minus the matching)
for (int l = 0; l < b.attributes.size() && l != b_attribute_location; l++) {
t.attributes.push_back(b.attributes[l]);
}
break;
}
}
}
//assisns the locations for matching attributes in tables to a new varaible
int a_record_location = a_attribute_location;
int b_record_location = b_attribute_location;
//for loop to iterate through records
for (unsigned int a_record_iterator = 0; a_record_iterator < a.record_container.size(); a_record_iterator++) {
for (unsigned int b_record_iterator = 0; b_record_iterator < b.record_container.size(); b_record_iterator++) {
//pushbacks all records in table a (minus the matching)
for (int i = 0; i < a.record_container.size() && i != a_record_location; i++) {
t.record_container.push_back(a.record_container[i]);
}
//pushbacks all records in table b (minus the matching)
for (int j = 0; j < b.record_container.size() && j != b_record_location; j++) {
t.record_container.push_back(b.record_container[j]);
}
}
}
}
//computes count
int db::Table::compute_count(string table_attribute) {
int count;
int index_location;
//for loop to find the location of the specified table attribute
for (int i = 0; i < record_container.size(); i++) {
if (attributes[i].attribute == table_attribute) {
index_location = i;
}
}
Record temp_record;
//for loop to count how many times the table attribute appears
for (int i = 0; record_container.size(); i++) {
temp_record = record_container.at(i);
if (temp_record.get(index_location) != "") {
count++;
}
}
return count;
}
//computes minimum record
string db::Table::compute_min(string table_attribute) {
string minimum_value = "";
int selection_location;
for (int i = 0; i < attributes.size(); i++) {
if (attributes[i].attribute == table_attribute) {
selection_location = i;
}
break;
}
for (int j = 0; j < record_container.size(); j++) {
Record temp_record = record_container.at(j);
if (minimum_value == "") {
minimum_value = temp_record.get(selection_location);
}
else if ((temp_record.get(selection_location) < minimum_value) &&
(temp_record.get(selection_location) != "")){
minimum_value = temp_record.get(selection_location);
}
else {
}
}
return minimum_value;
}
//computes maximum record
string db::Table::compute_max(string table_attribute) {
string maximum_value = "";
int selection_location;
for (int i = 0; i < attributes.size(); i++) {
if (attributes[i].attribute == table_attribute) {
selection_location = i;
}
break;
}
for (int j = 0; j < record_container.size(); j++) {
Record temp_record = record_container.at(j);
if (maximum_value == "") {
maximum_value = temp_record.get(selection_location);
}
else if ((temp_record.get(selection_location) > maximum_value) &&
(temp_record.get(selection_location) != "")) {
maximum_value = temp_record.get(selection_location);
}
else {
}
}
return maximum_value;
}
void db::Table::add_record(Record r)
{
record_container.push_back(r);
}
/*Extra credit functions*/
int db::Table::get_number_attributes() {
return attributes.size();
}
void db::Table::rename_attribute(string find, string replace) {
for (int find_iterator = 0; find_iterator < attributes.size(); find_iterator++) {
if (attributes.at(find_iterator).attribute == find) {
attributes.at(find_iterator).attribute = replace;
return;
}
}
std::cout << "Attribute not found" << std::endl;
}
void db::Table::specify_key(vector<string> key_values) {
for (int i = 0; i < attributes.size(); i++) {
for (int j = 0; j < key_values.size(); j++) {
if (attributes.at(i).attribute == key_values.at(j)) {
attributes.at(i).value_attr = "Key";
}
}
}
}