forked from gobuffalo/pop
/
many_to_many_association.go
156 lines (130 loc) · 4.99 KB
/
many_to_many_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
package associations
import (
"fmt"
"reflect"
"time"
"github.com/gobuffalo/flect"
"github.com/gobuffalo/uuid"
)
type manyToManyAssociation struct {
fieldType reflect.Type
fieldValue reflect.Value
model reflect.Value
manyToManyTableName string
owner interface{}
fkID string
orderBy string
primaryID string
*associationSkipable
*associationComposite
}
func init() {
associationBuilders["many_to_many"] = func(p associationParams) (Association, error) {
// Validates if model.ID is nil, this association will be skipped.
var skipped bool
model := p.modelValue
if fieldIsNil(model.FieldByName("ID")) {
skipped = true
}
return &manyToManyAssociation{
fieldType: p.modelValue.FieldByName(p.field.Name).Type(),
fieldValue: p.modelValue.FieldByName(p.field.Name),
owner: p.model,
model: model,
manyToManyTableName: p.popTags.Find("many_to_many").Value,
fkID: p.popTags.Find("fk_id").Value,
orderBy: p.popTags.Find("order_by").Value,
primaryID: p.popTags.Find("primary_id").Value,
associationSkipable: &associationSkipable{
skipped: skipped,
},
associationComposite: &associationComposite{innerAssociations: p.innerAssociations},
}, nil
}
}
func (m *manyToManyAssociation) Kind() reflect.Kind {
return m.fieldType.Kind()
}
func (m *manyToManyAssociation) Interface() interface{} {
val := reflect.New(m.fieldType.Elem())
if m.fieldValue.Kind() == reflect.Ptr {
m.fieldValue.Set(val)
return m.fieldValue.Interface()
}
// This piece of code clears a slice in case it is filled with elements.
if m.fieldValue.Kind() == reflect.Slice || m.fieldValue.Kind() == reflect.Array {
valPointer := m.fieldValue.Addr()
valPointer.Elem().Set(reflect.MakeSlice(valPointer.Type().Elem(), 0, valPointer.Elem().Cap()))
return valPointer.Interface()
}
return m.fieldValue.Addr().Interface()
}
// Constraint returns the content for a where clause, and the args
// needed to execute it.
func (m *manyToManyAssociation) Constraint() (string, []interface{}) {
modelColumnID := fmt.Sprintf("%s%s", flect.Underscore(m.model.Type().Name()), "_id")
var columnFieldID string
i := reflect.Indirect(m.fieldValue)
if i.Kind() == reflect.Slice || i.Kind() == reflect.Array {
t := i.Type().Elem()
columnFieldID = fmt.Sprintf("%s%s", flect.Underscore(t.Name()), "_id")
} else {
columnFieldID = fmt.Sprintf("%s%s", flect.Underscore(i.Type().Name()), "_id")
}
if m.fkID != "" {
columnFieldID = m.fkID
}
if m.primaryID != "" {
modelColumnID = m.primaryID
}
subQuery := fmt.Sprintf("select %s from %s where %s = ?", columnFieldID, m.manyToManyTableName, modelColumnID)
modelIDValue := m.model.FieldByName("ID").Interface()
return fmt.Sprintf("id in (%s)", subQuery), []interface{}{modelIDValue}
}
func (m *manyToManyAssociation) OrderBy() string {
return m.orderBy
}
func (m *manyToManyAssociation) BeforeInterface() interface{} {
if m.fieldValue.Kind() == reflect.Ptr {
return m.fieldValue.Interface()
}
return m.fieldValue.Addr().Interface()
}
func (m *manyToManyAssociation) BeforeSetup() error {
return nil
}
func (m *manyToManyAssociation) Statements() []AssociationStatement {
var statements []AssociationStatement
modelColumnID := fmt.Sprintf("%s%s", flect.Underscore(m.model.Type().Name()), "_id")
var columnFieldID string
i := reflect.Indirect(m.fieldValue)
if i.Kind() == reflect.Slice || i.Kind() == reflect.Array {
t := i.Type().Elem()
columnFieldID = fmt.Sprintf("%s%s", flect.Underscore(t.Name()), "_id")
} else {
columnFieldID = fmt.Sprintf("%s%s", flect.Underscore(i.Type().Name()), "_id")
}
for i := 0; i < m.fieldValue.Len(); i++ {
v := m.fieldValue.Index(i)
manyIDValue := v.FieldByName("ID").Interface()
modelIDValue := m.model.FieldByName("ID").Interface()
stm := "INSERT INTO %s (%s,%s,%s,%s) SELECT ?,?,?,? WHERE NOT EXISTS (SELECT * FROM %s WHERE %s = ? AND %s = ?)"
if IsZeroOfUnderlyingType(manyIDValue) || IsZeroOfUnderlyingType(modelIDValue) {
continue
}
associationStm := AssociationStatement{
Statement: fmt.Sprintf(stm, m.manyToManyTableName, modelColumnID, columnFieldID, "created_at", "updated_at", m.manyToManyTableName, modelColumnID, columnFieldID),
Args: []interface{}{modelIDValue, manyIDValue, time.Now(), time.Now(), modelIDValue, manyIDValue},
}
if m.model.FieldByName("ID").Type().Name() == "UUID" {
stm = "INSERT INTO %s (%s,%s,%s,%s,%s) SELECT ?,?,?,?,? WHERE NOT EXISTS (SELECT * FROM %s WHERE %s = ? AND %s = ?)"
id, _ := uuid.NewV4()
associationStm = AssociationStatement{
Statement: fmt.Sprintf(stm, m.manyToManyTableName, "id", modelColumnID, columnFieldID, "created_at", "updated_at", m.manyToManyTableName, modelColumnID, columnFieldID),
Args: []interface{}{id, modelIDValue, manyIDValue, time.Now(), time.Now(), modelIDValue, manyIDValue},
}
}
statements = append(statements, associationStm)
}
return statements
}