-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_engine.cpp
More file actions
68 lines (55 loc) · 1.71 KB
/
Copy pathdb_engine.cpp
File metadata and controls
68 lines (55 loc) · 1.71 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
// db_engine.cpp
#include <string>
#include <cstring>
#include <filesystem>
#include "SQL_LEXER.hpp"
#include "SQL_PARSER.hpp"
#include "initialLoad.hpp"
static std::string last_error;
static std::string base_directory;
extern "C" {
// --------------------------------------------------
// Initialize database with explicit base directory
// --------------------------------------------------
void initialize_database(const char* base_path) {
try {
base_directory = base_path;
// Set working directory explicitly
std::filesystem::current_path(base_directory);
initialDatabseLoad();
} catch (const std::exception& e) {
last_error = e.what();
}
}
// --------------------------------------------------
// Execute SQL
// - returns heap-allocated C string on success
// - returns nullptr on error
// --------------------------------------------------
const char* execute_sql(const char* sql) {
try {
Lexer lexer(sql);
auto tokens = lexer.tokenize();
Parser parser(tokens);
std::string output = parser.parse();
char* result = new char[output.size() + 1];
std::strcpy(result, output.c_str());
return result;
} catch (const std::exception& e) {
last_error = e.what();
return nullptr;
}
}
// --------------------------------------------------
// Free string allocated by execute_sql
// --------------------------------------------------
void free_string(const char* s) {
delete[] s;
}
// --------------------------------------------------
// Get last error message
// --------------------------------------------------
const char* get_last_error() {
return last_error.c_str();
}
} // extern "C"