/src/sql-parser/src/sql/CreateStatement.cpp
Line | Count | Source |
1 | | #include "CreateStatement.h" |
2 | | #include "SelectStatement.h" |
3 | | |
4 | | namespace hsql { |
5 | | |
6 | 0 | std::ostream& operator<<(std::ostream& os, const ConstraintType constraint_type) { |
7 | 0 | switch (constraint_type) { |
8 | 0 | case ConstraintType::Null: |
9 | 0 | os << "NULL"; |
10 | 0 | break; |
11 | 0 | case ConstraintType::NotNull: |
12 | 0 | os << "NOT NULL"; |
13 | 0 | break; |
14 | 0 | case ConstraintType::ForeignKey: |
15 | 0 | os << "FOREIGN KEY"; |
16 | 0 | break; |
17 | 0 | case ConstraintType::PrimaryKey: |
18 | 0 | os << "PRIMARY KEY"; |
19 | 0 | break; |
20 | 0 | case ConstraintType::Unique: |
21 | 0 | os << "UNIQUE"; |
22 | 0 | break; |
23 | 0 | } |
24 | 0 | return os; |
25 | 0 | } |
26 | | |
27 | | // Constraints |
28 | | TableConstraint::TableConstraint(ConstraintType type, std::vector<char*>* columnNames) |
29 | 1.89k | : type(type), columnNames(columnNames) {} |
30 | | |
31 | 1.89k | TableConstraint::~TableConstraint() { |
32 | 2.20k | for (auto* column : *columnNames) { |
33 | 2.20k | free(column); |
34 | 2.20k | } |
35 | 1.89k | delete columnNames; |
36 | 1.89k | } |
37 | | |
38 | | // Foreign key constraint |
39 | | ReferencesSpecification::ReferencesSpecification(char* schema, char* table, std::vector<char*>* columns) |
40 | 0 | : schema{schema}, table{table}, columns{columns} {}; |
41 | | |
42 | 0 | ReferencesSpecification::~ReferencesSpecification() { |
43 | 0 | free(schema); |
44 | 0 | free(table); |
45 | 0 | if (columns) { |
46 | 0 | for (auto* column : *columns) { |
47 | 0 | free(column); |
48 | 0 | } |
49 | 0 | delete columns; |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | | ForeignKeyConstraint::ForeignKeyConstraint(std::vector<char*>* columnNames, ReferencesSpecification* references) |
54 | 0 | : TableConstraint(ConstraintType::ForeignKey, columnNames), references{references} {} |
55 | | |
56 | 0 | ForeignKeyConstraint::~ForeignKeyConstraint() { delete references; } |
57 | | |
58 | | ColumnConstraints::ColumnConstraints() |
59 | 24.1k | : constraints{new std::unordered_set<ConstraintType>()}, references{new std::vector<ReferencesSpecification*>} {} |
60 | | |
61 | | // ColumnDefinition |
62 | | ColumnDefinition::ColumnDefinition(char* name, ColumnType type, std::unordered_set<ConstraintType>* column_constraints, |
63 | | std::vector<ReferencesSpecification*>* references) |
64 | 24.1k | : column_constraints(column_constraints), name(name), type(type), nullable(true), references(references) {} |
65 | | |
66 | 24.1k | ColumnDefinition::~ColumnDefinition() { |
67 | 24.1k | free(name); |
68 | 24.1k | delete column_constraints; |
69 | 24.1k | if (references) { |
70 | 24.1k | for (auto* ref : *references) { |
71 | 0 | delete ref; |
72 | 0 | } |
73 | 24.1k | } |
74 | 24.1k | delete references; |
75 | 24.1k | } |
76 | | |
77 | 24.1k | bool ColumnDefinition::trySetNullableExplicit() { |
78 | 24.1k | if (column_constraints->count(ConstraintType::NotNull) || column_constraints->count(ConstraintType::PrimaryKey)) { |
79 | 2.64k | if (column_constraints->count(ConstraintType::Null)) { |
80 | 1.77k | return false; |
81 | 1.77k | } |
82 | 870 | nullable = false; |
83 | 870 | } |
84 | | |
85 | 22.3k | return true; |
86 | 24.1k | } |
87 | | |
88 | | // CreateStatemnet |
89 | | CreateStatement::CreateStatement(CreateType type) |
90 | 3.49k | : SQLStatement(kStmtCreate), |
91 | 3.49k | type(type), |
92 | 3.49k | ifNotExists(false), |
93 | 3.49k | filePath(nullptr), |
94 | 3.49k | schema(nullptr), |
95 | 3.49k | tableName(nullptr), |
96 | 3.49k | indexName(nullptr), |
97 | 3.49k | indexColumns(nullptr), |
98 | 3.49k | columns(nullptr), |
99 | 3.49k | tableConstraints(nullptr), |
100 | 3.49k | viewColumns(nullptr), |
101 | 3.49k | select(nullptr) {} |
102 | | |
103 | 3.49k | CreateStatement::~CreateStatement() { |
104 | 3.49k | free(filePath); |
105 | 3.49k | free(schema); |
106 | 3.49k | free(tableName); |
107 | 3.49k | free(indexName); |
108 | 3.49k | delete select; |
109 | | |
110 | 3.49k | if (columns) { |
111 | 3.54k | for (auto* def : *columns) { |
112 | 3.54k | delete def; |
113 | 3.54k | } |
114 | 1.69k | delete columns; |
115 | 1.69k | } |
116 | | |
117 | 3.49k | if (tableConstraints) { |
118 | 1.69k | for (auto* def : *tableConstraints) { |
119 | 1.09k | delete def; |
120 | 1.09k | } |
121 | 1.69k | delete tableConstraints; |
122 | 1.69k | } |
123 | | |
124 | 3.49k | if (indexColumns) { |
125 | 4.95k | for (char* column : *indexColumns) { |
126 | 4.95k | free(column); |
127 | 4.95k | } |
128 | 1.12k | delete indexColumns; |
129 | 1.12k | } |
130 | | |
131 | 3.49k | if (viewColumns) { |
132 | 417 | for (char* column : *viewColumns) { |
133 | 417 | free(column); |
134 | 417 | } |
135 | 223 | delete viewColumns; |
136 | 223 | } |
137 | 3.49k | } |
138 | | |
139 | 1.69k | void CreateStatement::setColumnDefsAndConstraints(std::vector<TableElement*>* tableElements) { |
140 | 1.69k | columns = new std::vector<ColumnDefinition*>(); |
141 | 1.69k | tableConstraints = new std::vector<TableConstraint*>(); |
142 | | |
143 | 4.64k | for (auto tableElem : *tableElements) { |
144 | 4.64k | if (auto* colDef = dynamic_cast<ColumnDefinition*>(tableElem)) { |
145 | 3.54k | columns->emplace_back(colDef); |
146 | 3.54k | } else if (auto* tableConstraint = dynamic_cast<TableConstraint*>(tableElem)) { |
147 | 1.09k | tableConstraints->emplace_back(tableConstraint); |
148 | 1.09k | } |
149 | 4.64k | } |
150 | 1.69k | } |
151 | | |
152 | | } // namespace hsql |