forked from geoffreybauduin/yaorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subqueryload.go
101 lines (90 loc) · 2.6 KB
/
subqueryload.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
package yaorm
import (
"fmt"
"reflect"
"github.com/geoffreybauduin/yaorm/tools"
)
type SubqueryloadFunc func(dbp DBProvider, ids []interface{}) (interface{}, error)
type subqueryloader struct {
// fn to retrieve the data
fn SubqueryloadFunc
// field on loaded model from which to retrieve the associated fk from original model
mapperField string
}
var (
subqueryloaders = map[string]subqueryloader{}
)
func subqueryload(dbp DBProvider, m map[string]map[interface{}][]reflect.Value) (map[string][]Model, error) {
models := map[string][]Model{}
for model, data := range m {
modelData, err := subqueryloadForModel(dbp, model, data)
if err != nil {
return models, err
}
models[model] = modelData
}
return models, nil
}
// avoid hammering the database and selecting more than x rows at a time ?
// pg max is 65536
const loadStep = 1000
func subqueryloadForModel(dbp DBProvider, model string, data map[interface{}][]reflect.Value) ([]Model, error) {
d := []Model{}
if _, ok := subqueryloaders[model]; !ok {
return d, fmt.Errorf("Subqueryload model %s not defined yet", model)
}
ids := []interface{}{}
for id := range data {
ids = append(ids, id)
}
for factor := 0; factor*loadStep < len(ids); factor++ {
var subset []interface{}
currentStep := factor * loadStep
nextStep := (factor + 1) * loadStep
if nextStep < len(ids) {
subset = ids[currentStep:nextStep]
} else {
subset = ids[currentStep:]
}
models, err := subqueryloaders[model].fn(dbp, subset)
if err != nil {
return d, err
}
modelsSlice := reflect.ValueOf(models)
switch kind := modelsSlice.Kind(); kind {
case reflect.Array, reflect.Slice:
// only these kinds are allowed
default:
return nil, fmt.Errorf("a subquery returned a non-array and non-slice type '%s', which is not handled", kind)
}
for i := 0; i < modelsSlice.Len(); i++ {
m := modelsSlice.Index(i)
table, err := GetTableByModel(m.Interface().(Model))
if err != nil {
return nil, err
}
field := tools.GetNonPtrValue(m.Interface()).Field(table.FieldIndex(subqueryloaders[model].mapperField))
fk := reflect.Indirect(field).Interface()
for _, v := range data[fk] {
setOnReceiver(v, m.Interface())
}
d = append(d, m.Interface().(Model))
}
}
return d, nil
}
func setOnReceiver(v reflect.Value, value interface{}) {
ind := reflect.Indirect(v)
valueToAdd := reflect.ValueOf(value)
switch ind.Kind() {
case reflect.Slice:
newVal := reflect.Append(ind, valueToAdd)
ind.Set(newVal)
break
case reflect.Ptr:
ind.Set(valueToAdd)
break
default:
panic(fmt.Errorf("Wow, why do you have %+v receiver ?", ind.Kind()))
}
}