本文整理匯總了Golang中github.com/QLeelulu/goku.MysqlDB.Insert方法的典型用法代碼示例。如果您正苦於以下問題:Golang MysqlDB.Insert方法的具體用法?Golang MysqlDB.Insert怎麽用?Golang MysqlDB.Insert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/QLeelulu/goku.MysqlDB
的用法示例。
在下文中一共展示了MysqlDB.Insert方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: User_SaveMap
func User_SaveMap(m map[string]interface{}) (sql.Result, error) {
var db *goku.MysqlDB = GetDB()
defer db.Close()
m["email_lower"] = strings.ToLower(m["email"].(string))
r, err := db.Insert("user", m)
return r, err
}
示例2: SaveUserFavorite
//收藏link
func SaveUserFavorite(f map[string]interface{}) error {
var db *goku.MysqlDB = GetDB()
defer db.Close()
_, err := db.Insert("user_favorite_link", f)
return err
}
示例3: linkForUser_AddWithDb
// 減少DB操作
// @t: 推送類型, 1:關注的用戶, 2:關注的話題
func linkForUser_AddWithDb(db *goku.MysqlDB, userId, linkId int64, t int) error {
m := map[string]interface{}{
"user_id": userId,
"link_id": linkId,
"create_time": time.Now(),
}
if t == 1 {
m["user_count"] = 1
} else {
m["topic_count"] = 1
}
_, err := db.Insert(LinkForUser_TableName(userId), m)
if err != nil {
if strings.Index(err.Error(), "Duplicate entry") > -1 {
m := map[string]interface{}{}
if t == 1 {
m["user_count"] = 1
} else {
m["topic_count"] = 1
}
_, err = db.Update(LinkForUser_TableName(userId), m, "user_id=? and link_id=?", userId, linkId)
if err != nil {
goku.Logger().Errorln(err.Error())
}
} else {
goku.Logger().Errorln(err.Error())
}
}
return err
}
示例4: Topic_SaveTopics
// 保持topic到數據庫,同時建立topic與link的關係表
// 如果topic已經存在,則直接建立與link的關聯
// 全部成功則返回true
func Topic_SaveTopics(topics string, linkId int64) bool {
if topics == "" {
return true
}
var db *goku.MysqlDB = GetDB()
defer db.Close()
success := true
topicList := strings.Split(topics, ",")
for _, topic := range topicList {
topicLower := strings.ToLower(topic)
t := new(Topic)
err := db.GetStruct(t, "`name_lower`=?", topic)
if err != nil {
goku.Logger().Logln(topic)
goku.Logger().Errorln(err.Error())
success = false
continue
}
if t.Id < 1 {
t.Name = topic
t.NameLower = topicLower
_, err = db.InsertStruct(t)
if err != nil {
goku.Logger().Errorln(err.Error())
success = false
continue
}
}
if t.Id > 0 && linkId > 0 {
_, err = db.Insert("topic_link", map[string]interface{}{"topic_id": t.Id, "link_id": linkId})
if err != nil {
goku.Logger().Errorln(err.Error())
success = false
} else {
// 成功,更新話題的鏈接數量統計
Topic_IncCount(db, t.Id, "link_count", 1)
redisClient := GetRedis()
defer redisClient.Quit()
// 加入推送隊列
// 格式: pushtype,topicid,linkid,timestamp
qv := fmt.Sprintf("%v,%v,%v,%v", LinkForUser_ByTopic, t.Id, linkId, time.Now().Unix())
_, err = redisClient.Lpush(golink.KEY_LIST_PUSH_TO_USER, qv)
if err != nil {
goku.Logger().Errorln(err.Error())
}
}
}
}
return success
}
示例5: Save
func (ur *UserRecovery) Save() (sql.Result, error) {
var db *goku.MysqlDB = GetDB()
defer db.Close()
m := make(map[string]interface{})
m["user_id"] = ur.UserId
m["token"] = ur.Token
m["active"] = ur.Active
m["create_time"] = ur.CreateTime
m["recovery_time"] = ur.RecoveryTime
r, err := db.Insert("user_recovery", m)
return r, err
}
示例6: Link_SaveMap
// 保存link到數據庫,如果成功,則返回link的id
func Link_SaveMap(m map[string]interface{}) int64 {
var db *goku.MysqlDB = GetDB()
defer db.Close()
m["create_time"] = time.Now()
//新增link默認投票1次,顯示的時候默認減一
m["vote_up"] = 0 //1
m["reddit_score"] = utils.LinkSortAlgorithm(m["create_time"].(time.Time), int64(0), int64(0))
m["context_md5"] = utils.MD5_16(strings.ToLower(m["context"].(string)))
r, err := db.Insert("link", m)
if err != nil {
goku.Logger().Errorln(err.Error())
return 0
}
var id int64
id, err = r.LastInsertId()
if err != nil {
goku.Logger().Errorln(err.Error())
return 0
}
if id > 0 {
uid := m["user_id"].(int64)
// 更新用戶的鏈接計數
IncCountById(db, "user", uid, "link_count", 1)
// 直接推送給自己,自己必須看到
LinkForUser_Add(uid, id, LinkForUser_ByUser)
// 存入`tui_link_for_handle` 鏈接處理隊列表
db.Query("INSERT ignore INTO tui_link_for_handle(link_id,create_time,user_id,insert_time,data_type) VALUES (?, ?, ?, NOW(), ?)",
id, m["create_time"].(time.Time), uid, 1)
redisClient := GetRedis()
defer redisClient.Quit()
// 加入推送隊列
// 格式: pushtype,userid,linkid,timestamp
qv := fmt.Sprintf("%v,%v,%v,%v", LinkForUser_ByUser, uid, id, time.Now().Unix())
_, err = redisClient.Lpush(golink.KEY_LIST_PUSH_TO_USER, qv)
if err != nil {
goku.Logger().Errorln(err.Error())
// return 0
}
}
return id
}
示例7: CommentForUser_Add
// 添加一條推送評論到被評論的用戶,
func CommentForUser_Add(userId int64, comment Comment) error {
var db *goku.MysqlDB = GetDB()
defer db.Close()
m := map[string]interface{}{}
m["user_id"] = userId
m["comment_id"] = comment.Id
m["link_id"] = comment.LinkId
m["pcomment_id"] = comment.ParentId
m["create_time"] = comment.CreateTime
_, err := db.Insert(table_CommentForUser, m)
if err != nil {
goku.Logger().Errorln(err.Error())
}
return err
}
示例8: User_Follow
// userId 關注 followId
func User_Follow(userId, followId int64) (bool, error) {
if userId < 1 || followId < 1 {
return false, errors.New("參數錯誤")
}
if userId == followId {
return false, errors.New("不能關注自己")
}
var db *goku.MysqlDB = GetDB()
defer db.Close()
vals := map[string]interface{}{
"user_id": userId,
"follow_id": followId,
"create_time": time.Now(),
}
r, err := db.Insert("user_follow", vals)
if err != nil {
if strings.Index(err.Error(), "Duplicate entry") > -1 {
return false, errors.New("已經關注該用戶")
} else {
goku.Logger().Errorln(err.Error())
return false, err
}
}
var afrow int64
afrow, err = r.RowsAffected()
if err != nil {
goku.Logger().Errorln(err.Error())
return false, err
}
if afrow > 0 {
LinkForUser_FollowUser(userId, followId)
// 更新粉絲數
User_IncCount(db, userId, "friend_count", 1)
// 更新關注數
User_IncCount(db, followId, "follower_count", 1)
// 通知有新粉絲
Remind_Inc(followId, REMIND_FANS)
return true, nil
}
return false, nil
}
示例9: Save
func (u *ThirdPartyUser) Save() (sql.Result, error) {
var db *goku.MysqlDB = GetDB()
defer db.Close()
m := make(map[string]interface{})
m["user_id"] = u.UserId
m["third_party"] = u.ThirdParty
m["third_party_user_id"] = u.ThirdPartyUserId
m["third_party_email"] = u.ThirdPartyEmail
m["access_token"] = u.AccessToken
m["refresh_token"] = u.RefreshToken
m["token_expire_time"] = u.TokenExpireTime
m["create_time"] = u.CreateTime
m["last_active_time"] = u.LastActiveTime
m["avatar_url"] = u.AvatarUrl
m["link"] = u.Link
r, err := db.Insert("third_party_user", m)
return r, err
}
示例10: Topic_Follow
// 用戶userId 關注 話題topicId
func Topic_Follow(userId, topicId int64) (bool, error) {
if userId < 1 || topicId < 1 {
return false, errors.New("參數錯誤")
}
var db *goku.MysqlDB = GetDB()
defer db.Close()
vals := map[string]interface{}{
"user_id": userId,
"topic_id": topicId,
"create_time": time.Now(),
}
r, err := db.Insert("topic_follow", vals)
if err != nil {
if strings.Index(err.Error(), "Duplicate entry") > -1 {
return false, errors.New("已經關注該話題")
} else {
goku.Logger().Errorln(err.Error())
return false, err
}
}
var afrow int64
afrow, err = r.RowsAffected()
if err != nil {
goku.Logger().Errorln(err.Error())
return false, err
}
if afrow > 0 {
// 關注話題成功,將話題的鏈接推送給用戶
LinkForUser_FollowTopic(userId, topicId)
// 更新用戶關注話題的數量
User_IncCount(db, userId, "ftopic_count", 1)
// 更新話題的關注用戶數
Topic_IncCount(db, topicId, "follower_count", 1)
return true, nil
}
return false, nil
}
示例11: Comment_SaveMap
// 保存評論到數據庫,如果成功,則返回comment的id
func Comment_SaveMap(m map[string]interface{}) (int64, error) {
var db *goku.MysqlDB = GetDB()
defer db.Close()
// TODO: 鏈接評論的鏈接存不存在?
// 檢查父評論是否存在
var pComment *Comment
var err error
if id, ok := m["parent_id"].(int64); ok && id > 0 {
pComment, err = Comment_GetById(id)
if err != nil {
goku.Logger().Errorln(err.Error())
return int64(0), err
}
// 指定了父評論的id但是數據庫中沒有
if pComment == nil {
return int64(0), errors.New("指定的父評論不存在")
}
}
// 路徑相關
if pComment == nil {
m["parent_id"] = 0
m["top_parent_id"] = 0
m["parent_path"] = "/"
m["deep"] = 0
} else {
m["parent_id"] = pComment.Id
if pComment.TopParentId == 0 {
m["top_parent_id"] = pComment.Id
} else {
m["top_parent_id"] = pComment.TopParentId
}
m["parent_path"] = fmt.Sprintf("%v%v/", pComment.ParentPath, pComment.Id)
m["deep"] = pComment.Deep + 1
}
m["status"] = 1
m["create_time"] = time.Now()
//新增comment默認投票1次,顯示的時候默認減一
m["vote_up"] = 1
m["reddit_score"] = utils.RedditSortAlgorithm(m["create_time"].(time.Time), int64(1), int64(0))
r, err := db.Insert(Table_Comment, m)
if err != nil {
goku.Logger().Errorln(err.Error())
return 0, err
}
var id int64
id, err = r.LastInsertId()
if err != nil {
goku.Logger().Errorln(err.Error())
return 0, err
}
if id > 0 {
// 更新Link的計數器
IncCountById(db, Table_Link, m["link_id"].(int64), "comment_count", 1)
if pComment != nil {
IncCountById(db, Table_Comment, pComment.Id, "children_count", 1)
} else {
IncCountById(db, Table_Link, m["link_id"].(int64), "comment_root_count", 1)
}
}
return id, nil
}
示例12: Comment_SaveMap
// 保存評論到數據庫,如果成功,則返回comment的id
func Comment_SaveMap(m map[string]interface{}) (int64, error) {
var db *goku.MysqlDB = GetDB()
defer db.Close()
var err error
// 評論的鏈接存不存在?
linkId := m["link_id"].(int64)
link, err := Link_GetById(linkId)
if err != nil {
return 0, err
} else if link.Id < 1 {
return int64(0), errors.New("評論的鏈接不存在")
}
// 檢查父評論是否存在
var pComment *Comment
if id, ok := m["parent_id"].(int64); ok && id > 0 {
pComment, err = Comment_GetById(id)
if err != nil {
goku.Logger().Errorln(err.Error())
return int64(0), err
}
// 指定了父評論的id但是數據庫中沒有
if pComment == nil {
return int64(0), errors.New("指定的父評論不存在")
}
} else if !ok {
m["parent_id"] = int64(0)
}
// 路徑相關
if pComment == nil {
m["parent_id"] = int64(0)
m["top_parent_id"] = int64(0)
m["parent_path"] = "/"
m["deep"] = 0
} else {
m["parent_id"] = pComment.Id
if pComment.TopParentId == 0 {
m["top_parent_id"] = pComment.Id
} else {
m["top_parent_id"] = pComment.TopParentId
}
m["parent_path"] = fmt.Sprintf("%v%v/", pComment.ParentPath, pComment.Id)
m["deep"] = pComment.Deep + 1
}
m["status"] = 1
m["create_time"] = time.Now()
//新增comment默認投票1次,顯示的時候默認減一
m["vote_up"] = 1
m["reddit_score"] = utils.RedditSortAlgorithm(m["create_time"].(time.Time), int64(1), int64(0))
r, err := db.Insert(Table_Comment, m)
if err != nil {
goku.Logger().Errorln(err.Error())
return 0, err
}
var id int64
id, err = r.LastInsertId()
if err != nil {
goku.Logger().Errorln(err.Error())
return 0, err
}
if id > 0 {
// 更新Link的計數器
IncCountById(db, Table_Link, linkId, "comment_count", 1)
if pComment != nil {
IncCountById(db, Table_Comment, pComment.Id, "children_count", 1)
} else {
IncCountById(db, Table_Link, linkId, "comment_root_count", 1)
}
// 通知評論用戶
userId := m["user_id"].(int64)
toLinkUser := userId != link.UserId
// 如果是回複,則推送給所回複評論的用戶
toPcommentUser := (pComment != nil && userId != pComment.UserId && pComment.UserId != link.UserId)
if toLinkUser || toPcommentUser {
comment := Comment{}
comment.Id = id
comment.UserId = userId
comment.LinkId = linkId
comment.ParentId = m["parent_id"].(int64)
comment.CreateTime = m["create_time"].(time.Time)
if toLinkUser {
CommentForUser_Add(link.UserId, comment)
Remind_Inc(link.UserId, REMIND_COMMENT)
}
if toPcommentUser {
CommentForUser_Add(pComment.UserId, comment)
Remind_Inc(pComment.UserId, REMIND_COMMENT)
}
}
}
return id, nil
//.........這裏部分代碼省略.........
示例13: SaveTodo
func SaveTodo(m map[string]interface{}) (sql.Result, error) {
var db *goku.MysqlDB = GetDB()
defer db.Close()
r, err := db.Insert("todo", m)
return r, err
}