E92F Remove many to many TX condition for EagerPreload by larrymjordan · Pull Request #645 · gobuffalo/pop · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove many to many TX condition for EagerPreload #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 55 additions & 50 deletions preload_associations.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,72 +437,77 @@ func preloadManyToMany(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMeta
manyToManyTableName = strings.TrimSpace(manyToManyTableName[:strings.Index(manyToManyTableName, ":")])
}

if tx.TX != nil {
sql := fmt.Sprintf("SELECT %s, %s FROM %s WHERE %s in (?)", modelAssociationName, assocFkName, manyToManyTableName, modelAssociationName)
sql, args, _ := sqlx.In(sql, ids)
sql = tx.Dialect.TranslateSQL(sql)
log(logging.SQL, sql, args...)
rows, err := tx.TX.Queryx(sql, args...)
sql := fmt.Sprintf("SELECT %s, %s FROM %s WHERE %s in (?)", modelAssociationName, assocFkName, manyToManyTableName, modelAssociationName)
sql, args, _ := sqlx.In(sql, ids)
sql = tx.Dialect.TranslateSQL(sql)
log(logging.SQL, sql, args...)

cn, err := tx.Store.Transaction()
if err != nil {
return err
}

rows, err := cn.Queryx(sql, args...)
if err != nil {
return err
}

mapAssoc := map[string][]interface{}{}
fkids := []interface{}{}
for rows.Next() {
row, err := rows.SliceScan()
if err != nil {
return err
}

mapAssoc := map[string][]interface{}{}
fkids := []interface{}{}
for rows.Next() {
row, err := rows.SliceScan()
if err != nil {
return err
if len(row) > 0 {
if _, ok := row[0].([]uint8); ok { // -> it's UUID
row[0] = string(row[0].([]uint8))
}
if len(row) > 0 {
if _, ok := row[0].([]uint8); ok { // -> it's UUID
row[0] = string(row[0].([]uint8))
}
if _, ok := row[1].([]uint8); ok { // -> it's UUID
row[1] = string(row[1].([]uint8))
}
key := fmt.Sprintf("%v", row[0])
mapAssoc[key] = append(mapAssoc[key], row[1])
fkids = append(fkids, row[1])
if _, ok := row[1].([]uint8); ok { // -> it's UUID
row[1] = string(row[1].([]uint8))
}
key := fmt.Sprintf("%v", row[0])
mapAssoc[key] = append(mapAssoc[key], row[1])
fkids = append(fkids, row[1])
}
}

q := tx.Q()
q.eager = false
q.eagerFields = []string{}
q := tx.Q()
q.eager = false
q.eagerFields = []string{}

if strings.TrimSpace(asoc.Field.Tag.Get("order_by")) != "" {
q.Order(asoc.Field.Tag.Get("order_by"))
}
if strings.TrimSpace(asoc.Field.Tag.Get("order_by")) != "" {
q.Order(asoc.Field.Tag.Get("order_by"))
}

slice := asoc.toSlice()
q.Where("id in (?)", fkids).All(slice.Interface())
slice := asoc.toSlice()
q.Where("id in (?)", fkids).All(slice.Interface())

// 2.2) load all nested associations from this assoc.
if asocNestedFields, ok := mmi.nestedFields[asoc.Path]; ok {
for _, asocNestedField := range asocNestedFields {
if err := preload(tx, slice.Interface(), asocNestedField); err != nil {
return err
}
// 2.2) load all nested associations from this assoc.
if asocNestedFields, ok := mmi.nestedFields[asoc.Path]; ok {
for _, asocNestedField := range asocNestedFields {
if err := preload(tx, slice.Interface(), asocNestedField); err != nil {
return err
}
}
}

// 3) iterate over every model and fill it with the assoc.
mmi.iterate(func(mvalue reflect.Value) {
id := mmi.mapper.FieldByName(mvalue, "ID").Interface()
if assocFkIds, ok := mapAssoc[fmt.Sprintf("%v", id)]; ok {
modelAssociationField := mmi.mapper.FieldByName(mvalue, asoc.Name)
for i := 0; i < slice.Elem().Len(); i++ {
asocValue := slice.Elem().Index(i)
for _, fkid := range assocFkIds {
if fmt.Sprintf("%v", fkid) == fmt.Sprintf("%v", mmi.mapper.FieldByName(asocValue, "ID").Interface()) {
modelAssociationField.Set(reflect.Append(modelAssociationField, asocValue))
}
// 3) iterate over every model and fill it with the assoc.
mmi.iterate(func(mvalue reflect.Value) {
id := mmi.mapper.FieldByName(mvalue, "ID").Interface()
if assocFkIds, ok := mapAssoc[fmt.Sprintf("%v", id)]; ok {
modelAssociationField := mmi.mapper.FieldByName(mvalue, asoc.Name)
for i := 0; i < slice.Elem().Len(); i++ {
asocValue := slice.Elem().Index(i)
for _, fkid := range assocFkIds {
if fmt.Sprintf("%v", fkid) == fmt.Sprintf("%v", mmi.mapper.FieldByName(asocValue, "ID").Interface()) {
modelAssociationField.Set(reflect.Append(modelAssociationField, asocValue))
}
}
}
})
}
}
})

return nil
}

Expand Down
0