8

複数の構造で構成される階層があります

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作成は次のように定義されています:

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

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

すべてが明確で、次のように階層の「サブレベル」を 1 つだけプリロードするのに問題なく動作します。

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

また

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

GORM を使用して、ルート要素の 1 つで「First()」メソッドを呼び出して階層全体をプリロードする方法を探しています...関連するすべての「フィールド」を含む「エントリ」をロードしたいのですが、各「フィールド」にすべての「デコレータ」をプリロードする...

これは、いくつかの "Preload()" 関数を使用して実行することはできません:

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

iDB.Preload("child1").Preload("child2").First(root)「 」は、child1 と child2 の両方がルートの「葉」である場合に機能することを理解しています。

だから私の質問は次のとおりです: gorm でこれを行うことは可能ですか? はいの場合、完全な階層を再帰的にロードする最良の方法は何ですか?

ありがとう。よろしく

4

2 に答える 2

11

GORM はネストされたプリロードをサポートしています。

ドキュメントを参照してください: https://gorm.io/docs/preload.html#Nested-Preloading

于 2015-05-13T01:53:30.317 に答える
3

私の解決策:

/****** 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")
    }
}
于 2017-05-20T21:45:34.267 に答える