-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cpp
More file actions
47 lines (37 loc) · 1.27 KB
/
Database.cpp
File metadata and controls
47 lines (37 loc) · 1.27 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
#include "Database.hpp"
#include <iostream>
Database::Database(const std::string &dbName) : name(dbName) {
}
void Database::createTable(const std::string &tableName, const std::vector<std::string> &columnNames) {
if (tables.find(tableName) != tables.end()) {
std::cerr << "Table \"" << tableName << "\" already exists. \n" << std::endl;
return;
}
tables.emplace(tableName, Table(tableName, columnNames));
std::cout<<"Table \""<<tableName<<"\" created successfully. \n";
}
std::shared_ptr<Table> Database::getTable(const std::string &tableName) {
auto it = tables.find(tableName);
if (it != tables.end()) {
return std::shared_ptr<Table>(&it->second);
} else {
std::cerr << "Table \"" << tableName << "\" not found.\n";
return nullptr;
}
}
void Database::listTables() const {
if (tables.empty()) {
std::cout << "No tables in the database.\n";
return;
}
std::cout << "Tables in database \"" << name << "\":\n";
for (const auto& pair : tables) {
std::cout << "- " << pair.first << "\n";
}
}
void Database::saveAllTables() const {
for (const auto& [tableName, table] : tables) {
std::string filename = tableName + ".csv";
table.saveToFile(filename);
}
}