forked from piotrkowalczuk/pqt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraint.go
240 lines (210 loc) · 6.58 KB
/
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package pqt
import (
"crypto/md5"
"encoding/base64"
"fmt"
"strings"
)
const (
// ConstraintTypeUnknown ...
ConstraintTypeUnknown ConstraintType = "unknown"
// ConstraintTypePrimaryKey ...
ConstraintTypePrimaryKey ConstraintType = "pkey"
// ConstraintTypeCheck ...
ConstraintTypeCheck ConstraintType = "check"
// ConstraintTypeUnique ...
ConstraintTypeUnique ConstraintType = "key"
// ConstraintTypeIndex ...
ConstraintTypeIndex ConstraintType = "idx"
// ConstraintTypeForeignKey ...
ConstraintTypeForeignKey ConstraintType = "fkey"
// ConstraintTypeExclusion ...
ConstraintTypeExclusion ConstraintType = "excl"
// ConstraintTypeUniqueIndex ...
ConstraintTypeUniqueIndex ConstraintType = "uidx"
)
type ConstraintType string
// ConstraintOption ...
type ConstraintOption func(*Constraint)
// Constraint ...
type Constraint struct {
Type ConstraintType
Where, Check string
PrimaryTable, Table *Table
PrimaryColumns, Columns Columns
Attribute []*Attribute
Match, OnDelete, OnUpdate int32
NoInherit, DeferrableInitiallyDeferred, DeferrableInitiallyImmediate bool
MethodSuffix string
}
// Name ...
func (c *Constraint) Name() string {
var schema string
switch {
case c.PrimaryTable == nil:
return "<missing table>"
case c.PrimaryTable.Schema == nil || c.PrimaryTable.Schema.Name == "":
schema = "public"
default:
schema = c.PrimaryTable.Schema.Name
}
if len(c.PrimaryColumns) == 0 {
return fmt.Sprintf("%s.%s_%s", schema, c.PrimaryTable.ShortName, c.Type)
}
tmp := make([]string, 0, len(c.PrimaryColumns))
for _, col := range c.PrimaryColumns {
if col.ShortName != "" {
tmp = append(tmp, col.ShortName)
continue
}
tmp = append(tmp, col.Name)
}
if len(c.Where) > 0 {
tmp = append(tmp, c.whereClauseHash())
}
return fmt.Sprintf("%s.%s_%s_%s", schema, c.PrimaryTable.ShortName, strings.Join(tmp, "_"), c.Type)
}
// WhereClauseHash returns at least 8-character hash of a hash clause
func (c *Constraint) whereClauseHash() string {
sum := md5.Sum([]byte(c.Where))
encoded := base64.StdEncoding.EncodeToString(sum[:])
if len(encoded) > 8 {
encoded = encoded[:8]
}
return encoded
}
// Unique constraint ensure that the data contained in a column or a group of columns is unique with respect to all the rows in the table.
func Unique(table *Table, columns ...*Column) *Constraint {
return &Constraint{
Type: ConstraintTypeUnique,
PrimaryTable: table,
PrimaryColumns: columns,
}
}
// PrimaryKey constraint is simply a combination of a unique constraint and a not-null constraint.
func PrimaryKey(table *Table, columns ...*Column) *Constraint {
return &Constraint{
Type: ConstraintTypePrimaryKey,
PrimaryTable: table,
PrimaryColumns: columns,
}
}
// Check ...
func Check(table *Table, check string, columns ...*Column) *Constraint {
return &Constraint{
Type: ConstraintTypeCheck,
PrimaryTable: table,
PrimaryColumns: columns,
Check: check,
}
}
//
//// Exclusion constraint ensure that if any two rows are compared on the specified columns
//// or expressions using the specified operators,
//// at least one of these operator comparisons will return false or null.
//func Exclusion(table *Table, exclude Exclude, columns ...*Column) *Constraint {
// return &Constraint{
// Type: ConstraintTypeExclusion,
// Table: table,
// Exclude: exclude,
// Columns: columns,
// }
//}
// Reference ...
type Reference struct {
From, To *Column
}
// ForeignKey constraint specifies that the values in a column (or a group of columns)
// must match the values appearing in some row of another table.
// We say this maintains the referential integrity between two related tables.
func ForeignKey(primaryColumns, referenceColumns Columns, opts ...ConstraintOption) *Constraint {
if len(referenceColumns) == 0 {
panic("foreign key expects at least one reference column")
}
for _, c := range primaryColumns {
if c.Table != primaryColumns[0].Table {
panic("column tables inconsistency")
}
}
for _, r := range referenceColumns {
if r.Table != referenceColumns[0].Table {
panic("reference column tables inconsistency")
}
}
fk := &Constraint{
Type: ConstraintTypeForeignKey,
PrimaryTable: primaryColumns[0].Table,
PrimaryColumns: primaryColumns,
Table: referenceColumns[0].Table,
Columns: referenceColumns,
}
for _, o := range opts {
o(fk)
}
return fk
}
// Index ...
func Index(table *Table, columns ...*Column) *Constraint {
return &Constraint{
Type: ConstraintTypeIndex,
PrimaryTable: table,
PrimaryColumns: columns,
}
}
// UniqueIndex ...
func UniqueIndex(table *Table, methodSuffix, where string, columns ...*Column) *Constraint {
return &Constraint{
Type: ConstraintTypeUniqueIndex,
PrimaryTable: table,
PrimaryColumns: columns,
Where: where,
MethodSuffix: methodSuffix,
}
}
// String implements Stringer interface.
func (c *Constraint) String() string {
return c.Name()
}
// IsForeignKey returns true if string has suffix "_fkey".
func IsForeignKey(c string) bool {
return strings.HasSuffix(c, string(ConstraintTypeForeignKey))
}
// IsUnique returns true if string has suffix "_key".
func IsUnique(c string) bool {
return strings.HasSuffix(c, string(ConstraintTypeUnique))
}
// IsPrimaryKey returns true if string has suffix "_pkey".
func IsPrimaryKey(c string) bool {
return strings.HasSuffix(c, string(ConstraintTypePrimaryKey))
}
// IsCheck returns true if string has suffix "_check".
func IsCheck(c string) bool {
return strings.HasSuffix(c, string(ConstraintTypeCheck))
}
//// IsExclusion returns true if string has suffix "_excl".
//func IsExclusion(c string) bool {
// return strings.HasSuffix(c, string(ConstraintTypeExclusion))
//}
// IsIndex returns true if string has suffix "_idx".
func IsIndex(c string) bool {
return strings.HasSuffix(c, string(ConstraintTypeIndex))
}
type Constraints []*Constraint
// CountOf returns number of constraints of given type.
// If nothing is given return length of entire slice.
func (c Constraints) CountOf(types ...ConstraintType) int {
if len(types) == 0 {
return len(c)
}
var count int
OuterLoop:
for _, cc := range c {
for _, t := range types {
if cc.Type == t {
count++
continue OuterLoop
}
}
}
return count
}