-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstraint.hpp
More file actions
117 lines (100 loc) · 4.02 KB
/
Copy pathConstraint.hpp
File metadata and controls
117 lines (100 loc) · 4.02 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
//
// Created by Piotrek Rybiec on 04/05/2025.
//
#ifndef CONSTRAINT_H
#define CONSTRAINT_H
#include "data_types.hpp"
#include "Value.hpp"
#include "Row.hpp"
class Database; // forward declaration, remvoe later
class Constraint {
protected:
ConstraintType type;
std::string name;
public:
virtual ~Constraint() = default;
Constraint(ConstraintType type, std::string name) : type(type), name(std::move(name)) {}
ConstraintType getType() const;
const std::string& getName() const;
virtual std::string toString() const;
virtual bool validate(const Row& row, const Table& table) const = 0;
virtual bool validate(const Row& row, const Table& table, const Database& base) const = 0;
};
class PrimaryKeyConstraint : public Constraint {
private:
std::vector<std::string> column_names;
public:
PrimaryKeyConstraint(const std::string name, const std::vector<std::string>& columns)
: Constraint(ConstraintType::PRIMARY_KEY, std::move(name)),
column_names(std::move(columns)) {}
const std::vector<std::string>& getColumnNames() const;
std::string toString() const override;
bool validate(const Row& row, const Table& table) const override;
bool validate(const Row& row, const Table& table, const Database& base) const override;
};
class ForeignKeyConstraint : public Constraint {
private:
std::string column_name;
std::string ref_table;
std::string ref_column;
public:
ForeignKeyConstraint(const std::string name, const std::string& column_name,
const std::string& ref_table, const std::string& ref_column)
: Constraint(ConstraintType::FOREIGN_KEY, name),
column_name(column_name),
ref_table(ref_table),
ref_column(ref_column) {}
const std::string& getColumnName() const;
const std::string& getRefTable() const;
const std::string& getRefColumn() const;
std::string toString() const override;
bool validate(const Row& row, const Table& table) const override;
bool validate(const Row& row, const Table& table, const Database& base) const override;
};
class UniqueConstraint : public Constraint {
private:
std::vector<std::string> column_names;
public:
UniqueConstraint(const std::string name, const std::vector<std::string> &column_names)
: Constraint(ConstraintType::UNIQUE, name),
column_names(column_names) {}
const std::vector<std::string>& getColumnNames() const;
std::string toString() const override;
bool validate(const Row& row, const Table& table) const override;
bool validate(const Row& row, const Table& table, const Database& base) const override;
};
class NotNullConstraint : public Constraint {
private:
std::string column_name;
public:
NotNullConstraint(ConstraintType type, const std::string name, const std::string& column_name)
: Constraint(ConstraintType::NOT_NULL, name),
column_name(column_name) {}
const std::string& getColumnName() const;
std::string toString() const override;
bool validate(const Row& row, const Table& table) const override;
bool validate(const Row& row, const Table& table, const Database& base) const override;
};
class DefaultConstraint : public Constraint {
private:
std::string column_name;
Value default_value;
public:
DefaultConstraint(const std::string name, const std::string &column_name,
const Value &default_value)
: Constraint(ConstraintType::DEFAULT, name),
column_name(column_name),
default_value(default_value) {}
const std::string& getColumnName() const;
const Value& getDefaultValue() const;
std::string toString() const override;
bool validate(const Row& row, const Table& table) const override {
// Default constraints are always valid as they only provide default values
return true;
}
bool validate(const Row& row, const Table& table, const Database& base) const override {
// Default constraints are always valid as they only provide default values
return true;
}
};
#endif //CONSTRAINT_H