// addPluck basically adds select statement for // only required fields func addPluck(query *gorm.DB, plucked string) *gorm.DB { if plucked == "" { return query } return query.Select(plucked) }
func getUsersPost(uid string, allUsers bool, category int64, published string, db *gorm.DB) (posts []models.Post, err error) { db = db.Preload("Author").Order("created_at desc") if !allUsers { db = db.Where("author_id = ?", uid) } if category != 0 { db = db.Joins( "join post_categories as pc on posts.id = pc.post_id " + "join categories as c on c.id = pc.category_id") db = db.Where("c.id = ?", category) } published = strings.ToLower(published) if published != "all" && (published == "true" || published == "false") { pub := published == "true" db = db.Where("published = ?", pub) } // the Select call is needed because of a bug. // TODO: remove this call when the bug is fixed db = db.Select("posts.id, posts.title, posts.content, posts.published, posts.created_at, posts.updated_at, posts.author_id") if err = db.Find(&posts).Error; err != nil { return nil, err } return posts, nil }
func (p *LocaleDao) Get(db *gorm.DB, lang, code string) string { l := Locale{} db.Select("message").Where("lang = ? AND code = ?", lang, code).First(&l) return l.Message }
func (p *LocaleDao) Set(db *gorm.DB, lang, code, message string) { var l Locale if db.Select("id").Where("lang = ? AND code = ?", lang, code).First(&l).RecordNotFound() { db.Create(&Locale{Lang: lang, Code: code, Message: message}) } else { db.Model(Locale{}).Where("id = ?", l.ID).Updates(Locale{Message: message}) } }
func FillSrcInfo(path string, db *gorm.DB) error { f, err := os.Open(path) if err != nil { return err } defer f.Close() info, err := srcinfo.ParseSrcInfo(f) if err != nil { return err } var oldPkgBase PackageBase db.Select("id").First(&oldPkgBase, "pkg_base = ?", info.Global.PkgBase) pkgBase := PackageBase{ ID: oldPkgBase.ID, PkgBase: info.Global.PkgBase, Packages: []Package{}, Version: info.Global.PkgVer, Release: info.Global.PkgRel, Depenencies: []Package{}, Directory: filepath.Dir(path), } log.Printf(" {%s}", pkgBase.PkgBase) depend := func(dependency string) { pkgName := StripDependencyPkgName(dependency) log.Printf(" %s => %s", dependency, pkgName) var dep Package db.FirstOrCreate(&dep, Package{Name: pkgName}) log.Printf(" %+v", dep) pkgBase.Depenencies = append(pkgBase.Depenencies, dep) } for _, dependency := range info.Global.Depends { depend(dependency) } for _, dependency := range info.Global.MakeDepends { depend(dependency) } for _, pkginfo := range info.Packages { for _, dependency := range pkginfo.Depends { depend(dependency) } var pkg Package db.FirstOrCreate(&pkg, Package{ PackageBaseID: pkgBase.ID, Name: pkginfo.PkgName, }) log.Printf(" %s", pkginfo.PkgName) pkgBase.Packages = append(pkgBase.Packages, pkg) } db.Save(&pkgBase) return nil }
func assertExpectedGormTUFMeta(t *testing.T, expected []StoredTUFMeta, gormDB gorm.DB) { expectedGorm := make([]TUFFile, len(expected)) for i, tufObj := range expected { expectedGorm[i] = TUFFile{ Model: gorm.Model{ID: uint(i + 1)}, Gun: tufObj.Gun, Role: tufObj.Role, Version: tufObj.Version, SHA256: tufObj.SHA256, Data: tufObj.Data, } } // There should just be one row var rows []TUFFile query := gormDB.Select("id, gun, role, version, sha256, data").Find(&rows) require.NoError(t, query.Error) // to avoid issues with nil vs zero len list if len(expectedGorm) == 0 { require.Len(t, rows, 0) } else { require.Equal(t, expectedGorm, rows) } }