From 45d0318c4da3c35823709278bc6017d7de82ad40 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 18 Aug 2023 08:19:19 +0800 Subject: [PATCH] sqlx: add timeline logic implement WIP: 90% --- internal/dao/sakila/sakila.go | 28 +-- internal/dao/sakila/sqlx.go | 10 + internal/dao/sakila/timeline.go | 165 ++++++-------- internal/dao/sakila/tweets.go | 65 ++---- internal/dao/sakila/yesql/cc/yesql.go | 302 ++++++++------------------ internal/dao/sakila/yesql/yesql.sql | 118 ++++++---- 6 files changed, 259 insertions(+), 429 deletions(-) diff --git a/internal/dao/sakila/sakila.go b/internal/dao/sakila/sakila.go index 678dc677..1f4f4052 100644 --- a/internal/dao/sakila/sakila.go +++ b/internal/dao/sakila/sakila.go @@ -56,23 +56,11 @@ func NewDataService() (core.DataService, core.VersionInfo) { var ( v core.VersionInfo cis core.CacheIndexService - ips core.IndexPostsService ) pvs := security.NewPhoneVerifyService() ams := NewAuthorizationManageService() ths := newTweetHelpService(_db) - - // initialize core.IndexPostsService - if cfg.If("Friendship") { - ips = newFriendIndexService(_db, ams, ths) - } else if cfg.If("Followship") { - ips = newFollowIndexService(_db, ths) - } else if cfg.If("Lightship") { - ips = newLightIndexService(_db, ths) - } else { - // default use lightship post index service - ips = newLightIndexService(_db, ths) - } + ips := newShipIndexService(_db, ams, ths) // initialize core.CacheIndexService cfg.On(cfg.Actions{ @@ -119,23 +107,11 @@ func NewWebDataServantA() (core.WebDataServantA, core.VersionInfo) { var ( v core.VersionInfo cis core.CacheIndexService - ips core.IndexPostsService ) // pvs := security.NewPhoneVerifyService() ams := NewAuthorizationManageService() ths := newTweetHelpService(_db) - - // initialize core.IndexPostsService - if cfg.If("Friendship") { - ips = newFriendIndexService(_db, ams, ths) - } else if cfg.If("Followship") { - ips = newFollowIndexService(_db, ths) - } else if cfg.If("Lightship") { - ips = newLightIndexService(_db, ths) - } else { - // default use lightship post index service - ips = newLightIndexService(_db, ths) - } + ips := newShipIndexService(_db, ams, ths) // initialize core.CacheIndexService cfg.On(cfg.Actions{ diff --git a/internal/dao/sakila/sqlx.go b/internal/dao/sakila/sqlx.go index 25190b01..0d24368f 100644 --- a/internal/dao/sakila/sqlx.go +++ b/internal/dao/sakila/sqlx.go @@ -39,6 +39,7 @@ func (s *sqlxSrv) with(handle func(tx *sqlx.Tx) error) error { return tx.Commit() } +//lint:ignore U1000 withTx func (s *sqlxSrv) withTx(ctx context.Context, opts *sql.TxOptions, handle func(*sqlx.Tx) error) error { tx, err := s.db.BeginTxx(ctx, opts) if err != nil { @@ -51,6 +52,7 @@ func (s *sqlxSrv) withTx(ctx context.Context, opts *sql.TxOptions, handle func(* return tx.Commit() } +//lint:ignore U1000 in func (s *sqlxSrv) in(query string, args ...any) (string, []any, error) { q, params, err := sqlx.In(query, args...) if err != nil { @@ -59,6 +61,7 @@ func (s *sqlxSrv) in(query string, args ...any) (string, []any, error) { return s.db.Rebind(q), params, nil } +//lint:ignore U1000 inExec func (s *sqlxSrv) inExec(query string, args ...any) (sql.Result, error) { q, params, err := sqlx.In(query, args...) if err != nil { @@ -102,6 +105,8 @@ func (s *sqlxSrv) inSelectx(queryer sqlx.Queryer, dest any, query string, args . } // inGetx get for in clause with Transcation +// +//lint:ignore U1000 inGetx func (s *sqlxSrv) inGetx(queryer sqlx.Queryer, dest any, query string, args ...any) error { q, params, err := sqlx.In(query, args...) if err != nil { @@ -117,10 +122,12 @@ func newSqlxSrv(db *sqlx.DB) *sqlxSrv { } } +//lint:ignore U1000 r func r(query string) string { return _db.Rebind(t(query)) } +//lint:ignore U1000 c func c(query string) *sqlx.Stmt { query = _db.Rebind(t(query)) stmt, err := _db.Preparex(query) @@ -130,6 +137,7 @@ func c(query string) *sqlx.Stmt { return stmt } +//lint:ignore U1000 n func n(query string) *sqlx.NamedStmt { query = t(query) stmt, err := _db.PrepareNamed(query) @@ -165,6 +173,8 @@ func t(query string) string { } // yesqlScan yesql.Scan help function +// +//lint:ignore U1000 yesqlScan func yesqlScan[T any](query yesql.SQLQuery, obj T) T { if err := yesql.Scan(obj, query); err != nil { logrus.Fatal(err) diff --git a/internal/dao/sakila/timeline.go b/internal/dao/sakila/timeline.go index 92941312..85e252bb 100644 --- a/internal/dao/sakila/timeline.go +++ b/internal/dao/sakila/timeline.go @@ -11,37 +11,22 @@ import ( "github.com/rocboss/paopao-ce/internal/core/ms" "github.com/rocboss/paopao-ce/internal/dao/sakila/yesql/cc" "github.com/rocboss/paopao-ce/pkg/debug" + "github.com/sirupsen/logrus" ) var ( - _ core.IndexPostsService = (*friendIndexSrv)(nil) - _ core.IndexPostsService = (*followIndexSrv)(nil) - _ core.IndexPostsService = (*lightIndexSrv)(nil) + _ core.IndexPostsService = (*shipIndexSrv)(nil) _ core.IndexPostsService = (*simpleIndexPostsSrv)(nil) - _ core.IndexPostsServantA = (*friendIndexSrvA)(nil) - _ core.IndexPostsServantA = (*followIndexSrvA)(nil) - _ core.IndexPostsServantA = (*lightIndexSrvA)(nil) + _ core.IndexPostsServantA = (*shipIndexSrvA)(nil) _ core.IndexPostsServantA = (*simpleIndexPostsSrvA)(nil) ) -type friendIndexSrv struct { +type shipIndexSrv struct { *sqlxSrv ams core.AuthorizationManageService ths core.TweetHelpService - q *cc.FriendIndex -} - -type followIndexSrv struct { - *sqlxSrv - ths core.TweetHelpService - q *cc.FollowIndex -} - -type lightIndexSrv struct { - *sqlxSrv - ths core.TweetHelpService - q *cc.LightIndex + q *cc.ShipIndex } type simpleIndexPostsSrv struct { @@ -50,23 +35,11 @@ type simpleIndexPostsSrv struct { q *cc.SimpleIndex } -type friendIndexSrvA struct { +type shipIndexSrvA struct { *sqlxSrv ams core.AuthorizationManageService ths core.TweetHelpServantA - q *cc.FriendIndexA -} - -type followIndexSrvA struct { - *sqlxSrv - ths core.TweetHelpServantA - q *cc.FollowIndexA -} - -type lightIndexSrvA struct { - *sqlxSrv - ths core.TweetHelpServantA - q *cc.LightIndexA + q *cc.ShipIndexA } type simpleIndexPostsSrvA struct { @@ -76,43 +49,57 @@ type simpleIndexPostsSrvA struct { } // IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; -func (s *friendIndexSrv) IndexPosts(user *ms.User, offset int, limit int) (*ms.IndexTweetList, error) { - // TODO - return nil, debug.ErrNotImplemented -} - -// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; -func (s *followIndexSrv) IndexPosts(user *ms.User, offset int, limit int) (*ms.IndexTweetList, error) { - // TODO - return nil, debug.ErrNotImplemented -} - -// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; -func (s *lightIndexSrv) IndexPosts(user *ms.User, offset int, limit int) (*ms.IndexTweetList, error) { - // TODO - return nil, debug.ErrNotImplemented +func (s *shipIndexSrv) IndexPosts(user *ms.User, offset int, limit int) (res *ms.IndexTweetList, err error) { + var posts []*ms.Post + res = &ms.IndexTweetList{} + switch { + case user == nil: + if err = s.q.IndexByGuest.Select(&posts, limit, offset); err == nil { + err = s.q.IndexCountByGuest.Get(&res.Total) + } + case user != nil && user.IsAdmin: + if err = s.q.IndexByAdmin.Select(&posts, limit, offset); err == nil { + err = s.q.IndexByAdmin.Get(&res.Total) + } + default: + friendIds, _ := s.ams.BeFriendIds(user.ID) + friendIds = append(friendIds, user.ID) + err = s.inSelect(&posts, s.q.IndexBySelf, user.ID, friendIds, limit, offset) + if err == nil { + err = s.inGet(&res.Total, s.q.IndexCountBySelf, user.ID, friendIds) + } + } + if err != nil { + logrus.Debugf("shipIndex.IndexPosts err: %s", err) + return + } + if res.Tweets, err = s.ths.MergePosts(posts); err != nil { + logrus.Debugf("shipIndex.IndexPosts merge posts err: %s", err) + return + } + return } // simpleCacheIndexGetPosts simpleCacheIndex 专属获取广场推文列表函数 -func (s *simpleIndexPostsSrv) IndexPosts(_user *ms.User, offset int, limit int) (*ms.IndexTweetList, error) { - // TODO - return nil, debug.ErrNotImplemented -} - -// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; -func (s *friendIndexSrvA) IndexPosts(user *ms.User, limit int, offset int) (*cs.TweetBox, error) { - // TODO - return nil, debug.ErrNotImplemented -} - -// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; -func (s *followIndexSrvA) IndexPosts(user *ms.User, limit int, offset int) (*cs.TweetBox, error) { - // TODO - return nil, debug.ErrNotImplemented +func (s *simpleIndexPostsSrv) IndexPosts(_user *ms.User, offset int, limit int) (res *ms.IndexTweetList, err error) { + var posts []*ms.Post + res = &ms.IndexTweetList{} + if err = s.q.Index.Select(&posts, limit, offset); err == nil { + err = s.q.IndexCount.Get(&res.Total) + } + if err != nil { + logrus.Debugf("simpleIndexPostsSrv.IndexPosts err: %s", err) + return + } + if res.Tweets, err = s.ths.MergePosts(posts); err != nil { + logrus.Debugf("shipIndex.IndexPosts merge posts err: %s", err) + return + } + return } // IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; -func (s *lightIndexSrvA) IndexPosts(user *ms.User, limit int, offset int) (*cs.TweetBox, error) { +func (s *shipIndexSrvA) IndexPosts(user *ms.User, limit int, offset int) (*cs.TweetBox, error) { // TODO return nil, debug.ErrNotImplemented } @@ -123,28 +110,12 @@ func (s *simpleIndexPostsSrvA) IndexPosts(_user *ms.User, limit int, offset int) return nil, debug.ErrNotImplemented } -func newFriendIndexService(db *sqlx.DB, ams core.AuthorizationManageService, ths core.TweetHelpService) core.IndexPostsService { - return &friendIndexSrv{ +func newShipIndexService(db *sqlx.DB, ams core.AuthorizationManageService, ths core.TweetHelpService) core.IndexPostsService { + return &shipIndexSrv{ ams: ams, ths: ths, sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildFriendIndex), - } -} - -func newFollowIndexService(db *sqlx.DB, ths core.TweetHelpService) core.IndexPostsService { - return &followIndexSrv{ - ths: ths, - sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildFollowIndex), - } -} - -func newLightIndexService(db *sqlx.DB, ths core.TweetHelpService) core.IndexPostsService { - return &lightIndexSrv{ - ths: ths, - sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildLightIndex), + q: mustBuild(db, cc.BuildShipIndex), } } @@ -156,35 +127,21 @@ func newSimpleIndexPostsService(db *sqlx.DB, ths core.TweetHelpService) core.Ind } } -func newFriendIndexServantA(db *sqlx.DB, ams core.AuthorizationManageService, ths core.TweetHelpServantA) core.IndexPostsServantA { - return &friendIndexSrvA{ +//lint:ignore U1000 newShipIndexServantA +func newShipIndexServantA(db *sqlx.DB, ams core.AuthorizationManageService, ths core.TweetHelpServantA) core.IndexPostsServantA { + return &shipIndexSrvA{ ams: ams, ths: ths, sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildFriendIndexA), - } -} - -func newFollowIndexServantA(db *sqlx.DB, ths core.TweetHelpServantA) core.IndexPostsServantA { - return &followIndexSrvA{ - ths: ths, - sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildFollowIndexA), - } -} - -func newLightIndexServantA(db *sqlx.DB, ths core.TweetHelpServantA) core.IndexPostsServantA { - return &lightIndexSrvA{ - ths: ths, - sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildLightIndexA), + q: mustBuildFn(db, cc.BuildShipIndexA), } } +//lint:ignore U1000 newSimpleIndexPostsServantA func newSimpleIndexPostsServantA(db *sqlx.DB, ths core.TweetHelpServantA) core.IndexPostsServantA { return &simpleIndexPostsSrvA{ ths: ths, sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildSimpleIndexA), + q: mustBuildFn(db, cc.BuildSimpleIndexA), } } diff --git a/internal/dao/sakila/tweets.go b/internal/dao/sakila/tweets.go index bb923af2..32814f14 100644 --- a/internal/dao/sakila/tweets.go +++ b/internal/dao/sakila/tweets.go @@ -421,40 +421,18 @@ func (s *tweetSrv) GetPostContentByID(id int64) (res *ms.PostContent, err error) } func (s *tweetSrvA) TweetInfoById(id int64) (*cs.TweetInfo, error) { - res := &cs.TweetInfo{} - err := s.q.TweetInfoById.Get(res, id) - return res, err + // TODO + return nil, debug.ErrNotImplemented } func (s *tweetSrvA) TweetItemById(id int64) (*cs.TweetItem, error) { - res := &cs.TweetItem{} - err := s.q.TweetItemById.Get(res, id) - if err != nil { - return nil, err - } - // TODO need add contents info to res - return res, nil + // TODO + return nil, debug.ErrNotImplemented } func (s *tweetSrvA) UserTweets(visitorId, userId int64) (res cs.TweetList, err error) { - res = cs.TweetList{} - switch s.checkRelationBy(visitorId, userId) { - case 0: // admin - err = s.q.UserTweetsByAdmin.Select(&res, userId) - case 1: // self - err = s.q.UserTweetsBySelf.Select(&res, userId) - case 2: // friend - err = s.q.UserTweetsByFriend.Select(&res, userId) - case 3: // follower - fallthrough - default: // guest - err = s.q.UserTweetsByGuest.Select(&res, userId) - } - if err != nil { - return nil, err - } - // TODO need add contents info to res - return res, nil + // TODO + return nil, debug.ErrNotImplemented } // checkRelationBy check the relation of visitor with user @@ -464,33 +442,28 @@ func (s *tweetSrvA) checkRelationBy(visitorId, userId int64) uint { } func (s *tweetSrvA) ReactionByTweetId(userId int64, tweetId int64) (*cs.ReactionItem, error) { - res := &cs.ReactionItem{} - err := s.q.ReactionByTweetId.Get(res, userId, tweetId) - return res, err + // TODO + return nil, debug.ErrNotImplemented } func (s *tweetSrvA) UserReactions(userId int64, limit int, offset int) (cs.ReactionList, error) { - res := cs.ReactionList{} - err := s.q.UserReactions.Select(&res, userId) - return res, err + // TODO + return nil, debug.ErrNotImplemented } func (s *tweetSrvA) FavoriteByTweetId(userId int64, tweetId int64) (*cs.FavoriteItem, error) { - res := &cs.FavoriteItem{} - err := s.q.FavoriteByTweetId.Get(res, userId, tweetId) - return res, err + // TODO + return nil, debug.ErrNotImplemented } func (s *tweetSrvA) UserFavorites(userId int64, limit int, offset int) (cs.FavoriteList, error) { - res := cs.FavoriteList{} - err := s.q.UserFavorites.Select(&res, userId) - return res, err + // TODO + return nil, debug.ErrNotImplemented } func (s *tweetSrvA) AttachmentByTweetId(userId int64, tweetId int64) (*cs.AttachmentBill, error) { - res := &cs.AttachmentBill{} - err := s.q.AttachmentByTweetId.Get(res, userId, tweetId) - return res, err + // TODO + return nil, debug.ErrNotImplemented } func (s *tweetManageSrvA) CreateAttachment(obj *cs.Attachment) (int64, error) { @@ -578,7 +551,7 @@ func newTweetHelpService(db *sqlx.DB) core.TweetHelpService { func newTweetServantA(db *sqlx.DB) core.TweetServantA { return &tweetSrvA{ sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildTweetA), + q: mustBuildFn(db, cc.BuildTweetA), } } @@ -586,13 +559,13 @@ func newTweetManageServantA(db *sqlx.DB, cacheIndex core.CacheIndexService) core return &tweetManageSrvA{ sqlxSrv: newSqlxSrv(db), cis: cacheIndex, - q: mustBuild(db, cc.BuildTweetManageA), + q: mustBuildFn(db, cc.BuildTweetManageA), } } func newTweetHelpServantA(db *sqlx.DB) core.TweetHelpServantA { return &tweetHelpSrvA{ sqlxSrv: newSqlxSrv(db), - q: mustBuild(db, cc.BuildTweetHelpA), + q: mustBuildFn(db, cc.BuildTweetHelpA), } } diff --git a/internal/dao/sakila/yesql/cc/yesql.go b/internal/dao/sakila/yesql/cc/yesql.go index 0c114d0a..a92dc147 100644 --- a/internal/dao/sakila/yesql/cc/yesql.go +++ b/internal/dao/sakila/yesql/cc/yesql.go @@ -50,13 +50,7 @@ const ( _ContactManager_ListFriend = `SELECT c.friend_id user_id, u.username username, u.nickname nickname, u.avatar avatar, u.phone phone FROM @contact c JOIN @user u ON c.friend_id=u.id WHERE user_id=? AND status=2 AND is_del=0 ORDER BY u.nickname ASC LIMIT ? OFFSET ?` _ContactManager_RejectFriendMsgsUpdate = `UPDATE @message SET reply_id=?, modified_on=? WHERE sender_user_id = ? AND receiver_user_id = ? AND type = ? AND reply_id = ?` _ContactManager_TotalFriendsById = `SELECT count(*) FROM @contact WHERE user_id=? AND status=2 AND is_del=0` - _FollowIndexA_UserInfo = `SELECT * FROM @user WHERE username=?` - _FollowIndex_UserInfo = `SELECT * FROM @user WHERE username=?` _FollowingManager_CreateFollowing = `INSERT INTO @following (user_id, follow_id, created_on) VALUES (?, ?, ?)` - _FriendIndexA_UserInfo = `SELECT * FROM @user WHERE username=?` - _FriendIndex_UserInfo = `SELECT * FROM @user WHERE username=?` - _LightIndexA_UserInfo = `SELECT * FROM @user WHERE username=?` - _LightIndex_UserInfo = `SELECT * FROM @user WHERE username=?` _Message_CreateMessage = `INSERT INTO @message (sender_user_id, receiver_user_id, type, brief, content, post_id, comment_id, reply_id, created_on) VALUES (:sender_user_id, :receiver_user_id, :type, :brief, :content, :post_id, :comment_id, :reply_id, :created_on)` _Message_GetMessageById = `SELECT * FROM @message WHERE id=? AND is_del=0` _Message_GetMessageCount = `SELECT count(*) FROM @message WHERE receiver_user_id=:recerver_user_id AND is_del=0` @@ -66,8 +60,16 @@ const ( _Security_CreatePhoneCaptcha = `INSERT INTO @captcha (phone, captcha, expired_on, created_on) VALUES (:phone, :captcha, :expired_on, :created_on)` _Security_GetLatestPhoneCaptcha = `SELECT * FROM @captcha WHERE phone=? AND is_del=0` _Security_UsePhoneCaptcha = `UPDATE @captcha SET use_times=use_times+1, modified_on=? WHERE id=? AND is_del=0` + _ShipIndexA_UserInfo = `SELECT * FROM @user WHERE username=?` + _ShipIndex_IndexByAdmin = `SELECT * FROM @p_post WHERE is_del=0 ORDER BY is_top DESC, latest_replied_on DESC LIMIT ? OFFSET ?` + _ShipIndex_IndexByGuest = `SELECT * FROM @p_post WHERE visibility=0 AND is_del=0 ORDER BY is_top DESC, latest_replied_on DESC LIMIT ? OFFSET ?` + _ShipIndex_IndexBySelf = `SELECT * FROM @p_post WHERE is_del=0 AND (visibility=0 OR (visibility=1 AND user_id=?) OR (visibility=2 AND user_id IN ?)) ORDER BY is_top DESC, latest_replied_on DESC LIMIT ? OFFSET ?` + _ShipIndex_IndexCountByAdmin = `SELECT count(*) FROM @p_post WHERE is_del=0` + _ShipIndex_IndexCountByGuest = `SELECT count(*) FROM @p_post WHERE visibility=0 AND is_del=0` + _ShipIndex_IndexCountBySelf = `SELECT count(*) FROM @p_post WHERE is_del=0 AND (visibility=0 OR (visibility=1 AND user_id=?) OR (visibility=2 AND user_id IN ?))` _SimpleIndexA_UserInfo = `SELECT * FROM @user WHERE username=?` - _SimpleIndex_UserInfo = `SELECT * FROM @user WHERE username=?` + _SimpleIndex_Index = `SELECT * FROM @p_post WHERE visibility=0 ORDER BY is_top DESC, latest_replied_on DESC LIMIT ? OFFSET ?` + _SimpleIndex_IndexCount = `SELECT count(*) FROM @p_post WHERE visibility=0` _TopicA_DecrTagsById = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?)` _TopicA_HotTags = `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 ?` _TopicA_IncrTagsById = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?)` @@ -205,41 +207,11 @@ type ContactManager struct { CreateMessage *sqlx.NamedStmt `yesql:"create_message"` } -type FollowIndex struct { - yesql.Namespace `yesql:"follow_index"` - UserInfo *sqlx.Stmt `yesql:"user_info"` -} - -type FollowIndexA struct { - yesql.Namespace `yesql:"follow_index_a"` - UserInfo *sqlx.Stmt `yesql:"user_info"` -} - type FollowingManager struct { yesql.Namespace `yesql:"following_manager"` CreateFollowing *sqlx.Stmt `yesql:"create_following"` } -type FriendIndex struct { - yesql.Namespace `yesql:"friend_index"` - UserInfo *sqlx.Stmt `yesql:"user_info"` -} - -type FriendIndexA struct { - yesql.Namespace `yesql:"friend_index_a"` - UserInfo *sqlx.Stmt `yesql:"user_info"` -} - -type LightIndex struct { - yesql.Namespace `yesql:"light_index"` - UserInfo *sqlx.Stmt `yesql:"user_info"` -} - -type LightIndexA struct { - yesql.Namespace `yesql:"light_index_a"` - UserInfo *sqlx.Stmt `yesql:"user_info"` -} - type Message struct { yesql.Namespace `yesql:"message"` GetMessageById *sqlx.Stmt `yesql:"get_message_by_id"` @@ -257,14 +229,30 @@ type Security struct { CreatePhoneCaptcha *sqlx.NamedStmt `yesql:"create_phone_captcha"` } +type ShipIndex struct { + yesql.Namespace `yesql:"ship_index"` + IndexBySelf string `yesql:"index_by_self"` + IndexCountBySelf string `yesql:"index_count_by_self"` + IndexByAdmin *sqlx.Stmt `yesql:"index_by_admin"` + IndexByGuest *sqlx.Stmt `yesql:"index_by_guest"` + IndexCountByAdmin *sqlx.Stmt `yesql:"index_count_by_admin"` + IndexCountByGuest *sqlx.Stmt `yesql:"index_count_by_guest"` +} + +type ShipIndexA struct { + yesql.Namespace `yesql:"ship_index_a"` + UserInfo string `yesql:"user_info"` +} + type SimpleIndex struct { yesql.Namespace `yesql:"simple_index"` - UserInfo *sqlx.Stmt `yesql:"user_info"` + Index *sqlx.Stmt `yesql:"index"` + IndexCount *sqlx.Stmt `yesql:"index_count"` } type SimpleIndexA struct { yesql.Namespace `yesql:"simple_index_a"` - UserInfo *sqlx.Stmt `yesql:"user_info"` + UserInfo string `yesql:"user_info"` } type TopicA struct { @@ -301,18 +289,18 @@ type Tweet struct { type TweetA struct { yesql.Namespace `yesql:"tweet_a"` - AttachmentByTweetId *sqlx.Stmt `yesql:"attachment_by_tweet_id"` - FavoriteByTweetId *sqlx.Stmt `yesql:"favorite_by_tweet_id"` - ReactionByTweetId *sqlx.Stmt `yesql:"reaction_by_tweet_id"` - TweetInfoById *sqlx.Stmt `yesql:"tweet_info_by_id"` - TweetItemById *sqlx.Stmt `yesql:"tweet_item_by_id"` - UserFavorites *sqlx.Stmt `yesql:"user_favorites"` - UserInfo *sqlx.Stmt `yesql:"user_info"` - UserReactions *sqlx.Stmt `yesql:"user_reactions"` - UserTweetsByAdmin *sqlx.Stmt `yesql:"user_tweets_by_admin"` - UserTweetsByFriend *sqlx.Stmt `yesql:"user_tweets_by_friend"` - UserTweetsByGuest *sqlx.Stmt `yesql:"user_tweets_by_guest"` - UserTweetsBySelf *sqlx.Stmt `yesql:"user_tweets_by_self"` + AttachmentByTweetId string `yesql:"attachment_by_tweet_id"` + FavoriteByTweetId string `yesql:"favorite_by_tweet_id"` + ReactionByTweetId string `yesql:"reaction_by_tweet_id"` + TweetInfoById string `yesql:"tweet_info_by_id"` + TweetItemById string `yesql:"tweet_item_by_id"` + UserFavorites string `yesql:"user_favorites"` + UserInfo string `yesql:"user_info"` + UserReactions string `yesql:"user_reactions"` + UserTweetsByAdmin string `yesql:"user_tweets_by_admin"` + UserTweetsByFriend string `yesql:"user_tweets_by_friend"` + UserTweetsByGuest string `yesql:"user_tweets_by_guest"` + UserTweetsBySelf string `yesql:"user_tweets_by_self"` } type TweetHelp struct { @@ -323,7 +311,7 @@ type TweetHelp struct { type TweetHelpA struct { yesql.Namespace `yesql:"tweet_help_a"` - UserInfo *sqlx.Stmt `yesql:"user_info"` + UserInfo string `yesql:"user_info"` } type TweetManage struct { @@ -350,7 +338,7 @@ type TweetManage struct { type TweetManageA struct { yesql.Namespace `yesql:"tweet_manage_a"` - UserInfo *sqlx.Stmt `yesql:"user_info"` + UserInfo string `yesql:"user_info"` } type UserManage struct { @@ -542,34 +530,6 @@ func BuildContactManager(p yesql.PreparexBuilder, ctx ...context.Context) (obj * return } -func BuildFollowIndex(p yesql.PreparexBuilder, ctx ...context.Context) (obj *FollowIndex, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &FollowIndex{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowIndex_UserInfo))); err != nil { - return - } - return -} - -func BuildFollowIndexA(p yesql.PreparexBuilder, ctx ...context.Context) (obj *FollowIndexA, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &FollowIndexA{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowIndexA_UserInfo))); err != nil { - return - } - return -} - func BuildFollowingManager(p yesql.PreparexBuilder, ctx ...context.Context) (obj *FollowingManager, err error) { var c context.Context if len(ctx) > 0 && ctx[0] != nil { @@ -584,62 +544,6 @@ func BuildFollowingManager(p yesql.PreparexBuilder, ctx ...context.Context) (obj return } -func BuildFriendIndex(p yesql.PreparexBuilder, ctx ...context.Context) (obj *FriendIndex, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &FriendIndex{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FriendIndex_UserInfo))); err != nil { - return - } - return -} - -func BuildFriendIndexA(p yesql.PreparexBuilder, ctx ...context.Context) (obj *FriendIndexA, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &FriendIndexA{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FriendIndexA_UserInfo))); err != nil { - return - } - return -} - -func BuildLightIndex(p yesql.PreparexBuilder, ctx ...context.Context) (obj *LightIndex, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &LightIndex{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_LightIndex_UserInfo))); err != nil { - return - } - return -} - -func BuildLightIndexA(p yesql.PreparexBuilder, ctx ...context.Context) (obj *LightIndexA, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &LightIndexA{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_LightIndexA_UserInfo))); err != nil { - return - } - return -} - func BuildMessage(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Message, err error) { var c context.Context if len(ctx) > 0 && ctx[0] != nil { @@ -689,31 +593,60 @@ func BuildSecurity(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Securi return } -func BuildSimpleIndex(p yesql.PreparexBuilder, ctx ...context.Context) (obj *SimpleIndex, err error) { +func BuildShipIndex(p yesql.PreparexBuilder, ctx ...context.Context) (obj *ShipIndex, err error) { var c context.Context if len(ctx) > 0 && ctx[0] != nil { c = ctx[0] } else { c = context.Background() } - obj = &SimpleIndex{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_SimpleIndex_UserInfo))); err != nil { + obj = &ShipIndex{ + IndexBySelf: p.QueryHook(_ShipIndex_IndexBySelf), + IndexCountBySelf: p.QueryHook(_ShipIndex_IndexCountBySelf), + } + if obj.IndexByAdmin, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ShipIndex_IndexByAdmin))); err != nil { + return + } + if obj.IndexByGuest, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ShipIndex_IndexByGuest))); err != nil { + return + } + if obj.IndexCountByAdmin, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ShipIndex_IndexCountByAdmin))); err != nil { + return + } + if obj.IndexCountByGuest, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ShipIndex_IndexCountByGuest))); err != nil { return } return } -func BuildSimpleIndexA(p yesql.PreparexBuilder, ctx ...context.Context) (obj *SimpleIndexA, err error) { +func BuildShipIndexA(p yesql.PreparexBuilder) (obj *ShipIndexA, err error) { + obj = &ShipIndexA{ + UserInfo: p.QueryHook(_ShipIndexA_UserInfo), + } + return +} + +func BuildSimpleIndex(p yesql.PreparexBuilder, ctx ...context.Context) (obj *SimpleIndex, err error) { var c context.Context if len(ctx) > 0 && ctx[0] != nil { c = ctx[0] } else { c = context.Background() } - obj = &SimpleIndexA{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_SimpleIndexA_UserInfo))); err != nil { + obj = &SimpleIndex{} + if obj.Index, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_SimpleIndex_Index))); err != nil { return } + if obj.IndexCount, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_SimpleIndex_IndexCount))); err != nil { + return + } + return +} + +func BuildSimpleIndexA(p yesql.PreparexBuilder) (obj *SimpleIndexA, err error) { + obj = &SimpleIndexA{ + UserInfo: p.QueryHook(_SimpleIndexA_UserInfo), + } return } @@ -793,49 +726,20 @@ func BuildTweet(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Tweet, er return } -func BuildTweetA(p yesql.PreparexBuilder, ctx ...context.Context) (obj *TweetA, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &TweetA{} - if obj.AttachmentByTweetId, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_AttachmentByTweetId))); err != nil { - return - } - if obj.FavoriteByTweetId, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_FavoriteByTweetId))); err != nil { - return - } - if obj.ReactionByTweetId, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_ReactionByTweetId))); err != nil { - return - } - if obj.TweetInfoById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_TweetInfoById))); err != nil { - return - } - if obj.TweetItemById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_TweetItemById))); err != nil { - return - } - if obj.UserFavorites, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_UserFavorites))); err != nil { - return - } - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_UserInfo))); err != nil { - return - } - if obj.UserReactions, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_UserReactions))); err != nil { - return - } - if obj.UserTweetsByAdmin, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_UserTweetsByAdmin))); err != nil { - return - } - if obj.UserTweetsByFriend, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_UserTweetsByFriend))); err != nil { - return - } - if obj.UserTweetsByGuest, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_UserTweetsByGuest))); err != nil { - return - } - if obj.UserTweetsBySelf, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetA_UserTweetsBySelf))); err != nil { - return +func BuildTweetA(p yesql.PreparexBuilder) (obj *TweetA, err error) { + obj = &TweetA{ + AttachmentByTweetId: p.QueryHook(_TweetA_AttachmentByTweetId), + FavoriteByTweetId: p.QueryHook(_TweetA_FavoriteByTweetId), + ReactionByTweetId: p.QueryHook(_TweetA_ReactionByTweetId), + TweetInfoById: p.QueryHook(_TweetA_TweetInfoById), + TweetItemById: p.QueryHook(_TweetA_TweetItemById), + UserFavorites: p.QueryHook(_TweetA_UserFavorites), + UserInfo: p.QueryHook(_TweetA_UserInfo), + UserReactions: p.QueryHook(_TweetA_UserReactions), + UserTweetsByAdmin: p.QueryHook(_TweetA_UserTweetsByAdmin), + UserTweetsByFriend: p.QueryHook(_TweetA_UserTweetsByFriend), + UserTweetsByGuest: p.QueryHook(_TweetA_UserTweetsByGuest), + UserTweetsBySelf: p.QueryHook(_TweetA_UserTweetsBySelf), } return } @@ -848,16 +752,9 @@ func BuildTweetHelp(p yesql.PreparexBuilder) (obj *TweetHelp, err error) { return } -func BuildTweetHelpA(p yesql.PreparexBuilder, ctx ...context.Context) (obj *TweetHelpA, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &TweetHelpA{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetHelpA_UserInfo))); err != nil { - return +func BuildTweetHelpA(p yesql.PreparexBuilder) (obj *TweetHelpA, err error) { + obj = &TweetHelpA{ + UserInfo: p.QueryHook(_TweetHelpA_UserInfo), } return } @@ -922,16 +819,9 @@ func BuildTweetManage(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Twe return } -func BuildTweetManageA(p yesql.PreparexBuilder, ctx ...context.Context) (obj *TweetManageA, err error) { - var c context.Context - if len(ctx) > 0 && ctx[0] != nil { - c = ctx[0] - } else { - c = context.Background() - } - obj = &TweetManageA{} - if obj.UserInfo, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManageA_UserInfo))); err != nil { - return +func BuildTweetManageA(p yesql.PreparexBuilder) (obj *TweetManageA, err error) { + obj = &TweetManageA{ + UserInfo: p.QueryHook(_TweetManageA_UserInfo), } return } diff --git a/internal/dao/sakila/yesql/yesql.sql b/internal/dao/sakila/yesql/yesql.sql index 2cdac69e..5b413bfb 100644 --- a/internal/dao/sakila/yesql/yesql.sql +++ b/internal/dao/sakila/yesql/yesql.sql @@ -270,59 +270,83 @@ INSERT INTO @captcha (phone, captcha, expired_on, created_on) VALUES (:phone, :captcha, :expired_on, :created_on); -------------------------------------------------------------------------------- --- friend_index sql dml +-- ship_index sql dml -------------------------------------------------------------------------------- --- name: user_info@friend_index +-- name: index_by_admin@ship_index -- prepare: stmt -SELECT * FROM @user WHERE username=? - --------------------------------------------------------------------------------- --- follow_index sql dml --------------------------------------------------------------------------------- +SELECT * +FROM @p_post +WHERE is_del=0 +ORDER BY is_top DESC, latest_replied_on DESC +LIMIT ? OFFSET ?; --- name: user_info@follow_index +-- name: index_count_by_admin@ship_index -- prepare: stmt -SELECT * FROM @user WHERE username=? +SELECT count(*) +FROM @p_post +WHERE is_del=0; --------------------------------------------------------------------------------- --- light_index sql dml --------------------------------------------------------------------------------- +-- name: index_by_guest@ship_index +-- prepare: stmt +SELECT * +FROM @p_post +WHERE visibility=0 AND is_del=0 +ORDER BY is_top DESC, latest_replied_on DESC +LIMIT ? OFFSET ?; --- name: user_info@light_index +-- name: index_count_by_guest@ship_index -- prepare: stmt -SELECT * FROM @user WHERE username=? +SELECT count(*) +FROM @p_post +WHERE visibility=0 AND is_del=0; --------------------------------------------------------------------------------- --- simple_index sql dml --------------------------------------------------------------------------------- +-- name: index_by_self@ship_index +-- prepare: raw +-- clause: in +SELECT * +FROM @p_post +WHERE is_del=0 AND + (visibility=0 OR + (visibility=1 AND user_id=?) OR + (visibility=2 AND user_id IN ?)) +ORDER BY is_top DESC, latest_replied_on DESC +LIMIT ? OFFSET ?; --- name: user_info@simple_index --- prepare: stmt -SELECT * FROM @user WHERE username=? +-- name: index_count_by_self@ship_index +-- prepare: raw +-- clause: in +SELECT count(*) +FROM @p_post +WHERE is_del=0 AND + (visibility=0 OR + (visibility=1 AND user_id=?) OR + (visibility=2 AND user_id IN ?)); -------------------------------------------------------------------------------- --- friend_index_a sql dml +-- simple_index sql dml -------------------------------------------------------------------------------- --- name: user_info@friend_index_a +-- name: index@simple_index -- prepare: stmt -SELECT * FROM @user WHERE username=? - --------------------------------------------------------------------------------- --- follow_index_a sql dml --------------------------------------------------------------------------------- +SELECT * +FROM @p_post +WHERE visibility=0 +ORDER BY is_top DESC, latest_replied_on DESC +LIMIT ? OFFSET ?; --- name: user_info@follow_index_a +-- name: index_count@simple_index -- prepare: stmt -SELECT * FROM @user WHERE username=? +SELECT count(*) +FROM @p_post +WHERE visibility=0; -------------------------------------------------------------------------------- --- light_index_a sql dml +-- ship_index_a sql dml -------------------------------------------------------------------------------- --- name: user_info@light_index_a --- prepare: stmt +-- name: user_info@ship_index_a +-- prepare: raw SELECT * FROM @user WHERE username=? -------------------------------------------------------------------------------- @@ -330,7 +354,7 @@ SELECT * FROM @user WHERE username=? -------------------------------------------------------------------------------- -- name: user_info@simple_index_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -------------------------------------------------------------------------------- @@ -653,51 +677,51 @@ WHERE id IN (?) AND is_del=0; -------------------------------------------------------------------------------- -- name: user_info@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: tweet_info_by_id@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: tweet_item_by_id@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: user_tweets_by_admin@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: user_tweets_by_self@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: user_tweets_by_friend@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: user_tweets_by_guest@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: reaction_by_tweet_id@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: user_reactions@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: favorite_by_tweet_id@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: user_favorites@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -- name: attachment_by_tweet_id@tweet_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -------------------------------------------------------------------------------- @@ -705,7 +729,7 @@ SELECT * FROM @user WHERE username=? -------------------------------------------------------------------------------- -- name: user_info@tweet_manage_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? -------------------------------------------------------------------------------- @@ -713,7 +737,7 @@ SELECT * FROM @user WHERE username=? -------------------------------------------------------------------------------- -- name: user_info@tweet_help_a --- prepare: stmt +-- prepare: raw SELECT * FROM @user WHERE username=? --------------------------------------------------------------------------------