当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.Select方法代码示例

本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Select方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Select方法的具体用法?Golang DB.Select怎么用?Golang DB.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jinzhu/gorm.DB的用法示例。


在下文中一共展示了DB.Select方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: getUsersPost

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
}
开发者ID:bertzzie,项目名称:obrolansubuh-backend,代码行数:30,代码来源:post.go

示例2: Get

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
}
开发者ID:itpkg,项目名称:base,代码行数:5,代码来源:dao.go

示例3: Set

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})
	}
}
开发者ID:itpkg,项目名称:base,代码行数:8,代码来源:dao.go

示例4: FillSrcInfo

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
}
开发者ID:sarum9in,项目名称:arch-builder,代码行数:51,代码来源:srcinfo.go

示例5: assertExpectedGormTUFMeta

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)
	}
}
开发者ID:jfrazelle,项目名称:notary,代码行数:24,代码来源:sqldb_test.go


注:本文中的github.com/jinzhu/gorm.DB.Select方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。