當前位置: 首頁>>代碼示例>>Golang>>正文


Golang 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;未經允許,請勿轉載。