2015-02-11 08:43:53 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
2015-02-11 10:37:04 +03:00
|
|
|
"database/sql/driver"
|
2015-02-11 08:43:53 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
func getRealValue(value reflect.Value, field string) interface{} {
|
2015-02-11 12:58:19 +03:00
|
|
|
result := reflect.Indirect(value).FieldByName(field).Interface()
|
2015-02-11 10:37:04 +03:00
|
|
|
if r, ok := result.(driver.Valuer); ok {
|
|
|
|
result, _ = r.Value()
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func equalAsString(a interface{}, b interface{}) bool {
|
|
|
|
return fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b)
|
|
|
|
}
|
|
|
|
|
2015-02-11 08:43:53 +03:00
|
|
|
func Preload(scope *Scope) {
|
2015-03-12 08:52:29 +03:00
|
|
|
if scope.Search.preload != nil {
|
2015-04-16 13:39:30 +03:00
|
|
|
fields := scope.Fields()
|
|
|
|
isSlice := scope.IndirectValue().Kind() == reflect.Slice
|
|
|
|
|
2015-03-12 08:52:29 +03:00
|
|
|
for key, conditions := range scope.Search.preload {
|
2015-02-11 08:43:53 +03:00
|
|
|
for _, field := range fields {
|
|
|
|
if field.Name == key && field.Relationship != nil {
|
2015-02-17 17:55:14 +03:00
|
|
|
results := makeSlice(field.Struct.Type)
|
2015-02-11 08:43:53 +03:00
|
|
|
relation := field.Relationship
|
2015-03-11 06:28:30 +03:00
|
|
|
primaryName := scope.PrimaryField().Name
|
|
|
|
associationPrimaryKey := scope.New(results).PrimaryField().Name
|
2015-02-11 08:43:53 +03:00
|
|
|
|
|
|
|
switch relation.Kind {
|
|
|
|
case "has_one":
|
2015-03-02 05:52:18 +03:00
|
|
|
if primaryKeys := scope.getColumnAsArray(primaryName); len(primaryKeys) > 0 {
|
|
|
|
condition := fmt.Sprintf("%v IN (?)", scope.Quote(relation.ForeignDBName))
|
|
|
|
scope.NewDB().Where(condition, primaryKeys).Find(results, conditions...)
|
2015-02-11 10:37:04 +03:00
|
|
|
|
2015-03-02 05:52:18 +03:00
|
|
|
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
|
|
|
for i := 0; i < resultValues.Len(); i++ {
|
|
|
|
result := resultValues.Index(i)
|
|
|
|
if isSlice {
|
|
|
|
value := getRealValue(result, relation.ForeignFieldName)
|
|
|
|
objects := scope.IndirectValue()
|
|
|
|
for j := 0; j < objects.Len(); j++ {
|
|
|
|
if equalAsString(getRealValue(objects.Index(j), primaryName), value) {
|
|
|
|
reflect.Indirect(objects.Index(j)).FieldByName(field.Name).Set(result)
|
|
|
|
break
|
|
|
|
}
|
2015-02-11 10:37:04 +03:00
|
|
|
}
|
2015-03-02 05:52:18 +03:00
|
|
|
} else {
|
|
|
|
scope.SetColumn(field, result)
|
2015-02-11 10:37:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 08:43:53 +03:00
|
|
|
case "has_many":
|
2015-03-02 05:52:18 +03:00
|
|
|
if primaryKeys := scope.getColumnAsArray(primaryName); len(primaryKeys) > 0 {
|
|
|
|
condition := fmt.Sprintf("%v IN (?)", scope.Quote(relation.ForeignDBName))
|
|
|
|
scope.NewDB().Where(condition, primaryKeys).Find(results, conditions...)
|
|
|
|
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
|
|
|
if isSlice {
|
|
|
|
for i := 0; i < resultValues.Len(); i++ {
|
|
|
|
result := resultValues.Index(i)
|
|
|
|
value := getRealValue(result, relation.ForeignFieldName)
|
|
|
|
objects := scope.IndirectValue()
|
|
|
|
for j := 0; j < objects.Len(); j++ {
|
|
|
|
object := reflect.Indirect(objects.Index(j))
|
|
|
|
if equalAsString(getRealValue(object, primaryName), value) {
|
|
|
|
f := object.FieldByName(field.Name)
|
|
|
|
f.Set(reflect.Append(f, result))
|
|
|
|
break
|
|
|
|
}
|
2015-02-11 10:37:04 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-02 05:52:18 +03:00
|
|
|
} else {
|
|
|
|
scope.SetColumn(field, resultValues)
|
2015-02-11 10:37:04 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-11 08:43:53 +03:00
|
|
|
case "belongs_to":
|
2015-03-02 05:52:18 +03:00
|
|
|
if primaryKeys := scope.getColumnAsArray(relation.ForeignFieldName); len(primaryKeys) > 0 {
|
|
|
|
scope.NewDB().Where(primaryKeys).Find(results, conditions...)
|
|
|
|
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
|
|
|
for i := 0; i < resultValues.Len(); i++ {
|
|
|
|
result := resultValues.Index(i)
|
|
|
|
if isSlice {
|
|
|
|
value := getRealValue(result, associationPrimaryKey)
|
|
|
|
objects := scope.IndirectValue()
|
|
|
|
for j := 0; j < objects.Len(); j++ {
|
|
|
|
object := reflect.Indirect(objects.Index(j))
|
|
|
|
if equalAsString(getRealValue(object, relation.ForeignFieldName), value) {
|
|
|
|
object.FieldByName(field.Name).Set(result)
|
|
|
|
}
|
2015-02-11 10:37:04 +03:00
|
|
|
}
|
2015-03-02 05:52:18 +03:00
|
|
|
} else {
|
|
|
|
scope.SetColumn(field, result)
|
2015-02-11 10:37:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 08:43:53 +03:00
|
|
|
case "many_to_many":
|
|
|
|
scope.Err(errors.New("not supported relation"))
|
|
|
|
default:
|
|
|
|
scope.Err(errors.New("not supported relation"))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
func makeSlice(typ reflect.Type) interface{} {
|
|
|
|
if typ.Kind() == reflect.Slice {
|
2015-02-11 08:43:53 +03:00
|
|
|
typ = typ.Elem()
|
|
|
|
}
|
|
|
|
sliceType := reflect.SliceOf(typ)
|
|
|
|
slice := reflect.New(sliceType)
|
|
|
|
slice.Elem().Set(reflect.MakeSlice(sliceType, 0, 0))
|
|
|
|
return slice.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) getColumnAsArray(column string) (primaryKeys []interface{}) {
|
|
|
|
values := scope.IndirectValue()
|
|
|
|
switch values.Kind() {
|
|
|
|
case reflect.Slice:
|
2015-02-24 10:50:02 +03:00
|
|
|
primaryKeyMap := map[interface{}]bool{}
|
2015-02-11 08:43:53 +03:00
|
|
|
for i := 0; i < values.Len(); i++ {
|
2015-02-24 10:50:02 +03:00
|
|
|
primaryKeyMap[reflect.Indirect(values.Index(i)).FieldByName(column).Interface()] = true
|
|
|
|
}
|
|
|
|
for key := range primaryKeyMap {
|
|
|
|
primaryKeys = append(primaryKeys, key)
|
2015-02-11 08:43:53 +03:00
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
return []interface{}{values.FieldByName(column).Interface()}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|