mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
DatabaseCommand.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
9#include <vector>
10
11
12namespace m {
13
14// forward declarations
15struct ConstDatabaseCommandVisitor;
16struct DatabaseCommandVisitor;
17
18
21{
22 private:
24 std::unique_ptr<ast::Command> ast_;
27
28 public:
29 virtual ~DatabaseCommand() = default;
30
31 virtual void accept(DatabaseCommandVisitor &v) = 0;
32 virtual void accept(ConstDatabaseCommandVisitor &v) const = 0;
33
35 virtual void execute(Diagnostic &diag) = 0;
36
37 template<typename T = ast::Command>
38 requires std::derived_from<T, ast::Command>
39 T & ast() { return as<T>(*ast_); }
40 template<typename T = ast::Command>
41 requires std::derived_from<T, ast::Command>
42 const T & ast() const { return *as<T>(ast_); }
43
44 std::unique_ptr<ast::Command> ast(std::unique_ptr<ast::Command> new_ast) {
45 return std::exchange(ast_, std::move(new_ast));
46 }
47
50 return std::swap(t_, t);
51 }
52};
53
55{
56 public:
58
59 void accept(DatabaseCommandVisitor &v) override;
60 void accept(ConstDatabaseCommandVisitor &v) const override;
61
62 void execute(Diagnostic &diag) override;
63};
64
65
66/*======================================================================================================================
67 * Instructions
68 *====================================================================================================================*/
69
74{
75 private:
76 std::vector<std::string> args_;
77
78 public:
79 DatabaseInstruction(std::vector<std::string> args) : args_(std::move(args)) { }
80
82 const std::vector<std::string> & args() const { return args_; }
83};
84
87{
88 learn_spns(std::vector<std::string> args) : DatabaseInstruction(std::move(args)) { }
89
90 void accept(DatabaseCommandVisitor &v) override;
91 void accept(ConstDatabaseCommandVisitor &v) const override;
92
93 void execute(Diagnostic &diag) override;
94};
95
96#define M_DATABASE_INSTRUCTION_LIST(X) \
97 X(learn_spns)
98
99
100/*======================================================================================================================
101 * Structured Query Language (SQL)
102 *====================================================================================================================*/
103
105
106
107/*======================================================================================================================
108 * Data Manipulation Language (DML)
109 *====================================================================================================================*/
110
113
116{
117 private:
118 std::unique_ptr<QueryGraph> graph_;
119 std::unique_ptr<Consumer> logical_plan_;
120 std::unique_ptr<MatchBase> physical_plan_;
121
122 public:
123 void accept(DatabaseCommandVisitor &v) override;
124 void accept(ConstDatabaseCommandVisitor &v) const override;
125
126 void execute(Diagnostic &diag) override;
127};
128
131{
133
134 void accept(DatabaseCommandVisitor &v) override;
135 void accept(ConstDatabaseCommandVisitor &v) const override;
136
137 void execute(Diagnostic &diag) override;
138};
139
142{
143 void accept(DatabaseCommandVisitor &v) override;
144 void accept(ConstDatabaseCommandVisitor &v) const override;
145
146 void execute(Diagnostic &diag) override;
147};
148
151{
152 void accept(DatabaseCommandVisitor &v) override;
153 void accept(ConstDatabaseCommandVisitor &v) const override;
154
155 void execute(Diagnostic &diag) override;
156};
157
160{
162
163 private:
164 const Table &table_;
165 std::filesystem::path path_;
167
168 public:
169 ImportDSV(const Table &table, std::filesystem::path path, DSVConfig cfg)
170 : table_(table)
171 , path_(path)
172 , cfg_(std::move(cfg)) { }
173
174 void accept(DatabaseCommandVisitor &v) override;
175 void accept(ConstDatabaseCommandVisitor &v) const override;
176
177 void execute(Diagnostic &diag) override;
178};
179
180#define M_DATABASE_DML_LIST(X) \
181 X(QueryDatabase) \
182 X(InsertRecords) \
183 X(UpdateRecords) \
184 X(DeleteRecords) \
185 X(ImportDSV)
186
187
188/*======================================================================================================================
189 * Data Definition Language
190 *====================================================================================================================*/
191
193
195{
196 private:
198
199 public:
200 CreateDatabase(ThreadSafePooledString db_name) : db_name_(std::move(db_name)) { }
201
202 void accept(DatabaseCommandVisitor &v) override;
203 void accept(ConstDatabaseCommandVisitor &v) const override;
204
205 void execute(Diagnostic &diag) override;
206};
207
209{
210 private:
212
213 public:
214 DropDatabase(ThreadSafePooledString db_name) : db_name_(std::move(db_name)) { }
215
216 void accept(DatabaseCommandVisitor &v) override;
217 void accept(ConstDatabaseCommandVisitor &v) const override;
218
219 void execute(Diagnostic &diag) override;
220};
221
223{
224 private:
226
227 public:
228 UseDatabase(ThreadSafePooledString db_name) : db_name_(std::move(db_name)) { }
229
230 void accept(DatabaseCommandVisitor &v) override;
231 void accept(ConstDatabaseCommandVisitor &v) const override;
232
233 void execute(Diagnostic &diag) override;
234};
235
237{
238 private:
239 std::unique_ptr<Table> table_;
240
241 public:
242 CreateTable(std::unique_ptr<Table> table) : table_(M_notnull(std::move(table))) { }
243
244 void accept(DatabaseCommandVisitor &v) override;
245 void accept(ConstDatabaseCommandVisitor &v) const override;
246
247 void execute(Diagnostic &diag) override;
248};
249
251{
252 private:
253 std::vector<ThreadSafePooledString> table_names_;
254
255 public:
256 DropTable(std::vector<ThreadSafePooledString> table_names) : table_names_(std::move(table_names))
257 {
258#ifndef NDEBUG
259 for (auto t : table_names_)
260 M_notnull(*t);
261#endif
262 }
263
264 void accept(DatabaseCommandVisitor &v) override;
265 void accept(ConstDatabaseCommandVisitor &v) const override;
266
267 void execute(Diagnostic &diag) override;
268};
269
271{
272 private:
273 std::unique_ptr<idx::IndexBase> index_;
277
278 public:
279 CreateIndex(std::unique_ptr<idx::IndexBase> index, ThreadSafePooledString table_name,
280 ThreadSafePooledString attribute_name, ThreadSafePooledString index_name)
281 : index_(M_notnull(std::move(index)))
282 , table_name_(std::move(table_name))
283 , attribute_name_(std::move(attribute_name))
284 , index_name_(std::move(index_name))
285 { }
286
287 void accept(DatabaseCommandVisitor &v) override;
288 void accept(ConstDatabaseCommandVisitor &v) const override;
289
290 void execute(Diagnostic &diag) override;
291};
292
294{
295 private:
296 std::vector<ThreadSafePooledString> index_names_;
297
298 public:
299 DropIndex(std::vector<ThreadSafePooledString> index_names) : index_names_(std::move(index_names))
300 {
301#ifndef NDEBUG
302 for (auto i : index_names_)
303 M_notnull(*i);
304#endif
305 }
306
307 void accept(DatabaseCommandVisitor &v) override;
308 void accept(ConstDatabaseCommandVisitor &v) const override;
309
310 void execute(Diagnostic &diag) override;
311};
312
313#define M_DATABASE_DDL_LIST(X)\
314 X(CreateDatabase) \
315 X(DropDatabase) \
316 X(UseDatabase) \
317 X(CreateTable) \
318 X(DropTable) \
319 X(CreateIndex) \
320 X(DropIndex)
321
322
323#define M_DATABASE_SQL_LIST(X) \
324 M_DATABASE_DML_LIST(X) \
325 M_DATABASE_DDL_LIST(X)
326
327#define M_DATABASE_COMMAND_LIST(X) \
328 M_DATABASE_INSTRUCTION_LIST(X) \
329 M_DATABASE_SQL_LIST(X) \
330 X(EmptyCommand)
331
334
335}
#define M_DATABASE_COMMAND_LIST(X)
#define M_DECLARE_VISITOR(VISITOR_NAME, BASE_CLASS, CLASS_LIST)
Defines a visitor VISITOR_NAME to visit the class hierarchy rooted in BASE_CLASS and with subclasses ...
Definition: Visitor.hpp:181
#define M_notnull(ARG)
Definition: macro.hpp:182
‍mutable namespace
Definition: Backend.hpp:10
T(x)
ThreadSafeStringPool::proxy_type ThreadSafePooledString
Definition: Pool.hpp:464
STL namespace.
void accept(DatabaseCommandVisitor &v) override
CreateDatabase(ThreadSafePooledString db_name)
void execute(Diagnostic &diag) override
Executes the command.
ThreadSafePooledString db_name_
void accept(ConstDatabaseCommandVisitor &v) const override
ThreadSafePooledString table_name_
ThreadSafePooledString index_name_
CreateIndex(std::unique_ptr< idx::IndexBase > index, ThreadSafePooledString table_name, ThreadSafePooledString attribute_name, ThreadSafePooledString index_name)
void accept(ConstDatabaseCommandVisitor &v) const override
void execute(Diagnostic &diag) override
Executes the command.
ThreadSafePooledString attribute_name_
std::unique_ptr< idx::IndexBase > index_
void accept(DatabaseCommandVisitor &v) override
CreateTable(std::unique_ptr< Table > table)
void accept(ConstDatabaseCommandVisitor &v) const override
void execute(Diagnostic &diag) override
Executes the command.
void accept(DatabaseCommandVisitor &v) override
std::unique_ptr< Table > table_
Base class for all commands resulting from a data manipulation language (DML) statement.
Configuration parameters for importing a DSV file.
Definition: Reader.hpp:45
The command pattern for operations in the DBMS.
virtual void accept(DatabaseCommandVisitor &v)=0
const T & ast() const
std::unique_ptr< ast::Command > ast_
‍the AST of the command; optional
virtual void accept(ConstDatabaseCommandVisitor &v) const =0
Scheduler::Transaction * t_
‍the transaction this command belongs to
std::unique_ptr< ast::Command > ast(std::unique_ptr< ast::Command > new_ast)
virtual ~DatabaseCommand()=default
virtual void execute(Diagnostic &diag)=0
Executes the command.
Scheduler::Transaction * transaction()
void transaction(Scheduler::Transaction *t)
A DatabaseInstruction represents an invokation of an instruction with (optional) arguments.
std::vector< std::string > args_
the arguments of this instruction
const std::vector< std::string > & args() const
Returns the arguments of this instruction.
DatabaseInstruction(std::vector< std::string > args)
Delete records from a Table of a Database.
void execute(Diagnostic &diag) override
Executes the command.
void accept(ConstDatabaseCommandVisitor &v) const override
void accept(DatabaseCommandVisitor &v) override
DropDatabase(ThreadSafePooledString db_name)
void execute(Diagnostic &diag) override
Executes the command.
void accept(DatabaseCommandVisitor &v) override
void accept(ConstDatabaseCommandVisitor &v) const override
ThreadSafePooledString db_name_
DropIndex(std::vector< ThreadSafePooledString > index_names)
std::vector< ThreadSafePooledString > index_names_
void execute(Diagnostic &diag) override
Executes the command.
void accept(ConstDatabaseCommandVisitor &v) const override
void accept(DatabaseCommandVisitor &v) override
void accept(DatabaseCommandVisitor &v) override
void execute(Diagnostic &diag) override
Executes the command.
void accept(ConstDatabaseCommandVisitor &v) const override
std::vector< ThreadSafePooledString > table_names_
DropTable(std::vector< ThreadSafePooledString > table_names)
void accept(ConstDatabaseCommandVisitor &v) const override
void accept(DatabaseCommandVisitor &v) override
void execute(Diagnostic &diag) override
Executes the command.
Import records from a delimiter separated values (DSV) file into a Table of a Database.
const Table & table_
ImportDSV(const Table &table, std::filesystem::path path, DSVConfig cfg)
std::filesystem::path path_
void accept(DatabaseCommandVisitor &v) override
void execute(Diagnostic &diag) override
Executes the command.
void accept(ConstDatabaseCommandVisitor &v) const override
Insert records into a Table of a Database.
void accept(DatabaseCommandVisitor &v) override
void accept(ConstDatabaseCommandVisitor &v) const override
void execute(Diagnostic &diag) override
Executes the command.
Run a query against the selected database.
std::unique_ptr< Consumer > logical_plan_
std::unique_ptr< MatchBase > physical_plan_
std::unique_ptr< QueryGraph > graph_
void accept(DatabaseCommandVisitor &v) override
void accept(ConstDatabaseCommandVisitor &v) const override
void execute(Diagnostic &diag) override
Executes the command.
A table is a sorted set of attributes.
Definition: Schema.hpp:388
Modify records of a Table of a Database.
void execute(Diagnostic &diag) override
Executes the command.
void accept(ConstDatabaseCommandVisitor &v) const override
void accept(DatabaseCommandVisitor &v) override
UseDatabase(ThreadSafePooledString db_name)
void execute(Diagnostic &diag) override
Executes the command.
void accept(DatabaseCommandVisitor &v) override
void accept(ConstDatabaseCommandVisitor &v) const override
ThreadSafePooledString db_name_
Learn an SPN on every table in the database that is currently in use.
void accept(ConstDatabaseCommandVisitor &v) const override
void accept(DatabaseCommandVisitor &v) override
void execute(Diagnostic &diag) override
Executes the command.
learn_spns(std::vector< std::string > args)