This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
/
relation.go
102 lines (88 loc) · 2.88 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
package accountancy
import (
"time"
"github.com/google/uuid"
)
// Relation -
type Relation struct {
ID int64 `json:"-"`
UUID string `xorm:"varchar(36) notnull unique" json:"uuid"`
Name string `xorm:"varchar(1024) notnull unique" json:"name"`
Props map[string]interface{} `xorm:"jsonb" json:"props"`
Status int `xorm:"notnull index" json:"-"`
Created time.Time `xorm:"created" json:"-"`
Updated time.Time `xorm:"updated" json:"-"`
}
// RelationTraits -
type RelationTraits struct {
ID int64 `json:"-"`
RelationID int64 `xorm:"notnull unique(relation_traits)" json:"-"`
RelationName string `xorm:"-" json:"relation"`
TraitFromID int64 `xorm:"notnull unique(relation_traits)" json:"-"`
TraitFromName string `xorm:"-" json:"trait-from"`
TraitToID int64 `xorm:"notnull unique(relation_traits)" json:"-"`
TraitToName string `xorm:"-" json:"trait-to"`
Props map[string]interface{} `xorm:"jsonb" json:"props"`
Lib string `xorm:"text" json:"lib"`
LibContent string `xorm:"-" json:"-"`
Status int `xorm:"notnull index" json:"-"`
Created time.Time `xorm:"created" json:"-"`
Updated time.Time `xorm:"updated" json:"-"`
}
// RelationTraitsKey -
type RelationTraitsKey struct {
RelationName string
TraitFromName string
TraitToName string
}
// NewRelation -
func NewRelation(name string, props map[string]interface{}, lib string) (relation *Relation, err error) {
relation = &Relation{
UUID: uuid.New().String(),
Name: name,
Props: props,
}
return
}
// Insert -
func (relation *Relation) Insert(db DB) (affected int64, err error) {
affected, err = db.Insert(relation)
return
}
// Get -
func (relation *Relation) Get(db DB) (has bool, err error) {
has, err = db.Get(relation)
return
}
// Update -
func (relation *Relation) Update(db DB) (affected int64, err error) {
affected, err = db.ID(relation.ID).Update(relation)
return
}
// InsertOrUpdate -
func (relation *Relation) InsertOrUpdate(db DB, find *Relation) (affected int64, inserted bool, err error) {
ok, err := find.Get(db)
if err != nil {
return
}
if ok {
relation.ID = find.ID
_, err = relation.Update(db)
if err != nil {
return
}
inserted = false
} else {
_, err = relation.Insert(db)
if err != nil {
return
}
inserted = true
}
return
}
// AddRelationTraits -
func (relation *Relation) AddRelationTraits(db DB, traitFrom *Trait, traitTo *Trait, props map[string]interface{}, lib string) (affected int64, inserted bool, err error) {
affected, inserted, err = relationTraitsInsertOrUpdate(db, relation, traitFrom, traitTo, props, lib)
return
}