diff --git a/internal/conf/db.go b/internal/conf/db.go index 53ce0f16..80a80bc4 100644 --- a/internal/conf/db.go +++ b/internal/conf/db.go @@ -19,6 +19,30 @@ var ( _onceSql, _onceRedis sync.Once ) +const ( + TableAnouncement = "user" + TableAnouncementContent = "anouncement_content" + TableAttachment = "attachment" + TableCaptcha = "captcha" + TableComment = "comment" + TableCommentContent = "comment_content" + TableCommentReply = "comment_reply" + TableContact = "contact" + TableContactGroup = "contact_group" + TableMessage = "message" + TablePost = "post" + TablePostAttachmentBill = "post_attachment_bill" + TablePostCollection = "post_collection" + TablePostContent = "post_content" + TablePostStar = "post_star" + TableTag = "tag" + TableUser = "user" + TableWalletRecharge = "wallet_recharge" + TableWalletStatement = "wallet_statement" +) + +type TableNameMap map[string]string + func MustSqlDB() *sql.DB { _onceSql.Do(func() { var err error diff --git a/internal/conf/settting.go b/internal/conf/settting.go index 93443f64..71f852bd 100644 --- a/internal/conf/settting.go +++ b/internal/conf/settting.go @@ -381,6 +381,35 @@ func (s *MeiliSettingS) Endpoint() string { return endpoint(s.Host, s.Secure) } +func (s *DatabaseSetingS) TableNames() (res TableNameMap) { + tableNames := []string{ + TableAnouncement, + TableAnouncementContent, + TableAttachment, + TableCaptcha, + TableComment, + TableCommentContent, + TableCommentReply, + TableContact, + TableContactGroup, + TableMessage, + TablePost, + TablePostAttachmentBill, + TablePostCollection, + TablePostContent, + TablePostStar, + TableTag, + TableUser, + TableWalletRecharge, + TableWalletStatement, + } + res = make(TableNameMap, len(tableNames)) + for _, name := range tableNames { + res[name] = s.TablePrefix + name + } + return +} + func endpoint(host string, secure bool) string { schema := "http" if secure { diff --git a/internal/dao/jinzhu/gorm.go b/internal/dao/jinzhu/gorm.go new file mode 100644 index 00000000..62e044fd --- /dev/null +++ b/internal/dao/jinzhu/gorm.go @@ -0,0 +1,54 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package jinzhu + +import ( + "github.com/rocboss/paopao-ce/internal/conf" +) + +var ( + tableAnouncement string + tableAnouncementContent string + tableAttachment string + tableCaptcha string + tableComment string + tableCommentContent string + tableCommentReply string + tableContact string + tableContactGroup string + tableMessage string + tablePost string + tablePostAttachmentBill string + tablePostCollection string + tablePostContent string + tablePostStar string + tableTag string + tableUser string + tableWalletRecharge string + tableWalletStatement string +) + +func initTableName() { + m := conf.DatabaseSetting.TableNames() + tableAnouncement = m[conf.TableAnouncement] + tableAnouncementContent = m[conf.TableAnouncementContent] + tableAttachment = m[conf.TableAttachment] + tableCaptcha = m[conf.TableCaptcha] + tableComment = m[conf.TableComment] + tableCommentContent = m[conf.TableCommentContent] + tableCommentReply = m[conf.TableCommentReply] + tableContact = m[conf.TableContact] + tableContactGroup = m[conf.TableContactGroup] + tableMessage = m[conf.TableMessage] + tablePost = m[conf.TablePost] + tablePostAttachmentBill = m[conf.TablePostAttachmentBill] + tablePostCollection = m[conf.TablePostCollection] + tablePostContent = m[conf.TablePostContent] + tablePostStar = m[conf.TablePostStar] + tableTag = m[conf.TableTag] + tableUser = m[conf.TableUser] + tableWalletRecharge = m[conf.TableWalletRecharge] + tableWalletStatement = m[conf.TableWalletStatement] +} diff --git a/internal/dao/jinzhu/jinzhu.go b/internal/dao/jinzhu/jinzhu.go index 6d15f1b5..51fc28db 100644 --- a/internal/dao/jinzhu/jinzhu.go +++ b/internal/dao/jinzhu/jinzhu.go @@ -9,6 +9,8 @@ package jinzhu import ( + "sync" + "github.com/Masterminds/semver/v3" "github.com/alimy/cfg" "github.com/rocboss/paopao-ce/internal/conf" @@ -21,6 +23,8 @@ import ( var ( _ core.DataService = (*dataServant)(nil) _ core.VersionInfo = (*dataServant)(nil) + + _onceInitial sync.Once ) type dataServant struct { @@ -40,6 +44,8 @@ type dataServant struct { } func NewDataService() (core.DataService, core.VersionInfo) { + lazyInitial() + var ( v core.VersionInfo cis core.CacheIndexService @@ -104,3 +110,10 @@ func (s *dataServant) Name() string { func (s *dataServant) Version() *semver.Version { return semver.MustParse("v0.2.0") } + +// lazyInitial do some package lazy initialize for performance +func lazyInitial() { + _onceInitial.Do(func() { + initTableName() + }) +} diff --git a/internal/dao/sakila/sakila.go b/internal/dao/sakila/sakila.go index 76ccfd6b..7cb3d810 100644 --- a/internal/dao/sakila/sakila.go +++ b/internal/dao/sakila/sakila.go @@ -5,6 +5,8 @@ package sakila import ( + "sync" + "github.com/Masterminds/semver/v3" "github.com/alimy/cfg" "github.com/rocboss/paopao-ce/internal/core" @@ -16,6 +18,8 @@ import ( var ( _ core.DataService = (*dataServant)(nil) _ core.VersionInfo = (*dataServant)(nil) + + _onceInitial sync.Once ) type dataServant struct { @@ -35,32 +39,33 @@ type dataServant struct { } func NewDataService() (core.DataService, core.VersionInfo) { + lazyInitial() + var ( v core.VersionInfo cis core.CacheIndexService ips core.IndexPostsService ) - db := sqlxDB() pvs := security.NewPhoneVerifyService() ams := NewAuthorizationManageService() - ths := newTweetHelpService(db) + ths := newTweetHelpService(_db) // initialize core.IndexPostsService if cfg.If("Friendship") { - ips = newFriendIndexService(db, ams, ths) + ips = newFriendIndexService(_db, ams, ths) } else if cfg.If("Followship") { - ips = newFollowIndexService(db, ths) + ips = newFollowIndexService(_db, ths) } else if cfg.If("Lightship") { - ips = newLightIndexService(db, ths) + ips = newLightIndexService(_db, ths) } else { // default use lightship post index service - ips = newLightIndexService(db, ths) + ips = newLightIndexService(_db, ths) } // initialize core.CacheIndexService if cfg.If("SimpleCacheIndex") { // simpleCache use special post index service - ips = newSimpleIndexPostsService(db, ths) + ips = newSimpleIndexPostsService(_db, ths) cis, v = cache.NewSimpleCacheIndexService(ips) } else if cfg.If("BigCacheIndex") { // TODO: make cache index post in different scence like friendship/followship/lightship @@ -72,24 +77,25 @@ func NewDataService() (core.DataService, core.VersionInfo) { ds := &dataServant{ IndexPostsService: cis, - WalletService: newWalletService(db), - MessageService: newMessageService(db), - TopicService: newTopicService(db), - TweetService: newTweetService(db), - TweetManageService: newTweetManageService(db, cis), - TweetHelpService: newTweetHelpService(db), - CommentService: newCommentService(db), - CommentManageService: newCommentManageService(db), - UserManageService: newUserManageService(db), - ContactManageService: newContactManageService(db), - SecurityService: newSecurityService(db, pvs), + WalletService: newWalletService(_db), + MessageService: newMessageService(_db), + TopicService: newTopicService(_db), + TweetService: newTweetService(_db), + TweetManageService: newTweetManageService(_db, cis), + TweetHelpService: newTweetHelpService(_db), + CommentService: newCommentService(_db), + CommentManageService: newCommentManageService(_db), + UserManageService: newUserManageService(_db), + ContactManageService: newContactManageService(_db), + SecurityService: newSecurityService(_db, pvs), AttachmentCheckService: security.NewAttachmentCheckService(), } return ds, ds } func NewAuthorizationManageService() core.AuthorizationManageService { - return newAuthorizationManageService(sqlxDB()) + lazyInitial() + return newAuthorizationManageService(_db) } func (s *dataServant) Name() string { @@ -99,3 +105,10 @@ func (s *dataServant) Name() string { func (s *dataServant) Version() *semver.Version { return semver.MustParse("v0.1.0") } + +// lazyInitial do some package lazy initialize for performance +func lazyInitial() { + _onceInitial.Do(func() { + initSqlxDB() + }) +} diff --git a/internal/dao/sakila/sqlx.go b/internal/dao/sakila/sqlx.go index d5c2db8e..cfec069d 100644 --- a/internal/dao/sakila/sqlx.go +++ b/internal/dao/sakila/sqlx.go @@ -8,7 +8,6 @@ import ( "context" "database/sql" "strings" - "sync" "github.com/jmoiron/sqlx" "github.com/rocboss/paopao-ce/internal/conf" @@ -16,8 +15,7 @@ import ( ) var ( - _db *sqlx.DB - _once sync.Once + _db *sqlx.DB ) type sqlxServant struct { @@ -86,21 +84,12 @@ func newSqlxServant(db *sqlx.DB) *sqlxServant { } } -func sqlxDB() *sqlx.DB { - _once.Do(func() { - _db = conf.MustSqlxDB() - }) - return _db -} - func r(query string) string { - db := sqlxDB() - return db.Rebind(t(query)) + return _db.Rebind(t(query)) } func c(query string) *sqlx.Stmt { - db := sqlxDB() - stmt, err := db.Preparex(db.Rebind(t(query))) + stmt, err := _db.Preparex(_db.Rebind(t(query))) if err != nil { logrus.Fatalf("prepare query(%s) error: %s", query, err) } @@ -108,8 +97,7 @@ func c(query string) *sqlx.Stmt { } func n(query string) *sqlx.NamedStmt { - db := sqlxDB() - stmt, err := db.PrepareNamed(t(query)) + stmt, err := _db.PrepareNamed(t(query)) if err != nil { logrus.Fatalf("prepare named query(%s) error: %s", query, err) } @@ -120,3 +108,7 @@ func n(query string) *sqlx.NamedStmt { func t(query string) string { return strings.Replace(query, "@", conf.DatabaseSetting.TablePrefix, -1) } + +func initSqlxDB() { + _db = conf.MustSqlxDB() +}