relation.go 762 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package orm
  2. import (
  3. "sync"
  4. )
  5. // relType describes relation type
  6. type relType int8
  7. // Of these three
  8. const (
  9. hasOne relType = iota << 1
  10. hasMany relType = iota << 1
  11. belongsTo relType = iota << 1
  12. )
  13. // relation describes how tables are related
  14. type relation struct {
  15. *table
  16. f field
  17. on column
  18. key column
  19. }
  20. // relations holds all relation on types
  21. type relations struct {
  22. rmap map[relType][]relation
  23. m *sync.Mutex
  24. }
  25. // addRelation to collection. Requires fields to have equal type
  26. func (r relations) addRelation(rt relType, rel relation) {
  27. r.m.Lock()
  28. defer r.m.Unlock()
  29. if _, ok := r.rmap[rt]; !ok {
  30. r.rmap[rt] = make([]relation, 0)
  31. }
  32. if rel.on.getKind() != rel.key.getKind() {
  33. return
  34. }
  35. r.rmap[rt] = append(r.rmap[rt], rel)
  36. }