add missing method logic

x/sqlx
Michael Li 10 months ago
parent dcb43bfae1
commit d4cac5afbc
No known key found for this signature in database

@ -21,12 +21,14 @@ const (
_SimpleIndexA_UserInfo = `SELECT * FROM @user WHERE username=?` _SimpleIndexA_UserInfo = `SELECT * FROM @user WHERE username=?`
_TopicA_DecrTagsById = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?)` _TopicA_DecrTagsById = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?)`
_TopicA_ExistTopicUser = `SELECT 1 FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0` _TopicA_ExistTopicUser = `SELECT 1 FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0`
_TopicA_FollowTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", 1 as is_following, c.is_top, u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @topic_user c JOIN @user u ON c.user_id = u.id JOIN @tag t ON c.topic_id = t.id WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=? ORDER BY c.is_top DESC, t.quote_num DESC LIMIT ? OFFSET ?` _TopicA_FollowPinTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", 1 as is_following, c.is_top, c.is_pin, u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @topic_user c JOIN @user u ON c.user_id = u.id JOIN @tag t ON c.topic_id = t.id WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=? AND c.is_pin=1 ORDER BY c.is_top DESC, t.quote_num DESC LIMIT ? OFFSET ?`
_TopicA_FollowTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", 1 as is_following, c.is_top, c.is_pin, u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @topic_user c JOIN @user u ON c.user_id = u.id JOIN @tag t ON c.topic_id = t.id WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=? ORDER BY c.is_top DESC, t.quote_num DESC LIMIT ? OFFSET ?`
_TopicA_FollowTopic = `INSERT INTO @topic_user(user_id, topic_id, created_on) VALUES (?, ?, ?)` _TopicA_FollowTopic = `INSERT INTO @topic_user(user_id, topic_id, created_on) VALUES (?, ?, ?)`
_TopicA_HotTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?` _TopicA_HotTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?`
_TopicA_IncrTagsById = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?)` _TopicA_IncrTagsById = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?)`
_TopicA_InsertTag = `INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1)` _TopicA_InsertTag = `INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1)`
_TopicA_NewestTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.id DESC LIMIT ? OFFSET ?` _TopicA_NewestTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.id DESC LIMIT ? OFFSET ?`
_TopicA_PinTopic = `UPDATE @topic_user SET is_pin=1-is_pin, modified_on=? WHERE user_id=? AND topic_id=? AND is_del=0`
_TopicA_StickTopic = `UPDATE @topic_user SET is_top=1-is_top, modified_on=? WHERE user_id=? AND topic_id=? AND is_del=0` _TopicA_StickTopic = `UPDATE @topic_user SET is_top=1-is_top, modified_on=? WHERE user_id=? AND topic_id=? AND is_del=0`
_TopicA_TagsByIdA = `SELECT id FROM @tag WHERE id IN (?) AND is_del = 0 AND quote_num > 0` _TopicA_TagsByIdA = `SELECT id FROM @tag WHERE id IN (?) AND is_del = 0 AND quote_num > 0`
_TopicA_TagsByIdB = `SELECT id, user_id, tag, quote_num FROM @tag WHERE id IN (?)` _TopicA_TagsByIdB = `SELECT id, user_id, tag, quote_num FROM @tag WHERE id IN (?)`
@ -34,6 +36,7 @@ const (
_TopicA_TagsByKeywordB = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6` _TopicA_TagsByKeywordB = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6`
_TopicA_TagsForIncr = `SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?)` _TopicA_TagsForIncr = `SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?)`
_TopicA_TopicInfos = `SELECT topic_id, is_top FROM @topic_user WHERE is_del=0 AND user_id=? AND topic_id IN (?)` _TopicA_TopicInfos = `SELECT topic_id, is_top FROM @topic_user WHERE is_del=0 AND user_id=? AND topic_id IN (?)`
_TopicA_TopicIsPin = `SELECT is_pin FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0`
_TopicA_TopicIsTop = `SELECT is_top FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0` _TopicA_TopicIsTop = `SELECT is_top FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0`
_TopicA_UnfollowTopic = `DELETE FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0` _TopicA_UnfollowTopic = `DELETE FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0`
_TweetA_AttachmentByTweetId = `SELECT * FROM @user WHERE username=?` _TweetA_AttachmentByTweetId = `SELECT * FROM @user WHERE username=?`
@ -91,14 +94,17 @@ type TopicA struct {
TagsForIncr string `yesql:"tags_for_incr"` TagsForIncr string `yesql:"tags_for_incr"`
TopicInfos string `yesql:"topic_infos"` TopicInfos string `yesql:"topic_infos"`
ExistTopicUser *sqlx.Stmt `yesql:"exist_topic_user"` ExistTopicUser *sqlx.Stmt `yesql:"exist_topic_user"`
FollowPinTags *sqlx.Stmt `yesql:"follow_pin_tags"`
FollowTags *sqlx.Stmt `yesql:"follow_tags"` FollowTags *sqlx.Stmt `yesql:"follow_tags"`
FollowTopic *sqlx.Stmt `yesql:"follow_topic"` FollowTopic *sqlx.Stmt `yesql:"follow_topic"`
HotTags *sqlx.Stmt `yesql:"hot_tags"` HotTags *sqlx.Stmt `yesql:"hot_tags"`
InsertTag *sqlx.Stmt `yesql:"insert_tag"` InsertTag *sqlx.Stmt `yesql:"insert_tag"`
NewestTags *sqlx.Stmt `yesql:"newest_tags"` NewestTags *sqlx.Stmt `yesql:"newest_tags"`
PinTopic *sqlx.Stmt `yesql:"pin_topic"`
StickTopic *sqlx.Stmt `yesql:"stick_topic"` StickTopic *sqlx.Stmt `yesql:"stick_topic"`
TagsByKeywordA *sqlx.Stmt `yesql:"tags_by_keyword_a"` TagsByKeywordA *sqlx.Stmt `yesql:"tags_by_keyword_a"`
TagsByKeywordB *sqlx.Stmt `yesql:"tags_by_keyword_b"` TagsByKeywordB *sqlx.Stmt `yesql:"tags_by_keyword_b"`
TopicIsPin *sqlx.Stmt `yesql:"topic_is_pin"`
TopicIsTop *sqlx.Stmt `yesql:"topic_is_top"` TopicIsTop *sqlx.Stmt `yesql:"topic_is_top"`
UnfollowTopic *sqlx.Stmt `yesql:"unfollow_topic"` UnfollowTopic *sqlx.Stmt `yesql:"unfollow_topic"`
} }
@ -161,6 +167,9 @@ func BuildTopicA(p PreparexBuilder, ctx ...context.Context) (obj *TopicA, err er
if obj.ExistTopicUser, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_ExistTopicUser))); err != nil { if obj.ExistTopicUser, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_ExistTopicUser))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_ExistTopicUser error: %w", err) return nil, fmt.Errorf("prepare _TopicA_ExistTopicUser error: %w", err)
} }
if obj.FollowPinTags, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_FollowPinTags))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_FollowPinTags error: %w", err)
}
if obj.FollowTags, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_FollowTags))); err != nil { if obj.FollowTags, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_FollowTags))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_FollowTags error: %w", err) return nil, fmt.Errorf("prepare _TopicA_FollowTags error: %w", err)
} }
@ -176,6 +185,9 @@ func BuildTopicA(p PreparexBuilder, ctx ...context.Context) (obj *TopicA, err er
if obj.NewestTags, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_NewestTags))); err != nil { if obj.NewestTags, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_NewestTags))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_NewestTags error: %w", err) return nil, fmt.Errorf("prepare _TopicA_NewestTags error: %w", err)
} }
if obj.PinTopic, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_PinTopic))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_PinTopic error: %w", err)
}
if obj.StickTopic, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_StickTopic))); err != nil { if obj.StickTopic, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_StickTopic))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_StickTopic error: %w", err) return nil, fmt.Errorf("prepare _TopicA_StickTopic error: %w", err)
} }
@ -185,6 +197,9 @@ func BuildTopicA(p PreparexBuilder, ctx ...context.Context) (obj *TopicA, err er
if obj.TagsByKeywordB, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_TagsByKeywordB))); err != nil { if obj.TagsByKeywordB, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_TagsByKeywordB))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_TagsByKeywordB error: %w", err) return nil, fmt.Errorf("prepare _TopicA_TagsByKeywordB error: %w", err)
} }
if obj.TopicIsPin, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_TopicIsPin))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_TopicIsPin error: %w", err)
}
if obj.TopicIsTop, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_TopicIsTop))); err != nil { if obj.TopicIsTop, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_TopicIsTop))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_TopicIsTop error: %w", err) return nil, fmt.Errorf("prepare _TopicA_TopicIsTop error: %w", err)
} }

@ -18,6 +18,8 @@ var (
const ( const (
_TopicA_InsertTag = `INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1) RETURNING id` _TopicA_InsertTag = `INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1) RETURNING id`
_TopicA_PinTopic = `UPDATE @topic_user SET is_pin=1-is_pin, modified_on=? WHERE user_id=? AND topic_id=? AND is_del=0; RETURNING is_pin`
_TopicA_StickTopic = `UPDATE @topic_user SET is_top=1-is_top, modified_on=? WHERE user_id=? AND topic_id=? AND is_del=0; RETURNING is_top`
) )
// PreparexContext enhances the Conn interface with context. // PreparexContext enhances the Conn interface with context.
@ -43,6 +45,8 @@ type PreparexBuilder interface {
type TopicA struct { type TopicA struct {
yesql.Namespace `yesql:"topic_a"` yesql.Namespace `yesql:"topic_a"`
InsertTag *sqlx.Stmt `yesql:"insert_tag"` InsertTag *sqlx.Stmt `yesql:"insert_tag"`
PinTopic *sqlx.Stmt `yesql:"pin_topic"`
StickTopic *sqlx.Stmt `yesql:"stick_topic"`
} }
func BuildTopicA(p PreparexBuilder, ctx ...context.Context) (obj *TopicA, err error) { func BuildTopicA(p PreparexBuilder, ctx ...context.Context) (obj *TopicA, err error) {
@ -56,5 +60,11 @@ func BuildTopicA(p PreparexBuilder, ctx ...context.Context) (obj *TopicA, err er
if obj.InsertTag, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_InsertTag))); err != nil { if obj.InsertTag, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_InsertTag))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_InsertTag error: %w", err) return nil, fmt.Errorf("prepare _TopicA_InsertTag error: %w", err)
} }
if obj.PinTopic, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_PinTopic))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_PinTopic error: %w", err)
}
if obj.StickTopic, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TopicA_StickTopic))); err != nil {
return nil, fmt.Errorf("prepare _TopicA_StickTopic error: %w", err)
}
return return
} }

@ -133,8 +133,12 @@ func (s *topicSrvA) GetNewestTags(userId int64, limit int, offset int) (res cs.T
return return
} }
func (s *topicSrvA) GetFollowTags(userId int64, limit int, offset int) (res cs.TagList, err error) { func (s *topicSrvA) GetFollowTags(userId int64, isPin bool, limit int, offset int) (res cs.TagList, err error) {
if err = s.q.FollowTags.Select(&res, userId, limit, offset); err != nil { stmt := s.q.FollowTags
if isPin {
stmt = s.q.FollowPinTags
}
if err = stmt.Select(&res, userId, limit, offset); err != nil {
return return
} }
return s.tagsFormatA(userId, res) return s.tagsFormatA(userId, res)
@ -164,6 +168,17 @@ func (s *topicSrvA) StickTopic(userId int64, topicId int64) (res int8, err error
return return
} }
func (s *topicSrvA) PinTopic(userId int64, topicId int64) (res int8, err error) {
s.db.Withx(func(tx *sqlx.Tx) error {
_, err = tx.Stmtx(s.q.PinTopic).Exec(time.Now().Unix(), userId, topicId)
if err == nil {
err = tx.Stmtx(s.q.TopicIsPin).Get(&res, userId, topicId)
}
return err
})
return
}
func (s *topicSrvA) tagsFormatA(userId int64, tags cs.TagList) (cs.TagList, error) { func (s *topicSrvA) tagsFormatA(userId int64, tags cs.TagList) (cs.TagList, error) {
if len(tags) == 0 { if len(tags) == 0 {
return tags, nil return tags, nil

@ -78,3 +78,13 @@ func (s *pgaTopicSrvA) UpsertTags(userId int64, tags []string) (res cs.TagInfoLi
}) })
return return
} }
func (s *pgaTopicSrvA) StickTopic(userId int64, topicId int64) (res int8, err error) {
err = s.p.StickTopic.Get(&res, time.Now().Unix(), userId, topicId)
return
}
func (s *pgaTopicSrvA) PinTopic(userId int64, topicId int64) (res int8, err error) {
err = s.p.PinTopic.Get(&res, time.Now().Unix(), userId, topicId)
return
}

@ -139,6 +139,7 @@ SELECT t.id id,
u.id "u.id", u.id "u.id",
1 as is_following, 1 as is_following,
c.is_top, c.is_top,
c.is_pin,
u.nickname "u.nickname", u.nickname "u.nickname",
u.username "u.username", u.username "u.username",
u.status "u.status", u.status "u.status",
@ -151,6 +152,29 @@ WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=?
ORDER BY c.is_top DESC, t.quote_num DESC ORDER BY c.is_top DESC, t.quote_num DESC
LIMIT ? OFFSET ?; LIMIT ? OFFSET ?;
-- name: follow_pin_tags@topic_a
-- get get follow tag information
-- prepare: stmt
SELECT t.id id,
t.user_id user_id,
t.tag tag,
t.quote_num quote_num,
u.id "u.id",
1 as is_following,
c.is_top,
c.is_pin,
u.nickname "u.nickname",
u.username "u.username",
u.status "u.status",
u.avatar "u.avatar",
u.is_admin "u.is_admin"
FROM @topic_user c
JOIN @user u ON c.user_id = u.id
JOIN @tag t ON c.topic_id = t.id
WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=? AND c.is_pin=1
ORDER BY c.is_top DESC, t.quote_num DESC
LIMIT ? OFFSET ?;
-- name: topic_infos@topic_a -- name: topic_infos@topic_a
-- prepare: raw -- prepare: raw
-- clause: in -- clause: in
@ -177,10 +201,20 @@ UPDATE @topic_user
SET is_top=1-is_top, modified_on=? SET is_top=1-is_top, modified_on=?
WHERE user_id=? AND topic_id=? AND is_del=0; WHERE user_id=? AND topic_id=? AND is_del=0;
-- name: pin_topic@topic_a
-- prepare: stmt
UPDATE @topic_user
SET is_pin=1-is_pin, modified_on=?
WHERE user_id=? AND topic_id=? AND is_del=0;
-- name: topic_is_top@topic_a -- name: topic_is_top@topic_a
-- prepare: stmt -- prepare: stmt
SELECT is_top FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0; SELECT is_top FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0;
-- name: topic_is_pin@topic_a
-- prepare: stmt
SELECT is_pin FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0;
-- name: tags_by_keyword_a@topic_a -- name: tags_by_keyword_a@topic_a
-- get tags by keyword -- get tags by keyword
SELECT id, user_id, tag, quote_num SELECT id, user_id, tag, quote_num

@ -12,3 +12,17 @@
INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num)
VALUES (?, ?, ?, ?, 1) VALUES (?, ?, ?, ?, 1)
RETURNING id; RETURNING id;
-- name: stick_topic@topic_a
-- prepare: stmt
UPDATE @topic_user
SET is_top=1-is_top, modified_on=?
WHERE user_id=? AND topic_id=? AND is_del=0;
RETURNING is_top;
-- name: pin_topic@topic_a
-- prepare: stmt
UPDATE @topic_user
SET is_pin=1-is_pin, modified_on=?
WHERE user_id=? AND topic_id=? AND is_del=0;
RETURNING is_pin;

Loading…
Cancel
Save