vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
database.h
1#ifndef DATABASE_DATABASE_H
2#define DATABASE_DATABASE_H
3
4#include <vkdb/vq.h>
5#include <vkdb/table.h>
6
7namespace vkdb {
12static const FilePath DATABASE_DIRECTORY{
13 std::filesystem::path(getenv("HOME")) / ".vkdb"
14};
15
20class Database {
21public:
22 using size_type = uint64_t;
23 using error_callback = std::function<void(Token, const std::string&)>;
24 using runtime_error_callback = std::function<void(const RuntimeError&)>;
25
30 Database() = delete;
31
42 explicit Database(
43 DatabaseName name,
44 error_callback error = VQ::error,
45 runtime_error_callback runtime_error = VQ::runtimeError
46 );
47
52 Database(Database&&) noexcept = default;
53
58 Database& operator=(Database&&) noexcept = default;
59
64 Database(const Database&) = delete;
65
70 Database& operator=(const Database&) = delete;
71
76 ~Database() noexcept = default;
77
88 Table& createTable(const TableName& table_name);
89
98 [[nodiscard]] Table& getTable(const TableName& table_name);
99
109 void dropTable(const TableName& table_name);
110
117 void clear();
118
124 [[nodiscard]] DatabaseName name() const noexcept;
125
131 [[nodiscard]] FilePath path() const noexcept;
132
138 [[nodiscard]] std::vector<TableName> tables() const noexcept;
139
148 Database& run(
149 const std::string& source,
150 std::ostream& stream = std::cout
151 ) noexcept;
152
162 const std::filesystem::path path,
163 std::ostream& stream = std::cout
164 ) noexcept;
165
172 Database& runPrompt() noexcept;
173
174private:
179 using TableMap = std::unordered_map<TableName, Table>;
180
188 void error(Token token, const std::string& message) noexcept;
189
196 void runtime_error(const RuntimeError& error) noexcept;
197
205 void report(
206 size_type line,
207 const std::string& where,
208 const std::string& message
209 ) const noexcept;
210
218 void load();
219
224 TableMap table_map_;
225
230 DatabaseName name_;
231
236 bool had_error_;
237
242 bool had_runtime_error_;
243
248 error_callback callback_;
249
254 runtime_error_callback runtime_callback_;
255};
256} // namespace vkdb
257
258#endif // DATABASE_DATABASE_H
Represents a database in vkdb.
Definition database.h:20
Table & getTable(const TableName &table_name)
Get the Table object.
Definition database.cpp:32
Database()=delete
Deleted default constructor.
FilePath path() const noexcept
Get the path to the database directory.
Definition database.cpp:59
std::vector< TableName > tables() const noexcept
Get the names of the tables in the database.
Definition database.cpp:63
Database & runFile(const std::filesystem::path path, std::ostream &stream=std::cout) noexcept
Run a file.
Definition database.cpp:92
void clear()
Clear the database.
Definition database.cpp:51
Database(Database &&) noexcept=default
Move-construct a new Database object.
void dropTable(const TableName &table_name)
Drop the Table object.
Definition database.cpp:41
Database & runPrompt() noexcept
Run the prompt.
Definition database.cpp:122
DatabaseName name() const noexcept
Get the name of the database.
Definition database.cpp:55
Database & run(const std::string &source, std::ostream &stream=std::cout) noexcept
Run a source string.
Definition database.cpp:68
Table & createTable(const TableName &table_name)
Create a Table object.
Definition database.cpp:21
Runtime error.
Represents a table in vkdb.
Definition table.h:32
Represents a token.
Definition token.h:81
static void error(Token token, const std::string &message) noexcept
Handle an error.
Definition vq.cpp:58
static void runtimeError(const RuntimeError &error) noexcept
Handle a runtime error.
Definition vq.cpp:66
STL namespace.