sqlx: optimize datable table name process logic

pull/351/head
Michael Li 2 years ago
parent 914889994f
commit c12ed880c1
No known key found for this signature in database

@ -19,6 +19,30 @@ var (
_onceSql, _onceRedis sync.Once _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 { func MustSqlDB() *sql.DB {
_onceSql.Do(func() { _onceSql.Do(func() {
var err error var err error

@ -381,6 +381,35 @@ func (s *MeiliSettingS) Endpoint() string {
return endpoint(s.Host, s.Secure) 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 { func endpoint(host string, secure bool) string {
schema := "http" schema := "http"
if secure { if secure {

@ -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]
}

@ -9,6 +9,8 @@
package jinzhu package jinzhu
import ( import (
"sync"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/alimy/cfg" "github.com/alimy/cfg"
"github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/conf"
@ -21,6 +23,8 @@ import (
var ( var (
_ core.DataService = (*dataServant)(nil) _ core.DataService = (*dataServant)(nil)
_ core.VersionInfo = (*dataServant)(nil) _ core.VersionInfo = (*dataServant)(nil)
_onceInitial sync.Once
) )
type dataServant struct { type dataServant struct {
@ -40,6 +44,8 @@ type dataServant struct {
} }
func NewDataService() (core.DataService, core.VersionInfo) { func NewDataService() (core.DataService, core.VersionInfo) {
lazyInitial()
var ( var (
v core.VersionInfo v core.VersionInfo
cis core.CacheIndexService cis core.CacheIndexService
@ -104,3 +110,10 @@ func (s *dataServant) Name() string {
func (s *dataServant) Version() *semver.Version { func (s *dataServant) Version() *semver.Version {
return semver.MustParse("v0.2.0") return semver.MustParse("v0.2.0")
} }
// lazyInitial do some package lazy initialize for performance
func lazyInitial() {
_onceInitial.Do(func() {
initTableName()
})
}

@ -5,6 +5,8 @@
package sakila package sakila
import ( import (
"sync"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/alimy/cfg" "github.com/alimy/cfg"
"github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/core"
@ -16,6 +18,8 @@ import (
var ( var (
_ core.DataService = (*dataServant)(nil) _ core.DataService = (*dataServant)(nil)
_ core.VersionInfo = (*dataServant)(nil) _ core.VersionInfo = (*dataServant)(nil)
_onceInitial sync.Once
) )
type dataServant struct { type dataServant struct {
@ -35,32 +39,33 @@ type dataServant struct {
} }
func NewDataService() (core.DataService, core.VersionInfo) { func NewDataService() (core.DataService, core.VersionInfo) {
lazyInitial()
var ( var (
v core.VersionInfo v core.VersionInfo
cis core.CacheIndexService cis core.CacheIndexService
ips core.IndexPostsService ips core.IndexPostsService
) )
db := sqlxDB()
pvs := security.NewPhoneVerifyService() pvs := security.NewPhoneVerifyService()
ams := NewAuthorizationManageService() ams := NewAuthorizationManageService()
ths := newTweetHelpService(db) ths := newTweetHelpService(_db)
// initialize core.IndexPostsService // initialize core.IndexPostsService
if cfg.If("Friendship") { if cfg.If("Friendship") {
ips = newFriendIndexService(db, ams, ths) ips = newFriendIndexService(_db, ams, ths)
} else if cfg.If("Followship") { } else if cfg.If("Followship") {
ips = newFollowIndexService(db, ths) ips = newFollowIndexService(_db, ths)
} else if cfg.If("Lightship") { } else if cfg.If("Lightship") {
ips = newLightIndexService(db, ths) ips = newLightIndexService(_db, ths)
} else { } else {
// default use lightship post index service // default use lightship post index service
ips = newLightIndexService(db, ths) ips = newLightIndexService(_db, ths)
} }
// initialize core.CacheIndexService // initialize core.CacheIndexService
if cfg.If("SimpleCacheIndex") { if cfg.If("SimpleCacheIndex") {
// simpleCache use special post index service // simpleCache use special post index service
ips = newSimpleIndexPostsService(db, ths) ips = newSimpleIndexPostsService(_db, ths)
cis, v = cache.NewSimpleCacheIndexService(ips) cis, v = cache.NewSimpleCacheIndexService(ips)
} else if cfg.If("BigCacheIndex") { } else if cfg.If("BigCacheIndex") {
// TODO: make cache index post in different scence like friendship/followship/lightship // TODO: make cache index post in different scence like friendship/followship/lightship
@ -72,24 +77,25 @@ func NewDataService() (core.DataService, core.VersionInfo) {
ds := &dataServant{ ds := &dataServant{
IndexPostsService: cis, IndexPostsService: cis,
WalletService: newWalletService(db), WalletService: newWalletService(_db),
MessageService: newMessageService(db), MessageService: newMessageService(_db),
TopicService: newTopicService(db), TopicService: newTopicService(_db),
TweetService: newTweetService(db), TweetService: newTweetService(_db),
TweetManageService: newTweetManageService(db, cis), TweetManageService: newTweetManageService(_db, cis),
TweetHelpService: newTweetHelpService(db), TweetHelpService: newTweetHelpService(_db),
CommentService: newCommentService(db), CommentService: newCommentService(_db),
CommentManageService: newCommentManageService(db), CommentManageService: newCommentManageService(_db),
UserManageService: newUserManageService(db), UserManageService: newUserManageService(_db),
ContactManageService: newContactManageService(db), ContactManageService: newContactManageService(_db),
SecurityService: newSecurityService(db, pvs), SecurityService: newSecurityService(_db, pvs),
AttachmentCheckService: security.NewAttachmentCheckService(), AttachmentCheckService: security.NewAttachmentCheckService(),
} }
return ds, ds return ds, ds
} }
func NewAuthorizationManageService() core.AuthorizationManageService { func NewAuthorizationManageService() core.AuthorizationManageService {
return newAuthorizationManageService(sqlxDB()) lazyInitial()
return newAuthorizationManageService(_db)
} }
func (s *dataServant) Name() string { func (s *dataServant) Name() string {
@ -99,3 +105,10 @@ func (s *dataServant) Name() string {
func (s *dataServant) Version() *semver.Version { func (s *dataServant) Version() *semver.Version {
return semver.MustParse("v0.1.0") return semver.MustParse("v0.1.0")
} }
// lazyInitial do some package lazy initialize for performance
func lazyInitial() {
_onceInitial.Do(func() {
initSqlxDB()
})
}

@ -8,7 +8,6 @@ import (
"context" "context"
"database/sql" "database/sql"
"strings" "strings"
"sync"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/conf"
@ -16,8 +15,7 @@ import (
) )
var ( var (
_db *sqlx.DB _db *sqlx.DB
_once sync.Once
) )
type sqlxServant struct { 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 { func r(query string) string {
db := sqlxDB() return _db.Rebind(t(query))
return db.Rebind(t(query))
} }
func c(query string) *sqlx.Stmt { 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 { if err != nil {
logrus.Fatalf("prepare query(%s) error: %s", query, err) 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 { func n(query string) *sqlx.NamedStmt {
db := sqlxDB() stmt, err := _db.PrepareNamed(t(query))
stmt, err := db.PrepareNamed(t(query))
if err != nil { if err != nil {
logrus.Fatalf("prepare named query(%s) error: %s", query, err) 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 { func t(query string) string {
return strings.Replace(query, "@", conf.DatabaseSetting.TablePrefix, -1) return strings.Replace(query, "@", conf.DatabaseSetting.TablePrefix, -1)
} }
func initSqlxDB() {
_db = conf.MustSqlxDB()
}

Loading…
Cancel
Save