@ -27,21 +27,30 @@ 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 {
if name := config . GetOutFileName ( ) ; len ( name ) > 0 {
var stt GenOutInfo
stt . FileName = name + ".go"
stt . FileCtx = m . generate ( )
stt . FileName = info . DbName + ".go"
if name := config . GetOutFileName ( ) ; len ( name ) > 0 {
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,22 +127,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 +158,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