-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
101 lines (74 loc) · 2.58 KB
/
Copy pathdatabase.cpp
File metadata and controls
101 lines (74 loc) · 2.58 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
#include "database.h"
#include "common.h"
#include <iostream>
#include <filesystem>
#include <map>
#include "table.h"
bool createDatabase(const std::string& dbName) {
std::string dbPath = getDatabasePath(dbName);
if (fileExists(dbPath)) {
std::cerr << "Error: Database already exists." << std::endl;
return false;
}
try {
std::filesystem::create_directory(dbPath);
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Error: Failed to create database directory - " << e.what() << std::endl;
return false;
}
databases[dbName];
flushMetaFile(dbName);
std::cout << "Database created successfully: " << dbName << std::endl;
return true;
}
bool dropDatabase(const std::string& dbName) {
std::string dbPath = getDatabasePath(dbName);
if (!fileExists(dbPath)) {
std::cerr << "Error: Database does not exist." << std::endl;
return false;
}
std::filesystem::remove_all(dbPath);
std::cout << "Database dropped successfully." << std::endl;
return true;
}
bool useDatabase(const std::string& dbName) {
std::string dbPath = getDatabasePath(dbName);
if (!fileExists(dbPath)) {
std::cerr << "Error: Database does not exist." << std::endl;
return false;
}
currentDatabase = dbName;
auto it = databases.find(currentDatabase);
if (it == databases.end()) {
databases[currentDatabase] = std::map<std::string, Table>();
}
reloadTablesForCurrentDatabase();
return true;
}
void reloadTablesForCurrentDatabase() {
auto dbIt = databases.find(currentDatabase);
if (dbIt == databases.end()) {
std::cerr << "Error: Current database not found in memory." << std::endl;
return;
}
dbIt->second.clear();
std::string dbPath = getDatabasePath(currentDatabase);
for (const auto& entry : std::filesystem::directory_iterator(dbPath)) {
if (entry.is_regular_file() && entry.path().extension() == ".csv") {
std::string tableName = entry.path().stem().string();
Table newTable;
if (loadTableFromDisk(currentDatabase, tableName, &newTable)) {
dbIt->second[tableName] = std::move(newTable);
}
}
}
}
std::vector<std::string> showDatabases() {
std::vector<std::string> databases;
for (const auto& entry : std::filesystem::directory_iterator(workPath)) {
if (entry.is_directory()) {
databases.push_back(entry.path().filename().string());
}
}
return databases;
}