sqlx: add sqlxServent with help method for db transcation logic

pull/351/head
Michael Li 2 years ago
parent 6890f4f5fc
commit ef2925389c
No known key found for this signature in database

@ -15,7 +15,7 @@ var (
) )
type authorizationManageServant struct { type authorizationManageServant struct {
db *sqlx.DB *sqlxServant
stmtIdx *sqlx.Stmt stmtIdx *sqlx.Stmt
stmtUpdateFriend *sqlx.Stmt stmtUpdateFriend *sqlx.Stmt
} }
@ -52,7 +52,7 @@ func (s *authorizationManageServant) isFriend(userId int64, friendId int64) bool
func newAuthorizationManageService(db *sqlx.DB) core.AuthorizationManageService { func newAuthorizationManageService(db *sqlx.DB) core.AuthorizationManageService {
return &authorizationManageServant{ return &authorizationManageServant{
db: db, sqlxServant: newSqlxServant(db),
stmtIdx: c(`SELECT * FROM @person WHERE first_name=?`), stmtIdx: c(`SELECT * FROM @person WHERE first_name=?`),
stmtUpdateFriend: c(`SELECT * FROM @person WHERE first_name=?`), stmtUpdateFriend: c(`SELECT * FROM @person WHERE first_name=?`),
} }

@ -16,13 +16,13 @@ var (
) )
type commentServant struct { type commentServant struct {
db *sqlx.DB *sqlxServant
stmtGetComments *sqlx.Stmt stmtGetComments *sqlx.Stmt
stmtGetReply *sqlx.Stmt stmtGetReply *sqlx.Stmt
} }
type commentManageServant struct { type commentManageServant struct {
db *sqlx.DB *sqlxServant
stmtDelComments *sqlx.Stmt stmtDelComments *sqlx.Stmt
stmtAddComents *sqlx.Stmt stmtAddComents *sqlx.Stmt
} }
@ -95,7 +95,7 @@ func (s *commentManageServant) CreateCommentContent(content *core.CommentContent
func newCommentService(db *sqlx.DB) core.CommentService { func newCommentService(db *sqlx.DB) core.CommentService {
return &commentServant{ return &commentServant{
db: db, sqlxServant: newSqlxServant(db),
stmtGetComments: c(`SELECT * FROM @person WHERE first_name=?`), stmtGetComments: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetReply: c(`SELECT * FROM @person WHERE first_name=?`), stmtGetReply: c(`SELECT * FROM @person WHERE first_name=?`),
} }
@ -103,7 +103,7 @@ func newCommentService(db *sqlx.DB) core.CommentService {
func newCommentManageService(db *sqlx.DB) core.CommentManageService { func newCommentManageService(db *sqlx.DB) core.CommentManageService {
return &commentManageServant{ return &commentManageServant{
db: db, sqlxServant: newSqlxServant(db),
stmtAddComents: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddComents: c(`SELECT * FROM @person WHERE first_name=?`),
stmtDelComments: c(`SELECT * FROM @person WHERE first_name=?`), stmtDelComments: c(`SELECT * FROM @person WHERE first_name=?`),
} }

@ -15,7 +15,7 @@ var (
) )
type contactManageServant struct { type contactManageServant struct {
db *sqlx.DB *sqlxServant
stmtAddFriend *sqlx.Stmt stmtAddFriend *sqlx.Stmt
stmtDelFriend *sqlx.Stmt stmtDelFriend *sqlx.Stmt
} }
@ -58,7 +58,7 @@ func (s *contactManageServant) IsFriend(userId int64, friendId int64) bool {
func newContactManageService(db *sqlx.DB) core.ContactManageService { func newContactManageService(db *sqlx.DB) core.ContactManageService {
return &contactManageServant{ return &contactManageServant{
db: db, sqlxServant: newSqlxServant(db),
stmtAddFriend: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddFriend: c(`SELECT * FROM @person WHERE first_name=?`),
stmtDelFriend: c(`SELECT * FROM @person WHERE first_name=?`), stmtDelFriend: c(`SELECT * FROM @person WHERE first_name=?`),
} }

@ -16,12 +16,12 @@ var (
) )
type indexPostsServant struct { type indexPostsServant struct {
db *sqlx.DB *sqlxServant
stmtIndex *sqlx.Stmt stmtIndex *sqlx.Stmt
} }
type simpleIndexPostsServant struct { type simpleIndexPostsServant struct {
db *sqlx.DB *sqlxServant
stmtIndex *sqlx.Stmt stmtIndex *sqlx.Stmt
} }
@ -41,14 +41,14 @@ func (s *simpleIndexPostsServant) IndexPosts(_user *core.User, offset int, limit
func newIndexPostsService(db *sqlx.DB) core.IndexPostsService { func newIndexPostsService(db *sqlx.DB) core.IndexPostsService {
return &indexPostsServant{ return &indexPostsServant{
db: db, sqlxServant: newSqlxServant(db),
stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`),
} }
} }
func newSimpleIndexPostsService(db *sqlx.DB) core.IndexPostsService { func newSimpleIndexPostsService(db *sqlx.DB) core.IndexPostsService {
return &simpleIndexPostsServant{ return &simpleIndexPostsServant{
db: db, sqlxServant: newSqlxServant(db),
stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`),
} }
} }

@ -15,7 +15,7 @@ var (
) )
type messageServant struct { type messageServant struct {
db *sqlx.DB *sqlxServant
stmtAddMsg *sqlx.Stmt stmtAddMsg *sqlx.Stmt
stmtGetMsg *sqlx.Stmt stmtGetMsg *sqlx.Stmt
stmtReadMsg *sqlx.Stmt stmtReadMsg *sqlx.Stmt
@ -59,7 +59,7 @@ func (s *messageServant) GetMessageCount(conditions *core.ConditionsT) (int64, e
func newMessageService(db *sqlx.DB) core.MessageService { func newMessageService(db *sqlx.DB) core.MessageService {
return &messageServant{ return &messageServant{
db: db, sqlxServant: newSqlxServant(db),
stmtAddMsg: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddMsg: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetMsg: c(`SELECT * FROM @person WHERE first_name=?`), stmtGetMsg: c(`SELECT * FROM @person WHERE first_name=?`),
stmtReadMsg: c(`SELECT * FROM @person WHERE first_name=?`), stmtReadMsg: c(`SELECT * FROM @person WHERE first_name=?`),

@ -15,7 +15,7 @@ var (
) )
type securityServant struct { type securityServant struct {
db *sqlx.DB *sqlxServant
phoneVerify core.PhoneVerifyService phoneVerify core.PhoneVerifyService
stmtAddCaptcha *sqlx.Stmt stmtAddCaptcha *sqlx.Stmt
stmtGetCaptcha *sqlx.Stmt stmtGetCaptcha *sqlx.Stmt
@ -44,7 +44,7 @@ func (s *securityServant) SendPhoneCaptcha(phone string) error {
func newSecurityService(db *sqlx.DB, phoneVerify core.PhoneVerifyService) core.SecurityService { func newSecurityService(db *sqlx.DB, phoneVerify core.PhoneVerifyService) core.SecurityService {
return &securityServant{ return &securityServant{
db: db, sqlxServant: newSqlxServant(db),
phoneVerify: phoneVerify, phoneVerify: phoneVerify,
stmtAddCaptcha: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddCaptcha: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetCaptcha: c(`SELECT * FROM @person WHERE first_name=?`), stmtGetCaptcha: c(`SELECT * FROM @person WHERE first_name=?`),

@ -8,6 +8,8 @@
package sakila package sakila
import ( import (
"context"
"database/sql"
"strings" "strings"
"sync" "sync"
@ -21,6 +23,39 @@ var (
_once sync.Once _once sync.Once
) )
type sqlxServant struct {
db *sqlx.DB
}
func (s *sqlxServant) with(handle func(*sqlx.Tx) error) error {
tx, err := s.db.Beginx()
if err != nil {
return err
}
if err = handle(tx); err == nil {
return tx.Commit()
}
return tx.Rollback()
}
func (s *sqlxServant) withTx(ctx context.Context, opts *sql.TxOptions, handle func(*sqlx.Tx) error) error {
tx, err := s.db.BeginTxx(ctx, opts)
if err != nil {
return err
}
if err = handle(tx); err == nil {
return tx.Commit()
}
return tx.Rollback()
}
func newSqlxServant(db *sqlx.DB) *sqlxServant {
return &sqlxServant{
db: db,
}
}
func sqlxDB() *sqlx.DB { func sqlxDB() *sqlx.DB {
_once.Do(func() { _once.Do(func() {
_db = conf.MustSqlxDB() _db = conf.MustSqlxDB()

@ -15,7 +15,7 @@ var (
) )
type topicServant struct { type topicServant struct {
db *sqlx.DB *sqlxServant
stmtAddTag *sqlx.Stmt stmtAddTag *sqlx.Stmt
stmtDelTag *sqlx.Stmt stmtDelTag *sqlx.Stmt
stmtListTag *sqlx.Stmt stmtListTag *sqlx.Stmt
@ -47,7 +47,7 @@ func (s *topicServant) GetTagsByKeyword(keyword string) ([]*core.Tag, error) {
func newTopicService(db *sqlx.DB) core.TopicService { func newTopicService(db *sqlx.DB) core.TopicService {
return &topicServant{ return &topicServant{
db: db, sqlxServant: newSqlxServant(db),
stmtAddTag: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddTag: c(`SELECT * FROM @person WHERE first_name=?`),
stmtDelTag: c(`SELECT * FROM @person WHERE first_name=?`), stmtDelTag: c(`SELECT * FROM @person WHERE first_name=?`),
stmtListTag: c(`SELECT * FROM @person WHERE first_name=?`), stmtListTag: c(`SELECT * FROM @person WHERE first_name=?`),

@ -19,14 +19,14 @@ var (
) )
type tweetServant struct { type tweetServant struct {
db *sqlx.DB *sqlxServant
stmtGetTweet *sqlx.Stmt stmtGetTweet *sqlx.Stmt
stmtListTweet *sqlx.Stmt stmtListTweet *sqlx.Stmt
stmtListStar *sqlx.Stmt stmtListStar *sqlx.Stmt
} }
type tweetManageServant struct { type tweetManageServant struct {
db *sqlx.DB *sqlxServant
cacheIndex core.CacheIndexService cacheIndex core.CacheIndexService
stmtAddTweet *sqlx.Stmt stmtAddTweet *sqlx.Stmt
stmtDelTweet *sqlx.Stmt stmtDelTweet *sqlx.Stmt
@ -34,7 +34,7 @@ type tweetManageServant struct {
} }
type tweetHelpServant struct { type tweetHelpServant struct {
db *sqlx.DB *sqlxServant
stmtAddTag *sqlx.Stmt stmtAddTag *sqlx.Stmt
stmtDelTag *sqlx.Stmt stmtDelTag *sqlx.Stmt
stmtListTag *sqlx.Stmt stmtListTag *sqlx.Stmt
@ -230,7 +230,7 @@ func (s *tweetServant) GetPostContentByID(id int64) (*core.PostContent, error) {
func newTweetService(db *sqlx.DB) core.TweetService { func newTweetService(db *sqlx.DB) core.TweetService {
return &tweetServant{ return &tweetServant{
db: db, sqlxServant: newSqlxServant(db),
stmtGetTweet: c(`SELECT * FROM @person WHERE first_name=?`), stmtGetTweet: c(`SELECT * FROM @person WHERE first_name=?`),
stmtListTweet: c(`SELECT * FROM @person WHERE first_name=?`), stmtListTweet: c(`SELECT * FROM @person WHERE first_name=?`),
stmtListStar: c(`SELECT * FROM @person WHERE first_name=?`), stmtListStar: c(`SELECT * FROM @person WHERE first_name=?`),
@ -239,7 +239,7 @@ func newTweetService(db *sqlx.DB) core.TweetService {
func newTweetManageService(db *sqlx.DB, cacheIndex core.CacheIndexService) core.TweetManageService { func newTweetManageService(db *sqlx.DB, cacheIndex core.CacheIndexService) core.TweetManageService {
return &tweetManageServant{ return &tweetManageServant{
db: db, sqlxServant: newSqlxServant(db),
cacheIndex: cacheIndex, cacheIndex: cacheIndex,
stmtAddTweet: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddTweet: c(`SELECT * FROM @person WHERE first_name=?`),
stmtDelTweet: c(`SELECT * FROM @person WHERE first_name=?`), stmtDelTweet: c(`SELECT * FROM @person WHERE first_name=?`),
@ -249,7 +249,7 @@ func newTweetManageService(db *sqlx.DB, cacheIndex core.CacheIndexService) core.
func newTweetHelpService(db *sqlx.DB) core.TweetHelpService { func newTweetHelpService(db *sqlx.DB) core.TweetHelpService {
return &tweetHelpServant{ return &tweetHelpServant{
db: db, sqlxServant: newSqlxServant(db),
stmtAddTag: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddTag: c(`SELECT * FROM @person WHERE first_name=?`),
stmtDelTag: c(`SELECT * FROM @person WHERE first_name=?`), stmtDelTag: c(`SELECT * FROM @person WHERE first_name=?`),
stmtListTag: c(`SELECT * FROM @person WHERE first_name=?`), stmtListTag: c(`SELECT * FROM @person WHERE first_name=?`),

@ -15,7 +15,7 @@ var (
) )
type userManageServant struct { type userManageServant struct {
db *sqlx.DB *sqlxServant
stmtAddUser *sqlx.Stmt stmtAddUser *sqlx.Stmt
stmtUpdateUser *sqlx.Stmt stmtUpdateUser *sqlx.Stmt
stmtGetUser *sqlx.Stmt stmtGetUser *sqlx.Stmt
@ -71,7 +71,7 @@ func (s *userManageServant) UpdateUser(user *core.User) error {
func newUserManageService(db *sqlx.DB) core.UserManageService { func newUserManageService(db *sqlx.DB) core.UserManageService {
return &userManageServant{ return &userManageServant{
db: db, sqlxServant: newSqlxServant(db),
stmtAddUser: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddUser: c(`SELECT * FROM @person WHERE first_name=?`),
stmtUpdateUser: c(`SELECT * FROM @person WHERE first_name=?`), stmtUpdateUser: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetUser: c(`SELECT * FROM @person WHERE first_name=?`), stmtGetUser: c(`SELECT * FROM @person WHERE first_name=?`),

@ -15,7 +15,7 @@ var (
) )
type walletServant struct { type walletServant struct {
db *sqlx.DB *sqlxServant
stmtAddRecharge *sqlx.Stmt stmtAddRecharge *sqlx.Stmt
stmtGetRecharge *sqlx.Stmt stmtGetRecharge *sqlx.Stmt
stmtGetBills *sqlx.Stmt stmtGetBills *sqlx.Stmt
@ -58,7 +58,7 @@ func (s *walletServant) HandlePostAttachmentBought(post *core.Post, user *core.U
func newWalletService(db *sqlx.DB) core.WalletService { func newWalletService(db *sqlx.DB) core.WalletService {
return &walletServant{ return &walletServant{
db: db, sqlxServant: newSqlxServant(db),
stmtAddRecharge: c(`SELECT * FROM @person WHERE first_name=?`), stmtAddRecharge: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetRecharge: c(`SELECT * FROM @person WHERE first_name=?`), stmtGetRecharge: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetBills: c(`SELECT * FROM @person WHERE first_name=?`), stmtGetBills: c(`SELECT * FROM @person WHERE first_name=?`),

Loading…
Cancel
Save