From 0e917c808da5ee447bcdcbf7b2973f8565bb2fef Mon Sep 17 00:00:00 2001 From: wsfuyibing Date: Mon, 10 Feb 2025 14:36:33 +0800 Subject: [PATCH] updates 1. change exported template 2. use dsn name in configuration --- db/commands/base/table.go | 9 +++++---- db/commands/common.go | 18 +++++++++++++----- db/commands/gen_service/generator.tpl | 3 ++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/db/commands/base/table.go b/db/commands/base/table.go index ffb09f1..fd07bfc 100644 --- a/db/commands/base/table.go +++ b/db/commands/base/table.go @@ -41,10 +41,11 @@ type Table struct { Script template.HTML `xorm:"-"` // Execution fields. - ModelName string `xorm:"-"` - ModelPkg string `xorm:"-"` - ServiceName string `xorm:"-"` - ServicePkg string `xorm:"-"` + ConnectionKey string `xorm:"-"` + ModelName string `xorm:"-"` + ModelPkg string `xorm:"-"` + ServiceName string `xorm:"-"` + ServicePkg string `xorm:"-"` // Primary prop. PrimaryName string `xorm:"-"` diff --git a/db/commands/common.go b/db/commands/common.go index 6cab639..f972df8 100644 --- a/db/commands/common.go +++ b/db/commands/common.go @@ -36,7 +36,7 @@ var RegexMatchPkg = regexp.MustCompile(`([_a-zA-Z0-9]+)$`) type Common struct { BaseDir string - Dsn string + Dsn, DsnKey string Export, Prefix string ModelPath, ModelPkg string Override bool @@ -193,16 +193,23 @@ func (o *Common) ReadDsn(command *console.Command, key string) (err error) { // Read dsn // from command options. if opt, has := command.GetOption(key); has { - if s := opt.ToString(); s != "" { - o.Dsn = s - return + if k := opt.ToString(); k != "" { + if cfg, ok := db.Config.Get(k); ok && len(cfg.Dsn) > 0 { + o.Dsn = cfg.Dsn[0] + o.DsnKey = k + return + } + o.Dsn = k } + return } // Use default // configuration of config files. - if cfg, ok := db.Config.Get(); ok && len(cfg.Dsn) > 0 { + k := "db" + if cfg, ok := db.Config.Get(k); ok && len(cfg.Dsn) > 0 { o.Dsn = cfg.Dsn[0] + o.DsnKey = k return } @@ -379,6 +386,7 @@ func (o *Common) FormatTableAsModel(table *base.Table) { } func (o *Common) FormatTableAsService(table *base.Table) { + table.ConnectionKey = o.DsnKey table.ModelName = table.Name.StructName(o.Prefix) table.ServiceName = fmt.Sprintf(`%sService`, table.ModelName) diff --git a/db/commands/gen_service/generator.tpl b/db/commands/gen_service/generator.tpl index 63ffaf1..51862e2 100644 --- a/db/commands/gen_service/generator.tpl +++ b/db/commands/gen_service/generator.tpl @@ -27,6 +27,7 @@ type {{.ServiceName}} struct { func New{{.ServiceName}}(sess ... *db.Session) *{{.ServiceName}}{ o := &{{.ServiceName}}{} o.With(sess...) + o.WithKey("{{.ConnectionKey}}") return o } @@ -116,4 +117,4 @@ func (o *{{.ServiceName}}) GetBy{{.PrimaryName}}(ctx context.Context, val {{.Pri } return } -{{- end}} \ No newline at end of file +{{- end}} -- Gitee