Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how preload a full hierarchy in GO using GORM

Tags:

go

go-gorm

I have a hierachy composed by several structures

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
}

My DB creation is definded like this :

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

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

Everything is clear and works fine to preload just one "sub level" of the hierachy like this :

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

or

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

I'm looking for a way using GORM to preload the whole hierachy invoking the "First()" method on one of the root element... I want to load an "Entry" with all its related "Fields" and I also want to have each "Field" preloaded with all its "Decorators"...

This is not feasable using the several "Preload()" functions :

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

I understand "iDB.Preload("child1").Preload("child2").First(root)" works when both child1 and child2 are "leaf" of root.

So my question is : is it possible to do this with gorm and if yes what is the best way to load recursively a complete hierachy?

Thanks. Regards

like image 314
Guillaume Barré Avatar asked Mar 24 '15 10:03

Guillaume Barré


Video Answer


2 Answers

GORM supports nested preloading.

Please refer to the documentation: https://gorm.io/docs/preload.html#Nested-Preloading

like image 81
Jinzhu Avatar answered Oct 29 '22 20:10

Jinzhu


my solution:

/****** 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")
    }
}
like image 36
Zonkiie Avatar answered Oct 29 '22 21:10

Zonkiie