1.simple配置为true不输出gorm标签

2.将is_table_name中的column生成单独抽成配置,以免配置的耦合。
3.增加根据表名生成多个model的配置(一个表一个model)
-------------------------------------------
1. Simple is configured as true and does not output Gorm tag
2. Will be_ table_ The column in name generates a separate extraction configuration to avoid the coupling of configuration.
3. Add the configuration of generating multiple models according to the table name (one model for each table)
This commit is contained in:
yanjiangtao
2021-04-06 17:19:02 +08:00
parent 1162f35ceb
commit 02aead9615
6 changed files with 117 additions and 53 deletions

View File

@@ -52,6 +52,8 @@ is_web_tag: false
is_web_tag_pk_hidden: false is_web_tag_pk_hidden: false
table_prefix: "" #table prefix table_prefix: "" #table prefix
table_names: "" # Specified table generation, multiple tables with , separated table_names: "" # Specified table generation, multiple tables with , separated
is_column_name: true # Whether to generate column names
is_out_file_by_table_name: false # Whether to generate multiple models based on table names
db_info : db_info :
host : "127.0.0.1" host : "127.0.0.1"
port : 3306 port : 3306

View File

@@ -56,6 +56,8 @@ is_web_tag: false
is_web_tag_pk_hidden: false is_web_tag_pk_hidden: false
table_prefix: "" #表前缀 table_prefix: "" #表前缀
table_names: "" #指定表生成,多个表用,隔开 table_names: "" #指定表生成,多个表用,隔开
is_column_name: true # 是否生成列名
is_out_file_by_table_name: false # 是否根据表名生成多个model
db_info: db_info:
host : 127.0.0.1 host : 127.0.0.1
port : 3306 port : 3306

View File

@@ -29,6 +29,8 @@ type Config struct {
OutFileName string `yaml:"out_file_name"` OutFileName string `yaml:"out_file_name"`
WebTagType int `yaml:"web_tag_type"` // 默认小驼峰 WebTagType int `yaml:"web_tag_type"` // 默认小驼峰
TableNames string `yaml:"table_names"` // 表名(多个表名用","隔开) TableNames string `yaml:"table_names"` // 表名(多个表名用","隔开)
IsColumnName bool `yaml:"is_column_name"` //是否输出列名
IsOutFileByTableName bool `yaml:"is_out_file_by_table_name"` //是否根据表名生成文件(多个表名生成多个文件)
} }
// DBInfo mysql database information. mysql 数据库信息 // DBInfo mysql database information. mysql 数据库信息
@@ -293,3 +295,23 @@ func GetOriginTableNames() string {
func SetTableNames(tableNames string) { func SetTableNames(tableNames string) {
_map.TableNames = tableNames _map.TableNames = tableNames
} }
//GetIsColumnName get gen columnName config . 获取生成列名的config
func GetIsColumnName() bool {
return _map.IsColumnName
}
//SetIsColumnName set gen ColumnName config. 设置生成列名的config
func SetIsColumnName(isColumnName bool) {
_map.IsColumnName = isColumnName
}
//GetIsOutFileByTableName get gen columnName config . 设置是否根据表名生成文件
func GetIsOutFileByTableName() bool {
return _map.IsOutFileByTableName
}
//SetIsOutFileByTableName set gen ColumnName config. 设置是否根据表名生成文件
func SetIsOutFileByTableName(isOutFileByTableName bool) {
_map.IsColumnName = isOutFileByTableName
}

View File

@@ -47,6 +47,8 @@ var _map = Config{
SelfTypeDef: make(map[string]string), SelfTypeDef: make(map[string]string),
WebTagType: 0, WebTagType: 0,
TableNames: "", TableNames: "",
IsColumnName: true,
IsOutFileByTableName: false,
} }
var configPath string var configPath string

View File

@@ -282,7 +282,9 @@ func (p *GenPackage) Generate() string {
for _, v1 := range v.GenerateTableName() { for _, v1 := range v.GenerateTableName() {
pa.Add(v1) pa.Add(v1)
} }
}
if config.GetIsColumnName() {
for _, v2 := range v.GenerateColumnName() { // add column list for _, v2 := range v.GenerateColumnName() { // add column list
pa.Add(v2) pa.Add(v2)
} }

View File

@@ -27,6 +27,10 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
} }
// struct // struct
if config.GetIsOutFileByTableName() {
outByTable := m.GenerateByTableName()
out = append(out, outByTable...)
} else {
var stt GenOutInfo var stt GenOutInfo
stt.FileCtx = m.generate() stt.FileCtx = m.generate()
stt.FileName = info.DbName + ".go" stt.FileName = info.DbName + ".go"
@@ -34,14 +38,19 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
if name := config.GetOutFileName(); len(name) > 0 { if name := config.GetOutFileName(); len(name) > 0 {
stt.FileName = name + ".go" stt.FileName = name + ".go"
} }
out = append(out, stt) out = append(out, stt)
}
// ------end // ------end
// gen function // gen function
if config.GetIsOutFunc() { if config.GetIsOutFunc() {
out = append(out, m.generateFunc()...) out = append(out, m.generateFunc()...)
} }
for i, outInfo := range out {
fmt.Printf("-------------%d-----------", i)
fmt.Println(outInfo)
}
// -------------- end // -------------- end
return return
} }
@@ -76,6 +85,28 @@ func (m *_Model) GetPackage() genstruct.GenPackage {
return *m.pkg return *m.pkg
} }
// GetPackageByTableName Generate multiple model files based on the table name. 根据表名生成多个model文件
func (m *_Model) GenerateByTableName() (out []GenOutInfo) {
if m.pkg == nil {
for _, tab := range m.info.TabList {
var pkg genstruct.GenPackage
pkg.SetPackage(m.info.PackageName) //package name
var sct genstruct.GenStruct
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()
stt.FileName = tab.Name + ".go"
out = append(out, stt)
}
}
return
}
func (m *_Model) generate() string { func (m *_Model) generate() string {
m.pkg = nil m.pkg = nil
m.GetPackage() m.GetPackage()
@@ -96,6 +127,8 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
tmp.SetName(getCamelName(v.Name)) tmp.SetName(getCamelName(v.Name))
tmp.SetNotes(v.Notes) tmp.SetNotes(v.Notes)
tmp.SetType(getTypeName(v.Type, v.IsNull)) tmp.SetType(getTypeName(v.Type, v.IsNull))
// not simple output. 默认不输出gorm标签
if !config.GetSimple() {
for _, v1 := range v.Index { for _, v1 := range v.Index {
switch v1.Key { switch v1.Key {
// case ColumnsKeyDefault: // case ColumnsKeyDefault:
@@ -115,6 +148,7 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
} }
} }
} }
}
if len(v.Name) > 0 { if len(v.Name) > 0 {
// not simple output // not simple output
@@ -124,11 +158,11 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
if !v.IsNull { if !v.IsNull {
tmp.AddTag(_tagGorm, "not null") tmp.AddTag(_tagGorm, "not null")
} }
}
// default tag // default tag
if len(v.Gormt) > 0 { if len(v.Gormt) > 0 {
tmp.AddTag(_tagGorm, v.Gormt) tmp.AddTag(_tagGorm, v.Gormt)
} }
}
// json tag // json tag
if config.GetIsWEBTag() { if config.GetIsWEBTag() {