From aa7a0be729d7ecaa1e8bf835c504ed6fbdb62082 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Thu, 5 Jan 2023 00:36:08 +0800 Subject: [PATCH] sqlx: prepare base code for sqlx logic --- go.mod | 1 + go.sum | 2 ++ internal/conf/{db.go => db_gorm.go} | 11 +++--- internal/conf/db_sqlx.go | 23 ++++++++++++ internal/dao/sakila/index.go | 55 +++++++++++++++++++++++++++++ internal/dao/sakila/sakila.go | 2 ++ internal/dao/sakila/sqlx.go | 52 +++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 5 deletions(-) rename internal/conf/{db.go => db_gorm.go} (94%) create mode 100644 internal/conf/db_sqlx.go create mode 100644 internal/dao/sakila/index.go create mode 100644 internal/dao/sakila/sqlx.go diff --git a/go.mod b/go.mod index 61fe1d1e..047fa70a 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/golang-jwt/jwt/v4 v4.4.2 github.com/golang-migrate/migrate/v4 v4.15.2 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/meilisearch/meilisearch-go v0.21.0 github.com/minio/minio-go/v7 v7.0.45 diff --git a/go.sum b/go.sum index 3a27eb98..e09346b6 100644 --- a/go.sum +++ b/go.sum @@ -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/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.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/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= diff --git a/internal/conf/db.go b/internal/conf/db_gorm.go similarity index 94% rename from internal/conf/db.go rename to internal/conf/db_gorm.go index 62ad7fe3..5d28b6f2 100644 --- a/internal/conf/db.go +++ b/internal/conf/db_gorm.go @@ -20,19 +20,20 @@ import ( ) var ( - db *gorm.DB Redis *redis.Client - once sync.Once + + _gormdb *gorm.DB + _onceGorm sync.Once ) func MustGormDB() *gorm.DB { - once.Do(func() { + _onceGorm.Do(func() { var err error - if db, err = newDBEngine(); err != nil { + if _gormdb, err = newDBEngine(); err != nil { logrus.Fatalf("new gorm db failed: %s", err) } }) - return db + return _gormdb } func newDBEngine() (*gorm.DB, error) { diff --git a/internal/conf/db_sqlx.go b/internal/conf/db_sqlx.go new file mode 100644 index 00000000..941b219c --- /dev/null +++ b/internal/conf/db_sqlx.go @@ -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 +} diff --git a/internal/dao/sakila/index.go b/internal/dao/sakila/index.go new file mode 100644 index 00000000..c382993b --- /dev/null +++ b/internal/dao/sakila/index.go @@ -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=? + `), + } +} diff --git a/internal/dao/sakila/sakila.go b/internal/dao/sakila/sakila.go index d82a0d23..228264f0 100644 --- a/internal/dao/sakila/sakila.go +++ b/internal/dao/sakila/sakila.go @@ -14,6 +14,8 @@ import ( func NewDataService() (core.DataService, core.VersionInfo) { logrus.Fatal("not support now") + _ = newIndexPostsService(sqlxDB()) + _ = newSimpleIndexPostsService(sqlxDB()) return nil, nil } diff --git a/internal/dao/sakila/sqlx.go b/internal/dao/sakila/sqlx.go new file mode 100644 index 00000000..b1f9d1e7 --- /dev/null +++ b/internal/dao/sakila/sqlx.go @@ -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) +}