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 {
db *sqlx.DB
*sqlxServant
stmtIdx *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 {
return &authorizationManageServant{
db: db,
sqlxServant: newSqlxServant(db),
stmtIdx: c(`SELECT * FROM @person WHERE first_name=?`),
stmtUpdateFriend: c(`SELECT * FROM @person WHERE first_name=?`),
}

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

@ -15,7 +15,7 @@ var (
)
type contactManageServant struct {
db *sqlx.DB
*sqlxServant
stmtAddFriend *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 {
return &contactManageServant{
db: db,
sqlxServant: newSqlxServant(db),
stmtAddFriend: c(`SELECT * FROM @person WHERE first_name=?`),
stmtDelFriend: c(`SELECT * FROM @person WHERE first_name=?`),
}

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

@ -15,7 +15,7 @@ var (
)
type messageServant struct {
db *sqlx.DB
*sqlxServant
stmtAddMsg *sqlx.Stmt
stmtGetMsg *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 {
return &messageServant{
db: db,
sqlxServant: newSqlxServant(db),
stmtAddMsg: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetMsg: c(`SELECT * FROM @person WHERE first_name=?`),
stmtReadMsg: c(`SELECT * FROM @person WHERE first_name=?`),

@ -15,7 +15,7 @@ var (
)
type securityServant struct {
db *sqlx.DB
*sqlxServant
phoneVerify core.PhoneVerifyService
stmtAddCaptcha *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 {
return &securityServant{
db: db,
sqlxServant: newSqlxServant(db),
phoneVerify: phoneVerify,
stmtAddCaptcha: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetCaptcha: c(`SELECT * FROM @person WHERE first_name=?`),

@ -8,6 +8,8 @@
package sakila
import (
"context"
"database/sql"
"strings"
"sync"
@ -21,6 +23,39 @@ var (
_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 {
_once.Do(func() {
_db = conf.MustSqlxDB()

@ -15,7 +15,7 @@ var (
)
type topicServant struct {
db *sqlx.DB
*sqlxServant
stmtAddTag *sqlx.Stmt
stmtDelTag *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 {
return &topicServant{
db: db,
sqlxServant: newSqlxServant(db),
stmtAddTag: c(`SELECT * FROM @person WHERE first_name=?`),
stmtDelTag: c(`SELECT * FROM @person WHERE first_name=?`),
stmtListTag: c(`SELECT * FROM @person WHERE first_name=?`),

@ -19,14 +19,14 @@ var (
)
type tweetServant struct {
db *sqlx.DB
*sqlxServant
stmtGetTweet *sqlx.Stmt
stmtListTweet *sqlx.Stmt
stmtListStar *sqlx.Stmt
}
type tweetManageServant struct {
db *sqlx.DB
*sqlxServant
cacheIndex core.CacheIndexService
stmtAddTweet *sqlx.Stmt
stmtDelTweet *sqlx.Stmt
@ -34,7 +34,7 @@ type tweetManageServant struct {
}
type tweetHelpServant struct {
db *sqlx.DB
*sqlxServant
stmtAddTag *sqlx.Stmt
stmtDelTag *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 {
return &tweetServant{
db: db,
sqlxServant: newSqlxServant(db),
stmtGetTweet: c(`SELECT * FROM @person WHERE first_name=?`),
stmtListTweet: 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 {
return &tweetManageServant{
db: db,
sqlxServant: newSqlxServant(db),
cacheIndex: cacheIndex,
stmtAddTweet: 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 {
return &tweetHelpServant{
db: db,
sqlxServant: newSqlxServant(db),
stmtAddTag: c(`SELECT * FROM @person WHERE first_name=?`),
stmtDelTag: c(`SELECT * FROM @person WHERE first_name=?`),
stmtListTag: c(`SELECT * FROM @person WHERE first_name=?`),

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

@ -15,7 +15,7 @@ var (
)
type walletServant struct {
db *sqlx.DB
*sqlxServant
stmtAddRecharge *sqlx.Stmt
stmtGetRecharge *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 {
return &walletServant{
db: db,
sqlxServant: newSqlxServant(db),
stmtAddRecharge: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetRecharge: c(`SELECT * FROM @person WHERE first_name=?`),
stmtGetBills: c(`SELECT * FROM @person WHERE first_name=?`),

Loading…
Cancel
Save