-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_constraint.go
151 lines (137 loc) · 3.57 KB
/
table_constraint.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
package gosql
import "strings"
// [ CONSTRAINT constraint_name ]
// { CHECK ( expression ) [ NO INHERIT ] |
//
// UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] ) index_parameters |
// PRIMARY KEY ( column_name [, ... ] ) index_parameters |
// EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |
// FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
// [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }
//
// [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
type constraintTable struct {
// name
name string
// check expression
check *Condition
// unique index
unique columnIndexParameters
// primary key
primary columnIndexParameters
// exclude
exclude excludeTable
// foreign key
foreignKey foreignKey
// no inherit
noInherit bool
// deferrable
deferrable *bool
// initially
initially string
// nulls not distinct
nullsNotDistinct bool
}
// Name set name
func (c *constraintTable) Name(name string) *constraintTable {
c.name = name
return c
}
// Check get check expression
func (c *constraintTable) Check() *Condition {
if c.check == nil {
c.check = NewSqlCondition(ConditionOperatorAnd)
}
return c.check
}
// NoInherit set no inherit
func (c *constraintTable) NoInherit() *constraintTable {
c.noInherit = true
return c
}
// NullNotDistinct is unique constraint null not distinct
func (c *constraintTable) NullNotDistinct() *constraintTable {
c.nullsNotDistinct = true
return c
}
// Unique get unique expression
func (c *constraintTable) Unique() *columnIndexParameters {
return &c.unique
}
// PrimaryKey get primary key expression
func (c *constraintTable) PrimaryKey() *columnIndexParameters {
return &c.primary
}
// Exclude get exclude expression
func (c *constraintTable) Exclude() *excludeTable {
return &c.exclude
}
// ForeignKey get foreign key expression
func (c *constraintTable) ForeignKey() *foreignKey {
return &c.foreignKey
}
// Deferrable set deferrable
func (c *constraintTable) Deferrable(deferrable *bool) *constraintTable {
c.deferrable = deferrable
return c
}
// Initially set initially
func (c *constraintTable) Initially(initially string) *constraintTable {
c.initially = initially
return c
}
// IsEmpty check if constraint is empty
func (c *constraintTable) IsEmpty() bool {
return c == nil || (c.name == "" &&
c.check.IsEmpty() &&
c.unique.IsEmpty() &&
c.primary.IsEmpty() &&
c.exclude.IsEmpty() &&
c.foreignKey.IsEmpty() &&
c.deferrable == nil &&
c.initially == "" &&
!c.nullsNotDistinct)
}
// String render table constraint
func (c *constraintTable) String() string {
if c.IsEmpty() {
return ""
}
b := strings.Builder{}
if c.name != "" {
b.WriteString(" CONSTRAINT " + c.name)
}
if !c.check.IsEmpty() {
b.WriteString(" CHECK " + c.check.String())
}
if c.noInherit {
b.WriteString(" NO INHERIT")
}
if !c.unique.IsEmpty() {
if c.nullsNotDistinct {
b.WriteString(" UNIQUE NULLS NOT DISTINCT " + c.unique.String())
} else {
b.WriteString(" UNIQUE " + c.unique.String())
}
}
if !c.primary.IsEmpty() {
b.WriteString(" PRIMARY KEY " + c.primary.String())
}
if !c.exclude.IsEmpty() {
b.WriteString(c.exclude.String())
}
if !c.foreignKey.IsEmpty() {
b.WriteString(c.foreignKey.String())
}
if c.deferrable != nil {
if *c.deferrable {
b.WriteString(" DEFERRABLE")
} else {
b.WriteString(" NOT DEFERRABLE")
}
}
if c.initially != "" {
b.WriteString(" INITIALLY " + c.initially)
}
return b.String()[1:]
}