• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang db.DB类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/coralproject/shelf/pkg/db.DB的典型用法代码示例。如果您正苦于以下问题:Golang DB类的具体用法?Golang DB怎么用?Golang DB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: CreateUser

// CreateUser creates a new user resource
func CreateUser(context interface{}, db *db.DB, user *User) error {
	log.Dev(context, "CreateUser", "Started : User: ", user)

	/* This error condition should be performed by DB indexes
	dbUser, err := GetUserByUserName(context, db, user.UserName)
	if dbUser != nil {
		log.Error(context, "CreateUser", err, "User exists")
		return "CreateUser: user with same UserName already exists"
	}
	*/

	// set defaults, may want to move this to a factory method on the User struct
	if user.UserID == "" {
		user.UserID = uuid.New()
	}
	user.MemberSince = time.Now()

	f := func(col *mgo.Collection) error {
		log.Dev(context, "CreateUser", "MGO: db.%s.insert()", userCollection)
		return col.Insert(user)
	}

	// Write the user to mongo
	if err := db.ExecuteMGO(context, userCollection, f); err != nil {
		log.Error(context, "CreateUser", err, "Completed")
		return err
	}

	log.Dev(context, "CreateUser", "Completed")
	return nil
}
开发者ID:coralproject,项目名称:sponge,代码行数:32,代码来源:comment.go


示例2: removeUser

// removeUser is used to clear out all the test user from the collection.
func removeUser(db *db.DB, UserID string) error {
	f := func(c *mgo.Collection) error {
		q := bson.M{"user_id": UserID}
		return c.Remove(q)
	}

	if err := db.ExecuteMGO(tests.Context, "users", f); err != nil {
		return err
	}

	return nil
}
开发者ID:coralproject,项目名称:sponge,代码行数:13,代码来源:comment_test.go


示例3: removeComment

// removeComment is used to clear out all the test user from the collection.
func removeComment(db *db.DB, CommentID string) error {
	f := func(c *mgo.Collection) error {
		q := bson.M{"comment_id": CommentID}
		return c.Remove(q)
	}

	if err := db.ExecuteMGO(tests.Context, "comments", f); err != nil {
		return err
	}

	return nil
}
开发者ID:coralproject,项目名称:sponge,代码行数:13,代码来源:comment_test.go


示例4: GetUserByID

// GetUserByID retrieves an individual user by ID
func GetUserByID(context interface{}, db *db.DB, id string) (*User, error) {
	log.Dev(context, "GetUserById", "Started : Id[%s]", id)

	var user User
	f := func(c *mgo.Collection) error {
		q := bson.M{"_id": id}
		log.Dev(context, "GetUserById", "MGO : db.%s.findOne(%s)", userCollection, mongo.Query(q))
		return c.Find(q).One(&user)
	}

	if err := db.ExecuteMGO(context, userCollection, f); err != nil {
		log.Error(context, "GetUserById", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetUserById", "Completed")
	return &user, nil
}
开发者ID:coralproject,项目名称:sponge,代码行数:19,代码来源:comment.go


示例5: GetUserByUserName

// GetUserByUserName retrieves an individual user by email
func GetUserByUserName(context interface{}, db *db.DB, userName string) (*User, error) {
	log.Dev(context, "GetUserByUserName", "Started : User[%s]", userName)

	userName = strings.ToLower(userName)

	var user User
	f := func(c *mgo.Collection) error {
		q := bson.M{"user_name": userName}
		log.Dev(context, "GetUserByUserName", "MGO : db.%s.findOne(%s)", userCollection, mongo.Query(q))
		return c.Find(q).One(&user)
	}

	if err := db.ExecuteMGO(context, userCollection, f); err != nil {
		log.Error(context, "GetUserByUserName", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetUserByUserName", "Completed")
	return &user, nil
}
开发者ID:coralproject,项目名称:sponge,代码行数:21,代码来源:comment.go


示例6: CreateComment

// CreateComment adds a new comment in the database.
func CreateComment(context interface{}, db *db.DB, com *Comment) error {
	if com.CommentID == "" {
		com.CommentID = uuid.New()
	}
	com.DateCreated = time.Now()
	com.Status = "New"

	f := func(col *mgo.Collection) error {
		log.Dev(context, "CreateComment", "MGO: db.%s.insert()", commentCollection)
		return col.Insert(com)
	}

	if err := db.ExecuteMGO(context, commentCollection, f); err != nil {
		log.Error(context, "CreateComment", err, "Completed")
		return err
	}

	log.Dev(context, "CreateComment", "Completed")

	return nil
}
开发者ID:coralproject,项目名称:sponge,代码行数:22,代码来源:comment.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang database.FindOneLayerByID函数代码示例发布时间:2022-05-23
下一篇:
Golang mongo.Query函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap