sqlc: optimize pgx base servant logic

r/paopao-ce-pro
Michael Li 2 years ago
parent 20eab760ea
commit d666b73e83
No known key found for this signature in database

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -16,8 +15,7 @@ var (
)
type authorizationManageServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
func (s *authorizationManageServant) IsAllow(user *core.User, action *core.Action) bool {
@ -52,7 +50,6 @@ func (s *authorizationManageServant) isFriend(userId int64, friendId int64) bool
func newAuthorizationManageService(db *pgx.Conn) core.AuthorizationManageService {
return &authorizationManageServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -17,13 +16,11 @@ var (
)
type commentServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
type commentManageServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
func (s *commentServant) GetComments(conditions *core.ConditionsT, offset, limit int) ([]*core.Comment, error) {
@ -94,14 +91,12 @@ func (s *commentManageServant) CreateCommentContent(content *core.CommentContent
func newCommentService(db *pgx.Conn) core.CommentService {
return &commentServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}
func newCommentManageService(db *pgx.Conn) core.CommentManageService {
return &commentManageServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -16,8 +15,7 @@ var (
)
type contactManageServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
func (s *contactManageServant) RequestingFriend(userId int64, friendId int64, greetings string) (err error) {
@ -58,7 +56,6 @@ func (s *contactManageServant) IsFriend(userId int64, friendId int64) bool {
func newContactManageService(db *pgx.Conn) core.ContactManageService {
return &contactManageServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -17,13 +16,11 @@ var (
)
type indexPostsServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
type simpleIndexPostsServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
// IndexPosts 根据userId查询广场推文列表简单做到不同用户的主页都是不同的
@ -42,14 +39,12 @@ func (s *simpleIndexPostsServant) IndexPosts(_user *core.User, offset int, limit
func newIndexPostsService(db *pgx.Conn) core.IndexPostsService {
return &indexPostsServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}
func newSimpleIndexPostsService(db *pgx.Conn) core.IndexPostsService {
return &simpleIndexPostsServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -16,8 +15,7 @@ var (
)
type messageServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
func (s *messageServant) CreateMessage(msg *core.Message) (*core.Message, error) {
@ -58,7 +56,6 @@ func (s *messageServant) GetMessageCount(conditions *core.ConditionsT) (int64, e
func newMessageService(db *pgx.Conn) core.MessageService {
return &messageServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -5,10 +5,12 @@
package slonik
import (
"context"
"sync"
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/conf"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
)
var (
@ -16,6 +18,58 @@ var (
_oncePgx sync.Once
)
type pgxServant struct {
db *pgx.Conn
q dbr.Querier
}
func (s *pgxServant) begin(ctx context.Context) (pgx.Tx, dbr.Querier, error) {
tx, err := s.db.Begin(ctx)
if err != nil {
return nil, nil, err
}
return tx, dbr.New(tx), nil
}
func (s *pgxServant) beingTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, dbr.Querier, error) {
tx, err := s.db.BeginTx(ctx, txOptions)
if err != nil {
return nil, nil, err
}
return tx, dbr.New(tx), nil
}
func (s *pgxServant) with(handle func(dbr.Querier) error) error {
ctx := context.Background()
tx, err := s.db.Begin(ctx)
if err != nil {
return err
}
if err = handle(dbr.New(tx)); err == nil {
return tx.Commit(ctx)
}
return tx.Rollback(ctx)
}
func (s *pgxServant) withTx(txOptions pgx.TxOptions, handle func(dbr.Querier) error) error {
ctx := context.Background()
tx, err := s.db.BeginTx(ctx, txOptions)
if err != nil {
return err
}
if err = handle(dbr.New(tx)); err == nil {
return tx.Commit(ctx)
}
return tx.Rollback(ctx)
}
func newPgxServant(db *pgx.Conn) *pgxServant {
return &pgxServant{
db: db,
q: dbr.New(db),
}
}
func pgxDB() *pgx.Conn {
_oncePgx.Do(func() {
_pgxDB = conf.MustPgxDB()

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -16,8 +15,7 @@ var (
)
type securityServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
// GetLatestPhoneCaptcha 获取最新短信验证码
@ -43,7 +41,6 @@ func (s *securityServant) SendPhoneCaptcha(phone string) error {
func newSecurityService(db *pgx.Conn, phoneVerify core.PhoneVerifyService) core.SecurityService {
return &securityServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -16,8 +15,7 @@ var (
)
type topicServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
func (s *topicServant) CreateTag(tag *core.Tag) (*core.Tag, error) {
@ -46,7 +44,6 @@ func (s *topicServant) GetTagsByKeyword(keyword string) ([]*core.Tag, error) {
func newTopicService(db *pgx.Conn) core.TopicService {
return &topicServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
"gorm.io/gorm"
)
@ -19,18 +18,15 @@ var (
)
type tweetServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
type tweetManageServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
type tweetHelpServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
// MergePosts post数据整合
@ -223,21 +219,18 @@ func (s *tweetServant) GetPostContentByID(id int64) (*core.PostContent, error) {
func newTweetService(db *pgx.Conn) core.TweetService {
return &tweetServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}
func newTweetManageService(db *pgx.Conn, cacheIndex core.CacheIndexService) core.TweetManageService {
return &tweetManageServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}
func newTweetHelpService(db *pgx.Conn) core.TweetHelpService {
return &tweetHelpServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -16,8 +15,7 @@ var (
)
type userManageServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
func (s *userManageServant) GetUserByID(id int64) (*core.User, error) {
@ -70,7 +68,6 @@ func (s *userManageServant) UpdateUser(user *core.User) error {
func newUserManageService(db *pgx.Conn) core.UserManageService {
return &userManageServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

@ -7,7 +7,6 @@ package slonik
import (
"github.com/jackc/pgx/v5"
"github.com/rocboss/paopao-ce/internal/core"
dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres"
"github.com/rocboss/paopao-ce/pkg/debug"
)
@ -16,8 +15,7 @@ var (
)
type walletServant struct {
db *pgx.Conn
q dbr.Querier
*pgxServant
}
func (s *walletServant) GetRechargeByID(id int64) (*core.WalletRecharge, error) {
@ -57,7 +55,6 @@ func (s *walletServant) HandlePostAttachmentBought(post *core.Post, user *core.U
func newWalletService(db *pgx.Conn) core.WalletService {
return &walletServant{
db: db,
q: dbr.New(db),
pgxServant: newPgxServant(db),
}
}

Loading…
Cancel
Save