Skip to content

Commit

Permalink
Merge pull request unionj-cloud#193 from wubin1989/main
Browse files Browse the repository at this point in the history
...
  • Loading branch information
wubin1989 authored Apr 14, 2024
2 parents c60988a + 3add335 commit ed45723
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 16 deletions.
3 changes: 3 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var dbService bool
var dbTablePrefix string
var dbTableGlob string
var module bool
var dbGenGenGo bool

// initCmd initializes the service
var initCmd = &cobra.Command{
Expand All @@ -41,6 +42,7 @@ var initCmd = &cobra.Command{
Dsn: dbDsn,
TablePrefix: dbTablePrefix,
TableGlob: dbTableGlob,
GenGenGo: dbGenGenGo,
Orm: dbOrm,
Soft: dbSoft,
Grpc: dbGrpc,
Expand Down Expand Up @@ -72,6 +74,7 @@ func init() {
initCmd.Flags().StringVar(&dbSoft, "db_soft", "deleted_at", `Specify database soft delete column name`)
initCmd.Flags().BoolVar(&dbGrpc, "db_grpc", false, `If true, grpc code will also be generated`)
initCmd.Flags().BoolVar(&dbService, "db_service", false, `If false, service will not be generated, and db_grpc will be ignored. Only dao layer code will be generated.`)
initCmd.Flags().BoolVar(&dbGenGenGo, "db_gen_gen", false, `whether generate gen.go file`)
initCmd.Flags().StringVar(&dbTablePrefix, "db_table_prefix", "", `table prefix or schema name for pg`)
initCmd.Flags().StringVar(&dbTableGlob, "db_table_glob", "", `used to filter glob-matched tables`)
initCmd.Flags().StringVar(&naming, "case", "lowerCamel", `protobuf message field and json tag case, only support "lowerCamel" and "snake"`)
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/svc/codegen/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type OrmGeneratorConfig struct {
Dsn string
TablePrefix string
TableGlob string
GenGenGo bool
CaseConverter func(string) string
Dir string
Soft string
Expand All @@ -62,6 +63,7 @@ type AbstractBaseGenerator struct {
Dsn string
TablePrefix string
TableGlob string
GenGenGo bool
Dir string
g *gormgen.Generator
CaseConverter func(string) string
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/svc/codegen/database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (gg *GormGenerator) Initialize(conf OrmGeneratorConfig) {
gg.Grpc = conf.Grpc
gg.TablePrefix = strings.TrimSuffix(conf.TablePrefix, ".")
gg.TableGlob = conf.TableGlob
gg.GenGenGo = conf.GenGenGo
gg.CaseConverter = conf.CaseConverter
var db *gorm.DB
var err error
Expand Down Expand Up @@ -157,6 +158,7 @@ func (gg *GormGenerator) Initialize(conf OrmGeneratorConfig) {
g.WithJSONTagNameStrategy(func(n string) string { return gg.CaseConverter(n) + ",omitempty" })
g.WithImportPkgPath("github.com/unionj-cloud/go-doudou/v2/toolkit/customtypes")
g.UseDB(db)
g.GenGenGo = gg.GenGenGo
var models []interface{}
if stringutils.IsNotEmpty(gg.TableGlob) {
g.FilterTableGlob = glob.MustCompile(gg.TableGlob)
Expand Down
11 changes: 7 additions & 4 deletions cmd/internal/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ type DbConfig struct {
// or schema for pg
TablePrefix string
TableGlob string
Orm string
Soft string
Grpc bool
Service bool
// whether generate gen.go file
GenGenGo bool
Orm string
Soft string
Grpc bool
Service bool
}

func (receiver *Svc) SetWatcher(w *watcher.Watcher) {
Expand Down Expand Up @@ -188,6 +190,7 @@ func (receiver *Svc) Init() {
Dsn: receiver.DbConfig.Dsn,
TablePrefix: receiver.DbConfig.TablePrefix,
TableGlob: receiver.DbConfig.TableGlob,
GenGenGo: receiver.DbConfig.GenGenGo,
CaseConverter: receiver.CaseConverter,
Dir: receiver.dir,
Soft: receiver.DbConfig.Soft,
Expand Down
11 changes: 11 additions & 0 deletions framework/rest/bizerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rest

import (
"fmt"
"github.com/pkg/errors"
"net/http"
)

Expand Down Expand Up @@ -83,3 +84,13 @@ func PanicInternalServerError(err error) {
}
panic(NewBizError(err))
}

func UnWrap(err error) error {
var bizErr BizError
if errors.As(err, &bizErr) {
if bizErr.Cause != nil {
return UnWrap(bizErr.Cause)
}
}
return err
}
4 changes: 3 additions & 1 deletion framework/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func (srv *RestServer) PreMiddleware(mwf ...func(http.Handler) http.Handler) {

func (srv *RestServer) configure() {
if config.GddConfig.ManageEnable {
srv.middlewares = append([]MiddlewareFunc{srv.panicHandler}, srv.middlewares...)
srv.middlewares = append([]MiddlewareFunc{PrometheusMiddleware}, srv.middlewares...)
gddRouter := srv.rootRouter.NewGroup(gddPathPrefix)
corsOpts := cors.New(cors.Options{
Expand Down Expand Up @@ -350,8 +351,9 @@ func (srv *RestServer) configure() {
}
debugRouter.Handler(item.Method, "/"+strings.TrimPrefix(item.Pattern, debugPathPrefix), h, item.Name)
}
} else {
srv.middlewares = append([]MiddlewareFunc{srv.panicHandler}, srv.middlewares...)
}
srv.middlewares = append(srv.middlewares, srv.panicHandler)
for _, item := range srv.bizRoutes {
h := http.Handler(item.HandlerFunc)
for i := len(srv.middlewares) - 1; i >= 0; i-- {
Expand Down
1 change: 1 addition & 0 deletions toolkit/gormgen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Config struct {
ConfigPackage string

FilterTableGlob glob.Glob
GenGenGo bool
}

// WithOpts set global model options
Expand Down
2 changes: 1 addition & 1 deletion toolkit/gormgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ func (g *Generator) generateQueryFile() (err error) {
case <-pool.AsyncWaitAll():
}

if g.FilterTableGlob != nil {
if !g.GenGenGo {
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions toolkit/gormgen/internal/template/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (q *Query) Clone(db *gorm.DB) *Query {
}
}
func (q *Query) Db() *gorm.DB {
return q.db
}
func (q *Query) ReadDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
}
Expand Down
41 changes: 31 additions & 10 deletions toolkit/pagination/gorm/paginate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strconv"
"strings"

"github.com/iancoleman/strcase"
"github.com/morkid/gocache"
"gorm.io/gorm"
)
Expand All @@ -22,6 +21,7 @@ import (
type ResponseContext interface {
Cache(string) ResponseContext
Fields([]string) ResponseContext
Distinct([]string) ResponseContext
Response(interface{}) Page
Error() error
}
Expand Down Expand Up @@ -82,12 +82,14 @@ func (r reqContext) Request(parameter IParameter) ResponseContext {
}

type resContext struct {
Pagination *Pagination
Statement *gorm.DB
Parameter IParameter
cachePrefix string
fieldList []string
error error
Pagination *Pagination
Statement *gorm.DB
Parameter IParameter
cachePrefix string
fieldList []string
customSelect string
distinct bool
error error
}

func (r *resContext) Error() error {
Expand All @@ -104,6 +106,13 @@ func (r *resContext) Fields(fields []string) ResponseContext {
return r
}

// CustomSelect currently used for distinct on clause
func (r *resContext) Distinct(fields []string) ResponseContext {
r.fieldList = fields
r.distinct = true
return r
}

func (r *resContext) Response(res interface{}) Page {
p := r.Pagination
query := r.Statement
Expand Down Expand Up @@ -143,7 +152,6 @@ func (r *resContext) Response(res interface{}) Page {
hasAdapter = true
if cKey != "" && adapter.IsValid(cKey) {
if cache, err := adapter.Get(cKey); nil == err {
page.Items, _ = sliceutils.ConvertAny2Interface(res)
if err := p.Config.JSONUnmarshal([]byte(cache), &page); nil == err {
return page
}
Expand Down Expand Up @@ -188,13 +196,22 @@ func (r *resContext) Response(res interface{}) Page {
Table("(?) AS s", query)

if len(selects) > 0 {
result = result.Select(selects)
if r.distinct {
result = result.Distinct(selects)
} else {
result = result.Select(selects)
}
}

if len(causes.Params) > 0 || len(causes.WhereString) > 0 {
result = result.Where(causes.WhereString, causes.Params...)
}

dbs = query.Statement.DB.Session(&gorm.Session{NewDB: true})
result = dbs.
Unscoped().
Table("(?) AS s1", result)

result = result.Count(&page.Total).
Limit(int(causes.Limit)).
Offset(int(causes.Offset))
Expand Down Expand Up @@ -622,7 +639,11 @@ func contains(source []string, value string) bool {
}

func FieldAs(tableName, colName string) string {
return strcase.ToSnake(fmt.Sprintf("%s_%s", tableName, colName))
return fmt.Sprintf("%s_%s", strings.ToLower(tableName), strings.ToLower(colName))
}

func GetLowerColNameFromAlias(alias, tableName string) string {
return strings.TrimPrefix(alias, strings.ToLower(tableName)+"_")
}

func fieldName(field string) string {
Expand Down

0 comments on commit ed45723

Please sign in to comment.