Merge pull request #39 from wclssdn/master

sort tables by name
master
xxj 5 years ago committed by GitHub
commit 313e708691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,9 +15,9 @@ import (
"github.com/jinzhu/gorm"
)
var gloabIsRelated bool // 全局预加载
var globalIsRelated bool // 全局预加载
// prepare for outher
// prepare for other
type _BaseMgr struct {
*gorm.DB
ctx *context.Context
@ -67,12 +67,12 @@ func (f optionFunc) apply(o *options) {
// OpenRelated 打开全局预加载
func OpenRelated() {
gloabIsRelated = true
globalIsRelated = true
}
// CloseRelated 关闭全局预加载
func CloseRelated() {
gloabIsRelated = true
globalIsRelated = true
}
`
@ -87,7 +87,7 @@ func {{$obj.StructName}}Mgr(db *gorm.DB) *_{{$obj.StructName}}Mgr {
if db == nil {
panic(fmt.Errorf("{{$obj.StructName}}Mgr need init by db"))
}
return &_{{$obj.StructName}}Mgr{_BaseMgr: &_BaseMgr{DB: db, isRelated: gloabIsRelated}}
return &_{{$obj.StructName}}Mgr{_BaseMgr: &_BaseMgr{DB: db, isRelated: globalIsRelated}}
}
// GetTableName get sql table name.获取数据库名字

@ -6,9 +6,9 @@ import (
"github.com/jinzhu/gorm"
)
var gloabIsRelated bool // 全局预加载
var globalIsRelated bool // 全局预加载
// prepare for outher
// prepare for other
type _BaseMgr struct {
*gorm.DB
ctx *context.Context
@ -57,10 +57,10 @@ func (f optionFunc) apply(o *options) {
// OpenRelated 打开全局预加载
func OpenRelated() {
gloabIsRelated = true
globalIsRelated = true
}
// CloseRelated 关闭全局预加载
func CloseRelated() {
gloabIsRelated = true
globalIsRelated = true
}

@ -15,7 +15,7 @@ func AccountMgr(db *gorm.DB) *_AccountMgr {
if db == nil {
panic(fmt.Errorf("AccountMgr need init by db"))
}
return &_AccountMgr{_BaseMgr: &_BaseMgr{DB: db, isRelated: gloabIsRelated}}
return &_AccountMgr{_BaseMgr: &_BaseMgr{DB: db, isRelated: globalIsRelated}}
}
// GetTableName get sql table name.获取数据库名字

@ -15,7 +15,7 @@ func UserMgr(db *gorm.DB) *_UserMgr {
if db == nil {
panic(fmt.Errorf("UserMgr need init by db"))
}
return &_UserMgr{_BaseMgr: &_BaseMgr{DB: db, isRelated: gloabIsRelated}}
return &_UserMgr{_BaseMgr: &_BaseMgr{DB: db, isRelated: globalIsRelated}}
}
// GetTableName get sql table name.获取数据库名字

@ -16,6 +16,6 @@ func TestTools(t *testing.T) {
// out, _ := json.Marshal(pkg)
// tools.WriteFile("test.txt", []string{string(out)}, true)
list := model.Generate(pkg)
list, _ := model.Generate(pkg)
fmt.Println(list)
}

@ -94,7 +94,7 @@ func getGormModelElement() []EmInfo {
return result
}
func buildFList(list *[]FList, key ColumusKey, keyName, tp, colName string) {
func buildFList(list *[]FList, key ColumnsKey, keyName, tp, colName string) {
for i := 0; i < len(*list); i++ {
if (*list)[i].KeyName == keyName {
(*list)[i].Kem = append((*list)[i].Kem, FEm{
@ -171,14 +171,14 @@ func GenFListIndex(info FList, status int) string {
func widthFunctionName(info FList) string {
switch info.Key {
// case ColumusKeyDefault:
case ColumusKeyPrimary: // primary key.主键
// case ColumnsKeyDefault:
case ColumnsKeyPrimary: // primary key.主键
return "FetchByPrimaryKey"
case ColumusKeyUnique: // unique key.唯一索引
case ColumnsKeyUnique: // unique key.唯一索引
return "FetchByUnique"
case ColumusKeyIndex: // index key.复合索引
case ColumnsKeyIndex: // index key.复合索引
return "FetchBy" + getCamelName(info.KeyName) + "Index"
case ColumusKeyUniqueIndex: // unique index key.唯一复合索引
case ColumnsKeyUniqueIndex: // unique index key.唯一复合索引
return "FetchBy" + getCamelName(info.KeyName) + "UniqueIndex"
}

@ -5,20 +5,20 @@ const (
// _tagJSON = "json"
)
// ColumusKey Columus type elem. 类型枚举
type ColumusKey int
// ColumnsKey Columns type elem. 类型枚举
type ColumnsKey int
const (
// ColumusKeyDefault default
ColumusKeyDefault = iota
// ColumusKeyPrimary primary key.主键
ColumusKeyPrimary
// ColumusKeyUnique unique key.唯一索引
ColumusKeyUnique
// ColumusKeyIndex index key.复合索引
ColumusKeyIndex
// ColumusKeyUniqueIndex unique index key.唯一复合索引
ColumusKeyUniqueIndex
// ColumnsKeyDefault default
ColumnsKeyDefault = iota
// ColumnsKeyPrimary primary key.主键
ColumnsKeyPrimary
// ColumnsKeyUnique unique key.唯一索引
ColumnsKeyUnique
// ColumnsKeyIndex index key.复合索引
ColumnsKeyIndex
// ColumnsKeyUniqueIndex unique index key.唯一复合索引
ColumnsKeyUniqueIndex
)
// DBInfo database default info
@ -32,11 +32,11 @@ type DBInfo struct {
type TabInfo struct {
BaseInfo
SQLBuildStr string // Create SQL statements.创建sql语句
Em []ColumusInfo // Columus list .表列表组合
Em []ColumnsInfo // Columns list .表列表组合
}
// ColumusInfo Columus list .表列信息
type ColumusInfo struct {
// ColumnsInfo Columns list .表列信息
type ColumnsInfo struct {
BaseInfo
Type string // Type.类型标记
Index []KList // index list.index列表
@ -52,7 +52,7 @@ type ForeignKey struct {
// KList database index /unique_index list.数据库index /unique_index 列表
type KList struct {
Key ColumusKey // non_unique of (show keys from [table])
Key ColumnsKey // non_unique of (show keys from [table])
KeyName string // key_name of (show keys from [table])
}
@ -92,7 +92,7 @@ type FEm struct {
// FList index of list
type FList struct {
Key ColumusKey // non_unique of (show keys from [table])
Key ColumnsKey // non_unique of (show keys from [table])
KeyName string // key_name of (show keys from [table])
Kem []FEm
}

@ -2,6 +2,7 @@ package genmysql
import (
"fmt"
"sort"
"strings"
"github.com/xxjwxc/gormt/data/config"
@ -98,10 +99,14 @@ func getPackageInfo(orm *mysqldb.MySqlDB, info *model.DBInfo) {
info.TabList = append(info.TabList, tab)
}
// sort tables
sort.Slice(info.TabList, func(i, j int) bool {
return info.TabList[i].Name < info.TabList[j].Name
})
}
// getTableElement Get table columns and comments.获取表列及注释
func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo) {
func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumnsInfo) {
keyNums := make(map[string]int)
// get keys
var Keys []keys
@ -116,7 +121,7 @@ func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo)
orm.Raw("show FULL COLUMNS from " + tab).Scan(&list)
// filter gorm.Model.过滤 gorm.Model
if filterModel(&list) {
el = append(el, model.ColumusInfo{
el = append(el, model.ColumnsInfo{
Type: "gorm.Model",
})
}
@ -131,7 +136,7 @@ func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo)
// ------------------end
for _, v := range list {
var tmp model.ColumusInfo
var tmp model.ColumnsInfo
tmp.Name = v.Field
tmp.Notes = v.Desc
tmp.Type = v.Type
@ -139,20 +144,20 @@ func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo)
// keys
if strings.EqualFold(v.Key, "PRI") { // Set primary key.设置主键
tmp.Index = append(tmp.Index, model.KList{
Key: model.ColumusKeyPrimary,
Key: model.ColumnsKeyPrimary,
})
} else if strings.EqualFold(v.Key, "UNI") { // unique
tmp.Index = append(tmp.Index, model.KList{
Key: model.ColumusKeyUnique,
Key: model.ColumnsKeyUnique,
})
} else {
for _, v1 := range Keys {
if strings.EqualFold(v1.ColumnName, v.Field) {
var k model.KList
if v1.NonUnique == 1 { // index
k.Key = model.ColumusKeyIndex
k.Key = model.ColumnsKeyIndex
} else {
k.Key = model.ColumusKeyUniqueIndex
k.Key = model.ColumnsKeyUniqueIndex
}
if keyNums[v1.KeyName] > 1 { // Composite index.复合索引
k.KeyName = v1.KeyName

@ -42,7 +42,7 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
return
}
// GetPackage gen sturct on table
// GetPackage gen struct on table
func (m *_Model) GetPackage() genstruct.GenPackage {
if m.pkg == nil {
var pkg genstruct.GenPackage
@ -69,7 +69,7 @@ func (m *_Model) generate() string {
}
// genTableElement Get table columns and comments.获取表列及注释
func (m *_Model) genTableElement(cols []ColumusInfo) (el []genstruct.GenElement) {
func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement) {
_tagGorm := config.GetDBTag()
_tagJSON := config.GetURLTag()
@ -83,14 +83,14 @@ func (m *_Model) genTableElement(cols []ColumusInfo) (el []genstruct.GenElement)
tmp.SetType(getTypeName(v.Type))
for _, v1 := range v.Index {
switch v1.Key {
// case ColumusKeyDefault:
case ColumusKeyPrimary: // primary key.主键
// case ColumnsKeyDefault:
case ColumnsKeyPrimary: // primary key.主键
tmp.AddTag(_tagGorm, "primary_key")
case ColumusKeyUnique: // unique key.唯一索引
case ColumnsKeyUnique: // unique key.唯一索引
tmp.AddTag(_tagGorm, "unique")
case ColumusKeyIndex: // index key.复合索引
case ColumnsKeyIndex: // index key.复合索引
tmp.AddTag(_tagGorm, getUninStr("index", ":", v1.KeyName))
case ColumusKeyUniqueIndex: // unique index key.唯一复合索引
case ColumnsKeyUniqueIndex: // unique index key.唯一复合索引
tmp.AddTag(_tagGorm, getUninStr("unique_index", ":", v1.KeyName))
}
}
@ -130,12 +130,12 @@ func (m *_Model) genTableElement(cols []ColumusInfo) (el []genstruct.GenElement)
}
// genForeignKey Get information about foreign key of table column.获取表列外键相关信息
func (m *_Model) genForeignKey(col ColumusInfo) (fklist []genstruct.GenElement) {
func (m *_Model) genForeignKey(col ColumnsInfo) (fklist []genstruct.GenElement) {
_tagGorm := config.GetDBTag()
_tagJSON := config.GetURLTag()
for _, v := range col.ForeignKeyList {
isMulti, isFind, notes := m.getColumusKeyMulti(v.TableName, v.ColumnName)
isMulti, isFind, notes := m.getColumnsKeyMulti(v.TableName, v.ColumnName)
if isFind {
var tmp genstruct.GenElement
tmp.SetNotes(notes)
@ -162,7 +162,7 @@ func (m *_Model) genForeignKey(col ColumusInfo) (fklist []genstruct.GenElement)
return
}
func (m *_Model) getColumusKeyMulti(tableName, col string) (isMulti bool, isFind bool, notes string) {
func (m *_Model) getColumnsKeyMulti(tableName, col string) (isMulti bool, isFind bool, notes string) {
var haveGomod bool
for _, v := range m.info.TabList {
if strings.EqualFold(v.Name, tableName) {
@ -170,11 +170,11 @@ func (m *_Model) getColumusKeyMulti(tableName, col string) (isMulti bool, isFind
if strings.EqualFold(v1.Name, col) {
for _, v2 := range v1.Index {
switch v2.Key {
case ColumusKeyPrimary, ColumusKeyUnique, ColumusKeyUniqueIndex: // primary key unique key . 主键,唯一索引
case ColumnsKeyPrimary, ColumnsKeyUnique, ColumnsKeyUniqueIndex: // primary key unique key . 主键,唯一索引
{
return false, true, v.Notes
}
// case ColumusKeyIndex: // index key. 复合索引
// case ColumnsKeyIndex: // index key. 复合索引
// {
// isMulti = true
// }
@ -239,24 +239,24 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
if strings.EqualFold(el.Type, "gorm.Model") {
data.Em = append(data.Em, getGormModelElement()...)
pkg.AddImport(`"time"`)
buildFList(&primary, ColumusKeyPrimary, "", "int64", "id")
buildFList(&primary, ColumnsKeyPrimary, "", "int64", "id")
} else {
typeName := getTypeName(el.Type)
isMulti := true
for _, v1 := range el.Index {
switch v1.Key {
// case ColumusKeyDefault:
case ColumusKeyPrimary: // primary key.主键
// case ColumnsKeyDefault:
case ColumnsKeyPrimary: // primary key.主键
isMulti = false
buildFList(&primary, ColumusKeyPrimary, "", typeName, el.Name)
case ColumusKeyUnique: // unique key.唯一索引
buildFList(&primary, ColumnsKeyPrimary, "", typeName, el.Name)
case ColumnsKeyUnique: // unique key.唯一索引
isMulti = false
buildFList(&unique, ColumusKeyUnique, "", typeName, el.Name)
case ColumusKeyIndex: // index key.复合索引
buildFList(&index, ColumusKeyIndex, v1.KeyName, typeName, el.Name)
case ColumusKeyUniqueIndex: // unique index key.唯一复合索引
buildFList(&unique, ColumnsKeyUnique, "", typeName, el.Name)
case ColumnsKeyIndex: // index key.复合索引
buildFList(&index, ColumnsKeyIndex, v1.KeyName, typeName, el.Name)
case ColumnsKeyUniqueIndex: // unique index key.唯一复合索引
isMulti = false
buildFList(&uniqueIndex, ColumusKeyUniqueIndex, v1.KeyName, typeName, el.Name)
buildFList(&uniqueIndex, ColumnsKeyUniqueIndex, v1.KeyName, typeName, el.Name)
}
}
@ -276,7 +276,7 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
// 外键列表
for _, v := range el.ForeignKeyList {
isMulti, isFind, notes := m.getColumusKeyMulti(v.TableName, v.ColumnName)
isMulti, isFind, notes := m.getColumnsKeyMulti(v.TableName, v.ColumnName)
if isFind {
var info PreloadInfo
info.IsMulti = isMulti

Loading…
Cancel
Save