1#include "duckdb/parser/parsed_data/alter_table_info.hpp"
2#include "duckdb/common/serializer.hpp"
3
4using namespace duckdb;
5using namespace std;
6
7void AlterInfo::Serialize(Serializer &serializer) {
8 serializer.Write<AlterType>(type);
9}
10
11unique_ptr<AlterInfo> AlterInfo::Deserialize(Deserializer &source) {
12 auto type = source.Read<AlterType>();
13 switch (type) {
14 case AlterType::ALTER_TABLE:
15 return AlterTableInfo::Deserialize(source);
16 default:
17 throw SerializationException("Unknown alter type for deserialization!");
18 }
19}
20
21void AlterTableInfo::Serialize(Serializer &serializer) {
22 AlterInfo::Serialize(serializer);
23 serializer.Write<AlterTableType>(alter_table_type);
24 serializer.WriteString(schema);
25 serializer.WriteString(table);
26}
27
28unique_ptr<AlterInfo> AlterTableInfo::Deserialize(Deserializer &source) {
29 auto type = source.Read<AlterTableType>();
30 auto schema = source.Read<string>();
31 auto table = source.Read<string>();
32 unique_ptr<AlterTableInfo> info;
33 switch (type) {
34 case AlterTableType::RENAME_COLUMN:
35 return RenameColumnInfo::Deserialize(source, schema, table);
36 case AlterTableType::RENAME_TABLE:
37 return RenameTableInfo::Deserialize(source, schema, table);
38 case AlterTableType::ADD_COLUMN:
39 return AddColumnInfo::Deserialize(source, schema, table);
40 case AlterTableType::REMOVE_COLUMN:
41 return RemoveColumnInfo::Deserialize(source, schema, table);
42 case AlterTableType::ALTER_COLUMN_TYPE:
43 return ChangeColumnTypeInfo::Deserialize(source, schema, table);
44 case AlterTableType::SET_DEFAULT:
45 return SetDefaultInfo::Deserialize(source, schema, table);
46 default:
47 throw SerializationException("Unknown alter table type for deserialization!");
48 }
49}
50
51//===--------------------------------------------------------------------===//
52// RenameColumnInfo
53//===--------------------------------------------------------------------===//
54void RenameColumnInfo::Serialize(Serializer &serializer) {
55 AlterTableInfo::Serialize(serializer);
56 serializer.WriteString(name);
57 serializer.WriteString(new_name);
58}
59
60unique_ptr<AlterInfo> RenameColumnInfo::Deserialize(Deserializer &source, string schema, string table) {
61 auto name = source.Read<string>();
62 auto new_name = source.Read<string>();
63 return make_unique<RenameColumnInfo>(schema, table, name, new_name);
64}
65
66//===--------------------------------------------------------------------===//
67// RenameTableInfo
68//===--------------------------------------------------------------------===//
69void RenameTableInfo::Serialize(Serializer &serializer) {
70 AlterTableInfo::Serialize(serializer);
71 serializer.WriteString(new_table_name);
72}
73
74unique_ptr<AlterInfo> RenameTableInfo::Deserialize(Deserializer &source, string schema, string table) {
75 auto new_name = source.Read<string>();
76 return make_unique<RenameTableInfo>(schema, table, new_name);
77}
78
79//===--------------------------------------------------------------------===//
80// AddColumnInfo
81//===--------------------------------------------------------------------===//
82void AddColumnInfo::Serialize(Serializer &serializer) {
83 AlterTableInfo::Serialize(serializer);
84 new_column.Serialize(serializer);
85}
86
87unique_ptr<AlterInfo> AddColumnInfo::Deserialize(Deserializer &source, string schema, string table) {
88 auto new_column = ColumnDefinition::Deserialize(source);
89 return make_unique<AddColumnInfo>(schema, table, move(new_column));
90}
91
92//===--------------------------------------------------------------------===//
93// RemoveColumnInfo
94//===--------------------------------------------------------------------===//
95void RemoveColumnInfo::Serialize(Serializer &serializer) {
96 AlterTableInfo::Serialize(serializer);
97 serializer.WriteString(removed_column);
98 serializer.Write<bool>(if_exists);
99}
100
101unique_ptr<AlterInfo> RemoveColumnInfo::Deserialize(Deserializer &source, string schema, string table) {
102 auto new_name = source.Read<string>();
103 auto if_exists = source.Read<bool>();
104 return make_unique<RemoveColumnInfo>(schema, table, new_name, if_exists);
105}
106
107//===--------------------------------------------------------------------===//
108// ChangeColumnTypeInfo
109//===--------------------------------------------------------------------===//
110void ChangeColumnTypeInfo::Serialize(Serializer &serializer) {
111 AlterTableInfo::Serialize(serializer);
112 serializer.WriteString(column_name);
113 target_type.Serialize(serializer);
114 serializer.WriteOptional(expression);
115}
116
117unique_ptr<AlterInfo> ChangeColumnTypeInfo::Deserialize(Deserializer &source, string schema, string table) {
118 auto column_name = source.Read<string>();
119 auto target_type = SQLType::Deserialize(source);
120 auto expression = source.ReadOptional<ParsedExpression>();
121 return make_unique<ChangeColumnTypeInfo>(schema, table, move(column_name), move(target_type), move(expression));
122}
123
124//===--------------------------------------------------------------------===//
125// SetDefaultInfo
126//===--------------------------------------------------------------------===//
127void SetDefaultInfo::Serialize(Serializer &serializer) {
128 AlterTableInfo::Serialize(serializer);
129 serializer.WriteString(column_name);
130 serializer.WriteOptional(expression);
131}
132
133unique_ptr<AlterInfo> SetDefaultInfo::Deserialize(Deserializer &source, string schema, string table) {
134 auto column_name = source.Read<string>();
135 auto new_default = source.ReadOptional<ParsedExpression>();
136 return make_unique<SetDefaultInfo>(schema, table, move(column_name), move(new_default));
137}
138