-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
100 lines (70 loc) · 2.72 KB
/
Copy pathcommon.cpp
File metadata and controls
100 lines (70 loc) · 2.72 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
#include "common.h"
#include <iostream>
#include <fstream>
#include <vector>
std::string currentDatabase = "default_db";
std::map<std::string, std::map<std::string, Table>> databases;
void init() {
if (!std::filesystem::exists(workPath)) {
std::filesystem::create_directory(workPath);
std::cout << "Created work directory: " << workPath << std::endl;
} else {
std::cout << "Work directory already exists: " << workPath << std::endl;
}
currentDatabase = "";
std::string defaultDbPath = getDatabasePath("default");
if (!std::filesystem::exists(defaultDbPath)) {
std::filesystem::create_directory(defaultDbPath);
std::cout << "Created default database directory: " << defaultDbPath << std::endl;
}
flushMetaFile("default");
}
bool fileExists(const std::string& filePath) {
return std::filesystem::exists(filePath);
}
void flushMetaFile(const std::string& dbName) {
std::string dbPath = getDatabasePath(dbName);
if (!std::filesystem::exists(dbPath)) {
std::filesystem::create_directory(dbPath);
std::cout << "Created database directory: " << dbPath << std::endl;
}
std::string metaFilePath = dbPath + "/meta.dat";
std::ofstream ofs(metaFilePath);
if (ofs.is_open()) {
ofs << "Database: " << dbName << "\n";
ofs.close();
std::cout << "Meta file created successfully: " << metaFilePath << std::endl;
} else {
std::cerr << "Error: Failed to write meta file for database " << dbName << std::endl;
}
}
std::string getDatabasePath(const std::string& dbName) {
return std::string(workPath) + "/" + dbName;
}
std::string getTablePath(const std::string& dbName, const std::string& tableName) {
return getDatabasePath(dbName) + "/" + tableName;
}
std::string getCsvFilePath(const std::string& dbName, const std::string& tableName) {
if (tableName.empty()) {
std::cerr << "[ERROR] getCsvFilePath: tableName is empty!" << std::endl;
}
std::string path = "/tmp/jzwsqlbase/" + dbName + "/" + tableName + ".csv";
return path;
}
std::string getSchemaFilePath(const std::string& dbName, const std::string& tableName) {
return "/tmp/jzwsqlbase/" + dbName + "/" + tableName + ".schema";
}
std::string getCurrentDatabasePath() {
if (currentDatabase.empty()) {
throw std::runtime_error("No database selected. Please use 'USE DATABASE <name>' to select a database.");
}
return getDatabasePath(currentDatabase);
}
void printResult(const std::vector<std::vector<std::string>>& result) {
for (const auto& row : result) {
for (const auto& field : row) {
std::cout << field << " ";
}
std::cout << std::endl;
}
}