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