-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelationship.go
More file actions
52 lines (44 loc) · 1.24 KB
/
relationship.go
File metadata and controls
52 lines (44 loc) · 1.24 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
package gorm
import (
"fmt"
"reflect"
"strings"
)
// GetRelationshipIDs will return a list of FK values of a relationship, as well as fill the given list with
// empty structs of that object to the same size as the returned ID list
func (db *DatabaseConnection) GetRelationshipIDs(fkName string, fkValue int, relList interface{}) ([]int, error) {
l := reflect.ValueOf(relList).Elem()
t := l.Type().Elem()
pkname := db.getPrimaryKey(reflect.New(t.Elem()).Interface())
query := strings.Builder{}
query.WriteString("SELECT ")
query.WriteString(pkname)
query.WriteString(" FROM ")
query.WriteString(db.Schema)
query.WriteRune('.')
query.WriteString(t.Elem().Name())
query.WriteString("\nWHERE ")
query.WriteString(fkName)
query.WriteString(" = ")
query.WriteString(fmt.Sprintf("%d", fkValue))
query.WriteRune(';')
// return errors.New(query.String())
rows, err := db.connection.Query(query.String())
if err != nil {
return nil, err
}
ids := []int{}
for rows.Next() {
id := 0
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
v := reflect.New(t.Elem())
// if err := db.Populate(v.Interface().(DataObject), id); err != nil {
// return err
// }
l.Set(reflect.Append(l, v))
}
return ids, nil
}