From dc8a862255d4bd30188500d0faf5a9531095cf21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8B=87=E6=94=80?= <302941691@qq.com> Date: Fri, 12 Mar 2021 20:12:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E9=80=9A=E8=BF=87=E4=BD=BF=E7=94=A8--tabl?= =?UTF-8?q?e=5Fprefix=20=E7=BB=99=E8=A1=A8=E5=90=8D=E5=8A=A0=E5=89=8D?= =?UTF-8?q?=E7=BC=80=20=E4=BD=BF=E5=BE=97=EF=BC=9A=20func=20(obj=20*=5FSym?= =?UTF-8?q?bolMgr)=20GetTableName()=20string=20{=20=20return=20"symbol"=20?= =?UTF-8?q?}=20=E5=8F=98=E6=88=90=EF=BC=9A=20func=20(obj=20*=5FSymbolMgr)?= =?UTF-8?q?=20GetTableName()=20string=20{=20=20return=20"tableprefix.symbo?= =?UTF-8?q?l"=20}=20=E5=B9=B6=E5=9C=A8=E6=BA=90=E5=A4=B4=E7=A1=AE=E5=AE=9A?= =?UTF-8?q?=E8=A1=A8=E5=90=8D:tableprefix.symbol.=20=E5=8D=B3=E7=9C=9F?= =?UTF-8?q?=E6=AD=A3=E6=89=A7=E8=A1=8Cmysql=E8=AF=AD=E5=8F=A5=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=EF=BC=8C=E4=BC=9A=E6=8C=89=E7=85=A7tablepref?= =?UTF-8?q?ix.symbol=E8=BF=99=E4=B8=AA=E8=A1=A8=E5=90=8D=E5=8E=BB=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/view/genfunc/def.go | 4 ++-- data/view/model/common.go | 11 +++++++++++ data/view/model/model.go | 20 ++++++++++---------- 3 files changed, 23 insertions(+), 12 deletions(-) mode change 100644 => 100755 data/view/genfunc/def.go mode change 100644 => 100755 data/view/model/common.go mode change 100644 => 100755 data/view/model/model.go diff --git a/data/view/genfunc/def.go b/data/view/genfunc/def.go old mode 100644 new mode 100755 index 5c5d089..73a07cf --- a/data/view/genfunc/def.go +++ b/data/view/genfunc/def.go @@ -122,12 +122,12 @@ func {{$obj.StructName}}Mgr(db *gorm.DB) *_{{$obj.StructName}}Mgr { panic(fmt.Errorf("{{$obj.StructName}}Mgr need init by db")) } ctx, cancel := context.WithCancel(context.Background()) - return &_{{$obj.StructName}}Mgr{_BaseMgr: &_BaseMgr{DB: db.Table("{{$obj.TableName}}"), isRelated: globalIsRelated,ctx:ctx,cancel:cancel,timeout:-1}} + return &_{{$obj.StructName}}Mgr{_BaseMgr: &_BaseMgr{DB: db.Table("{{GetTablePrefixName $obj.TableName}}"), isRelated: globalIsRelated,ctx:ctx,cancel:cancel,timeout:-1}} } // GetTableName get sql table name.获取数据库名字 func (obj *_{{$obj.StructName}}Mgr) GetTableName() string { - return "{{$obj.TableName}}" + return "{{GetTablePrefixName $obj.TableName}}" } // Get 获取 diff --git a/data/view/model/common.go b/data/view/model/common.go old mode 100644 new mode 100755 index 6765844..5ea0544 --- a/data/view/model/common.go +++ b/data/view/model/common.go @@ -47,6 +47,17 @@ func CapLowercase(name string) string { // IDAPIID == > idAPIID return FilterKeywords(re) } +// GetTablePrefixName 获取带表前缀名字的tablename +func GetTablePrefixName(name string) string { // + tablePrefix := config.GetTablePrefix() + //如果设置了表前缀 + if tablePrefix != "" { + return fmt.Sprintf("%v.%v", tablePrefix, name) + } + + return name +} + func FilterKeywords(src string) string { if tools.IsKeywords(src) { return "_" + src diff --git a/data/view/model/model.go b/data/view/model/model.go old mode 100644 new mode 100755 index 66c5a15..37867d4 --- a/data/view/model/model.go +++ b/data/view/model/model.go @@ -48,7 +48,7 @@ func (m *_Model) GetPackage() genstruct.GenPackage { var pkg genstruct.GenPackage pkg.SetPackage(m.info.PackageName) //package name - tablePrefix := config.GetTablePrefix() + // tablePrefix := config.GetTablePrefix() for _, tab := range m.info.TabList { var sct genstruct.GenStruct @@ -56,9 +56,9 @@ func (m *_Model) GetPackage() genstruct.GenPackage { sct.SetTableName(tab.Name) //如果设置了表前缀 - if tablePrefix != "" { - tab.Name = strings.TrimLeft(tab.Name, tablePrefix) - } + // if tablePrefix != "" { + // tab.Name = strings.TrimLeft(tab.Name, tablePrefix) + // } sct.SetStructName(getCamelName(tab.Name)) // Big hump.大驼峰 sct.SetNotes(tab.Notes) @@ -257,11 +257,11 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) { // wxw 2021.2.26 17:17 var data funDef data.TableName = tab.Name - tablePrefix := config.GetTablePrefix() - //如果设置了表前缀 - if tablePrefix != "" { - tab.Name = strings.TrimLeft(tab.Name, tablePrefix) - } + // tablePrefix := config.GetTablePrefix() + // //如果设置了表前缀 + // if tablePrefix != "" { + // tab.Name = strings.TrimLeft(tab.Name, tablePrefix) + // } data.StructName = getCamelName(tab.Name) var primary, unique, uniqueIndex, index []FList @@ -335,7 +335,7 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) { data.Primay = append(data.Primay, uniqueIndex...) data.Index = append(data.Index, index...) tmpl, err := template.New("gen_logic"). - Funcs(template.FuncMap{"GenPreloadList": GenPreloadList, "GenFListIndex": GenFListIndex, "CapLowercase": CapLowercase}). + Funcs(template.FuncMap{"GenPreloadList": GenPreloadList, "GenFListIndex": GenFListIndex, "CapLowercase": CapLowercase, "GetTablePrefixName": GetTablePrefixName}). Parse(genfunc.GetGenLogicTemp()) if err != nil { panic(err)