model_table_many.go
2.3 KB
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
package orm
import (
"context"
"fmt"
"reflect"
)
type manyModel struct {
*sliceTableModel
baseTable *Table
rel *Relation
buf []byte
dstValues map[string][]reflect.Value
}
var _ TableModel = (*manyModel)(nil)
func newManyModel(j *join) *manyModel {
baseTable := j.BaseModel.Table()
joinModel := j.JoinModel.(*sliceTableModel)
dstValues := dstValues(joinModel, j.Rel.FKValues)
if len(dstValues) == 0 {
return nil
}
m := manyModel{
sliceTableModel: joinModel,
baseTable: baseTable,
rel: j.Rel,
dstValues: dstValues,
}
if !m.sliceOfPtr {
m.strct = reflect.New(m.table.Type).Elem()
}
return &m
}
func (m *manyModel) NewModel() ColumnScanner {
if m.sliceOfPtr {
m.strct = reflect.New(m.table.Type).Elem()
} else {
m.strct.Set(m.table.zeroStruct)
}
m.structInited = false
m.structTableModel.NewModel()
return m
}
func (m *manyModel) AddModel(model ColumnScanner) error {
m.buf = modelId(m.buf[:0], m.strct, m.rel.FKs)
dstValues, ok := m.dstValues[string(m.buf)]
if !ok {
return fmt.Errorf(
"pg: relation=%q has no base model=%q with id=%q (check join conditions)",
m.rel.Field.GoName, m.baseTable.TypeName, m.buf)
}
for _, v := range dstValues {
if m.sliceOfPtr {
v.Set(reflect.Append(v, m.strct.Addr()))
} else {
v.Set(reflect.Append(v, m.strct))
}
}
return nil
}
func (m *manyModel) AfterQuery(c context.Context, db DB) error {
if m.rel.JoinTable.HasFlag(AfterQueryHookFlag) {
var firstErrr error
for _, slices := range m.dstValues {
for _, slice := range slices {
err := callAfterQueryHookSlice(slice, m.sliceOfPtr, c, db)
if err != nil && firstErrr == nil {
firstErrr = err
}
}
}
return firstErrr
}
return nil
}
func (m *manyModel) AfterSelect(c context.Context, db DB) error {
return nil
}
func (m *manyModel) BeforeInsert(c context.Context, db DB) error {
return nil
}
func (m *manyModel) AfterInsert(c context.Context, db DB) error {
return nil
}
func (m *manyModel) BeforeUpdate(c context.Context, db DB) error {
return nil
}
func (m *manyModel) AfterUpdate(c context.Context, db DB) error {
return nil
}
func (m *manyModel) BeforeDelete(c context.Context, db DB) error {
return nil
}
func (m *manyModel) AfterDelete(c context.Context, db DB) error {
return nil
}