diff --git a/db/commands/base/table.go b/db/commands/base/table.go index ffb09f156f2cdc61a8075ded7d8fda533e3ec923..fd07bfca8be331105a1d50738383e7c2d978d57f 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 6cab639acdeca71622fd159ef6ecafada756e5e4..f972df83193857e86c774b7cb40a07643b224a6d 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 63ffaf12652f6efb8288789baad16abe0a58f0ac..51862e2234e982af1c318f306a1f08ebcd58ea3d 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}}