diff --git a/internal/dao/slonik/authority.go b/internal/dao/slonik/authority.go index b407f7a9..cb08e465 100644 --- a/internal/dao/slonik/authority.go +++ b/internal/dao/slonik/authority.go @@ -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), } } diff --git a/internal/dao/slonik/comments.go b/internal/dao/slonik/comments.go index c690dc3b..d4c0ece8 100644 --- a/internal/dao/slonik/comments.go +++ b/internal/dao/slonik/comments.go @@ -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), } } diff --git a/internal/dao/slonik/contacts.go b/internal/dao/slonik/contacts.go index e725989b..a03a9519 100644 --- a/internal/dao/slonik/contacts.go +++ b/internal/dao/slonik/contacts.go @@ -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), } } diff --git a/internal/dao/slonik/index.go b/internal/dao/slonik/index.go index 3a90fb64..373a54c6 100644 --- a/internal/dao/slonik/index.go +++ b/internal/dao/slonik/index.go @@ -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), } } diff --git a/internal/dao/slonik/messages.go b/internal/dao/slonik/messages.go index 635f8eac..51742aca 100644 --- a/internal/dao/slonik/messages.go +++ b/internal/dao/slonik/messages.go @@ -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), } } diff --git a/internal/dao/slonik/pgx.go b/internal/dao/slonik/pgx.go index a41251a1..d14c5f07 100644 --- a/internal/dao/slonik/pgx.go +++ b/internal/dao/slonik/pgx.go @@ -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() diff --git a/internal/dao/slonik/security.go b/internal/dao/slonik/security.go index e87a670a..00281421 100644 --- a/internal/dao/slonik/security.go +++ b/internal/dao/slonik/security.go @@ -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), } } diff --git a/internal/dao/slonik/topics.go b/internal/dao/slonik/topics.go index 76e9201a..63f4efa3 100644 --- a/internal/dao/slonik/topics.go +++ b/internal/dao/slonik/topics.go @@ -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), } } diff --git a/internal/dao/slonik/tweets.go b/internal/dao/slonik/tweets.go index 7528ec7f..f07ff198 100644 --- a/internal/dao/slonik/tweets.go +++ b/internal/dao/slonik/tweets.go @@ -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), } } diff --git a/internal/dao/slonik/user.go b/internal/dao/slonik/user.go index 71f9806f..94035833 100644 --- a/internal/dao/slonik/user.go +++ b/internal/dao/slonik/user.go @@ -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), } } diff --git a/internal/dao/slonik/wallet.go b/internal/dao/slonik/wallet.go index 4c7da5df..1ef38375 100644 --- a/internal/dao/slonik/wallet.go +++ b/internal/dao/slonik/wallet.go @@ -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), } }