diff --git a/config.yml b/config.yml index fd255b4..b4eaa91 100644 --- a/config.yml +++ b/config.yml @@ -16,8 +16,9 @@ is_gui : false # 是否ui模式显示 is_table_name : true # 是否直接生成表名 is_column_name : true # 是否直接生成列名 is_null_to_point : false # 数据库默认 'DEFAULT NULL' 时设置结构为指针类型 -table_prefix : "" # 表前缀, 如果有则使用, 没有留空 +table_prefix : "" # 表前缀, 如果有则使用, 没有留空(如果表前缀以"-"开头,则表示去掉该前缀,struct、文件名都会去掉该前缀) table_names: "" # 指定表生成,多个表用,隔开 +is_out_file_by_table_name: false # 是否根据表名生成多个model db_info: host : 127.0.0.1 # type=1的时候,host为yml文件全路径 diff --git a/data/view/model/common.go b/data/view/model/common.go index 4f0dd0b..59b411b 100755 --- a/data/view/model/common.go +++ b/data/view/model/common.go @@ -51,11 +51,11 @@ func CapLowercase(name string) string { // IDAPIID == > idAPIID func GetTablePrefixName(name string) string { // tablePrefix := config.GetTablePrefix() //如果设置了表前缀 - if tablePrefix != "" { - name = tablePrefix + name + if tablePrefix == "" || strings.HasPrefix(tablePrefix, "-") { + return name } - return name + return tablePrefix + name } func FilterKeywords(src string) string { diff --git a/data/view/model/model.go b/data/view/model/model.go index 7212b9f..abab3ec 100755 --- a/data/view/model/model.go +++ b/data/view/model/model.go @@ -51,24 +51,36 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) { return } +// getTableNameWithPrefix get table name with prefix +func getTableNameWithPrefix(tableName string) string { + tablePrefix := config.GetTablePrefix() + if tablePrefix == "" { + return tableName + } + + if strings.HasPrefix(tablePrefix, "-") { + trimPrefix := strings.TrimPrefix(tablePrefix, "-") + tableName = strings.TrimPrefix(tableName, trimPrefix) + } else { + tableName = tablePrefix + tableName + } + + return tableName +} + // GetPackage gen struct on table func (m *_Model) GetPackage() genstruct.GenPackage { if m.pkg == nil { var pkg genstruct.GenPackage pkg.SetPackage(m.info.PackageName) //package name - tablePrefix := config.GetTablePrefix() - for _, tab := range m.info.TabList { var sct genstruct.GenStruct - sct.SetTableName(tablePrefix + tab.Name) - - //如果设置了表前缀 - // if tablePrefix != "" { - // tab.Name = strings.TrimLeft(tab.Name, tablePrefix) - // } + sct.SetTableName(tab.Name) + tab.Name = getTableNameWithPrefix(tab.Name) + fmt.Println(tab.Name) sct.SetStructName(getCamelName(tab.Name)) // Big hump.大驼峰 sct.SetNotes(tab.Notes) sct.AddElement(m.genTableElement(tab.Em)...) // build element.构造元素 @@ -88,11 +100,12 @@ func (m *_Model) GenerateByTableName() (out []GenOutInfo) { var pkg genstruct.GenPackage pkg.SetPackage(m.info.PackageName) //package name var sct genstruct.GenStruct + sct.SetTableName(tab.Name) + tab.Name = getTableNameWithPrefix(tab.Name) sct.SetStructName(getCamelName(tab.Name)) // Big hump.大驼峰 sct.SetNotes(tab.Notes) sct.AddElement(m.genTableElement(tab.Em)...) // build element.构造元素 sct.SetCreatTableStr(tab.SQLBuildStr) - sct.SetTableName(tab.Name) pkg.AddStruct(sct) var stt GenOutInfo stt.FileCtx = pkg.Generate() @@ -320,11 +333,8 @@ 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) - // } + tab.Name = getTableNameWithPrefix(tab.Name) + data.StructName = getCamelName(tab.Name) var primary, unique, uniqueIndex, index []FList