This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathconstraint.cpp
79 lines (66 loc) · 2.38 KB
/
constraint.cpp
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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// constraint.cpp
//
// Identification: src/catalog/constraint.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "catalog/constraint.h"
#include <sstream>
namespace peloton {
namespace catalog {
// Serialize this constraint
void Constraint::SerializeTo(SerializeOutput &out) {
out.WriteTextString(constraint_name);
out.WriteInt((int)constraint_type);
out.WriteInt(fk_list_offset);
out.WriteInt(unique_index_list_offset);
if (constraint_type == ConstraintType::DEFAULT) {
default_value->SerializeTo(out);
}
if (constraint_type == ConstraintType::CHECK) {
out.WriteInt((int)exp.first);
exp.second.SerializeTo(out);
}
}
// Deserialize this constraint
Constraint Constraint::DeserializeFrom(SerializeInput &in,
const type::TypeId column_type) {
std::string constraint_name = in.ReadTextString();
ConstraintType constraint_type = (ConstraintType)in.ReadInt();
oid_t foreign_key_list_offset = in.ReadInt();
oid_t unique_index_offset = in.ReadInt();
auto column_constraint = Constraint(constraint_type, constraint_name);
column_constraint.SetForeignKeyListOffset(foreign_key_list_offset);
column_constraint.SetUniqueIndexOffset(unique_index_offset);
if (constraint_type == ConstraintType::DEFAULT) {
type::Value default_value = type::Value::DeserializeFrom(in, column_type);
column_constraint.addDefaultValue(default_value);
}
if (constraint_type == ConstraintType::CHECK) {
auto exp = column_constraint.GetCheckExpression();
ExpressionType exp_type = (ExpressionType)in.ReadInt();
type::Value exp_value = type::Value::DeserializeFrom(in, column_type);
column_constraint.AddCheck(exp_type, exp_value);
}
return column_constraint;
}
const std::string Constraint::GetInfo() const {
std::ostringstream os;
os << "Constraint[" << GetName() << ", "
<< ConstraintTypeToString(constraint_type);
if (GetType() == ConstraintType::CHECK) {
os << ", " << exp.first << " " << exp.second.GetInfo();
}
if (GetType() == ConstraintType::DEFAULT) {
os << ", " << default_value->GetInfo();
}
os << "]";
return os.str();
}
} // namespace catalog
} // namespace peloton