Answer the question
In order to leave comments, you need to log in
How to work with the base in Go without specifying types?
Hello everyone
There are functions that add/remove/update data in database tables. I work through the "ORM package" github.com/eaigner/hood.
While there are 6 tables. But I already see that 99% of the code is the same, where instead of var results []BuilderVersions you need var results []BuilderRepo
I decided to try to implement this through a universal function, but I ran into the fact that I could not get the number of results ( num : = len(num) ) invalid argument results (type interface {}) for len :
// Find Data
func (Builder *Builder) FindInDB(what string, where_string hood.Path, results interface{}) (interface{}, int, error) {
var hd = Builder.Con.Hd
err := hd.Where(where_string, "=", what).Find(results)
if err != nil {
panic(err)
}
//num := len(num)
num := 1
resultsret := results
return resultsret, num, err
}
var results []BuilderRdeVersions
fmt.Println("---1 ")
resultsret, num, err := Builder.FindInDB("1.3", "rde_version", &results)
fmt.Println("---2 ")
fmt.Println("")
fmt.Printf("resultsret %v\n", resultsret)
fmt.Printf("num %v\n", num)
fmt.Printf("err%v\n", err)
---1
---=
---=
tets
---2
resultsret &[{40 1.3}]
num 1
err<nil>
package builder
import (
"fmt"
_ "github.com/eaigner/hood"
_ "github.com/ziutek/mymysql/godrv"
_ "io/ioutil"
_ "os"
_ "github.com/docopt/docopt-go"
)
type Con struct {
jsonobject Cfgbject
Dsn string
Driver string
Hd *hood.Hood
Connected bool
Log bool
}
type BuilderRdeVersions struct {
RdeId hood.Id
Version string `sql:"size(55)"`
}
func (Builder *Builder) AddNewVersionToDB(ver string,repository string, forceupdate bool) (err error) {
var hd = Builder.Con.Hd
// Find If exist
var results []BuilderVersions
err = hd.Where("version", "=", ver).Find(&results)
if err != nil {
panic(err)
}
n:=len(results)
if n>0 && forceupdate==false {
fmt.Printf("There are already exist version = %v use --force-update to udpate it \n",ver)
return fmt.Errorf(" There are already exist version %v",ver)
}else{
//update - delete exist
if n>0 {
Builder.DeleteVersion(ver,false)
}
new_ver := BuilderVersions{RdeVersion:ver}
tx := hd.Begin()
_, err := tx.Save(&new_ver)
if err != nil {
panic(err)
}
// Commit changes
err = tx.Commit()
if err != nil {
panic(err)
}
new_ver_path:=BuilderRepo{Repo: repository, VersionId: int64(new_ver.Id)}
tx1:= hd.Begin()
_, err =tx1.Save(&new_ver_path)
err = tx1.Commit()
if err != nil {
panic(err)
}
return nil
}
}
Answer the question
In order to leave comments, you need to log in
Not sure if I understood the logic of the code, but maybe
// Find Data
func (Builder *Builder) FindInDB(what string, where_string hood.Path, results []interface{} ) (interface{}, int, error) {
var hd = Builder.Con.Hd
err := hd.Where(where_string, "=", what).Find(results)
if err != nil {
panic(err)
}
num := 1
resultsret : = results
return resultsret, num, err
}
var results []BuilderRdeVersions
fmt.Println("---1 ")
resultsret, num, err := Builder.FindInDB("1.3", "rde_version", results )
fmt.Println( "---2")
fmt.Println("")
fmt.Printf("resultsret %v\n", resultsret)
fmt.Printf("num %v\n", num)
fmt.Printf("err%v\n", err)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question