-
Notifications
You must be signed in to change notification settings - Fork 0
/
relation.go
440 lines (350 loc) · 17 KB
/
relation.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package goeloquent
import (
"fmt"
"reflect"
)
type Relations string
func GetMorphMap(name string) string {
v, ok := RegisteredMorphModelsMap.Load(name)
if !ok {
panic(fmt.Sprintf("no registered morph model found for %s,check your code or register it", name))
}
return v.(string)
}
const (
RelationHasOne Relations = "HasOne"
RelationBelongsTo Relations = "BelongsTo"
RelationHasMany Relations = "HasMany"
RelationHasOneThrough Relations = "HasOneThrough"
RelationHasManyThrough Relations = "HasManyThrough"
RelationBelongsToMany Relations = "BelongsToMany"
RelationMorphOne Relations = "MorphOne"
RelationMorphMany Relations = "MorphMany"
RelationMorphTo Relations = "MorphTo"
RelationMorphToMany Relations = "MorphToMany"
RelationMorphByMany Relations = "MorphByMany"
PivotAlias = "goelo_pivot_" // used for pivot alias
OrmPivotAlias = "goelo_orm_pivot_" // used for orm pivot alias
OrmAggregateAlias = "goelo_orm_aggregate_" // used for orm WithCount alias
)
type Relation struct {
SelfModel interface{} // parent/self model pointer,usually a pointer to a struct , &User{}
RelatedModel interface{} // related model,usually a pointer to a struct , &User{}
RelationTypeName Relations // relation type name
FieldName string // field name in self model corresponding to this relation
*EloquentBuilder // RelationBuilder
}
func (r *Relation) GetEloquentBuilder() *EloquentBuilder {
return r.EloquentBuilder
}
type RelationI interface {
AddEagerConstraints(models interface{})
AddConstraints()
GetEloquentBuilder() *EloquentBuilder
//Match(models []interface{}, results []interface{}, relation string) //Match the eagerly loaded results to their parents.
//GetResults() //Get the results of the relationship.
//GetEager() //Get the relationship for eager loading.
//Get(dest interface{}, columns ...interface{}) //Get the results of the relationship.
GetRelationExistenceQuery(relatedQuery *EloquentBuilder, selfQuery *EloquentBuilder, alias string, columns string) *EloquentBuilder
GetSelf() *Model
GetRelated() *Model
//GetRelationExistenceCountQuery(Builder $query, Builder $parentQuery) //Get the relationship count of an eager load query.
//GetKeys(models []interface{},key string) //Get all the primary keys for an array of models.
//GetQuery() *Builder //Get the underlying query for the relation.
//GetQualifiedParentKeyName()string //
}
func NewRelationBaseBuilder(related interface{}) *EloquentBuilder {
if related == nil {
return ToEloquentBuilder(NewQueryBuilder())
}
return NewEloquentBuilder(related)
}
func (r *Relation) GetSelfKey(key string) interface{} {
feild := GetParsedModel(r.SelfModel).FieldsByDbName[key]
return reflect.ValueOf(r.SelfModel).Elem().FieldByName(feild.Name).Interface()
}
/*
HasMany Define a one-to-many relationship.
if we have a user model and a post model,each user has many posts, user has a hasMany relation with post, the user(self) model has an id column as primary key, and the post model(related) has a user_id column,
post's user_id = user's id , so the relation is defined as follows:
func (u *User) PostRelation() *goeloquent.HasManyRelation {
return u.HasMany(u, &Post{}, "id", "user_id")
}
*/
func (m *EloquentModel) HasMany(selfModelPointer, relatedModelPointer interface{}, selfColumn, relatedColumn string) *HasManyRelation {
b := NewRelationBaseBuilder(relatedModelPointer)
relation := HasManyRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: relatedModelPointer,
RelationTypeName: RelationHasMany,
EloquentBuilder: b,
},
SelfColumn: selfColumn,
RelatedColumn: relatedColumn,
}
relation.AddConstraints()
return &relation
}
/*
BelongsTo Define an inverse one-to-one or many relationship.
if we have a user model and a post model,each post belongs to a user,user has a belongsTo relation with post,
the user(related) model has an id column as primary key, and the post model(self) has a user_id column,
post's user_id = user's id , so the relation is defined as follows:
func (p *Post) UserRelation() *goeloquent.BelongsToRelation {
return p.BelongsTo(p, &User{}, "user_id", "id")
}
*/
func (m *EloquentModel) BelongsTo(selfModelPointer, relatedModelPointer interface{}, selfColumn, relatedColumn string) *BelongsToRelation {
b := NewRelationBaseBuilder(relatedModelPointer)
relation := BelongsToRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: relatedModelPointer,
RelationTypeName: RelationBelongsTo,
EloquentBuilder: b,
},
SelfColumn: selfColumn,
RelatedColumn: relatedColumn,
}
relation.AddConstraints()
return &relation
}
/*
BelongsToMany Define a many-to-many relationship.
if we have a user model and a role model,each user have many roles,each role also belongs to many users,user(related) has a belongsToMany relation with role(self),
in this case, we need a pivot table to store the relationship between user and role, the pivot table(let's say user_roles) has user_id and role_id columns,
user_roles.user_id = users.uid, user_roles.role_id = roles.rid, so the relation is defined as follows:
func (r *Role) UsersRelation() *goeloquent.BelongsToManyRelation {
return r.BelongsToMany(r, &User{}, "role_users", "role_id", "user_id", "rid", "uid")
}
usually we use id as priamry key for both users,roles table, here we use uid,rid as primary key for users,roles table only for demonstration purpose.
*/
func (m *EloquentModel) BelongsToMany(selfModelPointer, relatedModelPointer interface{}, pivotTable, pivotSelfColumn, pivotRelatedColumn, selfModelColumn, relatedModelColumn string) *BelongsToManyRelation {
b := NewRelationBaseBuilder(relatedModelPointer)
relation := BelongsToManyRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: relatedModelPointer,
RelationTypeName: RelationBelongsToMany,
EloquentBuilder: b,
},
PivotTable: pivotTable,
PivotSelfColumn: pivotSelfColumn,
PivotRelatedColumn: pivotRelatedColumn,
SelfColumn: selfModelColumn,
RelatedColumn: relatedModelColumn,
}
relatedModel := GetParsedModel(relatedModelPointer)
b.Join(relation.PivotTable, relation.PivotTable+"."+relation.PivotRelatedColumn, "=", relatedModel.Table+"."+relation.RelatedColumn)
b.Select(relatedModel.Table + "." + "*")
b.Select(fmt.Sprintf("%s.%s as %s%s", relation.PivotTable, relation.PivotSelfColumn, OrmPivotAlias, relation.PivotSelfColumn))
b.Select(fmt.Sprintf("%s.%s as %s%s", relation.PivotTable, relation.PivotRelatedColumn, OrmPivotAlias, relation.PivotRelatedColumn))
relation.AddConstraints()
return &relation
}
/*
HasOne Define a one-to-one relationship.
let's say we have a User model and a Phone model,each user has a phone , user has a hasOne relation with phone,
the user model(self) has an id column as primary key, and the phone model(related) has a user_id column,
phone's user_id = user's id , so the relation is defined as follows:
func (u *User) PhoneRelation() *goeloquent.HasOneRelation {
return u.HasOne(u, &Phone{}, "id", "user_id")
}
*/
func (m *EloquentModel) HasOne(selfModelPointer, relatedModelPointer interface{}, selfColumn, relatedColumn string) *HasOneRelation {
b := NewRelationBaseBuilder(relatedModelPointer)
relation := HasOneRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: relatedModelPointer,
RelationTypeName: RelationHasOne,
EloquentBuilder: b,
},
RelatedColumn: relatedColumn,
SelfColumn: selfColumn,
}
relation.AddConstraints()
return &relation
}
/*
MorphTo Create a new morph to relationship instance.
let's say we have a post model , a video model and a comment model,each post/video may have many comments,
and each comment belongs to a post or a video, so a comment have a morphTo relation with post/video model ,
we use comments table to store the comments ,in this case we would need a column(commentable_type) in comments table to
decide the type of the related model(post/video) and another column(commentable_id) to store the id of the related model(post/video),
func (c *Comment) CommentableRelation() *goeloquent.MorphToRelation {
return c.MorphTo(c, "commentable_id", "commentable_type", "id")
}
*/
func (m *EloquentModel) MorphTo(selfModelPointer interface{}, selfRelatedIdColumn, selfRelatedTypeColumn, relatedModelIdColumn string) *MorphToRelation {
builder := NewRelationBaseBuilder(nil)
relation := MorphToRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: nil,
RelationTypeName: RelationMorphTo,
EloquentBuilder: builder,
},
SelfRelatedIdColumn: selfRelatedIdColumn,
RelatedModelIdColumn: relatedModelIdColumn,
SelfRelatedTypeColumn: selfRelatedTypeColumn,
}
relation.AddConstraints()
return &relation
}
/*
MorphOne Define a polymorphic one-to-one relationship.
let's say we have a user model and an image model(save all other models related images ,post images , video screenshots,user avatars),each user has an avatar image, user has a morphOne relation with image,
the user model(self) has an id column as primary key, and the image model(related) has an imageable_id column and an imageable_type column,
image's imageable_id = user's id , image's imageable_type = user's model name, so the relation is defined as follows:
func (u *User) AvatarRelation() *goeloquent.MorphOneRelation {
return u.MorphOne(u, &Image{}, "id", "imageable_id", "imageable_type", "user")
}
*/
func (m *EloquentModel) MorphOne(selfModelPointer, relatedModelPointer interface{}, selfColumn, relatedModelIdColumn, relatedModelTypeColumn string, morphType ...string) *MorphOneRelation {
b := NewRelationBaseBuilder(relatedModelPointer)
relation := MorphOneRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: relatedModelPointer,
RelationTypeName: RelationMorphOne,
EloquentBuilder: b,
},
RelatedModelIdColumn: relatedModelIdColumn,
SelfColumn: selfColumn,
RelatedModelTypeColumn: relatedModelTypeColumn,
}
selfModel := GetParsedModel(selfModelPointer)
if len(morphType) > 0 {
relation.RelatedModelTypeColumnValue = morphType[0]
} else {
relation.RelatedModelTypeColumnValue = GetMorphMap(selfModel.Name)
}
relation.AddConstraints()
return &relation
}
/*
MorphMany Define a polymorphic one-to-many relationship.
let's say we have a post model and an image model,each post has many images, post has a morphMany relation with image,
the post model(self) has an id column as primary key, and the image model(related) has an imageable_id column and an imageable_type column,
image's imageable_id = post's id , image's imageable_type = post's model name, so the relation is defined as follows:
func (p *Post) ImageRelation() *goeloquent.MorphManyRelation {
return p.MorphMany(p, &Image{}, "id", "imageable_id", "imageable_type")
}
func (u *User) ImageRelation() *goeloquent.MorphManyRelation {
return u.MorphMany(u, &Image{}, "id", "imageable_id", "imageable_type")
}
*/
func (m *EloquentModel) MorphMany(selfModelPointer, relatedModelPointer interface{}, selfColumn, relatedModelIdColumn, relatedModelTypeColumn string, relatedModelTypeColumnValue ...string) *MorphManyRelation {
b := NewRelationBaseBuilder(relatedModelPointer)
relation := MorphManyRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: relatedModelPointer,
RelationTypeName: RelationMorphOne,
EloquentBuilder: b,
},
RelatedModelIdColumn: relatedModelIdColumn,
SelfColumn: selfColumn,
RelatedModelTypeColumn: relatedModelTypeColumn,
}
selfModel := GetParsedModel(selfModelPointer)
if len(relatedModelTypeColumnValue) > 0 {
relation.RelatedModelTypeColumnValue = relatedModelTypeColumnValue[0]
} else {
relation.RelatedModelTypeColumnValue = GetMorphMap(selfModel.Name)
}
relation.AddConstraints()
return &relation
}
/*
MorphToMany Define a polymorphic many-to-many relationship.
let's say we have a video model and a tag model,each video has many tags,each tag also belongs to many videos,video has a morphToMany relation with tag,
the video model(self) has an id column as primary key, and the tag model(related) has a id column, we need a pivot table to store the relationship between video and tag,
the pivot table(let's say tagables) has tag_id , tagable_id , tagable_type columns(to decide the type of the related model(post/video))
tagables.tag_id = tags.id, tagables.tagable_id = videos.id/posts.id,tagable_type = post/video, so the relation is defined as follows:
for post model
func (p *Post) TagsRelation() *goeloquent.MorphToManyRelation {
return p.MorphToMany(p, &Tag{}, "id", "id", "tagables", "tagable_id", "tagable_type", "tag_id", "post")
}
for video model
func (v *Video) TagsRelation() *goeloquent.MorphToManyRelation {
return v.MorphToMany(v, "tagable_id", "tagable_type", "tagables", "tag_id", "tag_id", "id", "id")
}
*/
func (m *EloquentModel) MorphToMany(selfModelPointer, relatedModelPointer interface{}, selfIdColumn, relatedIdColumn, pivotTable, pivotSelfIdColumn, pivotSelfTypeColumn, pivotRelatedIdColumn string, morphType ...string) *MorphToManyRelation {
b := NewRelationBaseBuilder(relatedModelPointer)
relation := MorphToManyRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: relatedModelPointer,
RelationTypeName: RelationMorphToMany,
EloquentBuilder: b,
},
PivotTable: pivotTable,
PivotSelfIdColumn: pivotSelfIdColumn,
PivotSelfTypeColumn: pivotSelfTypeColumn,
SelfIdColumn: selfIdColumn,
RelatedIdColumn: relatedIdColumn,
PivotRelatedIdColumn: pivotRelatedIdColumn,
}
relatedModel := GetParsedModel(relatedModelPointer)
selfModel := GetParsedModel(selfModelPointer)
b.Join(relation.PivotTable, relation.PivotTable+"."+relation.PivotRelatedIdColumn, "=", relatedModel.Table+"."+relation.RelatedIdColumn)
b.Select(relatedModel.Table + "." + "*")
b.Select(fmt.Sprintf("%s.%s as %s%s", relation.PivotTable, relation.PivotRelatedIdColumn, OrmPivotAlias, relation.PivotRelatedIdColumn))
b.Select(fmt.Sprintf("%s.%s as %s%s", relation.PivotTable, relation.PivotSelfIdColumn, OrmPivotAlias, relation.PivotSelfIdColumn))
var selfModelTypeColumnValue string
if len(morphType) > 0 {
selfModelTypeColumnValue = morphType[0]
} else {
selfModelTypeColumnValue = GetMorphMap(selfModel.Name)
}
relation.SelfModelTypeColumnValue = selfModelTypeColumnValue
relation.AddConstraints()
return &relation
}
/*
MorphByMany Define a polymorphic many-to-many relationship.
let's say we have a tag model and a post model,each tag has many posts,each post also belongs to many tags,tag has a morphByMany relation with post,
the tag model(self) has an id column as primary key, and the post model(related) has a id column, we need a pivot table to store the relationship between tag and post,
the pivot table(let's say tagables) has tag_id , tagable_id , tagable_type columns(to decide the type of the related model(post/video))
tagables.tag_id = tags.id, tagables.tagable_id = videos.id/posts.id,tagable_type = post/video, so the relation is defined as follows:
func (t *Tag) PostsRelation() *goeloquent.MorphByManyRelation {
return t.MorphByMany(t, &Post{}, "tagables", "id", "id", "tag_id", "tagable_id", "tagable_type")
}
func (t *Tag) VideosRelation() *goeloquent.MorphByManyRelation {
return t.MorphByMany(t, &Video{}, "tagables", "id", "id", "tag_id", "tagable_id", "tagable_type")
}
*/
func (m *EloquentModel) MorphByMany(selfModelPointer, relatedModelPointer interface{}, pivotTable, selfIdColumn, relatedIdColumn, pivotSelfColumn, pivotRelatedIdColumn, pivotRelatedTypeColumn string, relatedModelTypeColumnValue ...string) *MorphByManyRelation {
b := NewRelationBaseBuilder(relatedModelPointer)
relation := MorphByManyRelation{
Relation: &Relation{
SelfModel: selfModelPointer,
RelatedModel: relatedModelPointer,
RelationTypeName: RelationMorphByMany,
EloquentBuilder: b,
},
PivotTable: pivotTable,
PivotSelfColumn: pivotSelfColumn,
PivotRelatedIdColumn: pivotRelatedIdColumn,
SelfIdColumn: selfIdColumn,
RelatedIdColumn: relatedIdColumn,
PivotRelatedTypeColumn: pivotRelatedTypeColumn,
}
relatedModel := GetParsedModel(relatedModelPointer)
if len(relatedModelTypeColumnValue) > 0 {
relation.RelatedModelTypeColumnValue = relatedModelTypeColumnValue[0]
} else {
relation.RelatedModelTypeColumnValue = GetMorphMap(relatedModel.Name)
}
b.Join(relation.PivotTable, relation.PivotTable+"."+relation.PivotRelatedIdColumn, "=", relatedModel.Table+"."+relation.RelatedIdColumn)
b.Select(relatedModel.Table + "." + "*")
b.Select(fmt.Sprintf("%s.%s as %s%s", relation.PivotTable, relation.PivotRelatedIdColumn, OrmPivotAlias, relation.PivotRelatedIdColumn))
b.Select(fmt.Sprintf("%s.%s as %s%s", relation.PivotTable, relation.PivotSelfColumn, OrmPivotAlias, relation.PivotSelfColumn))
b.Select(fmt.Sprintf("%s.%s as %s%s", relation.PivotTable, relation.PivotRelatedTypeColumn, OrmPivotAlias, relation.PivotRelatedTypeColumn))
relation.AddConstraints()
return &relation
}