forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
association.go
255 lines (212 loc) · 7.31 KB
/
association.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package field
import (
"fmt"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
)
// RelationshipType table relationship
type RelationshipType schema.RelationshipType
const (
// HasOne a has one association sets up a one-to-one connection with another model. Reference https://gorm.io/docs/has_one.html
HasOne RelationshipType = RelationshipType(schema.HasOne) // HasOneRel has one relationship
// HasMany a has many association sets up a one-to-many connection with another model. Reference https://gorm.io/docs/has_many.html
HasMany RelationshipType = RelationshipType(schema.HasMany) // HasManyRel has many relationships
// BelongsTo A belongs to association sets up a one-to-one connection with another model. Reference https://gorm.io/docs/belongs_to.html
BelongsTo RelationshipType = RelationshipType(schema.BelongsTo) // BelongsToRel belongs to relationship
// Many2Many Many to Many add a join table between two models. Reference https://gorm.io/docs/many2many.html
Many2Many RelationshipType = RelationshipType(schema.Many2Many) // Many2ManyRel many to many relationship
)
type relationScope func(*gorm.DB) *gorm.DB
var (
// RelationFieldUnscoped relation fild unscoped
RelationFieldUnscoped relationScope = func(tx *gorm.DB) *gorm.DB {
return tx.Unscoped()
}
)
var ns = schema.NamingStrategy{}
// RelationField interface for relation field
type RelationField interface {
Name() string
Path() string
// Field return expr for Select
// Field() return "<self>" field name in struct
// Field("RelateField") return "<self>.RelateField" for Select
// Field("RelateField", "RelateRelateField") return "<self>.RelateField.RelateRelateField" for Select
// ex:
// Select(u.CreditCards.Field()) equals to GORM: Select("CreditCards")
// Select(u.CreditCards.Field("Bank")) equals to GORM: Select("CreditCards.Bank")
// Select(u.CreditCards.Field("Bank","Owner")) equals to GORM: Select("CreditCards.Bank.Owner")
Field(fields ...string) Expr
On(conds ...Expr) RelationField
Select(conds ...Expr) RelationField
Order(columns ...Expr) RelationField
Clauses(hints ...clause.Expression) RelationField
Scopes(funcs ...relationScope) RelationField
Offset(offset int) RelationField
Limit(limit int) RelationField
GetConds() []Expr
GetSelects() []Expr
GetOrderCol() []Expr
GetClauses() []clause.Expression
GetScopes() []relationScope
GetPage() (offset, limit int)
}
// Relation relation meta info
type Relation struct {
relationship RelationshipType
fieldName string
fieldType string
fieldPath string
fieldModel interface{} // store relaiton model
childRelations []Relation
conds []Expr
selects []Expr
order []Expr
clauses []clause.Expression
scopes []relationScope
limit, offset int
}
// Name relation field' name
func (r Relation) Name() string { return r.fieldName }
// Path relation field's path
func (r Relation) Path() string { return r.fieldPath }
// Type relation field's type
func (r Relation) Type() string { return r.fieldType }
// Model relation field's model
func (r Relation) Model() interface{} { return r.fieldModel }
// Relationship relationship between field and table struct
func (r Relation) Relationship() RelationshipType { return r.relationship }
// RelationshipName relationship's name
func (r Relation) RelationshipName() string { return ns.SchemaName(string(r.relationship)) }
// ChildRelations return child relations
func (r Relation) ChildRelations() []Relation { return r.childRelations }
// Field build field
func (r Relation) Field(fields ...string) Expr {
if len(fields) > 0 {
return NewString("", r.fieldName+"."+strings.Join(fields, ".")).appendBuildOpts(WithoutQuote)
}
return NewString("", r.fieldName).appendBuildOpts(WithoutQuote)
}
// AppendChildRelation append child relationship
func (r *Relation) AppendChildRelation(relations ...Relation) {
r.childRelations = append(r.childRelations, wrapPath(r.fieldPath, relations)...)
}
// On relation condition
func (r Relation) On(conds ...Expr) RelationField {
r.conds = append(r.conds, conds...)
return &r
}
// Select relation select columns
func (r Relation) Select(columns ...Expr) RelationField {
r.selects = append(r.selects, columns...)
return &r
}
// Order relation order columns
func (r Relation) Order(columns ...Expr) RelationField {
r.order = append(r.order, columns...)
return &r
}
// Clauses set relation clauses
func (r Relation) Clauses(hints ...clause.Expression) RelationField {
r.clauses = append(r.clauses, hints...)
return &r
}
// Scopes set scopes func
func (r Relation) Scopes(funcs ...relationScope) RelationField {
r.scopes = append(r.scopes, funcs...)
return &r
}
// Offset set relation offset
func (r Relation) Offset(offset int) RelationField {
r.offset = offset
return &r
}
// Limit set relation limit
func (r Relation) Limit(limit int) RelationField {
r.limit = limit
return &r
}
// GetConds get query conditions
func (r *Relation) GetConds() []Expr { return r.conds }
// GetSelects get select columns
func (r *Relation) GetSelects() []Expr { return r.selects }
// GetOrderCol get order columns
func (r *Relation) GetOrderCol() []Expr { return r.order }
// GetClauses get clauses
func (r *Relation) GetClauses() []clause.Expression { return r.clauses }
// GetScopes get scope functions
func (r *Relation) GetScopes() []relationScope { return r.scopes } // nolint
// GetPage get offset and limit
func (r *Relation) GetPage() (offset, limit int) { return r.offset, r.limit }
// StructField return struct field code
func (r *Relation) StructField() (fieldStr string) {
for _, relation := range r.childRelations {
fieldStr += relation.fieldName + " struct {\nfield.RelationField\n" + relation.StructField() + "}\n"
}
return fieldStr
}
// StructFieldInit return field initialize code
func (r *Relation) StructFieldInit() string {
initStr := fmt.Sprintf("RelationField: field.NewRelation(%q, %q),\n", r.fieldPath, r.fieldType)
for _, relation := range r.childRelations {
initStr += relation.fieldName + ": struct {\nfield.RelationField\n" + strings.TrimSpace(relation.StructField()) + "}"
initStr += "{\n" + relation.StructFieldInit() + "},\n"
}
return initStr
}
func wrapPath(root string, rs []Relation) []Relation {
result := make([]Relation, len(rs))
for i, r := range rs {
r.fieldPath = root + "." + r.fieldPath
r.childRelations = wrapPath(root, r.childRelations)
result[i] = r
}
return result
}
var defaultRelationshipPrefix = map[RelationshipType]string{
// HasOne: "",
// BelongsTo: "",
HasMany: "[]",
Many2Many: "[]",
}
// RelateConfig config for relationship
type RelateConfig struct {
RelatePointer bool
RelateSlice bool
RelateSlicePointer bool
JSONTag string
GORMTag GormTag
Tag Tag
OverwriteTag Tag
}
// RelateFieldPrefix return generated relation field's type
func (c *RelateConfig) RelateFieldPrefix(relationshipType RelationshipType) string {
switch {
case c.RelatePointer:
return "*"
case c.RelateSlice:
return "[]"
case c.RelateSlicePointer:
return "[]*"
default:
return defaultRelationshipPrefix[relationshipType]
}
}
func (c *RelateConfig) GetTag(fieldName string) Tag {
if c == nil {
return Tag{}
}
if c.OverwriteTag != nil {
return c.OverwriteTag
}
if c.Tag == nil {
c.Tag = Tag{}
}
if c.JSONTag == "" {
c.JSONTag = ns.ColumnName("", fieldName)
}
c.Tag.Set(TagKeyJson, c.JSONTag)
return c.Tag
}