sqlx: change sqlx as defualt db feature and fixed some error

r/paopao-ce-plus
Michael Li 2 years ago
parent 5dc4c03a57
commit ebebfb1ccd
No known key found for this signature in database

@ -54,7 +54,7 @@ func newAuthorizationManageService() (ams core.AuthorizationManageService) {
} else if cfg.If("Sqlc") && cfg.Any("Postgres", "PostgreSQL") { } else if cfg.If("Sqlc") && cfg.Any("Postgres", "PostgreSQL") {
ams = slonik.NewAuthorizationManageService() ams = slonik.NewAuthorizationManageService()
} else { } else {
ams = jinzhu.NewAuthorizationManageService() ams = sakila.NewAuthorizationManageService()
} }
return return
} }
@ -80,9 +80,9 @@ func initDsX() {
ds, dsVer = slonik.NewDataService() ds, dsVer = slonik.NewDataService()
webDsa, dsaVer = slonik.NewWebDataServantA() webDsa, dsaVer = slonik.NewWebDataServantA()
} else { } else {
// default use gorm as orm for sql database // default use sqlx as orm for sql database
ds, dsVer = jinzhu.NewDataService() ds, dsVer = sakila.NewDataService()
webDsa, dsaVer = jinzhu.NewWebDataServantA() webDsa, dsaVer = sakila.NewWebDataServantA()
} }
logrus.Infof("use %s as core.DataService with version %s", dsVer.Name(), dsVer.Version()) logrus.Infof("use %s as core.DataService with version %s", dsVer.Name(), dsVer.Version())
logrus.Infof("use %s as core.ServantA with version %s", dsaVer.Name(), dsaVer.Version()) logrus.Infof("use %s as core.ServantA with version %s", dsaVer.Name(), dsaVer.Version())

@ -21,7 +21,7 @@ const (
_SimpleIndexA_UserInfo = `SELECT * FROM @user WHERE username=?` _SimpleIndexA_UserInfo = `SELECT * FROM @user WHERE username=?`
_TopicA_DecrTagsById = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?)` _TopicA_DecrTagsById = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?)`
_TopicA_ExistTopicUser = `SELECT 1 FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0` _TopicA_ExistTopicUser = `SELECT 1 FROM @topic_user WHERE user_id=? AND topic_id=? AND is_del=0`
_TopicA_FollowTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", 1 as is_following, c.is_top, u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @topic_user c JOIN @user u ON c.user_id = u.id JOIN @tag t ON c.topic_id = t.id WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=? ORDER BY t.quote_num DESC LIMIT ? OFFSET ?` _TopicA_FollowTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", 1 as is_following, c.is_top, u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @topic_user c JOIN @user u ON c.user_id = u.id JOIN @tag t ON c.topic_id = t.id WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=? ORDER BY c.is_top DESC, t.quote_num DESC LIMIT ? OFFSET ?`
_TopicA_FollowTopic = `INSERT INTO @topic_user(user_id, topic_id, created_on) VALUES (?, ?, ?)` _TopicA_FollowTopic = `INSERT INTO @topic_user(user_id, topic_id, created_on) VALUES (?, ?, ?)`
_TopicA_HotTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?` _TopicA_HotTags = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id "u.id", u.nickname "u.nickname", u.username "u.username", u.status "u.status", u.avatar "u.avatar", u.is_admin "u.is_admin" FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?`
_TopicA_IncrTagsById = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?)` _TopicA_IncrTagsById = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?)`

@ -134,7 +134,7 @@ const (
_Tweet_UserStarTweetsCountByFriend = `SELECT count(*) FROM @post_star star JOIN @post post ON star.post_id = post.ID WHERE star.is_del=0 AND star.user_id=? AND (post.visibility=0 OR post.visibility=2)` _Tweet_UserStarTweetsCountByFriend = `SELECT count(*) FROM @post_star star JOIN @post post ON star.post_id = post.ID WHERE star.is_del=0 AND star.user_id=? AND (post.visibility=0 OR post.visibility=2)`
_Tweet_UserStarTweetsCountByGuest = `SELECT count(*) FROM @post_star star JOIN @post post ON star.post_id = post.ID WHERE star.is_del=0 AND star.user_id=? AND post.visibility=0` _Tweet_UserStarTweetsCountByGuest = `SELECT count(*) FROM @post_star star JOIN @post post ON star.post_id = post.ID WHERE star.is_del=0 AND star.user_id=? AND post.visibility=0`
_Tweet_UserStarTweetsCountBySelf = `SELECT count(*) FROM @post_star star JOIN @post post ON star.post_id = post.ID WHERE star.is_del=0 AND star.user_id=? AND (post.visibility<>0 OR (post.visibility=0 AND post.user_id=?))` _Tweet_UserStarTweetsCountBySelf = `SELECT count(*) FROM @post_star star JOIN @post post ON star.post_id = post.ID WHERE star.is_del=0 AND star.user_id=? AND (post.visibility<>0 OR (post.visibility=0 AND post.user_id=?))`
_UserManage_CreateUser = `INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on) VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on)` _UserManage_CreateUser = `INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on, balance) VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on, 0)`
_UserManage_GetAnyUsers = `SELECT * FROM @user WHERE is_del=0 ORDER BY id ASC limit 6` _UserManage_GetAnyUsers = `SELECT * FROM @user WHERE is_del=0 ORDER BY id ASC limit 6`
_UserManage_GetUserById = `SELECT * FROM @user WHERE id=? AND is_del=0` _UserManage_GetUserById = `SELECT * FROM @user WHERE id=? AND is_del=0`
_UserManage_GetUserByPhone = `SELECT * FROM @user WHERE phone=? AND is_del=0` _UserManage_GetUserByPhone = `SELECT * FROM @user WHERE phone=? AND is_del=0`

@ -27,7 +27,7 @@ const (
_TweetManage_AddPostCollection = `INSERT INTO @post_collection (post_id, user_id, created_on) VALUES (?, ?, ?) RETURNING *` _TweetManage_AddPostCollection = `INSERT INTO @post_collection (post_id, user_id, created_on) VALUES (?, ?, ?) RETURNING *`
_TweetManage_AddPostContent = `INSERT INTO @post_content (post_id, user_id, content, type, sort, created_on) VALUES (:post_id, :user_id, :content, :type, :sort, :created_on) RETURNING id` _TweetManage_AddPostContent = `INSERT INTO @post_content (post_id, user_id, content, type, sort, created_on) VALUES (:post_id, :user_id, :content, :type, :sort, :created_on) RETURNING id`
_TweetManage_AddPostStar = `INSERT INTO @post_star (post_id, user_id, created_on) VALUES (?, ?, ?) RETURNING *` _TweetManage_AddPostStar = `INSERT INTO @post_star (post_id, user_id, created_on) VALUES (?, ?, ?) RETURNING *`
_UserManage_CreateUser = `INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on) VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on) RETURNING id` _UserManage_CreateUser = `INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on, balance) VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on, 0) RETURNING id`
_Wallet_CreateRecharge = `INSERT INTO @wallet_recharge (user_id, amount, created_on) VALUES (?, ?, ?) RETURNING *` _Wallet_CreateRecharge = `INSERT INTO @wallet_recharge (user_id, amount, created_on) VALUES (?, ?, ?) RETURNING *`
) )

@ -22,21 +22,21 @@ type pgcCommentManageSrv struct {
} }
func (s *pgcCommentManageSrv) CreateComment(r *ms.Comment) (res *ms.Comment, err error) { func (s *pgcCommentManageSrv) CreateComment(r *ms.Comment) (res *ms.Comment, err error) {
err = stmtGet(s.q.CreateComment, res, err = stmtGet(s.p.CreateComment, &res,
r.PostID, r.UserID, r.IP, r.PostID, r.UserID, r.IP,
r.IPLoc, time.Now().Unix()) r.IPLoc, time.Now().Unix())
return return
} }
func (s *pgcCommentManageSrv) CreateCommentReply(r *ms.CommentReply) (res *ms.CommentReply, err error) { func (s *pgcCommentManageSrv) CreateCommentReply(r *ms.CommentReply) (res *ms.CommentReply, err error) {
err = stmtGet(s.q.CreateCommentReply, res, err = stmtGet(s.p.CreateCommentReply, &res,
r.CommentID, r.UserID, r.Content, r.CommentID, r.UserID, r.Content,
r.AtUserID, r.IP, r.IPLoc, time.Now().Unix()) r.AtUserID, r.IP, r.IPLoc, time.Now().Unix())
return return
} }
func (s *pgcCommentManageSrv) CreateCommentContent(r *ms.CommentContent) (res *ms.CommentContent, err error) { func (s *pgcCommentManageSrv) CreateCommentContent(r *ms.CommentContent) (res *ms.CommentContent, err error) {
err = stmtGet(s.q.CreateCommentContent, res, err = stmtGet(s.p.CreateCommentContent, &res,
r.CommentID, r.UserID, r.Content, r.CommentID, r.UserID, r.Content,
r.Type, r.Sort, time.Now().Unix()) r.Type, r.Sort, time.Now().Unix())
return return

@ -163,7 +163,7 @@ func (s *contactManageSrv) IsFriend(userId int64, friendId int64) (res bool) {
} }
func (s *contactManageSrv) fetchOrNewContact(tx *sqlx.Tx, userId int64, friendId int64, status int8) (res *cs.Contact, err error) { func (s *contactManageSrv) fetchOrNewContact(tx *sqlx.Tx, userId int64, friendId int64, status int8) (res *cs.Contact, err error) {
if err = stmtGet(tx.Stmtx(s.q.GetContact), res, userId, friendId); err == nil { if err = stmtGet(tx.Stmtx(s.q.GetContact), &res, userId, friendId); err == nil {
return return
} }
result, xerr := tx.Stmtx(s.q.CreateContact).Exec(userId, friendId, status, time.Now().Unix()) result, xerr := tx.Stmtx(s.q.CreateContact).Exec(userId, friendId, status, time.Now().Unix())

@ -23,8 +23,8 @@ type pgcContactManageSrv struct {
} }
func (s *pgcContactManageSrv) pgcFetchOrNewContact(tx *sqlx.Tx, userId int64, friendId int64, status int8) (res *cs.Contact, err error) { func (s *pgcContactManageSrv) pgcFetchOrNewContact(tx *sqlx.Tx, userId int64, friendId int64, status int8) (res *cs.Contact, err error) {
if err = stmtGet(tx.Stmtx(s.q.GetContact), res, userId, friendId); err != nil { if err = stmtGet(tx.Stmtx(s.q.GetContact), &res, userId, friendId); err != nil {
err = stmtGet(tx.Stmtx(s.p.CreateContact), res, userId, friendId, status, time.Now().Unix()) err = stmtGet(tx.Stmtx(s.p.CreateContact), &res, userId, friendId, status, time.Now().Unix())
} }
return return
} }

@ -46,7 +46,7 @@ func (s *messageSrv) GetUnreadCount(userID int64) (res int64, err error) {
} }
func (s *messageSrv) GetMessageByID(id int64) (res *ms.Message, err error) { func (s *messageSrv) GetMessageByID(id int64) (res *ms.Message, err error) {
err = stmtGet(s.q.GetMessageById, res, id) err = stmtGet(s.q.GetMessageById, &res, id)
return return
} }

@ -29,7 +29,7 @@ type securitySrv struct {
// GetLatestPhoneCaptcha 获取最新短信验证码 // GetLatestPhoneCaptcha 获取最新短信验证码
func (s *securitySrv) GetLatestPhoneCaptcha(phone string) (res *ms.Captcha, err error) { func (s *securitySrv) GetLatestPhoneCaptcha(phone string) (res *ms.Captcha, err error) {
err = stmtGet(s.q.GetLatestPhoneCaptcha, res, phone) err = stmtGet(s.q.GetLatestPhoneCaptcha, &res, phone)
return return
} }

@ -46,33 +46,33 @@ func newSqlxSrv(db *sqlx.DB) (s *sqlxSrv) {
} }
//lint:ignore U1000 stmtGet //lint:ignore U1000 stmtGet
func stmtGet[T any](stmt *sqlx.Stmt, dest *T, args ...any) error { func stmtGet[T any](stmt *sqlx.Stmt, dest **T, args ...any) error {
*dest = *new(T) *dest = new(T)
return stmt.Get(dest, args...) return stmt.Get(*dest, args...)
} }
//lint:ignore U1000 stmtGetContext //lint:ignore U1000 stmtGetContext
func stmtGetContext[T any](ctx context.Context, stmt *sqlx.Stmt, dest *T, args ...any) error { func stmtGetContext[T any](ctx context.Context, stmt *sqlx.Stmt, dest **T, args ...any) error {
*dest = *new(T) *dest = new(T)
return stmt.GetContext(ctx, dest, args...) return stmt.GetContext(ctx, *dest, args...)
} }
//lint:ignore U1000 namedGet //lint:ignore U1000 namedGet
func namedGet[T any](stmt *sqlx.NamedStmt, dest *T, arg any) error { func namedGet[T any](stmt *sqlx.NamedStmt, dest *T, arg any) error {
*dest = *new(T) *dest = *new(T)
return stmt.Get(dest, arg) return stmt.Get(*dest, arg)
} }
//lint:ignore U1000 namesGetContext //lint:ignore U1000 namedGetContext
func namesGetContext[T any](ctx context.Context, stmt *sqlx.NamedStmt, dest *T, arg any) error { func namedGetContext[T any](ctx context.Context, stmt *sqlx.NamedStmt, dest **T, arg any) error {
*dest = *new(T) *dest = new(T)
return stmt.GetContext(ctx, dest, arg) return stmt.GetContext(ctx, *dest, arg)
} }
//lint:ignore U1000 inGet //lint:ignore U1000 inGet
func inGet[T any](q sqlx.Queryable, dest *T, query string, args ...any) error { func inGet[T any](q sqlx.Queryable, dest **T, query string, args ...any) error {
*dest = *new(T) *dest = new(T)
return q.InGet(dest, query, args...) return q.InGet(*dest, query, args...)
} }
//lint:ignore U1000 r //lint:ignore U1000 r

@ -439,7 +439,7 @@ func (s *tweetSrv) GetUserPostStarCount(userID int64) (res int64, err error) {
} }
func (s *tweetSrv) GetUserPostCollection(postID, userID int64) (res *ms.PostCollection, err error) { func (s *tweetSrv) GetUserPostCollection(postID, userID int64) (res *ms.PostCollection, err error) {
err = stmtGet(s.q.GetUserPostCollection, res, postID, userID, userID) err = stmtGet(s.q.GetUserPostCollection, &res, postID, userID, userID)
return return
} }
@ -465,7 +465,7 @@ func (s *tweetSrv) GetPostContentsByIDs(ids []int64) (res []*ms.PostContent, err
} }
func (s *tweetSrv) GetPostContentByID(id int64) (res *ms.PostContent, err error) { func (s *tweetSrv) GetPostContentByID(id int64) (res *ms.PostContent, err error) {
err = stmtGet(s.q.GetPostContentById, res, id) err = stmtGet(s.q.GetPostContentById, &res, id)
return return
} }

@ -48,11 +48,11 @@ func (s *pgcTweetManageSrv) CreateAttachment(r *ms.Attachment) (res int64, err e
} }
func (s *pgcTweetManageSrv) CreatePostStar(postID, userID int64) (res *ms.PostStar, err error) { func (s *pgcTweetManageSrv) CreatePostStar(postID, userID int64) (res *ms.PostStar, err error) {
err = stmtGet(s.q.AddPostStar, res, postID, userID, time.Now().Unix()) err = stmtGet(s.p.AddPostStar, &res, postID, userID, time.Now().Unix())
return return
} }
func (s *pgcTweetManageSrv) CreatePostCollection(postID, userID int64) (res *ms.PostCollection, err error) { func (s *pgcTweetManageSrv) CreatePostCollection(postID, userID int64) (res *ms.PostCollection, err error) {
err = stmtGet(s.q.AddPostCollection, res, postID, userID, time.Now().Unix()) err = stmtGet(s.p.AddPostCollection, &res, postID, userID, time.Now().Unix())
return return
} }

@ -26,17 +26,17 @@ type userManageSrv struct {
} }
func (s *userManageSrv) GetUserByID(id int64) (res *ms.User, err error) { func (s *userManageSrv) GetUserByID(id int64) (res *ms.User, err error) {
err = stmtGet(s.q.GetUserById, res, id) err = stmtGet(s.q.GetUserById, &res, id)
return return
} }
func (s *userManageSrv) GetUserByUsername(username string) (res *ms.User, err error) { func (s *userManageSrv) GetUserByUsername(username string) (res *ms.User, err error) {
err = stmtGet(s.q.GetUserByUsername, res, username) err = stmtGet(s.q.GetUserByUsername, &res, username)
return return
} }
func (s *userManageSrv) GetUserByPhone(phone string) (res *ms.User, err error) { func (s *userManageSrv) GetUserByPhone(phone string) (res *ms.User, err error) {
err = stmtGet(s.q.GetUserByPhone, res, phone) err = stmtGet(s.q.GetUserByPhone, &res, phone)
return return
} }

@ -23,7 +23,7 @@ type pgcUserManageSrv struct {
func (s *pgcUserManageSrv) CreateUser(r *ms.User) (*ms.User, error) { func (s *pgcUserManageSrv) CreateUser(r *ms.User) (*ms.User, error) {
r.Model = &ms.Model{CreatedOn: time.Now().Unix()} r.Model = &ms.Model{CreatedOn: time.Now().Unix()}
if err := s.q.CreateUser.Get(&r.ID, r); err != nil { if err := s.p.CreateUser.Get(&r.ID, r); err != nil {
return nil, err return nil, err
} }
return r, nil return r, nil

@ -26,7 +26,7 @@ type walletSrv struct {
} }
func (s *walletSrv) GetRechargeByID(id int64) (res *ms.WalletRecharge, err error) { func (s *walletSrv) GetRechargeByID(id int64) (res *ms.WalletRecharge, err error) {
err = stmtGet(s.q.GetRechargeById, res, id) err = stmtGet(s.q.GetRechargeById, &res, id)
return return
} }

@ -22,6 +22,6 @@ type pgcWalletSrv struct {
} }
func (s *pgcWalletSrv) CreateRecharge(userId, amount int64) (res *ms.WalletRecharge, err error) { func (s *pgcWalletSrv) CreateRecharge(userId, amount int64) (res *ms.WalletRecharge, err error) {
err = stmtGet(s.q.CreateRecharge, res, userId, amount, time.Now().Unix()) err = stmtGet(s.p.CreateRecharge, &res, userId, amount, time.Now().Unix())
return return
} }

@ -148,7 +148,7 @@ FROM @topic_user c
JOIN @user u ON c.user_id = u.id JOIN @user u ON c.user_id = u.id
JOIN @tag t ON c.topic_id = t.id JOIN @tag t ON c.topic_id = t.id
WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=? WHERE c.is_del = 0 AND t.quote_num > 0 AND c.user_id=?
ORDER BY t.quote_num DESC ORDER BY c.is_top DESC, t.quote_num DESC
LIMIT ? OFFSET ?; LIMIT ? OFFSET ?;
-- name: topic_infos@topic_a -- name: topic_infos@topic_a

@ -1086,8 +1086,8 @@ SELECT * FROM @user WHERE is_del=0 ORDER BY id ASC limit 6;
-- name: create_user@user_manage -- name: create_user@user_manage
-- prepare: named_stmt -- prepare: named_stmt
INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on) INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on, balance)
VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on); VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on, 0);
-- name: update_user@user_manage -- name: update_user@user_manage
-- prepare: named_stmt -- prepare: named_stmt

@ -44,8 +44,8 @@ RETURNING *;
-- name: create_user@user_manage -- name: create_user@user_manage
-- prepare: named_stmt -- prepare: named_stmt
INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on) INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on, balance)
VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on) VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on, 0)
RETURNING id; RETURNING id;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

Loading…
Cancel
Save