sqlx: optimize get comments for post detail page logic

r/paopao-ce-plus
Michael Li 1 year ago
parent ccba606b73
commit 5c0e97d4c4
No known key found for this signature in database

@ -22,11 +22,12 @@ const (
_AuthorizationManage_MyFriendSet = `SELECT friend_id FROM @contact WHERE user_id=? AND status=2 AND is_del=0`
_Comment_GetCommentById = `SELECT * FROM @comment WHERE id=? AND is_del=0`
_Comment_GetCommentContentsByIds = `SELECT * FROM @comment_content WHERE comment_id IN (?)`
_Comment_GetCommentCount = `SELECT count(*) FROM @comment WHERE post_id=:post_id AND is_del=0`
_Comment_GetCommentCount = `SELECT count(*) FROM @comment WHERE post_id=? AND is_del=0`
_Comment_GetCommentReplyById = `SELECT * FROM @comment_reply WHERE id=? AND is_del=0`
_Comment_GetCommentThumbs = `SELECT user_id, tweet_id, comment_id, reply_id, comment_type, is_thumbs_up, is_thumbs_down FROM @tweet_comment_thumbs WHERE user_id=? AND tweet_id=?`
_Comment_GetCommmentRepliesByIds = `SELECT * FROM @comment_reply WHERE comment_id IN (?) ORDER BY id ASC`
_Comment_GetDefaultComments = `SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY id ASC LIMIT ? OFFSET ?`
_Comment_GetHotsComments = `SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY thumbs_up_count DESC, id DESC LIMIT ? OFFSET ?`
_Comment_GetNewestComments = `SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY id DESC LIMIT ? OFFSET ?`
_Comment_GetUsersByIds = `SELECT id, nickname, username, status, avatar, is_admin FROM @user WHERE id IN (?)`
_CommentManage_CreateComment = `INSERT INTO @comment (post_id, user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?)`
@ -208,15 +209,16 @@ type AuthorizationManage struct {
type Comment struct {
yesql.Namespace `yesql:"comment"`
GetCommentContentsByIds string `yesql:"get_comment_contents_by_ids"`
GetCommmentRepliesByIds string `yesql:"get_commment_replies_by_ids"`
GetUsersByIds string `yesql:"get_users_by_ids"`
GetCommentById *sqlx.Stmt `yesql:"get_comment_by_id"`
GetCommentReplyById *sqlx.Stmt `yesql:"get_comment_reply_by_id"`
GetCommentThumbs *sqlx.Stmt `yesql:"get_comment_thumbs"`
GetDefaultComments *sqlx.Stmt `yesql:"get_default_comments"`
GetNewestComments *sqlx.Stmt `yesql:"get_newest_comments"`
GetCommentCount *sqlx.NamedStmt `yesql:"get_comment_count"`
GetCommentContentsByIds string `yesql:"get_comment_contents_by_ids"`
GetCommmentRepliesByIds string `yesql:"get_commment_replies_by_ids"`
GetUsersByIds string `yesql:"get_users_by_ids"`
GetCommentById *sqlx.Stmt `yesql:"get_comment_by_id"`
GetCommentCount *sqlx.Stmt `yesql:"get_comment_count"`
GetCommentReplyById *sqlx.Stmt `yesql:"get_comment_reply_by_id"`
GetCommentThumbs *sqlx.Stmt `yesql:"get_comment_thumbs"`
GetDefaultComments *sqlx.Stmt `yesql:"get_default_comments"`
GetHotsComments *sqlx.Stmt `yesql:"get_hots_comments"`
GetNewestComments *sqlx.Stmt `yesql:"get_newest_comments"`
}
type CommentManage struct {
@ -454,6 +456,9 @@ func BuildComment(p PreparexBuilder, ctx ...context.Context) (obj *Comment, err
if obj.GetCommentById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Comment_GetCommentById))); err != nil {
return nil, fmt.Errorf("prepare _Comment_GetCommentById error: %w", err)
}
if obj.GetCommentCount, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Comment_GetCommentCount))); err != nil {
return nil, fmt.Errorf("prepare _Comment_GetCommentCount error: %w", err)
}
if obj.GetCommentReplyById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Comment_GetCommentReplyById))); err != nil {
return nil, fmt.Errorf("prepare _Comment_GetCommentReplyById error: %w", err)
}
@ -463,12 +468,12 @@ func BuildComment(p PreparexBuilder, ctx ...context.Context) (obj *Comment, err
if obj.GetDefaultComments, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Comment_GetDefaultComments))); err != nil {
return nil, fmt.Errorf("prepare _Comment_GetDefaultComments error: %w", err)
}
if obj.GetHotsComments, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Comment_GetHotsComments))); err != nil {
return nil, fmt.Errorf("prepare _Comment_GetHotsComments error: %w", err)
}
if obj.GetNewestComments, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Comment_GetNewestComments))); err != nil {
return nil, fmt.Errorf("prepare _Comment_GetNewestComments error: %w", err)
}
if obj.GetCommentCount, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_Comment_GetCommentCount))); err != nil {
return nil, fmt.Errorf("prepare _Comment_GetCommentCount error: %w", err)
}
return
}

@ -32,13 +32,22 @@ type commentManageSrv struct {
q *cc.CommentManage
}
func (s *commentSrv) GetComments(r *ms.ConditionsT, offset, limit int) (res []*ms.Comment, err error) {
order := (*r)["ORDER"].(string)
postId := (*r)["post_id"]
if order == "id ASC" {
err = s.q.GetDefaultComments.Select(&res, postId, limit, offset)
} else {
err = s.q.GetNewestComments.Select(&res, postId, limit, offset)
func (s *commentSrv) GetComments(tweetId int64, style cs.StyleCommentType, limit int, offset int) (res []*ms.Comment, total int64, err error) {
switch style {
case cs.StyleCommentHots:
if err = s.q.GetHotsComments.Select(&res, tweetId, limit, offset); err == nil {
err = s.q.GetCommentCount.Get(&total, tweetId)
}
case cs.StyleCommentNewest:
if err = s.q.GetNewestComments.Select(&res, tweetId, limit, offset); err == nil {
err = s.q.GetCommentCount.Get(&total, tweetId)
}
case cs.StyleCommentDefault:
fallthrough
default:
if err = s.q.GetDefaultComments.Select(&res, tweetId, limit, offset); err == nil {
err = s.q.GetCommentCount.Get(&total, tweetId)
}
}
return
}
@ -55,11 +64,6 @@ func (s *commentSrv) GetCommentReplyByID(id int64) (res *ms.CommentReply, err er
return
}
func (s *commentSrv) GetCommentCount(r *ms.ConditionsT) (res int64, err error) {
err = s.q.GetCommentCount.Get(&res, *r)
return
}
func (s *commentSrv) GetCommentContentsByIDs(ids []int64) (res []*ms.CommentContent, err error) {
if len(ids) == 0 {
return nil, nil
@ -211,7 +215,7 @@ func (s *commentManageSrv) ThumbsUpComment(userId int64, tweetId, commentId int6
}
commentThumbs.IsThumbsUp = 1 - commentThumbs.IsThumbsUp
commentThumbs.ModifiedOn = now
if _, err := tx.Stmtx(s.q.UpdateThumbsUpdownComment).Exec(commentThumbs); err != nil {
if _, err := tx.NamedStmt(s.q.UpdateThumbsUpdownComment).Exec(commentThumbs); err != nil {
return err
}
} else {

@ -27,6 +27,10 @@ SELECT status FROM @contact WHERE user_id=? AND friend_id=? AND is_del=0;
-- prepare: stmt
SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY id DESC LIMIT ? OFFSET ?;
-- name: get_hots_comments@comment
-- prepare: stmt
SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY thumbs_up_count DESC, id DESC LIMIT ? OFFSET ?;
-- name: get_default_comments@comment
-- prepare: stmt
SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY id ASC LIMIT ? OFFSET ?;
@ -36,8 +40,8 @@ SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY id ASC LIMIT ? OFFS
SELECT * FROM @comment WHERE id=? AND is_del=0;
-- name: get_comment_count@comment
-- prepare: named_stmt
SELECT count(*) FROM @comment WHERE post_id=:post_id AND is_del=0;
-- prepare: stmt
SELECT count(*) FROM @comment WHERE post_id=? AND is_del=0;
-- name: get_comment_reply_by_id@comment
-- prepare: stmt

Loading…
Cancel
Save