sqlx: fixed select limit/offset sql clause error in MySQL/Sqlite3

x/navis
Michael Li 1 year ago
parent 0c9053dd4d
commit d583d1649c
No known key found for this signature in database

@ -12,6 +12,6 @@ import (
type TopicService interface {
UpsertTags(userId int64, tags []string) (cs.TagInfoList, error)
DecrTagsById(ids []int64) error
ListTags(typ cs.TagType, offset int, limit int) (cs.TagList, error)
ListTags(typ cs.TagType, limit int, offset int) (cs.TagList, error)
TagsByKeyword(keyword string) (cs.TagInfoList, error)
}

@ -100,12 +100,12 @@ func (s *topicServant) DecrTagsById(ids []int64) error {
})
}
func (s *topicServant) ListTags(typ cs.TagType, offset int, limit int) (res cs.TagList, err error) {
func (s *topicServant) ListTags(typ cs.TagType, limit int, offset int) (res cs.TagList, err error) {
switch typ {
case cs.TagTypeHot:
err = s.stmtHotTags.Select(&res, offset, limit)
err = s.stmtHotTags.Select(&res, limit, offset)
case cs.TagTypeNew:
err = s.stmtNewestTags.Select(&res, offset, limit)
err = s.stmtNewestTags.Select(&res, limit, offset)
}
return
}
@ -123,10 +123,10 @@ func (s *topicServant) TagsByKeyword(keyword string) (res cs.TagInfoList, err er
func newTopicService(db *sqlx.DB) core.TopicService {
return &topicServant{
sqlxServant: newSqlxServant(db),
stmtNewestTags: c(`SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, 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 OFFSET ? LIMIT ?`),
stmtHotTags: c(`SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, 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 OFFSET ? LIMIT ?`),
stmtTagsByKeywordA: c(`SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 ORDER BY quote_num DESC OFFSET 0 LIMIT 6`),
stmtTagsByKeywordB: c(`SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC OFFSET 0 LIMIT 6`),
stmtNewestTags: c(`SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, 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 ?`),
stmtHotTags: c(`SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, 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 ?`),
stmtTagsByKeywordA: c(`SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 ORDER BY quote_num DESC LIMIT 6`),
stmtTagsByKeywordB: c(`SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6`),
stmtInsertTag: c(`INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1)`),
sqlTagsByIdA: r(`SELECT id FROM @tag WHERE id IN (?) AND is_del = 0 AND quote_num > 0`),
sqlTagsByIdB: r(`SELECT id, user_id, tag, quote_num FROM @tag WHERE id IN (?)`),

@ -535,7 +535,7 @@ func GetPostTags(param *PostTagsReq) (cs.TagList, error) {
if num > conf.AppSetting.MaxPageSize {
num = conf.AppSetting.MaxPageSize
}
tags, err := ds.ListTags(param.Type, 0, num)
tags, err := ds.ListTags(param.Type, num, 0)
if err != nil {
return nil, err
}

@ -75,7 +75,7 @@ func (s *pubSrv) TopicList(req *web.TopicListReq) (*web.TopicListResp, mir.Error
if num > conf.AppSetting.MaxPageSize {
num = conf.AppSetting.MaxPageSize
}
tags, err := s.Ds.ListTags(req.Type, 0, num)
tags, err := s.Ds.ListTags(req.Type, num, 0)
if err != nil {
return nil, _errGetPostTagsFailed
}

Loading…
Cancel
Save