-
Notifications
You must be signed in to change notification settings - Fork 0
/
association.go
509 lines (434 loc) · 13.5 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package activerecord
import (
"fmt"
"sort"
"strings"
. "github.com/activegraph/activegraph/activesupport"
)
type ErrAssociation struct {
Message string
}
func (e ErrAssociation) Error() string {
return e.Message
}
type ErrUnknownAssociation struct {
RecordName string
Assoc string
}
func (e ErrUnknownAssociation) Error() string {
return fmt.Sprintf("unknown association %q for %s", e.Assoc, e.RecordName)
}
type Association interface {
// AssociationOwner() *Relation
AssociationName() string
AssociationForeignKey() string
}
type SingularAssociation interface {
Association
AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult
AccessAssociation(owner *ActiveRecord) RecordResult
}
type CollectionAssociation interface {
Association
AssignCollection(owner *ActiveRecord, targets ...*ActiveRecord) RecordResult
AccessCollection(owner *ActiveRecord) CollectionResult
}
type AssociationMethods interface {
AssociationNames() []string
HasAssociation(assocName string) bool
HasAssociations(assocNames ...string) bool
ReflectOnAssociation(assocName string) *AssociationReflection
ReflectOnAllAssociations() []*AssociationReflection
// AssociationForInspect(assocName string) Association
// AssociationsForInspect(assocNames ...string) []Association
}
type AssociationAccessors interface {
// AssignAssociation(string, assoc *ActiveRecord) error
Association(assocName string) RecordResult
AccessAssociation(assocName string) (*ActiveRecord, error)
}
type CollectionAccessors interface {
// AssignCollection(collName string, coll []*ActiveRecord) error
Collection(collName string) CollectionResult
AccessCollection(collName string) (*Relation, error)
}
type AssociationReflection struct {
*Relation
Association
}
type BelongsTo struct {
owner *Relation
reflection *Reflection
targetName string
foreignKey string
}
func (a *BelongsTo) AssociationOwner() *Relation {
return a.owner
}
func (a *BelongsTo) AssociationName() string {
return a.targetName
}
// ForeignKey sets the foreign key used for the association. By default this is
// guessed to be the name of this relation in lower-case and "_id" suffixed.
//
// So a relation that defines a BelongsTo("person") association will use "person_id"
// as a default foreign key.
func (a *BelongsTo) ForeignKey(fk string) {
a.foreignKey = fk
}
func (a *BelongsTo) AssociationForeignKey() string {
if a.foreignKey != "" {
return a.foreignKey
}
// target_id
return a.targetName + "_" + defaultPrimaryKeyName
}
// AccessAssociation returns a record of the target.
//
// activerecord.New("owner", func(r *activerecord.R) {
// r.BelongsTo("target")
// })
//
// This association considers the following tables relation:
//
// +------------------------+ +----------------+
// | owners | | targets |
// +------------+-----------+ +------+---------+
// | id | integer | +-->| id | integer | pk
// | target_id | string |*---+ | name | string |
// | updated_at | timestamp | +------+---------+
// +------------+-----------+
//
func (a *BelongsTo) AccessAssociation(owner *ActiveRecord) RecordResult {
// Find target association relation given it's name.
targets, err := a.reflection.Reflection(a.targetName)
if err != nil {
return ErrRecord(err)
}
targetId := owner.Attribute(a.AssociationForeignKey())
return targets.WithContext(owner.Context()).Find(targetId)
}
func (a *BelongsTo) AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult {
err := owner.AssignAttribute(a.AssociationForeignKey(), target.ID())
if err != nil {
return ErrRecord(err)
}
return ReturnRecord(owner.Update())
}
func (a *BelongsTo) String() string {
return fmt.Sprintf("#<Association type: 'belongs_to', name: '%s'>", a.targetName)
}
type HasMany struct {
owner *Relation
reflection *Reflection
targetName string
foreignKey string
}
func (a *HasMany) AssociationName() string {
return a.targetName
}
func (a *HasMany) AssociationForeignKey() string {
// TODO: this is completely wrong.
if a.foreignKey != "" {
return a.foreignKey
}
return strings.ToLower(a.owner.Name()) + "_" + defaultPrimaryKeyName
}
// AccessCollection returns a collection of the target records.
//
// HasMany association indicates a one-to-many association with another model. The
// association indicates that each instance of the model has zero or more instances
// of target model.
//
// activerecord.New("owner", func(r *activerecord.R) {
// r.HasMany("targets")
// })
//
// This association considers the following tables relation:
//
// +----------------+ +--------------------+
// | owners | | targets |
// +------+---------+ +----------+---------+
// | id | integer |<---+ | id | integer |
// | name | string | +---*| owner_id | integer |
// +------+---------+ | name | string |
// +----------+---------+
//
func (a *HasMany) AccessCollection(owner *ActiveRecord) CollectionResult {
targets, err := a.reflection.Reflection(a.targetName)
if err != nil {
return CollectionResult{Err[*Relation](err)}
}
targets = targets.WithContext(owner.Context())
// TODO: Make "scope" accessable and understandable.
targets = targets.Where(a.AssociationForeignKey(), owner.ID())
return CollectionResult{Ok(targets)}
}
func (a *HasMany) AssignCollection(owner *ActiveRecord, targets ...*ActiveRecord) RecordResult {
// Perform very naive approach delete existing targets and set new targets.
err := owner.Collection(a.targetName + "s").DeleteAll()
if err != nil {
return ErrRecord(err)
}
for i := 0; i < len(targets); i++ {
// TODO: Ensure each target record is an instance of the association's owner.
// Put a reference of the owner (owner_id) to the target record.
err = targets[i].AssignAttribute(a.AssociationForeignKey(), owner.ID())
if err != nil {
return ErrRecord(err)
}
_, err = targets[i].WithContext(owner.Context()).Insert()
if err != nil {
return ErrRecord(err)
}
}
return OkRecord(owner)
}
func (a *HasMany) String() string {
return fmt.Sprintf("#<Association type: 'has_many', name: '%s'>", a.targetName)
}
type HasOne struct {
owner *Relation
reflection *Reflection
targetName string
foreignKey string
}
func (a *HasOne) AssociationOwner() *Relation {
return a.owner
}
func (a *HasOne) AssociationName() string {
return a.targetName
}
func (a *HasOne) AssociationForeignKey() string {
return a.owner.Name() + "_" + defaultPrimaryKeyName
}
// The association indicates that one model has a reference to this model.
// That "target" model can be fetched through this association.
//
// activerecord.New("owner", func(r *activerecord.R) {
// r.HasOne("target")
// })
//
// This association considers the following tables relation:
//
// +----------------+ +--------------------+
// | owners | | targets |
// +------+---------+ +----------+---------+
// | id | integer |<---+ | id | integer |
// | name | string | +---*| owner_id | integer |
// +------+---------+ | name | string |
// +----------+---------+
//
func (a *HasOne) AccessAssociation(owner *ActiveRecord) RecordResult {
// Find target association relation given it's name.
targets, err := a.reflection.Reflection(a.targetName)
if err != nil {
return ErrRecord(err)
}
targets = targets.WithContext(owner.Context())
targets = targets.Where(a.AssociationForeignKey(), owner.ID())
records, err := targets.Limit(2).ToA()
if err != nil {
return ErrRecord(err)
}
switch len(records) {
case 0:
return OkRecord(nil)
case 1:
return OkRecord(records[0])
default:
return ErrRecord(ErrAssociation{
fmt.Sprintf("declared 'has_one' association, but has many: %s", records),
})
}
}
func (a *HasOne) AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult {
targets, err := a.reflection.Reflection(a.targetName)
if err != nil {
return ErrRecord(err)
}
if target.Name() != targets.Name() {
const format = "cannot assign '%q' to '%q' as %s association in '%q'"
return ErrRecord(fmt.Errorf(
format, target.Name(), targets.Name(), a.targetName, owner.Name(),
))
}
// Put a reference of the owner (owner_id) to the target record.
err = target.AssignAttribute(a.AssociationForeignKey(), owner.ID())
if err != nil {
return ErrRecord(err)
}
_, err = target.WithContext(owner.Context()).Insert()
if err != nil {
return ErrRecord(err)
}
// Update value of the target for owner, so new calls to access the
// target won't generate SQL queries to the database.
// TODO: Cache target association in "associations" type for further access.
//owner.associations.set(target.Name(), target)
// TODO: if the new target repaces existing one, what to do with the existing?
// Return an owner, which is not modified after the target insertion.
return OkRecord(owner)
}
func (a *HasOne) String() string {
return fmt.Sprintf("#<Assocation type: 'has_one', name: '%s'>", a.targetName)
}
type associationsMap map[string]Association
func (m associationsMap) copy() associationsMap {
mm := make(associationsMap, len(m))
for name, assoc := range m {
mm[name] = assoc
}
return mm
}
type associations struct {
recordName string
rec *ActiveRecord
reflection *Reflection
keys associationsMap
values map[string]*ActiveRecord
}
func newAssociations(
recordName string, assocs associationsMap, reflection *Reflection,
) *associations {
return &associations{
recordName: recordName,
reflection: reflection,
keys: assocs,
values: make(map[string]*ActiveRecord),
}
}
func (a *associations) delegateAccessors(rec *ActiveRecord) *associations {
a.rec = rec
return a
}
func (a *associations) copy() *associations {
values := make(map[string]*ActiveRecord, len(a.values))
for k, v := range a.values {
values[k] = v
}
return &associations{
recordName: a.recordName,
reflection: a.reflection,
keys: a.keys.copy(),
values: values,
}
}
func (a *associations) HasAssociation(assocName string) bool {
_, ok := a.keys[assocName]
return ok
}
func (a *associations) HasAssociations(assocNames ...string) bool {
for _, assocName := range assocNames {
if !a.HasAssociation(assocName) {
return false
}
}
return true
}
func (a *associations) find(assocName string) (Association, error) {
if !a.HasAssociation(assocName) {
return nil, ErrUnknownAssociation{RecordName: a.rec.Name(), Assoc: assocName}
}
return a.keys[assocName], nil
}
func (a *associations) findSingular(assocName string) (SingularAssociation, error) {
assoc, err := a.find(assocName)
if err != nil {
return nil, err
}
sa, ok := assoc.(SingularAssociation)
if !ok {
message := fmt.Sprintf("'%s' is not a singular association", assocName)
return nil, ErrAssociation{Message: message}
}
return sa, nil
}
func (a *associations) findCollection(assocName string) (CollectionAssociation, error) {
assoc, err := a.find(assocName)
if err != nil {
return nil, err
}
ca, ok := assoc.(CollectionAssociation)
if !ok {
message := fmt.Sprintf("'%s' is not a collection association", assocName)
return nil, ErrAssociation{Message: message}
}
return ca, nil
}
func (a *associations) set(assocName string, rec *ActiveRecord) {
if a.HasAssociation(assocName) {
a.values[assocName] = rec
}
}
// ReflectOnAssociation returns AssociationReflection for the specified association.
func (a *associations) ReflectOnAssociation(assocName string) *AssociationReflection {
if !a.HasAssociation(assocName) {
return nil
}
rel, err := a.reflection.Reflection(a.keys[assocName].AssociationName())
if err != nil {
return nil
}
return &AssociationReflection{Relation: rel, Association: a.keys[assocName]}
}
// ReflectOnAllAssociations returns an array of AssociationReflection types for all
// associations in the Relation.
func (a *associations) ReflectOnAllAssociations() []*AssociationReflection {
arefs := make([]*AssociationReflection, 0, len(a.keys))
for _, assoc := range a.keys {
rel, _ := a.reflection.Reflection(assoc.AssociationName())
if rel == nil {
continue
}
arefs = append(arefs, &AssociationReflection{Relation: rel, Association: assoc})
}
return arefs
}
func (a *associations) AssociationNames() []string {
names := make([]string, 0, len(a.keys))
for name := range a.keys {
names = append(names, name)
}
sort.StringSlice(names).Sort()
return names
}
func (a *associations) Association(assocName string) RecordResult {
sa, err := a.findSingular(assocName)
if err != nil {
return ErrRecord(err)
}
if rec, ok := a.values[assocName]; ok {
return OkRecord(rec)
}
return sa.AccessAssociation(a.rec)
}
func (a *associations) AccessAssociation(assocName string) (*ActiveRecord, error) {
assoc := a.Association(assocName)
return assoc.Ok().UnwrapOr(nil), assoc.Err()
}
func (a *associations) AssignAssociation(assocName string, target *ActiveRecord) error {
sa, err := a.findSingular(assocName)
if err != nil {
return err
}
return sa.AssignAssociation(a.rec, target).Err()
}
func (a *associations) Collection(collName string) CollectionResult {
ca, err := a.findCollection(collName)
if err != nil {
return ErrCollection(err)
}
return CollectionResult{ca.AccessCollection(a.rec)}
}
func (a *associations) AccessCollection(collName string) (*Relation, error) {
collection := a.Collection(collName)
return collection.Ok().UnwrapOr(nil), collection.Err()
}
func (a *associations) AssignCollection(collName string, targets ...*ActiveRecord) error {
ca, err := a.findCollection(collName)
if err != nil {
return err
}
return ca.AssignCollection(a.rec, targets...).Err()
}