39 | inline P load(PageHandle *handle) {
40 | return reinterpret_cast(load(handle));
41 | }
42 |
43 | template
44 | inline P loadRaw(const PageHandle &handle) {
45 | return reinterpret_cast(loadRaw(handle));
46 | }
47 |
48 | inline void markDirty(const PageHandle &handle) {
49 | FileCoordinator::shared.markDirty(handle);
50 | }
51 |
52 | inline PageHandle renew(const PageHandle &handle) {
53 | return FileCoordinator::shared.renew(handle);
54 | }
55 |
56 | } // namespace PF
57 | } // namespace Internal
58 | } // namespace SimpleDB
59 |
60 | #endif
--------------------------------------------------------------------------------
/SimpleDB/include/SimpleDB/internal/PageHandle.h:
--------------------------------------------------------------------------------
1 | #ifndef _SIMPLEDB_PAGE_HANDLE_H
2 | #define _SIMPLEDB_PAGE_HANDLE_H
3 |
4 | #include "internal/CacheManager.h"
5 |
6 | namespace SimpleDB {
7 | namespace Internal {
8 |
9 | // A handle of a page cache used to access a page.
10 | struct PageHandle {
11 | friend class CacheManager;
12 |
13 | public:
14 | // This constructor is only for declaration, and must be initialized before
15 | // use.
16 | PageHandle() = default;
17 | bool validate() const { return cache->generation == generation; }
18 |
19 | #if !TESTING
20 | private:
21 | #else
22 | public:
23 | #endif
24 | PageHandle(CacheManager::PageCache *cache)
25 | : generation(cache->generation), cache(cache) {}
26 | int generation = -1;
27 | CacheManager::PageCache *cache;
28 | };
29 |
30 | } // namespace Internal
31 | } // namespace SimpleDB
32 |
33 | #endif
--------------------------------------------------------------------------------
/SimpleDB/include/SimpleDB/internal/ParseHelper.h:
--------------------------------------------------------------------------------
1 | #ifndef _SIMPLEDB_PARSE_HELPER_H
2 | #define _SIMPLEDB_PARSE_HELPER_H
3 |
4 | #include
5 |
6 | #include
7 | #include
8 | #include
9 |
10 | #include "Error.h"
11 | #include "internal/Column.h"
12 | #include "internal/Macros.h"
13 | #include "internal/QueryFilter.h"
14 | #include "internal/Table.h"
15 |
16 | namespace SimpleDB {
17 | namespace Internal {
18 |
19 | class ParseHelper {
20 | public:
21 | static void parseName(const std::string &name, char *dest) {
22 | if (name.size() >= MAX_COLUMN_NAME_LEN) {
23 | throw Error::IncompatableValueError("Column name too long");
24 | }
25 | std::strcpy(dest, name.c_str());
26 | }
27 |
28 | static DataType parseDataType(const std::string &type) {
29 | if (type == "INT") {
30 | return INT;
31 | } else if (type == "FLOAT") {
32 | return FLOAT;
33 | } else if (type.compare(0, sizeof("VARCHAR") - 1, "VARCHAR") == 0) {
34 | return VARCHAR;
35 | }
36 | assert(false);
37 | return INT;
38 | }
39 |
40 | static void parseDefaultValue(const std::string &value, DataType type,
41 | ColumnValue &dest, int size) {
42 | switch (type) {
43 | case INT:
44 | dest.intValue = parseInt(value);
45 | break;
46 | case FLOAT:
47 | dest.floatValue = parseFloat(value);
48 | break;
49 | case VARCHAR:
50 | parseString(value, std::min(MAX_VARCHAR_LEN, size),
51 | dest.stringValue);
52 | break;
53 | }
54 | }
55 |
56 | static int parseInt(const std::string &value) {
57 | try {
58 | return std::stoi(value);
59 | } catch (std::exception &e) {
60 | throw Error::IncompatableValueError("Invalid integer value");
61 | }
62 | }
63 |
64 | static float parseFloat(const std::string &value) {
65 | try {
66 | return std::stof(value);
67 | } catch (std::exception &e) {
68 | throw Error::IncompatableValueError("Invalid float value");
69 | }
70 | }
71 |
72 | static void parseString(const std::string &value, int maxSize, char *dest) {
73 | // Here we deal with the raw string from antlr, which starts and ends
74 | // with a single quote (').
75 | if (value.size() - 2 >= maxSize) {
76 | throw Error::IncompatableValueError("VARCHAR too long");
77 | }
78 | std::strcpy(dest, value.substr(1, value.size() - 2).c_str());
79 | }
80 |
81 | static CompareOp parseCompareOp(const std::string &op) {
82 | if (op == "=") {
83 | return EQ;
84 | } else if (op == "<>") {
85 | return NE;
86 | } else if (op == "<") {
87 | return LT;
88 | } else if (op == "<=") {
89 | return LE;
90 | } else if (op == ">") {
91 | return GT;
92 | } else if (op == ">=") {
93 | return GE;
94 | }
95 | assert(false);
96 | }
97 |
98 | static Column parseColumnValue(SQLParser::SqlParser::ValueContext *ctx) {
99 | Column column;
100 | if (ctx->Integer() != nullptr) {
101 | column.data.intValue = parseInt(ctx->Integer()->getText());
102 | column.type = INT;
103 | } else if (ctx->Float() != nullptr) {
104 | column.data.floatValue = parseFloat(ctx->Float()->getText());
105 | column.type = FLOAT;
106 | } else if (ctx->String() != nullptr) {
107 | parseString(ctx->String()->getText(), MAX_VARCHAR_LEN,
108 | column.data.stringValue);
109 | column.type = VARCHAR;
110 | } else if (ctx->Null() != nullptr) {
111 | column.isNull = true;
112 | // The data type is unknown here, must be set outside.
113 | }
114 | return column;
115 | }
116 |
117 | // static CompareValueCondition parseCompareValueCondition(
118 | // const std::string &columnName, const std::string operator_,
119 | // SQLParser::SqlParser::ValueContext *ctx) {
120 | // return CompareValueCondition(columnName, parseCompareOp(operator_),
121 | // parseColumnValue(ctx).data);
122 | // }
123 | };
124 |
125 | } // namespace Internal
126 | } // namespace SimpleDB
127 |
128 | #endif
--------------------------------------------------------------------------------
/SimpleDB/include/SimpleDB/internal/ParseTreeVisitor.h:
--------------------------------------------------------------------------------
1 | #ifndef _SIMPLEDB_PARSE_TREE_VISITOR_H
2 | #define _SIMPLEDB_PARSE_TREE_VISITOR_H
3 |
4 | #include
5 |
6 | #include "SQLParser/SqlBaseVisitor.h"
7 | #include "SimpleDBService/query.pb.h"
8 |
9 | namespace SimpleDB {
10 |
11 | class DBMS;
12 |
13 | namespace Internal {
14 |
15 | class ParseTreeVisitor : public SQLParser::SqlBaseVisitor {
16 | public:
17 | ParseTreeVisitor() = default;
18 | ParseTreeVisitor(::SimpleDB::DBMS *dbms);
19 |
20 | virtual antlrcpp::Any visitProgram(
21 | SQLParser::SqlParser::ProgramContext *ctx) override;
22 | virtual antlrcpp::Any visitStatement(
23 | SQLParser::SqlParser::StatementContext *ctx) override;
24 | virtual antlrcpp::Any visitCreate_db(
25 | SQLParser::SqlParser::Create_dbContext *ctx) override;
26 | virtual antlrcpp::Any visitDrop_db(
27 | SQLParser::SqlParser::Drop_dbContext *ctx) override;
28 | virtual antlrcpp::Any visitShow_dbs(
29 | SQLParser::SqlParser::Show_dbsContext *ctx) override;
30 | virtual antlrcpp::Any visitUse_db(
31 | SQLParser::SqlParser::Use_dbContext *ctx) override;
32 | virtual antlrcpp::Any visitShow_tables(
33 | SQLParser::SqlParser::Show_tablesContext *ctx) override;
34 | virtual antlrcpp::Any visitCreate_table(
35 | SQLParser::SqlParser::Create_tableContext *ctx) override;
36 | virtual antlrcpp::Any visitDrop_table(
37 | SQLParser::SqlParser::Drop_tableContext *ctx) override;
38 | virtual antlrcpp::Any visitDescribe_table(
39 | SQLParser::SqlParser::Describe_tableContext *ctx) override;
40 |
41 | virtual antlrcpp::Any visitField_list(
42 | SQLParser::SqlParser::Field_listContext *ctx) override;
43 | virtual antlrcpp::Any visitNormal_field(
44 | SQLParser::SqlParser::Normal_fieldContext *ctx) override;
45 |
46 | virtual antlrcpp::Any visitAlter_table_drop_pk(
47 | SQLParser::SqlParser::Alter_table_drop_pkContext *ctx) override;
48 | virtual antlrcpp::Any visitAlter_table_add_pk(
49 | SQLParser::SqlParser::Alter_table_add_pkContext *ctx) override;
50 | virtual antlrcpp::Any visitAlter_table_drop_foreign_key(
51 | SQLParser::SqlParser::Alter_table_drop_foreign_keyContext *ctx)
52 | override;
53 | virtual antlrcpp::Any visitAlter_table_add_foreign_key(
54 | SQLParser::SqlParser::Alter_table_add_foreign_keyContext *ctx) override;
55 | virtual antlrcpp::Any visitAlter_add_index(
56 | SQLParser::SqlParser::Alter_add_indexContext *ctx) override;
57 | virtual antlrcpp::Any visitAlter_drop_index(
58 | SQLParser::SqlParser::Alter_drop_indexContext *ctx) override;
59 | virtual antlrcpp::Any visitShow_indexes(
60 | SQLParser::SqlParser::Show_indexesContext *ctx) override;
61 |
62 | virtual antlrcpp::Any visitWhere_and_clause(
63 | SQLParser::SqlParser::Where_and_clauseContext *ctx) override;
64 | virtual antlrcpp::Any visitWhere_operator_expression(
65 | SQLParser::SqlParser::Where_operator_expressionContext *ctx) override;
66 | virtual antlrcpp::Any visitWhere_null(
67 | SQLParser::SqlParser::Where_nullContext *ctx) override;
68 | virtual antlrcpp::Any visitColumn(
69 | SQLParser::SqlParser::ColumnContext *ctx) override;
70 |
71 | virtual antlrcpp::Any visitInsert_into_table(
72 | SQLParser::SqlParser::Insert_into_tableContext *ctx) override;
73 | virtual antlrcpp::Any visitSelect_table_(
74 | SQLParser::SqlParser::Select_table_Context *ctx) override;
75 | virtual antlrcpp::Any visitUpdate_table(
76 | SQLParser::SqlParser::Update_tableContext *ctx) override;
77 | virtual antlrcpp::Any visitDelete_from_table(
78 | SQLParser::SqlParser::Delete_from_tableContext *ctx) override;
79 |
80 | private:
81 | DBMS *dbms;
82 |
83 | #define DECLARE_WRAPPER(upperName, lowerName) \
84 | Service::ExecutionResult wrap(const Service::upperName##Result &result) { \
85 | Service::ExecutionResult execResult; \
86 | execResult.mutable_##lowerName()->CopyFrom(result); \
87 | return execResult; \
88 | }
89 |
90 | DECLARE_WRAPPER(Plain, plain);
91 | DECLARE_WRAPPER(ShowDatabases, show_databases);
92 | DECLARE_WRAPPER(ShowTable, show_table);
93 | DECLARE_WRAPPER(DescribeTable, describe_table);
94 | DECLARE_WRAPPER(ShowIndexes, show_indexes);
95 | DECLARE_WRAPPER(Query, query);
96 |
97 | #undef DECLARE_WRAPPER
98 | };
99 |
100 | } // namespace Internal
101 | } // namespace SimpleDB
102 |
103 | #endif
--------------------------------------------------------------------------------
/SimpleDB/include/SimpleDB/internal/QueryBuilder.h:
--------------------------------------------------------------------------------
1 | #ifndef _SIMPLEDB_QUERY_BUILDER_H
2 | #define _SIMPLEDB_QUERY_BUILDER_H
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | #include "internal/QueryDataSource.h"
9 | #include "internal/QueryFilter.h"
10 |
11 | namespace SimpleDB {
12 | namespace Internal {
13 |
14 | // Build a query schedule using a series of selectors.
15 | class QueryBuilder : public QueryDataSource {
16 | public:
17 | using Result = std::vector>;
18 | QueryBuilder(QueryDataSource *dataSource);
19 | QueryBuilder(std::shared_ptr dataSource);
20 | QueryBuilder() = default;
21 |
22 | QueryBuilder &condition(const CompareValueCondition &condition);
23 | QueryBuilder &condition(const std::string &columnName, CompareOp op,
24 | const char *string);
25 | QueryBuilder &condition(const std::string &columnName, CompareOp op,
26 | int value);
27 | QueryBuilder &condition(const ColumnId &columnId, CompareOp op,
28 | const char *string);
29 | QueryBuilder &condition(const ColumnId &columnId, CompareOp op,
30 | const ColumnValue &value);
31 |
32 | QueryBuilder &condition(const CompareColumnCondition &condition);
33 | QueryBuilder &nullCondition(const CompareNullCondition &condition);
34 | QueryBuilder &nullCondition(const std::string &columnName, bool isNull);
35 | QueryBuilder &nullCondition(const ColumnId &columnId, bool isNull);
36 | QueryBuilder &select(const QuerySelector &selector);
37 | QueryBuilder &select(const std::string &column);
38 | QueryBuilder &select(const ColumnId &id);
39 | QueryBuilder &limit(int count);
40 | QueryBuilder &offset(int offset);
41 |
42 | [[nodiscard]] Result execute();
43 |
44 | // QueryDataSource requirements, allowing chained pipelines.
45 | virtual void iterate(IterateCallback callback) override;
46 | virtual std::vector getColumnInfo() override;
47 |
48 | bool validForUpdateOrDelete() const;
49 | QueryDataSource *getDataSource();
50 |
51 | private:
52 | std::vector valueConditionFilters;
53 | std::vector columnConditionFilters;
54 | std::vector nullConditionFilters;
55 | SelectFilter selectFilter;
56 | LimitFilter limitFilter;
57 | OffsetFilter offsetFilter;
58 |
59 | QueryDataSource *dataSourceRawPtr = nullptr;
60 | std::shared_ptr dataSourceSharedPtr;
61 | bool isRaw;
62 | VirtualTable virtualTable;
63 |
64 | void checkDataSource();
65 | AggregatedFilter aggregateAllFilters();
66 | };
67 |
68 | } // namespace Internal
69 | } // namespace SimpleDB
70 |
71 | #endif
--------------------------------------------------------------------------------
/SimpleDB/include/SimpleDB/internal/QueryDataSource.h:
--------------------------------------------------------------------------------
1 | #ifndef _SIMPLEDB_QUERY_DATASOURCE_H
2 | #define _SIMPLEDB_QUERY_DATASOURCE_H
3 |
4 | #include
5 | #include
6 |
7 | #include "internal/Column.h"
8 |
9 | namespace SimpleDB {
10 | namespace Internal {
11 |
12 | class QueryDataSource {
13 | public:
14 | using IterateCallback = std::function;
15 | virtual ~QueryDataSource() = default;
16 | virtual void iterate(IterateCallback callback) = 0;
17 | virtual std::vector getColumnInfo() = 0;
18 | virtual bool acceptCondition(
19 | const struct CompareValueCondition &condition) {
20 | return false;
21 | }
22 | };
23 |
24 | } // namespace Internal
25 | } // namespace SimpleDB
26 |
27 | #endif
--------------------------------------------------------------------------------
/SimpleDB/include/SimpleDB/internal/QueryFilter.h:
--------------------------------------------------------------------------------
1 | #ifndef _SIMPLEDB_QUERY_FILTER
2 | #define _SIMPLEDB_QUERY_FILTER
3 |
4 | #include
5 | #include