-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
354 lines (273 loc) · 9.9 KB
/
Copy pathtable.cpp
File metadata and controls
354 lines (273 loc) · 9.9 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
347
348
349
350
351
352
353
354
#include "globals.h"
#include <iostream>
#include <fstream>
#include <filesystem>
#include <sstream>
#include <algorithm>
#include "database.h"
#include "table.h"
#include "common.h"
#include "condition_ast.h"
#include "condition_evaluator.h"
extern std::vector<Table> tables;
bool loadTableFromDisk(const std::string& dbName, const std::string& tableName, Table* tableOut) {
tableOut->columns.clear();
std::string schemaPath = getSchemaFilePath(dbName, tableName);
std::string dataPath = getCsvFilePath(dbName, tableName);
std::ifstream schemaFile(schemaPath);
if (!schemaFile.is_open()) {
std::cerr << "Error: Failed to open schema file: " << schemaPath << std::endl;
return false;
}
std::string line;
while (std::getline(schemaFile, line)) {
size_t commaPos = line.find(',');
if (commaPos != std::string::npos) {
std::string field = line.substr(0, commaPos);
field.erase(0, field.find_first_not_of(" \t"));
field.erase(field.find_last_not_of(" \t") + 1);
tableOut->columns.push_back(field);
}
}
schemaFile.close();
std::ifstream dataFile(dataPath);
if (!dataFile.is_open()) {
std::cerr << "Error: Failed to open data file: " << dataPath << std::endl;
return false;
}
std::getline(dataFile, line);
while (std::getline(dataFile, line)) {
std::stringstream ss(line);
std::map<std::string, std::string> row;
std::string value;
int i = 0;
for (const auto& col : tableOut->columns) {
if (std::getline(ss, value, ',')) {
value.erase(0, value.find_first_not_of(" \t"));
value.erase(value.find_last_not_of(" \t") + 1);
row[col] = value;
} else {
break;
}
++i;
}
if (i == tableOut->columns.size()) {
tableOut->rows.push_back(row);
}
}
dataFile.close();
tableOut->name = tableName;
return true;
}
std::map<std::string, int> getColumnMap(const std::string& tablePath) {
std::map<std::string, int> columnMap;
std::ifstream inFile(tablePath);
if (!inFile.is_open()) return columnMap;
std::string header;
std::getline(inFile, header);
std::stringstream ss(header);
std::string field;
int index = 0;
while (std::getline(ss, field, ',')) {
columnMap[field] = index++;
}
inFile.close();
return columnMap;
}
bool createTable(const std::string& tableName, const std::vector<std::pair<std::string, std::string>>& fields) {
auto& dbTables = databases[currentDatabase];
if (dbTables.find(tableName) != dbTables.end()) {
std::cerr << "Error: Table already exists in current database: " << tableName << std::endl;
return false;
}
std::string schemaPath = getSchemaFilePath(currentDatabase, tableName);
std::string dataPath = getCsvFilePath(currentDatabase, tableName);
if (fileExists(schemaPath)) std::filesystem::remove(schemaPath);
if (fileExists(dataPath)) std::filesystem::remove(dataPath);
std::ofstream schemaFile(schemaPath);
if (!schemaFile.is_open()) {
std::cerr << "Error: Failed to create schema file." << std::endl;
return false;
}
for (const auto& field : fields) {
schemaFile << field.first << "," << field.second << "\n";
}
schemaFile.close();
std::ofstream csvFile(dataPath);
if (!csvFile.is_open()) {
std::cerr << "Error: Failed to create CSV file." << std::endl;
return false;
}
csvFile.close();
Table newTable;
newTable.name = tableName;
for (const auto& field : fields) {
newTable.columns.push_back(field.first);
}
dbTables[tableName] = newTable;
std::cout << "Created table and files: " << tableName << std::endl;
return true;
}
bool dropTable(const std::string& tableName) {
auto& dbTables = databases[currentDatabase];
if (dbTables.find(tableName) == dbTables.end()) {
std::cerr << "Error: Table does not exist: " << tableName << std::endl;
return false;
}
std::string csvPath = getCsvFilePath(currentDatabase, tableName);
std::string schemaPath = getSchemaFilePath(currentDatabase, tableName);
if (fileExists(csvPath)) std::filesystem::remove(csvPath);
if (fileExists(schemaPath)) std::filesystem::remove(schemaPath);
dbTables.erase(tableName);
std::cout << "Dropped table: " << tableName << std::endl;
return true;
}
std::vector<std::string> showTables() {
std::vector<std::string> tables;
auto dbIt = databases.find(currentDatabase);
if (dbIt != databases.end()) {
for (const auto& [tableName, table] : dbIt->second) {
tables.push_back(tableName);
}
}
return tables;
}
bool insertData(const std::string& tableName, const std::map<std::string, std::string>& values) {
auto& dbTables = databases[currentDatabase];
auto it = dbTables.find(tableName);
if (it == dbTables.end()) {
std::cerr << "Error: Table does not exist: " << tableName << std::endl;
return false;
}
Table* table = &it->second;
for (const auto& col : table->columns) {
if (values.find(col) == values.end()) {
std::cerr << "Error: Missing column value: " << col << std::endl;
return false;
}
}
table->rows.push_back(values);
return saveTableToDisk(currentDatabase, *table);
}
std::vector<std::vector<std::string>> selectData(const std::string& tableName) {
auto dbIt = databases.find(currentDatabase);
if (dbIt == databases.end()) {
std::cerr << "Error: Current database not set." << std::endl;
return {};
}
auto& dbTables = dbIt->second;
auto it = dbTables.find(tableName);
if (it == dbTables.end()) {
std::cerr << "Error: Table does not exist in memory: " << tableName << std::endl;
return {};
}
const Table& table = it->second;
std::vector<std::vector<std::string>> result;
for (const auto& row : table.rows) {
std::vector<std::string> rowData;
for (const auto& col : table.columns) {
rowData.push_back(row.at(col));
}
result.push_back(rowData);
}
return result;
}
std::vector<std::vector<std::string>> selectDataWithCondition(
const std::string& tableName, ConditionNode* condition)
{
auto dbIt = databases.find(currentDatabase);
if (dbIt == databases.end()) {
std::cerr << "Error: Current database not set." << std::endl;
return {};
}
auto& dbTables = dbIt->second;
auto tableIt = dbTables.find(tableName);
if (tableIt == dbTables.end()) {
std::cerr << "Error: Table does not exist in current database: " << tableName << std::endl;
return {};
}
Table& table = tableIt->second;
std::vector<std::vector<std::string>> result;
for (const auto& row : table.rows) {
if (!condition || evaluateCondition(row, condition)) {
std::vector<std::string> currentRow;
for (const auto& col : table.columns) {
currentRow.push_back(row.at(col));
}
result.push_back(currentRow);
}
}
return result;
}
bool updateTuple(const std::string& tableName,
const std::vector<std::pair<std::string, std::string>>& updates,
ConditionNode* condition) {
auto& dbTables = databases[currentDatabase];
auto it = dbTables.find(tableName);
if (it == dbTables.end()) return false;
Table* table = &it->second;
bool updated = false;
for (auto& row : table->rows) {
if (!condition || evaluateCondition(row, condition)) {
for (const auto& [field, value] : updates) {
row[field] = value;
}
updated = true;
}
}
if (updated) {
std::cout << "Updating table and saving to disk: " << table->name << std::endl;
saveTableToDisk(currentDatabase, *table);
}
return updated;
}
bool saveTableToDisk(const std::string& dbName, const Table& table) {
std::string dataPath = getCsvFilePath(dbName, table.name);
std::cout << "Saving table to" << std::endl;
std::ofstream out(dataPath);
if (!out) return false;
for (size_t i = 0; i < table.columns.size(); ++i) {
out << table.columns[i];
if (i != table.columns.size() - 1) out << ",";
}
out << "\n";
for (const auto& row : table.rows) {
for (size_t i = 0; i < table.columns.size(); ++i) {
const std::string& colName = table.columns[i];
out << row.at(colName);
if (i != table.columns.size() - 1) out << ",";
}
out << "\n";
}
out.close();
return true;
}
bool deleteTuple(const std::string& tableName,
ConditionNode* condition) {
auto& dbTables = databases[currentDatabase];
auto it = dbTables.find(tableName);
if (it == dbTables.end()) return false;
Table* table = &it->second;
bool deleted = false;
auto newEnd = std::remove_if(table->rows.begin(), table->rows.end(),
[&](const std::map<std::string, std::string>& row) {
bool shouldDelete = !condition || evaluateCondition(row, condition);
if (shouldDelete) deleted = true;
return shouldDelete;
});
table->rows.erase(newEnd, table->rows.end());
if (deleted) {
return saveTableToDisk(currentDatabase, *table);
}
return deleted;
}
Table* getTableByName(const std::string& tableName) {
auto dbIt = databases.find(currentDatabase);
if (dbIt != databases.end()) {
auto it = dbIt->second.find(tableName);
if (it != dbIt->second.end()) {
return &it->second;
}
}
return nullptr;
}