-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreate-publication-builder.go
More file actions
123 lines (97 loc) · 2.3 KB
/
create-publication-builder.go
File metadata and controls
123 lines (97 loc) · 2.3 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
package postgres
import (
"fmt"
"strings"
)
type CreatePublicationBuilder struct {
name string
tablesPart string
allTables string
withPart string
owner string
tables []string
schemaList []string
}
func NewCreatePublicationBuilder() *CreatePublicationBuilder {
return &CreatePublicationBuilder{}
}
func (b *CreatePublicationBuilder) Build() {
if b.allTables != "" {
b.tablesPart = b.allTables
return
}
// Build
res := "FOR "
// Check if tables are set
if len(b.tables) != 0 {
res += "TABLE " + strings.Join(b.tables, ", ")
}
// Check if schema are set
if len(b.schemaList) != 0 {
// Check if tables were added
if len(b.tables) != 0 {
// Append
res += ", "
}
res += "TABLES IN SCHEMA " + strings.Join(b.schemaList, ", ")
}
// Save
b.tablesPart = res
}
func (b *CreatePublicationBuilder) AddTable(name string, columns *[]string, additionalWhere *string) *CreatePublicationBuilder {
res := name
// Manage columns
if columns != nil {
res += " (" + strings.Join(*columns, ", ") + ")"
}
// Add where is set
if additionalWhere != nil {
res += " WHERE (" + *additionalWhere + ")"
}
// Save
b.tables = append(b.tables, res)
return b
}
func (b *CreatePublicationBuilder) SetTablesInSchema(schemaList []string) *CreatePublicationBuilder {
b.schemaList = schemaList
return b
}
func (b *CreatePublicationBuilder) SetForAllTables() *CreatePublicationBuilder {
b.allTables = "FOR ALL TABLES"
return b
}
func (b *CreatePublicationBuilder) SetOwner(n string) *CreatePublicationBuilder {
b.owner = n
return b
}
func (b *CreatePublicationBuilder) SetName(n string) *CreatePublicationBuilder {
b.name = n
return b
}
func (b *CreatePublicationBuilder) SetWith(publish string, publishViaPartitionRoot *bool) *CreatePublicationBuilder {
var with string
// Check if publish is set
if publish != "" {
with += "publish = '" + publish + "'"
}
// Check publish via partition root
if publishViaPartitionRoot != nil {
// Check if there is already a with set
if with != "" {
with += ", "
}
// Manage bool
with += "publish_via_partition_root = "
if *publishViaPartitionRoot {
with += "true"
} else {
with += "false"
}
}
// Check if there isn't something
if with != "" {
// Save
b.withPart = fmt.Sprintf("WITH (%s)", with)
}
return b
}