-
Notifications
You must be signed in to change notification settings - Fork 0
/
fk.go
55 lines (47 loc) · 943 Bytes
/
fk.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
package dblock
import (
"git.eaciitapp.com/sebar/dbflex"
"github.com/eaciit/toolkit"
)
type FKField struct {
Field string
FieldRef string
}
type FK struct {
Table1 string
Table2 string
Fields []FKField
}
func NewFK(name1, name2 string, fields ...FKField) *FK {
fk := new(FK)
fk.Table1 = name1
fk.Table2 = name2
fk.Fields = fields
return fk
}
func (fk *FK) WhereT1(data toolkit.M) *dbflex.Filter {
ws := []*dbflex.Filter{}
for _, f := range fk.Fields {
ws = append(ws, dbflex.Eq(f.Field, data.Get(f.FieldRef)))
}
if len(ws) == 1 {
return ws[0]
} else if len(ws) > 1 {
return dbflex.And(ws...)
} else {
return nil
}
}
func (fk *FK) WhereT2(data toolkit.M) *dbflex.Filter {
ws := []*dbflex.Filter{}
for _, f := range fk.Fields {
ws = append(ws, dbflex.Eq(f.FieldRef, data.Get(f.Field)))
}
if len(ws) == 1 {
return ws[0]
} else if len(ws) > 1 {
return dbflex.And(ws...)
} else {
return nil
}
}