-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_partition.go
158 lines (138 loc) · 3.5 KB
/
table_partition.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package gosql
import "strings"
// { column_name | ( expression ) } [ COLLATE collation ] [ opclass ]
type partitionColumn struct {
expression expression
// collation
collate string
// opclass
opclass string
}
// Expression get expression
func (p *partitionColumn) Expression() *expression {
return &p.expression
}
// Collate set sort rule
func (p *partitionColumn) Collate(collation string) *partitionColumn {
p.collate = collation
return p
}
// OpClass set opclass
func (p *partitionColumn) OpClass(opclass string) *partitionColumn {
p.opclass = opclass
return p
}
// IsEmpty check is partition is empty
func (p *partitionColumn) IsEmpty() bool {
return p == nil || (p.expression.Len() == 0 && p.collate == "" && p.opclass == "")
}
// String Render partition into string
func (p *partitionColumn) String() string {
if p.IsEmpty() {
return ""
}
b := strings.Builder{}
if p.expression.Len() > 0 {
b.WriteString(" (" + p.expression.String(", ") + ")")
}
if p.collate != "" {
b.WriteString(" COLLATE " + p.collate)
}
if p.opclass != "" {
b.WriteString(" " + p.opclass)
}
return b.String()
}
// [ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]
type partitionTable struct {
// type of partition
partitionBy string
// partition columns list
clause expression
}
// By type of partition RANGE | LIST | HASH
func (p *partitionTable) By(class string) *partitionTable {
p.partitionBy = class
return p
}
// Clause Add partition clause
// { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ]
func (p *partitionTable) Clause(expression ...string) *expression {
p.clause.Add(expression...)
return &p.clause
}
// IsEmpty check is partition is empty
func (p *partitionTable) IsEmpty() bool {
return p == nil || (p.partitionBy == "" && p.clause.Len() == 0)
}
// String Render partition into string
func (p *partitionTable) String() string {
if p.IsEmpty() {
return ""
}
b := strings.Builder{}
if p.partitionBy != "" {
b.WriteString(" " + p.partitionBy)
}
if p.clause.Len() > 0 {
b.WriteString(" (" + p.clause.String(", ") + ")")
}
return b.String()[1:]
}
// partition_bound_spec
// IN ( partition_bound_expr [, ...] ) |
// FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )
//
// TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |
//
// WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )
type partitionBound struct {
// in
in expression
// from
from expression
// to
to expression
// with
with expression
}
// In expression
func (p *partitionBound) In() *expression {
return &p.in
}
// From expression
func (p *partitionBound) From() *expression {
return &p.from
}
// To expression
func (p *partitionBound) To() *expression {
return &p.to
}
// With expression
func (p *partitionBound) With() *expression {
return &p.with
}
// IsEmpty check is partition bound is empty
func (p *partitionBound) IsEmpty() bool {
return p == nil || (p.in.Len() == 0 && p.from.Len() == 0 && p.to.Len() == 0 && p.with.Len() == 0)
}
// String Render partition bound into string
func (p *partitionBound) String() string {
if p.IsEmpty() {
return ""
}
b := strings.Builder{}
if p.in.Len() > 0 {
b.WriteString(" IN (" + p.in.String(", ") + ")")
}
if p.from.Len() > 0 {
b.WriteString(" FROM (" + p.from.String(", ") + ")")
}
if p.to.Len() > 0 {
b.WriteString(" TO (" + p.to.String(", ") + ")")
}
if p.with.Len() > 0 {
b.WriteString(" WITH (" + p.with.String(", ") + ")")
}
return b.String()[1:]
}