-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cpp
More file actions
176 lines (149 loc) · 5.05 KB
/
Table.cpp
File metadata and controls
176 lines (149 loc) · 5.05 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Created by Piotrek Rybiec on 05/05/2025.
//
//
#include <string>
#include <vector>
#include <fmt/format.h>
#include "Constraint.hpp"
#include "Row.hpp"
#include "Table.hpp"
#include "Column.hpp"
Table::Table(std::string name) : name_(std::move(name)) {
if (name_.empty()) {
throw std::runtime_error("table name cannot be empty");
}
}
Table::Table(const std::string& name, const std::vector<Column>& columns) : name_(name), columns_(columns) {
if (name_.empty()) {
throw std::runtime_error("table name cannot be empty");
}
for (int i = 0; i < columns_.size(); i++) {
column_index_map_[columns_[i].getName()] = i;
}
}
auto Table::addColumn(const Column& column) -> void {
if (hasColumn(column.getName())) {
throw std::runtime_error(
fmt::format("column already exists: {}", column.getName())
);
}
columns_.push_back(std::move(column));
column_index_map_[columns_.back().getName()] = columns_.size() - 1;
}
auto Table::getColumn(const std::string& name) const -> const Column& {
if (!hasColumn(name)) {
throw std::runtime_error(
fmt::format("column not found: {}", name)
);
}
return columns_[column_index_map_.at(name)];
}
auto Table::getColumns() const -> const ColumnList& { return columns_; }
auto Table::hasColumn(const std::string& name) const -> bool {
return column_index_map_.find(name) != column_index_map_.end();
}
auto Table::getColumnIndex(const std::string& name) const -> size_t {
if (!hasColumn(name)) {
throw std::runtime_error(
fmt::format("column not found: {}", name)
);
}
return column_index_map_.at(name);
}
auto Table::addRow(const Row& row) -> void {
if (!validateRow(row)) {
throw std::runtime_error("row validation failed");
}
rows_.push_back(std::move(row));
}
auto Table::getRows() const -> const RowList& { return rows_; }
auto Table::getRow(size_t index) -> Row& {
if (index >= rows_.size()) {
throw std::out_of_range(
std::format("row index out of range, exists: {} accessing: {}", rows_.size(), index)
);
}
return rows_[index];
}
auto Table::rowCount() const -> size_t { return rows_.size(); }
auto Table::addConstraint(ConstraintPtr c) -> void {
constraints_.push_back(std::move(c));
}
auto Table::getConstraints() const -> const ConstraintList& { return constraints_; }
auto Table::getConstraintsOfType(ConstraintType t) const -> ConstraintList {
ConstraintList res;
for (const auto& constraint : constraints_) {
if (constraint->getType() == t) {
res.push_back(constraint);
}
}
return res;
}
auto Table::getName() const -> const std::string& { return name_; }
auto Table::setName(std::string new_name) -> void {
if (new_name.empty()) {
throw std::runtime_error("table name cannot be empty");
}
name_ = std::move(new_name);
}
auto Table::validateRow(const Row& row) const -> bool {
// all columns in the row are present
for (const auto& col : columns_) {
if (!col.isNullable() && !row.hasColumn(col.getName())) {
return false;
}
}
for (const auto& constraint : constraints_) {
//throw std::logic_error("function Table::validateRow not implemented");
if (!constraint->validate(row, *this)) {
return false;
}
}
return true;
}
auto Table::clear() -> void {
columns_.clear();
rows_.clear();
constraints_.clear();
column_index_map_.clear();
}
auto Table::clearRows() -> void { rows_.clear(); }
auto Table::getPrimaryKeyConstraint() const -> ConstraintPtr {
for (const auto& constraint : constraints_) {
if (constraint->getType() == ConstraintType::PRIMARY_KEY) {
return constraint;
}
}
return nullptr;
}
auto Table::getPrimaryKeyColumns() const -> std::vector<std::string> {
auto pk_columns = std::vector<std::string>();
if (auto pk = getPrimaryKeyConstraint()) {
if (auto primary_key = dynamic_cast<PrimaryKeyConstraint*>(pk.get())) {
return primary_key->getColumnNames();
}
}
return pk_columns;;
}
auto Table::dropColumn(const std::string& name) -> void {
auto it = std::find_if(columns_.begin(), columns_.end(),
[&name](const Column& col) { return col.getName() == name; });
if (it != columns_.end()) {
columns_.erase(it);
column_index_map_.erase(name);
// update indices for remaining columns, it may be a little overhead?
for (size_t i = 0; i < columns_.size(); i++) {
column_index_map_[columns_[i].getName()] = i;
}
}
}
auto Table::renameColumn(const std::string& old_name, const std::string& new_name) -> void {
auto it = std::find_if(columns_.begin(), columns_.end(),
[&old_name](const Column& col) { return col.getName() == old_name; });
if (it != columns_.end()) {
it->setName(new_name);
column_index_map_.erase(old_name);
column_index_map_[new_name] = std::distance(columns_.begin(), it);
}
}