/src/sql-parser/src/sql/CreateStatement.h
Line | Count | Source |
1 | | #ifndef SQLPARSER_CREATE_STATEMENT_H |
2 | | #define SQLPARSER_CREATE_STATEMENT_H |
3 | | |
4 | | #include "ColumnType.h" |
5 | | #include "SQLStatement.h" |
6 | | |
7 | | #include <ostream> |
8 | | #include <unordered_set> |
9 | | |
10 | | namespace hsql { |
11 | | struct SelectStatement; |
12 | | |
13 | | enum struct ConstraintType { ForeignKey, NotNull, Null, PrimaryKey, Unique }; |
14 | | std::ostream& operator<<(std::ostream& os, const ConstraintType constraint_type); |
15 | | |
16 | | // Superclass for both TableConstraint and ColumnDefinition. |
17 | | struct TableElement { |
18 | 21.5k | virtual ~TableElement() = default; |
19 | | }; |
20 | | |
21 | | // Represents definition of a table constraint. |
22 | | struct TableConstraint : TableElement { |
23 | | TableConstraint(ConstraintType keyType, std::vector<char*>* columnNames); |
24 | | |
25 | | ~TableConstraint() override; |
26 | | |
27 | | ConstraintType type; |
28 | | std::vector<char*>* columnNames; |
29 | | }; |
30 | | |
31 | | // Table and columns referenced by foreign key constraint on table or column level. |
32 | | struct ReferencesSpecification { |
33 | | ReferencesSpecification(char* schema, char* table, std::vector<char*>* columns); |
34 | | ~ReferencesSpecification(); |
35 | | |
36 | | char* schema; |
37 | | char* table; |
38 | | std::vector<char*>* columns; |
39 | | }; |
40 | | |
41 | | // Foreign key constraint on table level (when specified as table element). |
42 | | struct ForeignKeyConstraint : TableConstraint { |
43 | | ForeignKeyConstraint(std::vector<char*>* columnNames, ReferencesSpecification* references); |
44 | | ~ForeignKeyConstraint() override; |
45 | | |
46 | | ReferencesSpecification* references; |
47 | | }; |
48 | | |
49 | | // Represents definition of a table column |
50 | | struct ColumnDefinition : TableElement { |
51 | | ColumnDefinition(char* name, ColumnType type, std::unordered_set<ConstraintType>* column_constraints, |
52 | | std::vector<ReferencesSpecification*>* references); |
53 | | ~ColumnDefinition() override; |
54 | | |
55 | | // By default, columns are nullable. However, we track if a column is explicitly requested to be nullable to |
56 | | // notice conflicts with PRIMARY KEY table constraints. |
57 | | bool trySetNullableExplicit(); |
58 | | |
59 | | std::unordered_set<ConstraintType>* column_constraints; |
60 | | char* name; |
61 | | ColumnType type; |
62 | | bool nullable; |
63 | | std::vector<ReferencesSpecification*>* references; |
64 | | }; |
65 | | |
66 | | struct ColumnConstraints { |
67 | | explicit ColumnConstraints(); |
68 | | |
69 | | std::unordered_set<ConstraintType>* constraints; |
70 | | std::vector<ReferencesSpecification*>* references; |
71 | | }; |
72 | | |
73 | | enum CreateType { |
74 | | kCreateTable, |
75 | | kCreateTableFromTbl, // Hyrise file format |
76 | | kCreateView, |
77 | | kCreateIndex |
78 | | }; |
79 | | |
80 | | // Represents SQL Create statements. |
81 | | // Example: "CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)" |
82 | | struct CreateStatement : SQLStatement { |
83 | | CreateStatement(CreateType type); |
84 | | ~CreateStatement() override; |
85 | | |
86 | | void setColumnDefsAndConstraints(std::vector<TableElement*>* tableElements); |
87 | | |
88 | | CreateType type; |
89 | | bool ifNotExists; // default: false |
90 | | char* filePath; // default: nullptr |
91 | | char* schema; // default: nullptr |
92 | | char* tableName; // default: nullptr |
93 | | char* indexName; // default: nullptr |
94 | | std::vector<char*>* indexColumns; // default: nullptr |
95 | | std::vector<ColumnDefinition*>* columns; // default: nullptr |
96 | | std::vector<TableConstraint*>* tableConstraints; // default: nullptr |
97 | | std::vector<char*>* viewColumns; |
98 | | SelectStatement* select; |
99 | | }; |
100 | | |
101 | | } // namespace hsql |
102 | | |
103 | | #endif |