-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnest.go
42 lines (36 loc) · 861 Bytes
/
nest.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
package main
import (
"database/sql"
"fmt"
)
func queryNest(db *sql.DB, nest *Nest, list []map[string]interface{}) error {
keys := distinct(take(list, nest.ParentKey))
query := fmt.Sprintf(nest.fetch, csv(keys))
rows, err := db.Query(query)
if err != nil {
return err
}
defer rows.Close()
children, err := getAllRows(rows, args.nulls)
if err != nil {
return err
}
applyTimezoneAll(nest.location, children)
for _, parent := range list {
slice := make([]interface{}, 0)
for _, child := range children {
if val, ok := child["_parent"]; ok {
if toUint64(val) == toUint64(parent[nest.ParentKey]) {
delete(child, "_parent")
if _, ok := child["_value"]; ok {
slice = append(slice, child["_value"])
} else {
slice = append(slice, child)
}
}
}
}
parent[nest.ChildKey] = slice
}
return nil
}