-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cpp
298 lines (258 loc) · 7.04 KB
/
Database.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
#include "Database.h"
namespace db{
Database::Database()
{
/*
*initialized the vector
*/
database = vector<Table>();
names_of_table = vector<std::string>();
}
Database::Database(const Database &d)
{
/*
*copied database vectors from one to the other
*/
database = d.database;
names_of_table = d.names_of_table;
}
Database::~Database()
{
/*
* Remove all the elements of the database
*/
for (int i = names_of_table.size(); i >= 0; i--)
{
names_of_table.erase(names_of_table.begin() + i);
}
for (int i = database.size(); i >= 0; i--)
{
database.erase(database.begin()+i);
}
}
void Database::add_table(Table object, std::string name)
{
/*
* Add new table to database and to names of tables. Allows for copies to be inserted.
*/
names_of_table.push_back(name);
database.push_back(object);
}
void Database::drop_table(std::string name)
{
/*
* Remove table from database and from names of tables.
*/
for (unsigned int i = 0; i < names_of_table.size(); i++)
{
if (names_of_table[i] == name)
{
names_of_table.erase(names_of_table.begin() + i);
database.erase(database.begin() + i);
}
else
{
continue;
}
}
}
vector<std::string> Database::list_table()
{
/*
* Return all table names
*/
return names_of_table;
}
vector<Table> Database::get_table()
{
/*
* Return all tables
*/
return database;
}
Table Database::query_command(std::string select, std::string from, std::string where)
{
/*
* Query table for select elements, from table, and where definition. The table
* created will be returned at the end of the program.
*/
Table table;
std::vector<std::string>attr;
for (int i = 0; i < database.size(); i++)
{
if (names_of_table[i] == from)
{
table = database[i];
}
}
if (select == "*")
{
std::vector<Attribute>all_attributes = table.get_attribute_in_order();
for (int i = 0; i < table.get_attr_size(); i++)
{
std::string attribute = all_attributes[i].attribute;
attr.push_back(attribute);
}
}
else {
std::regex reg("[^,\\s]+");
std::sregex_token_iterator iter(select.begin(), select.end(), reg, 0);
std::sregex_token_iterator end;
for (; iter != end; ++iter) {
std::string attr_name_record = *iter;
attr.push_back(attr_name_record);
}
}
return table.query(attr, where);
}
/*Extra credit functions*/
//function will save at the specified name the database is a csv file
void Database::save_function(string save_file) {
//name used to save the file.
string file_name = save_file + ".csv";
ofstream outputFile(file_name);
//get number of tables in the database
int number_of_tables = database.size();
vector<int> rows_table;
vector<int> columns_table;
bool attributes_set = false;
//initializes the rows_table and columns_table vectors
for (int i = 0; i < database.size(); i++) {
Table tableObj = database.at(i);
//gets the size of the table's rows and columns
int rows_size = tableObj.get_size();
int columns_size = tableObj.get_number_attributes();
rows_table.push_back(rows_size);
columns_table.push_back(columns_size);
}
//keeps the new file open and keeps adding stuff to the files
if (outputFile.is_open()) {
for (int i = 0; i < number_of_tables; i++) {
Table obj = database.at(i);
//sets the table name in the csv with a marker ':' to specify that
//it is a table name
outputFile << ":" + names_of_table.at(i) + "\n";
for (int j = 0; j < obj.get_size(); j++) {
Record temp = obj.record_iterator(j);
//sets the attributes in the csv with a marker ";" to specify that
//it is the table attributes
if (!attributes_set) {
vector<Attribute> table_attr = obj.get_attribute_in_order();
vector<string> tableAttributes;
for (int i = 0; i < table_attr.size(); i++)
{
tableAttributes.push_back(table_attr[i].attribute);
}
outputFile << ";";
for (int k = 0; k < obj.get_number_attributes(); k++) {
outputFile << tableAttributes.at(k) + ",";
}
outputFile << "\n";
attributes_set = true;
}
//sets the records in the csv file no marker is needed
for (int k = 0; k < temp.size(); k++) {
outputFile << temp.get(k) + ",";
}
outputFile << "\n";
}
//this marker will state that a table has finished
outputFile << "New-Table \n";
attributes_set = false;
}
}
outputFile.close();
}
//loads a csv and creates a database with is using the specified format
//for the title the marker is a ':' in the first cell
//for the attribuets the ';' in the first cell
//and when a new table is specified then "New-Table" is in a new row
void Database::load_function(string target_file) {
//string used to concatenate characters to it
string line;
ifstream inputFile (target_file);
//opens the target file
if (inputFile.is_open()) {
Table t1;
//gets lines from the cvs file
while (getline(inputFile, line)) {
//this sets the title of the table
if (line[0] == ':') {
string tableName = line.substr(1, line.size());
names_of_table.push_back(tableName);
}
//this sets the attributes of the table
else if (line[0] == ';') {
string tableattr = line.substr(1, line.size());
string attr;
int counter = 0;
//the csv splits the cell in a row by ',' so this loop
//splits the attributes by ',' and pushes the attribute
for (int i = 0; i < tableattr.size(); i++) {
//pushes the attributes into the table
if (tableattr.at(i) == ',') {
t1.add_attribute(attr);
attr = "";
counter++;
}
//concatenates characters into the string
else {
attr = attr + tableattr[i];
}
}
}
//this pushes a new table into the database
else if (line == "New-Table ") {
database.push_back(t1);
t1.~Table();
}
//this condition splts the records by ',' and puts them
//in the record
else {
string tokens;
Record r1;
//counter to keep track at what index the record is in
int counter = 0;
//loop that sets cells in the record by concatenating
//characters until a ',' is found
for (int i = 0; i < line.size(); i++) {
//pushes a token in to the record
if (line.at(i) == ',') {
r1.set(counter,tokens);
tokens = "";
counter++;
}
//concatenates the string
else {
tokens = tokens + line[i];
}
}
//adds the record to the table
t1.add_record(r1);
}
}
}
inputFile.close();
}
//merges two databases together
void Database::merge_database(Database target_database) {
//get the names of the table and database tables in a vector
vector<string> table_names = target_database.list_table();
vector<Table> database_tables = target_database.get_table();
//pushes back the names in table_names vector into the current database
for (int i = 0; i < table_names.size(); i++) {
names_of_table.push_back(table_names.at(i));
}
//pushed back the tables of the target databse into the current database.
for (int i = 0; i < database_tables.size(); i++) {
database.push_back(database_tables.at(i));
}
}
int Database::size()
{
/*
* Return number of tables present
*/
return database.size();
}
}