Skip to content

Commit

Permalink
Merge pull request #39 from wclssdn/master
Browse files Browse the repository at this point in the history
sort tables by name
  • Loading branch information
xxjwxc authored Apr 6, 2020
2 parents 6271a32 + 5983be1 commit 313e708
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 65 deletions.
10 changes: 5 additions & 5 deletions data/view/genfunc/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,12 +67,12 @@ func (f optionFunc) apply(o *options) {
// OpenRelated 打开全局预加载
func OpenRelated() {
gloabIsRelated = true
globalIsRelated = true
}
// CloseRelated 关闭全局预加载
func CloseRelated() {
gloabIsRelated = true
globalIsRelated = true
}
`
Expand All @@ -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.获取数据库名字
Expand Down
8 changes: 4 additions & 4 deletions data/view/genfunc/model/gen.base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,10 +57,10 @@ func (f optionFunc) apply(o *options) {

// OpenRelated 打开全局预加载
func OpenRelated() {
gloabIsRelated = true
globalIsRelated = true
}

// CloseRelated 关闭全局预加载
func CloseRelated() {
gloabIsRelated = true
globalIsRelated = true
}
2 changes: 1 addition & 1 deletion data/view/genfunc/model/matrix.gen.account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.获取数据库名字
Expand Down
2 changes: 1 addition & 1 deletion data/view/genfunc/model/matrix.gen.user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.获取数据库名字
Expand Down
2 changes: 1 addition & 1 deletion data/view/gtools/gtools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions data/view/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"
}

Expand Down
34 changes: 17 additions & 17 deletions data/view/model/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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列表
Expand All @@ -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])
}

Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 12 additions & 7 deletions data/view/model/genmysql/genmysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package genmysql

import (
"fmt"
"sort"
"strings"

"github.com/xxjwxc/gormt/data/config"
Expand Down Expand Up @@ -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
Expand All @@ -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",
})
}
Expand All @@ -131,28 +136,28 @@ 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

// 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
Expand Down
46 changes: 23 additions & 23 deletions data/view/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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))
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -162,19 +162,19 @@ 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) {
for _, v1 := range v.Em {
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
// }
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
Expand Down

0 comments on commit 313e708

Please sign in to comment.