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
|
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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -9,26 +9,28 @@ import (
|
|||||||
|
|
||||||
// Config custom config struct
|
// Config custom config struct
|
||||||
type Config struct {
|
type Config struct {
|
||||||
CfgBase `yaml:"base"`
|
CfgBase `yaml:"base"`
|
||||||
DBInfo DBInfo `yaml:"db_info"`
|
DBInfo DBInfo `yaml:"db_info"`
|
||||||
OutDir string `yaml:"out_dir"`
|
OutDir string `yaml:"out_dir"`
|
||||||
URLTag string `yaml:"url_tag"` // url tag
|
URLTag string `yaml:"url_tag"` // url tag
|
||||||
Language string `yaml:"language"` // language
|
Language string `yaml:"language"` // language
|
||||||
DbTag string `yaml:"db_tag"` // 数据库标签(gormt,db)
|
DbTag string `yaml:"db_tag"` // 数据库标签(gormt,db)
|
||||||
Simple bool `yaml:"simple"`
|
Simple bool `yaml:"simple"`
|
||||||
IsWEBTag bool `yaml:"is_web_tag"`
|
IsWEBTag bool `yaml:"is_web_tag"`
|
||||||
IsWebTagPkHidden bool `yaml:"is_web_tag_pk_hidden"` // web标记是否隐藏主键
|
IsWebTagPkHidden bool `yaml:"is_web_tag_pk_hidden"` // web标记是否隐藏主键
|
||||||
IsForeignKey bool `yaml:"is_foreign_key"`
|
IsForeignKey bool `yaml:"is_foreign_key"`
|
||||||
IsOutSQL bool `yaml:"is_out_sql"`
|
IsOutSQL bool `yaml:"is_out_sql"`
|
||||||
IsOutFunc bool `yaml:"is_out_func"`
|
IsOutFunc bool `yaml:"is_out_func"`
|
||||||
IsGUI bool `yaml:"is_gui"` //
|
IsGUI bool `yaml:"is_gui"` //
|
||||||
IsTableName bool `yaml:"is_table_name"`
|
IsTableName bool `yaml:"is_table_name"`
|
||||||
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
|
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
|
||||||
TablePrefix string `yaml:"table_prefix"` // 表前缀
|
TablePrefix string `yaml:"table_prefix"` // 表前缀
|
||||||
SelfTypeDef map[string]string `yaml:"self_type_define"`
|
SelfTypeDef map[string]string `yaml:"self_type_define"`
|
||||||
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,14 +39,16 @@ var _map = Config{
|
|||||||
Simple: false,
|
Simple: false,
|
||||||
IsWEBTag: false,
|
IsWEBTag: false,
|
||||||
// SingularTable: true,
|
// SingularTable: true,
|
||||||
IsForeignKey: true,
|
IsForeignKey: true,
|
||||||
IsOutSQL: false,
|
IsOutSQL: false,
|
||||||
IsOutFunc: true,
|
IsOutFunc: true,
|
||||||
IsGUI: false,
|
IsGUI: false,
|
||||||
TablePrefix: "",
|
TablePrefix: "",
|
||||||
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
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,15 +27,20 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// struct
|
// struct
|
||||||
var stt GenOutInfo
|
if config.GetIsOutFileByTableName() {
|
||||||
stt.FileCtx = m.generate()
|
outByTable := m.GenerateByTableName()
|
||||||
stt.FileName = info.DbName + ".go"
|
out = append(out, outByTable...)
|
||||||
|
} else {
|
||||||
|
var stt GenOutInfo
|
||||||
|
stt.FileCtx = m.generate()
|
||||||
|
stt.FileName = info.DbName + ".go"
|
||||||
|
|
||||||
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
|
||||||
@@ -76,6 +81,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,22 +123,25 @@ 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))
|
||||||
for _, v1 := range v.Index {
|
// not simple output. 默认不输出gorm标签
|
||||||
switch v1.Key {
|
if !config.GetSimple() {
|
||||||
// case ColumnsKeyDefault:
|
for _, v1 := range v.Index {
|
||||||
case ColumnsKeyPrimary: // primary key.主键
|
switch v1.Key {
|
||||||
tmp.AddTag(_tagGorm, "primaryKey")
|
// case ColumnsKeyDefault:
|
||||||
isPK = true
|
case ColumnsKeyPrimary: // primary key.主键
|
||||||
case ColumnsKeyUnique: // unique key.唯一索引
|
tmp.AddTag(_tagGorm, "primaryKey")
|
||||||
tmp.AddTag(_tagGorm, "unique")
|
isPK = true
|
||||||
case ColumnsKeyIndex: // index key.复合索引
|
case ColumnsKeyUnique: // unique key.唯一索引
|
||||||
if v1.KeyType == "FULLTEXT" {
|
tmp.AddTag(_tagGorm, "unique")
|
||||||
tmp.AddTag(_tagGorm, getUninStr("index", ":", v1.KeyName)+",class:FULLTEXT")
|
case ColumnsKeyIndex: // index key.复合索引
|
||||||
} else {
|
if v1.KeyType == "FULLTEXT" {
|
||||||
tmp.AddTag(_tagGorm, getUninStr("index", ":", v1.KeyName))
|
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 {
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user