1#include "duckdb/parser/constraints/unique_constraint.hpp"
2
3#include "duckdb/common/serializer.hpp"
4
5using namespace std;
6using namespace duckdb;
7
8string UniqueConstraint::ToString() const {
9 return is_primary_key ? "PRIMARY KEY constraint" : "UNIQUE Constraint";
10}
11
12unique_ptr<Constraint> UniqueConstraint::Copy() {
13 if (index == INVALID_INDEX) {
14 return make_unique<UniqueConstraint>(columns, is_primary_key);
15 } else {
16 assert(columns.size() == 0);
17 return make_unique<UniqueConstraint>(index, is_primary_key);
18 }
19}
20
21void UniqueConstraint::Serialize(Serializer &serializer) {
22 Constraint::Serialize(serializer);
23 serializer.Write<bool>(is_primary_key);
24 serializer.Write<uint64_t>(index);
25 assert(columns.size() <= numeric_limits<uint32_t>::max());
26 serializer.Write<uint32_t>((uint32_t)columns.size());
27 for (auto &column : columns) {
28 serializer.WriteString(column);
29 }
30}
31
32unique_ptr<Constraint> UniqueConstraint::Deserialize(Deserializer &source) {
33 auto is_primary_key = source.Read<bool>();
34 auto index = source.Read<uint64_t>();
35 auto column_count = source.Read<uint32_t>();
36
37 if (index != INVALID_INDEX) {
38 // single column parsed constraint
39 return make_unique<UniqueConstraint>(index, is_primary_key);
40 } else {
41 // column list parsed constraint
42 vector<string> columns;
43 for (uint32_t i = 0; i < column_count; i++) {
44 auto column_name = source.Read<string>();
45 columns.push_back(column_name);
46 }
47 return make_unique<UniqueConstraint>(columns, is_primary_key);
48 }
49}
50