sqlx: prepare base code for sqlx logic

pull/351/head
Michael Li 2 years ago
parent 8e47faec1e
commit aa7a0be729
No known key found for this signature in database

@ -23,6 +23,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.4.2 github.com/golang-jwt/jwt/v4 v4.4.2
github.com/golang-migrate/migrate/v4 v4.15.2 github.com/golang-migrate/migrate/v4 v4.15.2
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.12+incompatible github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.12+incompatible
github.com/jmoiron/sqlx v1.3.5
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/meilisearch/meilisearch-go v0.21.0 github.com/meilisearch/meilisearch-go v0.21.0
github.com/minio/minio-go/v7 v7.0.45 github.com/minio/minio-go/v7 v7.0.45

@ -911,6 +911,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/joefitzgerald/rainbow-reporter v0.1.0/go.mod h1:481CNgqmVHQZzdIbN52CupLJyoVwB10FQ/IQlF1pdL8= github.com/joefitzgerald/rainbow-reporter v0.1.0/go.mod h1:481CNgqmVHQZzdIbN52CupLJyoVwB10FQ/IQlF1pdL8=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=

@ -20,19 +20,20 @@ import (
) )
var ( var (
db *gorm.DB
Redis *redis.Client Redis *redis.Client
once sync.Once
_gormdb *gorm.DB
_onceGorm sync.Once
) )
func MustGormDB() *gorm.DB { func MustGormDB() *gorm.DB {
once.Do(func() { _onceGorm.Do(func() {
var err error var err error
if db, err = newDBEngine(); err != nil { if _gormdb, err = newDBEngine(); err != nil {
logrus.Fatalf("new gorm db failed: %s", err) logrus.Fatalf("new gorm db failed: %s", err)
} }
}) })
return db return _gormdb
} }
func newDBEngine() (*gorm.DB, error) { func newDBEngine() (*gorm.DB, error) {

@ -0,0 +1,23 @@
// 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 conf
import (
"sync"
"github.com/jmoiron/sqlx"
)
var (
_sqlxdb *sqlx.DB
_onceSqlx sync.Once
)
func MustSqlxDB() *sqlx.DB {
_onceSqlx.Do(func() {
// TODO: init sqlx.DB
})
return _sqlxdb
}

@ -0,0 +1,55 @@
// 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 sakila
import (
"github.com/jmoiron/sqlx"
"github.com/rocboss/paopao-ce/internal/core"
)
var (
_ core.IndexPostsService = (*indexPostsServant)(nil)
_ core.IndexPostsService = (*simpleIndexPostsServant)(nil)
)
type indexPostsServant struct {
db *sqlx.DB
stmtIndex *sqlx.Stmt
}
type simpleIndexPostsServant struct {
db *sqlx.DB
stmtIndex *sqlx.Stmt
}
// IndexPosts 根据userId查询广场推文列表简单做到不同用户的主页都是不同的
func (s *indexPostsServant) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) {
// TODO
return nil, nil
}
// simpleCacheIndexGetPosts simpleCacheIndex 专属获取广场推文列表函数
func (s *simpleIndexPostsServant) IndexPosts(_user *core.User, offset int, limit int) (*core.IndexTweetList, error) {
// TODO
return nil, nil
}
func newIndexPostsService(db *sqlx.DB) core.IndexPostsService {
return &indexPostsServant{
db: db,
stmtIndex: c(`
SELECT * FROM @person WHERE first_name=?
`),
}
}
func newSimpleIndexPostsService(db *sqlx.DB) core.IndexPostsService {
return &simpleIndexPostsServant{
db: db,
stmtIndex: c(`
SELECT * FROM @person WHERE first_name=?
`),
}
}

@ -14,6 +14,8 @@ import (
func NewDataService() (core.DataService, core.VersionInfo) { func NewDataService() (core.DataService, core.VersionInfo) {
logrus.Fatal("not support now") logrus.Fatal("not support now")
_ = newIndexPostsService(sqlxDB())
_ = newSimpleIndexPostsService(sqlxDB())
return nil, nil return nil, nil
} }

@ -0,0 +1,52 @@
// 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.
// Core service implement base sqlx+mysql. All sub-service
// will declare here and provide initial function.
package sakila
import (
"strings"
"sync"
"github.com/jmoiron/sqlx"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/sirupsen/logrus"
)
var (
_db *sqlx.DB
_once sync.Once
)
func sqlxDB() *sqlx.DB {
_once.Do(func() {
_db = conf.MustSqlxDB()
})
return _db
}
func c(query string) *sqlx.Stmt {
db := sqlxDB()
stmt, err := db.Preparex(db.Rebind(t(query)))
if err != nil {
logrus.Fatalf("prepare query(%s) error: %s", query, err)
}
return stmt
}
func n(query string) *sqlx.NamedStmt {
db := sqlxDB()
stmt, err := db.PrepareNamed(t(query))
if err != nil {
logrus.Fatalf("prepare named query(%s) error: %s", query, err)
}
return stmt
}
// t repace table prefix for query
func t(query string) string {
return strings.Replace(query, "@", conf.DatabaseSetting.TablePrefix, -1)
}
Loading…
Cancel
Save