如何使用 GORM 在 GO 中预加载完整的层次结构

how preload a full hierarchy in GO using GORM

我有一个由多个结构组成的层次结构

type Entry struct {
    Id          int
    CreatedAt   time.Time
    UpdatedAt   time.Time
    Fields      []Field
}

type SyncField struct {
    Id                   int
    CreatedAt            time.Time
    UpdatedAt            time.Time
    TechnicalName        string
    JsonName             string
    EntryId              int
    Decorators           []Decorator
}

type Decorator struct {
    Id           int
    CreatedAt    time.Time
    UpdatedAt    time.Time
    Name         string
    Description  string
    SortingOrder int
    Params       string
    SyncFieldId  int
}

我的数据库创建是这样定义的:

db.CreateTable(&Entry{})
db.CreateTable(&SyncField{})
db.Model(&Entry{}).Related(&SyncField{}, "EntryId")

db.CreateTable(&Decorator{})
db.Model(&SyncField{}).Related(&Decorator{}, "DecoratorId")

一切都很清楚,并且可以正常预加载层次结构的一个“子级别”,如下所示:

entry := &Entry{Id: i}
iDB.Preload("SyncFields").First(entry)

field := &SyncField{Id: idf}
iDB.Preload("Decorators").First(field)

我正在寻找一种使用 GORM 预加载整个层次结构的方法,在其中一个根元素上调用“First()”方法... 我想加载一个“条目”及其所有相关的“字段”,我还想让每个“字段”预加载其所有“装饰器”...

使用几个“Preload()”函数不可行:

entry := &Entry{Id: i}
iDB.Preload("Decorators").Preload("SyncFields").First(entry)

我理解“iDB.Preload("child1").Preload("child2").First(root)”在 child1 和 child2 都是根的“叶”时有效。

所以我的问题是:是否可以使用 gorm 执行此操作,如果可以,递归加载完整层次结构的最佳方法是什么?

谢谢。 问候

GORM 支持嵌套预加载。

请参考文档:https://gorm.io/docs/preload.html#Nested-Preloading

我的解决方案:

/****** MIT License **********
Copyright (c) 2017 Zonkiie
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***************************/

package main

/// 
/// 
/// 

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "fmt"
    "reflect"
    "regexp"
    "encoding/json"
    "encoding/xml"
    //"strconv"
    "os"
    "flag"
    "strings"
)

type rs struct {
    //Parent    *rs `gorm:"ForeignKey:ID;AssociationForeignKey:ParentID"`
    Childs  []*rs   `gorm:"ForeignKey:ID;AssociationForeignKey:ParentID" walkrec:"true"`
    ID  int64
    ParentID    int64   `gorm:"column:ParentID"`
    Value   string
    Sub []rs_sub    `gorm:"ForeignKey:ID;AssociationForeignKey:Rs_ID" walkrec:"true"`
}

type rs_sub struct {
    ID  int64
    Rs_ID   int64   `gorm:"column:rs_id"`
    Value   string
}

var db *gorm.DB

func populateDb(db *gorm.DB) {

    rs1 := rs{ID: 1, ParentID: 0, Value: "root"}
    db.Save(&rs1)
    rs2 := rs{ID: 2, ParentID: 1, Value: "Child1"}
    db.Save(&rs2)
    rs3 := rs{ID: 3, ParentID: 2, Value: "Child2"}
    db.Save(&rs3)
    rs4 := rs{ID: 4, ParentID: 3, Value: "Child3"}
    db.Save(&rs4)
    rs5 := rs{ID: 5, ParentID: 3, Value: "Child4"}
    db.Save(&rs5)
    rs_s := rs_sub{ID:1, Rs_ID:4, Value: "SubChild1"}
    db.Save(&rs_s)
}

func PStdErr(format string, args ...interface{}) {
    fmt.Fprintf(os.Stderr, format, args...)
}

func JsonMarshal(data interface{}) string {
    b, err := json.Marshal(data)
    if err != nil {
        return "Error"
    }
    return string(b[:])
}

func XmlMarshal(data interface{}) string {
    b, err := xml.Marshal(data)
    if err != nil {
        return "Error"
    }
    return string(b[:])
}

func InitDB() *gorm.DB {
db, err := gorm.Open("sqlite3", ":memory:")
if err != nil {
    panic("failed to connect database")
}
db.Exec("DROP TABLE IF EXISTS rs;")

    db.SingularTable(true)

// Migrate the schema
db.AutoMigrate(&rs{})
db.AutoMigrate(&rs_sub{})

populateDb(db)
//db.LogMode(true)
return db

}

// This function fetches all related objects from a given object in the data parameter.
// The struct must be fully tagged, we don't recognize automatically related IDs and so on.
// The function works only with not combined keys.
// Every field which should be fetched must be tagged with:
// walkrec:"true" gorm:"ForeignKey:ID;AssociationForeignKey:ForeignKey"
// See: 
// See: 
func fetchRec(db *gorm.DB, data interface{}) {
    // With data *rs: Type: *main.rs
    // With data interface{}: *main.rs
    var ref reflect.Value
    if reflect.TypeOf(data).Kind() == reflect.Struct {
        ref = reflect.ValueOf(data)
    } else if reflect.TypeOf(data).Kind() == reflect.Ptr {
        ref = reflect.Indirect(reflect.ValueOf(data))
    }
    if ref.Type().Kind() == reflect.Slice {
        for i := 0; i < ref.Len(); i++ {
            if ref.Index(i).Type().Kind() == reflect.Ptr {
                fetchRec(db, ref.Index(i).Elem().Addr().Interface())
            } else if ref.Index(i).Type().Kind() == reflect.Struct {
                // What should we do here?
            }
        }

    } else if ref.Type().Kind() == reflect.Struct {
        for i := 0; i < ref.NumField(); i++ {
            var IDFieldRaw string
            var IDFields []string
            var RefFieldRaw string
            var RefFields []string
            var re *regexp.Regexp
            var matches []string

            if ref.Field(i).CanAddr() && strings.EqualFold(ref.Type().Field(i).Tag.Get("walkrec"), "true") {
                gormflags := ref.Type().Field(i).Tag.Get("gorm")
                if gormflags == "" {
                    panic("No gorm flags found!")
                } else {
                    re = regexp.MustCompile(`\bForeignKey:([a-zA-Z0-9_,]+)\b`)
                    matches = re.FindStringSubmatch(gormflags)
                    if len(matches) == 2 {
                        IDFieldRaw = matches[1]
                        IDFields = strings.Split(IDFieldRaw, ",")
                    }
                    re = regexp.MustCompile(`\bAssociationForeignKey:([a-zA-Z0-9_,]+)\b`)
                    matches = re.FindStringSubmatch(gormflags)
                    if len(matches) == 2 {
                        RefFieldRaw = matches[1]
                        RefFields = strings.Split(RefFieldRaw, ",")
                    }
                }
                if len(IDFields) == 0 { continue }
                if len(RefFields) != 0 {
                    WhereMap := make(map[string]interface{})
                    for fk := 0; fk < len(RefFields); fk++ {
                        WhereMap[RefFields[fk]] = fmt.Sprint(ref.FieldByName(IDFields[fk]))
                    }
                    db.Where(WhereMap).Find(ref.Field(i).Addr().Interface())
                    if ref.Field(i).Addr().Interface() != nil {
                        fetchRec(db, ref.Field(i).Addr().Interface())
                    }
                } else {
                    panic("AssociationForeignKey empty!")
                }
            }
        }
    }
}

func getParams() (id int) {
    flag.IntVar(&id, "id", 1, "the id to fetch")
    flag.Parse()
    return
}

func fetch(db *gorm.DB, id interface{}) (d rs, found bool) {

    //db.First(&d, id)
    found = false
    found = !db.Find(&d, id).RecordNotFound()
    if found {
        fetchRec(db, &d)
    }
    return
}

// Execute this program. For example:
// go run main.go --id=2
func main() {
    db = InitDB()
    defer db.Close()
    id := getParams()
    PStdErr("Loading data with ID %d\n", id)
    rs, found := fetch(db, id)
    if found {
        fmt.Print(XmlMarshal(rs) + "\n")
    }
}