Merge pull request #129 from crabmanY/master
修改simple配置为true不输出gorm标签,将is_table_name中的column生成单独抽成配置,以免配置的耦合,增加根据表名生成多个model的配置(一个表一个model)
This commit is contained in:
@@ -52,6 +52,8 @@ is_web_tag: false
|
||||
is_web_tag_pk_hidden: false
|
||||
table_prefix: "" #table prefix
|
||||
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 :
|
||||
host : "127.0.0.1"
|
||||
port : 3306
|
||||
|
||||
@@ -56,6 +56,8 @@ is_web_tag: false
|
||||
is_web_tag_pk_hidden: false
|
||||
table_prefix: "" #表前缀
|
||||
table_names: "" #指定表生成,多个表用,隔开
|
||||
is_column_name: true # 是否生成列名
|
||||
is_out_file_by_table_name: false # 是否根据表名生成多个model
|
||||
db_info:
|
||||
host : 127.0.0.1
|
||||
port : 3306
|
||||
|
||||
@@ -9,26 +9,28 @@ import (
|
||||
|
||||
// Config custom config struct
|
||||
type Config struct {
|
||||
CfgBase `yaml:"base"`
|
||||
DBInfo DBInfo `yaml:"db_info"`
|
||||
OutDir string `yaml:"out_dir"`
|
||||
URLTag string `yaml:"url_tag"` // url tag
|
||||
Language string `yaml:"language"` // language
|
||||
DbTag string `yaml:"db_tag"` // 数据库标签(gormt,db)
|
||||
Simple bool `yaml:"simple"`
|
||||
IsWEBTag bool `yaml:"is_web_tag"`
|
||||
IsWebTagPkHidden bool `yaml:"is_web_tag_pk_hidden"` // web标记是否隐藏主键
|
||||
IsForeignKey bool `yaml:"is_foreign_key"`
|
||||
IsOutSQL bool `yaml:"is_out_sql"`
|
||||
IsOutFunc bool `yaml:"is_out_func"`
|
||||
IsGUI bool `yaml:"is_gui"` //
|
||||
IsTableName bool `yaml:"is_table_name"`
|
||||
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
|
||||
TablePrefix string `yaml:"table_prefix"` // 表前缀
|
||||
SelfTypeDef map[string]string `yaml:"self_type_define"`
|
||||
OutFileName string `yaml:"out_file_name"`
|
||||
WebTagType int `yaml:"web_tag_type"` // 默认小驼峰
|
||||
TableNames string `yaml:"table_names"` // 表名(多个表名用","隔开)
|
||||
CfgBase `yaml:"base"`
|
||||
DBInfo DBInfo `yaml:"db_info"`
|
||||
OutDir string `yaml:"out_dir"`
|
||||
URLTag string `yaml:"url_tag"` // url tag
|
||||
Language string `yaml:"language"` // language
|
||||
DbTag string `yaml:"db_tag"` // 数据库标签(gormt,db)
|
||||
Simple bool `yaml:"simple"`
|
||||
IsWEBTag bool `yaml:"is_web_tag"`
|
||||
IsWebTagPkHidden bool `yaml:"is_web_tag_pk_hidden"` // web标记是否隐藏主键
|
||||
IsForeignKey bool `yaml:"is_foreign_key"`
|
||||
IsOutSQL bool `yaml:"is_out_sql"`
|
||||
IsOutFunc bool `yaml:"is_out_func"`
|
||||
IsGUI bool `yaml:"is_gui"` //
|
||||
IsTableName bool `yaml:"is_table_name"`
|
||||
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
|
||||
TablePrefix string `yaml:"table_prefix"` // 表前缀
|
||||
SelfTypeDef map[string]string `yaml:"self_type_define"`
|
||||
OutFileName string `yaml:"out_file_name"`
|
||||
WebTagType int `yaml:"web_tag_type"` // 默认小驼峰
|
||||
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 数据库信息
|
||||
@@ -293,3 +295,23 @@ func GetOriginTableNames() string {
|
||||
func SetTableNames(tableNames string) {
|
||||
_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
|
||||
}
|
||||
|
||||
@@ -39,14 +39,16 @@ var _map = Config{
|
||||
Simple: false,
|
||||
IsWEBTag: false,
|
||||
// SingularTable: true,
|
||||
IsForeignKey: true,
|
||||
IsOutSQL: false,
|
||||
IsOutFunc: true,
|
||||
IsGUI: false,
|
||||
TablePrefix: "",
|
||||
SelfTypeDef: make(map[string]string),
|
||||
WebTagType: 0,
|
||||
TableNames: "",
|
||||
IsForeignKey: true,
|
||||
IsOutSQL: false,
|
||||
IsOutFunc: true,
|
||||
IsGUI: false,
|
||||
TablePrefix: "",
|
||||
SelfTypeDef: make(map[string]string),
|
||||
WebTagType: 0,
|
||||
TableNames: "",
|
||||
IsColumnName: true,
|
||||
IsOutFileByTableName: false,
|
||||
}
|
||||
|
||||
var configPath string
|
||||
|
||||
@@ -282,7 +282,9 @@ func (p *GenPackage) Generate() string {
|
||||
for _, v1 := range v.GenerateTableName() {
|
||||
pa.Add(v1)
|
||||
}
|
||||
}
|
||||
|
||||
if config.GetIsColumnName() {
|
||||
for _, v2 := range v.GenerateColumnName() { // add column list
|
||||
pa.Add(v2)
|
||||
}
|
||||
|
||||
@@ -27,15 +27,20 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
|
||||
}
|
||||
|
||||
// struct
|
||||
var stt GenOutInfo
|
||||
stt.FileCtx = m.generate()
|
||||
stt.FileName = info.DbName + ".go"
|
||||
if config.GetIsOutFileByTableName() {
|
||||
outByTable := m.GenerateByTableName()
|
||||
out = append(out, outByTable...)
|
||||
} else {
|
||||
var stt GenOutInfo
|
||||
stt.FileCtx = m.generate()
|
||||
stt.FileName = info.DbName + ".go"
|
||||
|
||||
if name := config.GetOutFileName(); len(name) > 0 {
|
||||
stt.FileName = name + ".go"
|
||||
if name := config.GetOutFileName(); len(name) > 0 {
|
||||
stt.FileName = name + ".go"
|
||||
}
|
||||
out = append(out, stt)
|
||||
}
|
||||
|
||||
out = append(out, stt)
|
||||
// ------end
|
||||
|
||||
// gen function
|
||||
@@ -76,6 +81,28 @@ func (m *_Model) GetPackage() genstruct.GenPackage {
|
||||
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 {
|
||||
m.pkg = nil
|
||||
m.GetPackage()
|
||||
@@ -96,22 +123,25 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
|
||||
tmp.SetName(getCamelName(v.Name))
|
||||
tmp.SetNotes(v.Notes)
|
||||
tmp.SetType(getTypeName(v.Type, v.IsNull))
|
||||
for _, v1 := range v.Index {
|
||||
switch v1.Key {
|
||||
// case ColumnsKeyDefault:
|
||||
case ColumnsKeyPrimary: // primary key.主键
|
||||
tmp.AddTag(_tagGorm, "primaryKey")
|
||||
isPK = true
|
||||
case ColumnsKeyUnique: // unique key.唯一索引
|
||||
tmp.AddTag(_tagGorm, "unique")
|
||||
case ColumnsKeyIndex: // index key.复合索引
|
||||
if v1.KeyType == "FULLTEXT" {
|
||||
tmp.AddTag(_tagGorm, getUninStr("index", ":", v1.KeyName)+",class:FULLTEXT")
|
||||
} else {
|
||||
tmp.AddTag(_tagGorm, getUninStr("index", ":", v1.KeyName))
|
||||
// not simple output. 默认不输出gorm标签
|
||||
if !config.GetSimple() {
|
||||
for _, v1 := range v.Index {
|
||||
switch v1.Key {
|
||||
// case ColumnsKeyDefault:
|
||||
case ColumnsKeyPrimary: // primary key.主键
|
||||
tmp.AddTag(_tagGorm, "primaryKey")
|
||||
isPK = true
|
||||
case ColumnsKeyUnique: // unique key.唯一索引
|
||||
tmp.AddTag(_tagGorm, "unique")
|
||||
case ColumnsKeyIndex: // index key.复合索引
|
||||
if v1.KeyType == "FULLTEXT" {
|
||||
tmp.AddTag(_tagGorm, getUninStr("index", ":", v1.KeyName)+",class:FULLTEXT")
|
||||
} else {
|
||||
tmp.AddTag(_tagGorm, getUninStr("index", ":", v1.KeyName))
|
||||
}
|
||||
case ColumnsKeyUniqueIndex: // unique index key.唯一复合索引
|
||||
tmp.AddTag(_tagGorm, getUninStr("uniqueIndex", ":", v1.KeyName))
|
||||
}
|
||||
case ColumnsKeyUniqueIndex: // unique index key.唯一复合索引
|
||||
tmp.AddTag(_tagGorm, getUninStr("uniqueIndex", ":", v1.KeyName))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,10 +154,10 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
|
||||
if !v.IsNull {
|
||||
tmp.AddTag(_tagGorm, "not null")
|
||||
}
|
||||
}
|
||||
// default tag
|
||||
if len(v.Gormt) > 0 {
|
||||
tmp.AddTag(_tagGorm, v.Gormt)
|
||||
// default tag
|
||||
if len(v.Gormt) > 0 {
|
||||
tmp.AddTag(_tagGorm, v.Gormt)
|
||||
}
|
||||
}
|
||||
|
||||
// json tag
|
||||
|
||||
Reference in New Issue
Block a user