From ebebfb1ccd9b39e91f6b0c13f3c3de52dd83cd45 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Thu, 24 Aug 2023 12:13:10 +0800 Subject: [PATCH] sqlx: change sqlx as defualt db feature and fixed some error --- internal/dao/dao.go | 8 +++---- internal/dao/sakila/auto/ac/ac.go | 2 +- internal/dao/sakila/auto/cc/cc.go | 2 +- internal/dao/sakila/auto/pgc/pgc.go | 2 +- internal/dao/sakila/comments_pgc.go | 6 +++--- internal/dao/sakila/contacts.go | 2 +- internal/dao/sakila/contacts_pgc.go | 4 ++-- internal/dao/sakila/messages.go | 2 +- internal/dao/sakila/security.go | 2 +- internal/dao/sakila/sqlx.go | 28 ++++++++++++------------- internal/dao/sakila/tweets.go | 4 ++-- internal/dao/sakila/tweets_pgc.go | 4 ++-- internal/dao/sakila/user.go | 6 +++--- internal/dao/sakila/user_pgc.go | 2 +- internal/dao/sakila/wallet.go | 2 +- internal/dao/sakila/wallet_pgc.go | 2 +- internal/dao/sakila/yesql/yesql_ac.sql | 2 +- internal/dao/sakila/yesql/yesql_cc.sql | 4 ++-- internal/dao/sakila/yesql/yesql_pgc.sql | 4 ++-- 19 files changed, 44 insertions(+), 44 deletions(-) diff --git a/internal/dao/dao.go b/internal/dao/dao.go index b1f7d9f2..451ea533 100644 --- a/internal/dao/dao.go +++ b/internal/dao/dao.go @@ -54,7 +54,7 @@ func newAuthorizationManageService() (ams core.AuthorizationManageService) { } else if cfg.If("Sqlc") && cfg.Any("Postgres", "PostgreSQL") { ams = slonik.NewAuthorizationManageService() } else { - ams = jinzhu.NewAuthorizationManageService() + ams = sakila.NewAuthorizationManageService() } return } @@ -80,9 +80,9 @@ func initDsX() { ds, dsVer = slonik.NewDataService() webDsa, dsaVer = slonik.NewWebDataServantA() } else { - // default use gorm as orm for sql database - ds, dsVer = jinzhu.NewDataService() - webDsa, dsaVer = jinzhu.NewWebDataServantA() + // default use sqlx as orm for sql database + ds, dsVer = sakila.NewDataService() + webDsa, dsaVer = sakila.NewWebDataServantA() } 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()) diff --git a/internal/dao/sakila/auto/ac/ac.go b/internal/dao/sakila/auto/ac/ac.go index 760d42d8..dbf0b794 100644 --- a/internal/dao/sakila/auto/ac/ac.go +++ b/internal/dao/sakila/auto/ac/ac.go @@ -21,7 +21,7 @@ const ( _SimpleIndexA_UserInfo = `SELECT * FROM @user WHERE username=?` _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_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_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 (?)` diff --git a/internal/dao/sakila/auto/cc/cc.go b/internal/dao/sakila/auto/cc/cc.go index f645c61d..f0ba2efe 100644 --- a/internal/dao/sakila/auto/cc/cc.go +++ b/internal/dao/sakila/auto/cc/cc.go @@ -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_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=?))` - _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_GetUserById = `SELECT * FROM @user WHERE id=? AND is_del=0` _UserManage_GetUserByPhone = `SELECT * FROM @user WHERE phone=? AND is_del=0` diff --git a/internal/dao/sakila/auto/pgc/pgc.go b/internal/dao/sakila/auto/pgc/pgc.go index 84b81d7b..00780f9f 100644 --- a/internal/dao/sakila/auto/pgc/pgc.go +++ b/internal/dao/sakila/auto/pgc/pgc.go @@ -27,7 +27,7 @@ const ( _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_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 *` ) diff --git a/internal/dao/sakila/comments_pgc.go b/internal/dao/sakila/comments_pgc.go index fe763e54..e232e530 100644 --- a/internal/dao/sakila/comments_pgc.go +++ b/internal/dao/sakila/comments_pgc.go @@ -22,21 +22,21 @@ type pgcCommentManageSrv struct { } 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.IPLoc, time.Now().Unix()) return } 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.AtUserID, r.IP, r.IPLoc, time.Now().Unix()) return } 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.Type, r.Sort, time.Now().Unix()) return diff --git a/internal/dao/sakila/contacts.go b/internal/dao/sakila/contacts.go index 20348502..0bb10e23 100644 --- a/internal/dao/sakila/contacts.go +++ b/internal/dao/sakila/contacts.go @@ -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) { - 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 } result, xerr := tx.Stmtx(s.q.CreateContact).Exec(userId, friendId, status, time.Now().Unix()) diff --git a/internal/dao/sakila/contacts_pgc.go b/internal/dao/sakila/contacts_pgc.go index e1d3376a..7c79da42 100644 --- a/internal/dao/sakila/contacts_pgc.go +++ b/internal/dao/sakila/contacts_pgc.go @@ -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) { - 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()) + 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()) } return } diff --git a/internal/dao/sakila/messages.go b/internal/dao/sakila/messages.go index 209295b6..a6342ca0 100644 --- a/internal/dao/sakila/messages.go +++ b/internal/dao/sakila/messages.go @@ -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) { - err = stmtGet(s.q.GetMessageById, res, id) + err = stmtGet(s.q.GetMessageById, &res, id) return } diff --git a/internal/dao/sakila/security.go b/internal/dao/sakila/security.go index 476f5475..e89c86b1 100644 --- a/internal/dao/sakila/security.go +++ b/internal/dao/sakila/security.go @@ -29,7 +29,7 @@ type securitySrv struct { // GetLatestPhoneCaptcha 获取最新短信验证码 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 } diff --git a/internal/dao/sakila/sqlx.go b/internal/dao/sakila/sqlx.go index 34e8f5d3..eff2e4ed 100644 --- a/internal/dao/sakila/sqlx.go +++ b/internal/dao/sakila/sqlx.go @@ -46,33 +46,33 @@ func newSqlxSrv(db *sqlx.DB) (s *sqlxSrv) { } //lint:ignore U1000 stmtGet -func stmtGet[T any](stmt *sqlx.Stmt, dest *T, args ...any) error { - *dest = *new(T) - return stmt.Get(dest, args...) +func stmtGet[T any](stmt *sqlx.Stmt, dest **T, args ...any) error { + *dest = new(T) + return stmt.Get(*dest, args...) } //lint:ignore U1000 stmtGetContext -func stmtGetContext[T any](ctx context.Context, stmt *sqlx.Stmt, dest *T, args ...any) error { - *dest = *new(T) - return stmt.GetContext(ctx, dest, args...) +func stmtGetContext[T any](ctx context.Context, stmt *sqlx.Stmt, dest **T, args ...any) error { + *dest = new(T) + return stmt.GetContext(ctx, *dest, args...) } //lint:ignore U1000 namedGet func namedGet[T any](stmt *sqlx.NamedStmt, dest *T, arg any) error { *dest = *new(T) - return stmt.Get(dest, arg) + return stmt.Get(*dest, arg) } -//lint:ignore U1000 namesGetContext -func namesGetContext[T any](ctx context.Context, stmt *sqlx.NamedStmt, dest *T, arg any) error { - *dest = *new(T) - return stmt.GetContext(ctx, dest, arg) +//lint:ignore U1000 namedGetContext +func namedGetContext[T any](ctx context.Context, stmt *sqlx.NamedStmt, dest **T, arg any) error { + *dest = new(T) + return stmt.GetContext(ctx, *dest, arg) } //lint:ignore U1000 inGet -func inGet[T any](q sqlx.Queryable, dest *T, query string, args ...any) error { - *dest = *new(T) - return q.InGet(dest, query, args...) +func inGet[T any](q sqlx.Queryable, dest **T, query string, args ...any) error { + *dest = new(T) + return q.InGet(*dest, query, args...) } //lint:ignore U1000 r diff --git a/internal/dao/sakila/tweets.go b/internal/dao/sakila/tweets.go index 98a68372..7c06ade5 100644 --- a/internal/dao/sakila/tweets.go +++ b/internal/dao/sakila/tweets.go @@ -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) { - err = stmtGet(s.q.GetUserPostCollection, res, postID, userID, userID) + err = stmtGet(s.q.GetUserPostCollection, &res, postID, userID, userID) 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) { - err = stmtGet(s.q.GetPostContentById, res, id) + err = stmtGet(s.q.GetPostContentById, &res, id) return } diff --git a/internal/dao/sakila/tweets_pgc.go b/internal/dao/sakila/tweets_pgc.go index 48c90d0c..be8e2f29 100644 --- a/internal/dao/sakila/tweets_pgc.go +++ b/internal/dao/sakila/tweets_pgc.go @@ -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) { - err = stmtGet(s.q.AddPostStar, res, postID, userID, time.Now().Unix()) + err = stmtGet(s.p.AddPostStar, &res, postID, userID, time.Now().Unix()) return } 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 } diff --git a/internal/dao/sakila/user.go b/internal/dao/sakila/user.go index 655c7442..d9f8d866 100644 --- a/internal/dao/sakila/user.go +++ b/internal/dao/sakila/user.go @@ -26,17 +26,17 @@ type userManageSrv struct { } 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 } 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 } 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 } diff --git a/internal/dao/sakila/user_pgc.go b/internal/dao/sakila/user_pgc.go index f6128b33..8973242c 100644 --- a/internal/dao/sakila/user_pgc.go +++ b/internal/dao/sakila/user_pgc.go @@ -23,7 +23,7 @@ type pgcUserManageSrv struct { func (s *pgcUserManageSrv) CreateUser(r *ms.User) (*ms.User, error) { 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 r, nil diff --git a/internal/dao/sakila/wallet.go b/internal/dao/sakila/wallet.go index a70e2a59..be77d0ba 100644 --- a/internal/dao/sakila/wallet.go +++ b/internal/dao/sakila/wallet.go @@ -26,7 +26,7 @@ type walletSrv struct { } 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 } diff --git a/internal/dao/sakila/wallet_pgc.go b/internal/dao/sakila/wallet_pgc.go index 7213139d..20e8dd5b 100644 --- a/internal/dao/sakila/wallet_pgc.go +++ b/internal/dao/sakila/wallet_pgc.go @@ -22,6 +22,6 @@ type pgcWalletSrv struct { } 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 } diff --git a/internal/dao/sakila/yesql/yesql_ac.sql b/internal/dao/sakila/yesql/yesql_ac.sql index 9e36c114..45d6ba31 100644 --- a/internal/dao/sakila/yesql/yesql_ac.sql +++ b/internal/dao/sakila/yesql/yesql_ac.sql @@ -148,7 +148,7 @@ 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 +ORDER BY c.is_top DESC, t.quote_num DESC LIMIT ? OFFSET ?; -- name: topic_infos@topic_a diff --git a/internal/dao/sakila/yesql/yesql_cc.sql b/internal/dao/sakila/yesql/yesql_cc.sql index 88428ebd..d1415503 100644 --- a/internal/dao/sakila/yesql/yesql_cc.sql +++ b/internal/dao/sakila/yesql/yesql_cc.sql @@ -1086,8 +1086,8 @@ SELECT * FROM @user WHERE is_del=0 ORDER BY id ASC limit 6; -- name: create_user@user_manage -- prepare: named_stmt -INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on) -VALUES (: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, 0); -- name: update_user@user_manage -- prepare: named_stmt diff --git a/internal/dao/sakila/yesql/yesql_pgc.sql b/internal/dao/sakila/yesql/yesql_pgc.sql index b8ba0b59..ecca700c 100644 --- a/internal/dao/sakila/yesql/yesql_pgc.sql +++ b/internal/dao/sakila/yesql/yesql_pgc.sql @@ -44,8 +44,8 @@ RETURNING *; -- name: create_user@user_manage -- prepare: named_stmt -INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on) -VALUES (: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, 0) RETURNING id; --------------------------------------------------------------------------------