forked from mirror/gorm
Refactor Preload
This commit is contained in:
parent
7ef8b06cb4
commit
7693c093a9
30
preload.go
30
preload.go
|
@ -106,10 +106,9 @@ func (scope *Scope) handleHasOnePreload(field *Field, conditions []interface{})
|
||||||
results := makeSlice(field.Struct.Type)
|
results := makeSlice(field.Struct.Type)
|
||||||
relation := field.Relationship
|
relation := field.Relationship
|
||||||
condition := fmt.Sprintf("%v IN (?)", scope.Quote(relation.ForeignDBName))
|
condition := fmt.Sprintf("%v IN (?)", scope.Quote(relation.ForeignDBName))
|
||||||
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
|
||||||
|
|
||||||
// TODO: handle error?
|
scope.Err(scope.NewDB().Where(condition, primaryKeys).Find(results, conditions...).Error)
|
||||||
scope.NewDB().Where(condition, primaryKeys).Find(results, conditions...)
|
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
||||||
|
|
||||||
for i := 0; i < resultValues.Len(); i++ {
|
for i := 0; i < resultValues.Len(); i++ {
|
||||||
result := resultValues.Index(i)
|
result := resultValues.Index(i)
|
||||||
|
@ -123,8 +122,7 @@ func (scope *Scope) handleHasOnePreload(field *Field, conditions []interface{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := scope.SetColumn(field, result)
|
if err := scope.SetColumn(field, result); err != nil {
|
||||||
if err != nil {
|
|
||||||
scope.Err(err)
|
scope.Err(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -142,9 +140,9 @@ func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{})
|
||||||
results := makeSlice(field.Struct.Type)
|
results := makeSlice(field.Struct.Type)
|
||||||
relation := field.Relationship
|
relation := field.Relationship
|
||||||
condition := fmt.Sprintf("%v IN (?)", scope.Quote(relation.ForeignDBName))
|
condition := fmt.Sprintf("%v IN (?)", scope.Quote(relation.ForeignDBName))
|
||||||
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
|
||||||
|
|
||||||
scope.NewDB().Where(condition, primaryKeys).Find(results, conditions...)
|
scope.Err(scope.NewDB().Where(condition, primaryKeys).Find(results, conditions...).Error)
|
||||||
|
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
||||||
|
|
||||||
if scope.IndirectValue().Kind() == reflect.Slice {
|
if scope.IndirectValue().Kind() == reflect.Slice {
|
||||||
for i := 0; i < resultValues.Len(); i++ {
|
for i := 0; i < resultValues.Len(); i++ {
|
||||||
|
@ -173,10 +171,10 @@ func (scope *Scope) handleBelongsToPreload(field *Field, conditions []interface{
|
||||||
}
|
}
|
||||||
|
|
||||||
results := makeSlice(field.Struct.Type)
|
results := makeSlice(field.Struct.Type)
|
||||||
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
|
||||||
associationPrimaryKey := scope.New(results).PrimaryField().Name
|
associationPrimaryKey := scope.New(results).PrimaryField().Name
|
||||||
|
|
||||||
scope.NewDB().Where(primaryKeys).Find(results, conditions...)
|
scope.Err(scope.NewDB().Where(primaryKeys).Find(results, conditions...).Error)
|
||||||
|
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
||||||
|
|
||||||
for i := 0; i < resultValues.Len(); i++ {
|
for i := 0; i < resultValues.Len(); i++ {
|
||||||
result := resultValues.Index(i)
|
result := resultValues.Index(i)
|
||||||
|
@ -212,16 +210,16 @@ func (scope *Scope) getColumnsAsScope(column string) *Scope {
|
||||||
values := scope.IndirectValue()
|
values := scope.IndirectValue()
|
||||||
switch values.Kind() {
|
switch values.Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
model := values.Type().Elem()
|
modelType := values.Type().Elem()
|
||||||
if model.Kind() == reflect.Ptr {
|
if modelType.Kind() == reflect.Ptr {
|
||||||
model = model.Elem()
|
modelType = modelType.Elem()
|
||||||
}
|
}
|
||||||
fieldType, _ := model.FieldByName(column)
|
fieldStruct, _ := modelType.FieldByName(column)
|
||||||
var columns reflect.Value
|
var columns reflect.Value
|
||||||
if fieldType.Type.Kind() == reflect.Slice {
|
if fieldStruct.Type.Kind() == reflect.Slice {
|
||||||
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldType.Type.Elem()))).Elem()
|
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type.Elem()))).Elem()
|
||||||
} else {
|
} else {
|
||||||
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldType.Type))).Elem()
|
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type))).Elem()
|
||||||
}
|
}
|
||||||
for i := 0; i < values.Len(); i++ {
|
for i := 0; i < values.Len(); i++ {
|
||||||
column := reflect.Indirect(values.Index(i)).FieldByName(column)
|
column := reflect.Indirect(values.Index(i)).FieldByName(column)
|
||||||
|
|
|
@ -156,7 +156,6 @@ func TestNestedPreload2(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
want := Level3{
|
want := Level3{
|
||||||
Name: "name",
|
|
||||||
Level2s: []Level2{
|
Level2s: []Level2{
|
||||||
{
|
{
|
||||||
Level1s: []*Level1{
|
Level1s: []*Level1{
|
||||||
|
@ -211,7 +210,6 @@ func TestNestedPreload3(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
want := Level3{
|
want := Level3{
|
||||||
Name: "name",
|
|
||||||
Level2s: []Level2{
|
Level2s: []Level2{
|
||||||
{Level1: Level1{Value: "value1"}},
|
{Level1: Level1{Value: "value1"}},
|
||||||
{Level1: Level1{Value: "value2"}},
|
{Level1: Level1{Value: "value2"}},
|
||||||
|
@ -368,6 +366,7 @@ func TestNestedPreload6(t *testing.T) {
|
||||||
if err := DB.Create(&want[0]).Error; err != nil {
|
if err := DB.Create(&want[0]).Error; err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
want[1] = Level3{
|
want[1] = Level3{
|
||||||
Level2s: []Level2{
|
Level2s: []Level2{
|
||||||
{
|
{
|
||||||
|
@ -432,6 +431,7 @@ func TestNestedPreload7(t *testing.T) {
|
||||||
if err := DB.Create(&want[0]).Error; err != nil {
|
if err := DB.Create(&want[0]).Error; err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
want[1] = Level3{
|
want[1] = Level3{
|
||||||
Level2s: []Level2{
|
Level2s: []Level2{
|
||||||
{Level1: Level1{Value: "value3"}},
|
{Level1: Level1{Value: "value3"}},
|
||||||
|
|
Loading…
Reference in New Issue