diff --git a/internal/dao/sakila/auto/pgc/pgc.go b/internal/dao/sakila/auto/pgc/pgc.go index 00780f9f..12143fa6 100644 --- a/internal/dao/sakila/auto/pgc/pgc.go +++ b/internal/dao/sakila/auto/pgc/pgc.go @@ -22,6 +22,7 @@ const ( _CommentManage_CreateCommentReply = `INSERT INTO @comment_reply (comment_id, user_id, content, at_user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING *` _ContactManager_CreateContact = `INSERT INTO @contact (user_id, friend_id, status, created_on) VALUES (?, ?, ?, ?) RETURNING *` _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) RETURNING id` + _Tweet_ListIndexHotsTweets = `SELECT post.* FROM @post post LEFT JOIN @post_metric metric ON post.id=metric.post_id WHERE post.visibility>=90 AND post.is_del=0 ORDER BY post.is_top DESC, metric.rank_score DESC NULLS LAST, post.latest_replied_on DESC LIMIT ? OFFSET ?` _TweetManage_AddAttachment = `INSERT INTO @attachment (user_id, file_size, img_width, img_height, type, content, created_on) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id` _TweetManage_AddPost = `INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, latest_replied_on, created_on) VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replied_on, :created_on) RETURNING id` _TweetManage_AddPostCollection = `INSERT INTO @post_collection (post_id, user_id, created_on) VALUES (?, ?, ?) RETURNING *` @@ -68,6 +69,11 @@ type Message struct { CreateMessage *sqlx.NamedStmt `yesql:"create_message"` } +type Tweet struct { + yesql.Namespace `yesql:"tweet"` + ListIndexHotsTweets *sqlx.Stmt `yesql:"list_index_hots_tweets"` +} + type TweetManage struct { yesql.Namespace `yesql:"tweet_manage"` AddAttachment *sqlx.Stmt `yesql:"add_attachment"` @@ -135,6 +141,20 @@ func BuildMessage(p PreparexBuilder, ctx ...context.Context) (obj *Message, err return } +func BuildTweet(p PreparexBuilder, ctx ...context.Context) (obj *Tweet, err error) { + var c context.Context + if len(ctx) > 0 && ctx[0] != nil { + c = ctx[0] + } else { + c = context.Background() + } + obj = &Tweet{} + if obj.ListIndexHotsTweets, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Tweet_ListIndexHotsTweets))); err != nil { + return nil, fmt.Errorf("prepare _Tweet_ListIndexHotsTweets error: %w", err) + } + return +} + func BuildTweetManage(p PreparexBuilder, ctx ...context.Context) (obj *TweetManage, err error) { var c context.Context if len(ctx) > 0 && ctx[0] != nil { diff --git a/internal/dao/sakila/tweets.go b/internal/dao/sakila/tweets.go index fb40b889..c5416969 100644 --- a/internal/dao/sakila/tweets.go +++ b/internal/dao/sakila/tweets.go @@ -562,11 +562,19 @@ func (s *tweetSrv) GetPostContentByID(id int64) (res *ms.PostContent, err error) return db.Get[ms.PostContent](s.q.GetPostContentById, &res, id) } -func newTweetService(db *sqlx.DB) core.TweetService { - return &tweetSrv{ +func newTweetService(db *sqlx.DB) (s core.TweetService) { + ts := &tweetSrv{ sqlxSrv: newSqlxSrv(db), q: ccBuild(db, cc.BuildTweet), } + s = ts + if cfg.Any("PostgreSQL", "PgSQL", "Postgres") { + s = &pgcTweetSrv{ + tweetSrv: ts, + p: pgcBuild(db, pgc.BuildTweet), + } + } + return } func newTweetManageService(db *sqlx.DB, cacheIndex core.CacheIndexService) (s core.TweetManageService) { diff --git a/internal/dao/sakila/tweets_pgc.go b/internal/dao/sakila/tweets_pgc.go index e4f38772..8fd177b3 100644 --- a/internal/dao/sakila/tweets_pgc.go +++ b/internal/dao/sakila/tweets_pgc.go @@ -17,11 +17,23 @@ var ( _ core.TweetManageService = (*pgcTweetManageSrv)(nil) ) +type pgcTweetSrv struct { + *tweetSrv + p *pgc.Tweet +} + type pgcTweetManageSrv struct { *tweetManageSrv p *pgc.TweetManage } +func (s *pgcTweetSrv) ListIndexHotsTweets(limit int, offset int) (res []*ms.Post, total int64, err error) { + if err = s.p.ListIndexHotsTweets.Select(&res, limit, offset); err == nil { + err = s.q.CountIndexHotsTweets.Get(&total) + } + return +} + func (s *pgcTweetManageSrv) CreatePost(r *ms.Post) (*ms.Post, error) { now := time.Now().Unix() r.Model = &ms.Model{CreatedOn: now} diff --git a/internal/dao/sakila/yesql/yesql_pgc.sql b/internal/dao/sakila/yesql/yesql_pgc.sql index ecca700c..edf98ea4 100644 --- a/internal/dao/sakila/yesql/yesql_pgc.sql +++ b/internal/dao/sakila/yesql/yesql_pgc.sql @@ -4,6 +4,20 @@ -- version is c* -------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-- tweet sql dml +-------------------------------------------------------------------------------- + +-- name: list_index_hots_tweets@tweet +-- prepare: stmt +SELECT post.* +FROM @post post +LEFT JOIN @post_metric metric +ON post.id=metric.post_id +WHERE post.visibility>=90 AND post.is_del=0 +ORDER BY post.is_top DESC, metric.rank_score DESC NULLS LAST, post.latest_replied_on DESC +LIMIT ? OFFSET ?; + -------------------------------------------------------------------------------- -- tweet_manage sql dml --------------------------------------------------------------------------------