首页 文章

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

提问于
浏览
6

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

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()”方法...我想加载一个“条目”及其所有相关的“字段”,我也想要让每个“Field”预装所有“装饰者”......

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

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

我理解root的“ iDB.Preload("child1").Preload("child2").First(root) " works when both child1 and child2 are " leaf” .

所以我的问题是:是否有可能用gorm做到这一点,如果是,那么递归加载comlete层次结构的最佳方法是什么?

谢谢 . 问候

3 回答

  • 2

    这已经得到支持,请参阅文档:http://jinzhu.me/gorm/crud.html#nested-preloading

  • 8

    我的解决方案

    /****** 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
    
    /// http://stackoverflow.com/questions/23030884/is-there-a-way-to-create-an-instance-of-a-struct-from-a-string
    /// http://stackoverflow.com/questions/7850140/how-do-you-create-a-new-instance-of-a-struct-from-its-type-at-runtime-in-go
    /// http://stackoverflow.com/questions/29435783/gorm-golang-orm-associations
    
    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: http://stackoverflow.com/questions/24537525/reflect-value-fieldbyname-causing-panic
    // See: http://stackoverflow.com/questions/34493062/how-to-reflect-struct-recursive-in-golang
    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")
        }
    }
    
  • 1

    不,这是不可能的 . 这是一个尚未实现的请求功能:

    https://github.com/jinzhu/gorm/issues/392

相关问题