From e3e175313ea4c2f98705e86f5ebc39cd1f94434f Mon Sep 17 00:00:00 2001 From: xxj <346944475@qq.com> Date: Sun, 13 Jun 2021 21:55:36 +0800 Subject: [PATCH] nwe --- data/view/genfunc/def.go | 60 +++++++++++++++++++ data/view/genfunc/genfunc_test.go | 21 +++++++ data/view/genfunc/model/gen.base.go | 60 +++++++++++++++++++ data/view/genfunc/model/matrix.gen.account.go | 47 +++++++-------- data/view/genfunc/model/matrix.gen.user.go | 37 ++++++------ data/view/genfunc/model/matrix.go | 54 ++++++++++++++--- data/view/model/model.go | 2 +- go.mod | 2 +- go.sum | 8 +-- 9 files changed, 233 insertions(+), 58 deletions(-) diff --git a/data/view/genfunc/def.go b/data/view/genfunc/def.go index 81f20a8..9eef17b 100755 --- a/data/view/genfunc/def.go +++ b/data/view/genfunc/def.go @@ -19,6 +19,7 @@ var {{.StructName}}Columns = struct { {{range $em := .Em}} package {{.PackageName}} import ( "context" + "fmt" "time" "gorm.io/gorm" @@ -109,6 +110,65 @@ func CloseRelated() { globalIsRelated = true } + +// 自定义sql查询 +type Condetion struct { + list []*condetionInfo +} + +// And a condition by and .and 一个条件 +func (c *Condetion) And(column string, cases string, value ...interface{}) { + c.list = append(c.list, &condetionInfo{ + andor: "and", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) +} + +// Or a condition by or .or 一个条件 +func (c *Condetion) Or(column string, cases string, value ...interface{}) { + c.list = append(c.list, &condetionInfo{ + andor: "or", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) +} + +func (c *Condetion) Get() (where string, out []interface{}) { + firstAnd := -1 + for i := 0; i < len(c.list); i++ { // 查找第一个and + if c.list[i].andor == "and" { + where = fmt.Sprintf("{{GetVV }} %v ?", c.list[i].column, c.list[i].case_) + out = append(out, c.list[i].value) + firstAnd = i + break + } + } + + if firstAnd < 0 && len(c.list) > 0 { // 补刀 + where = fmt.Sprintf("{{GetVV }} %v ?", c.list[0].column, c.list[0].case_) + out = append(out, c.list[0].value) + firstAnd = 0 + } + + for i := 0; i < len(c.list); i++ { // 添加剩余的 + if firstAnd != i { + where += fmt.Sprintf(" %v {{GetVV }} %v ?", c.list[i].andor, c.list[i].column, c.list[i].case_) + out = append(out, c.list[i].value) + } + } + + return +} + +type condetionInfo struct { + andor string + column string // 列名 + case_ string // 条件(in,>=,<=) + value interface{} +} ` genlogic = `{{$obj := .}}{{$list := $obj.Em}} diff --git a/data/view/genfunc/genfunc_test.go b/data/view/genfunc/genfunc_test.go index 93c9ddb..b74cb1a 100644 --- a/data/view/genfunc/genfunc_test.go +++ b/data/view/genfunc/genfunc_test.go @@ -137,3 +137,24 @@ func TestFuncFetchBy(t *testing.T) { fmt.Println(err) fmt.Println(accounts) } + +// TestCondetion 测试sql构建 +func TestCondetion(t *testing.T) { + condetion := model.Condetion{} + condetion.And(model.AccountColumns.AccountID, ">=", "1") + condetion.And(model.AccountColumns.UserID, "in", "1", "2", "3") + condetion.Or(model.AccountColumns.Type, "in", "1", "2", "3") + + where, obj := condetion.Get() + fmt.Println(where) + fmt.Println(obj...) + + db := GetGorm("root:qwer@tcp(127.0.0.1:3306)/matrix?charset=utf8&parseTime=True&loc=Local&interpolateParams=True") + defer func() { + sqldb, _ := db.DB() + sqldb.Close() + }() + + accountMgr := model.AccountMgr(db.Where(condetion.Get())) + accountMgr.Gets() +} diff --git a/data/view/genfunc/model/gen.base.go b/data/view/genfunc/model/gen.base.go index e81a08b..33ece45 100644 --- a/data/view/genfunc/model/gen.base.go +++ b/data/view/genfunc/model/gen.base.go @@ -2,6 +2,7 @@ package model import ( "context" + "fmt" "time" "gorm.io/gorm" @@ -90,3 +91,62 @@ func OpenRelated() { func CloseRelated() { globalIsRelated = true } + +// 自定义sql查询 +type Condetion struct { + list []*condetionInfo +} + +// And a condition by and .and 一个条件 +func (c *Condetion) And(column string, cases string, value ...interface{}) { + c.list = append(c.list, &condetionInfo{ + andor: "and", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) +} + +// Or a condition by or .or 一个条件 +func (c *Condetion) Or(column string, cases string, value ...interface{}) { + c.list = append(c.list, &condetionInfo{ + andor: "or", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) +} + +func (c *Condetion) Get() (where string, out []interface{}) { + firstAnd := -1 + for i := 0; i < len(c.list); i++ { // 查找第一个and + if c.list[i].andor == "and" { + where = fmt.Sprintf("`%v` %v ?", c.list[i].column, c.list[i].case_) + out = append(out, c.list[i].value) + firstAnd = i + break + } + } + + if firstAnd < 0 && len(c.list) > 0 { // 补刀 + where = fmt.Sprintf("`%v` %v ?", c.list[0].column, c.list[0].case_) + out = append(out, c.list[0].value) + firstAnd = 0 + } + + for i := 0; i < len(c.list); i++ { // 添加剩余的 + if firstAnd != i { + where += fmt.Sprintf(" %v `%v` %v ?", c.list[i].andor, c.list[i].column, c.list[i].case_) + out = append(out, c.list[i].value) + } + } + + return +} + +type condetionInfo struct { + andor string + column string // 列名 + case_ string // 条件(in,>=,<=) + value interface{} +} diff --git a/data/view/genfunc/model/matrix.gen.account.go b/data/view/genfunc/model/matrix.gen.account.go index 00a02c2..9d04d89 100644 --- a/data/view/genfunc/model/matrix.gen.account.go +++ b/data/view/genfunc/model/matrix.gen.account.go @@ -3,7 +3,6 @@ package model import ( "context" "fmt" - "gorm.io/gorm" ) @@ -17,7 +16,7 @@ func AccountMgr(db *gorm.DB) *_AccountMgr { panic(fmt.Errorf("AccountMgr need init by db")) } ctx, cancel := context.WithCancel(context.Background()) - return &_AccountMgr{_BaseMgr: &_BaseMgr{DB: db.Table("account"), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}} + return &_AccountMgr{_BaseMgr: &_BaseMgr{DB: db.Model(Account{}), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}} } // GetTableName get sql table name.获取数据库名字 @@ -27,7 +26,7 @@ func (obj *_AccountMgr) GetTableName() string { // Get 获取 func (obj *_AccountMgr) Get() (result Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error if err == nil && obj.isRelated { if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { // if err != gorm.ErrRecordNotFound { // 非 没找到 @@ -41,7 +40,7 @@ func (obj *_AccountMgr) Get() (result Account, err error) { // Gets 获取批量结果 func (obj *_AccountMgr) Gets() (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -90,7 +89,7 @@ func (obj *_AccountMgr) GetByOption(opts ...Option) (result Account, err error) o.apply(&options) } - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error if err == nil && obj.isRelated { if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { // if err != gorm.ErrRecordNotFound { // 非 没找到 @@ -111,7 +110,7 @@ func (obj *_AccountMgr) GetByOptions(opts ...Option) (results []*Account, err er o.apply(&options) } - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -128,7 +127,7 @@ func (obj *_AccountMgr) GetByOptions(opts ...Option) (results []*Account, err er // GetFromID 通过id获取内容 func (obj *_AccountMgr) GetFromID(id int) (result Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error if err == nil && obj.isRelated { if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { // if err != gorm.ErrRecordNotFound { // 非 没找到 @@ -140,9 +139,9 @@ func (obj *_AccountMgr) GetFromID(id int) (result Account, err error) { return } -// GetBatchFromID 批量唯一主键查找 +// GetBatchFromID 批量查找 func (obj *_AccountMgr) GetBatchFromID(ids []int) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id IN (?)", ids).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` IN (?)", ids).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -157,7 +156,7 @@ func (obj *_AccountMgr) GetBatchFromID(ids []int) (results []*Account, err error // GetFromAccountID 通过account_id获取内容 func (obj *_AccountMgr) GetFromAccountID(accountID int) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id = ?", accountID).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`account_id` = ?", accountID).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -170,9 +169,9 @@ func (obj *_AccountMgr) GetFromAccountID(accountID int) (results []*Account, err return } -// GetBatchFromAccountID 批量唯一主键查找 +// GetBatchFromAccountID 批量查找 func (obj *_AccountMgr) GetBatchFromAccountID(accountIDs []int) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id IN (?)", accountIDs).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`account_id` IN (?)", accountIDs).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -187,7 +186,7 @@ func (obj *_AccountMgr) GetBatchFromAccountID(accountIDs []int) (results []*Acco // GetFromUserID 通过user_id获取内容 func (obj *_AccountMgr) GetFromUserID(userID int) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ?", userID).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -200,9 +199,9 @@ func (obj *_AccountMgr) GetFromUserID(userID int) (results []*Account, err error return } -// GetBatchFromUserID 批量唯一主键查找 +// GetBatchFromUserID 批量查找 func (obj *_AccountMgr) GetBatchFromUserID(userIDs []int) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id IN (?)", userIDs).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` IN (?)", userIDs).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -217,7 +216,7 @@ func (obj *_AccountMgr) GetBatchFromUserID(userIDs []int) (results []*Account, e // GetFromType 通过type获取内容 func (obj *_AccountMgr) GetFromType(_type int) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("type = ?", _type).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`type` = ?", _type).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -230,9 +229,9 @@ func (obj *_AccountMgr) GetFromType(_type int) (results []*Account, err error) { return } -// GetBatchFromType 批量唯一主键查找 +// GetBatchFromType 批量查找 func (obj *_AccountMgr) GetBatchFromType(_types []int) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("type IN (?)", _types).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`type` IN (?)", _types).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -247,7 +246,7 @@ func (obj *_AccountMgr) GetBatchFromType(_types []int) (results []*Account, err // GetFromName 通过name获取内容 func (obj *_AccountMgr) GetFromName(name string) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name = ?", name).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`name` = ?", name).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -260,9 +259,9 @@ func (obj *_AccountMgr) GetFromName(name string) (results []*Account, err error) return } -// GetBatchFromName 批量唯一主键查找 +// GetBatchFromName 批量查找 func (obj *_AccountMgr) GetBatchFromName(names []string) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name IN (?)", names).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`name` IN (?)", names).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // @@ -279,7 +278,7 @@ func (obj *_AccountMgr) GetBatchFromName(names []string) (results []*Account, er // FetchByPrimaryKey primay or index 获取唯一内容 func (obj *_AccountMgr) FetchByPrimaryKey(id int) (result Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error if err == nil && obj.isRelated { if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { // if err != gorm.ErrRecordNotFound { // 非 没找到 @@ -293,7 +292,7 @@ func (obj *_AccountMgr) FetchByPrimaryKey(id int) (result Account, err error) { // FetchUniqueIndexByAccount primay or index 获取唯一内容 func (obj *_AccountMgr) FetchUniqueIndexByAccount(accountID int, userID int) (result Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id = ? AND user_id = ?", accountID, userID).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`account_id` = ? AND `user_id` = ?", accountID, userID).Find(&result).Error if err == nil && obj.isRelated { if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { // if err != gorm.ErrRecordNotFound { // 非 没找到 @@ -307,7 +306,7 @@ func (obj *_AccountMgr) FetchUniqueIndexByAccount(accountID int, userID int) (re // FetchIndexByTp 获取多个内容 func (obj *_AccountMgr) FetchIndexByTp(userID int, _type int) (results []*Account, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ? AND type = ?", userID, _type).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ? AND `type` = ?", userID, _type).Find(&results).Error if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { // diff --git a/data/view/genfunc/model/matrix.gen.user.go b/data/view/genfunc/model/matrix.gen.user.go index 9b04150..956a9d5 100644 --- a/data/view/genfunc/model/matrix.gen.user.go +++ b/data/view/genfunc/model/matrix.gen.user.go @@ -3,7 +3,6 @@ package model import ( "context" "fmt" - "gorm.io/gorm" ) @@ -17,7 +16,7 @@ func UserMgr(db *gorm.DB) *_UserMgr { panic(fmt.Errorf("UserMgr need init by db")) } ctx, cancel := context.WithCancel(context.Background()) - return &_UserMgr{_BaseMgr: &_BaseMgr{DB: db.Table("user"), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}} + return &_UserMgr{_BaseMgr: &_BaseMgr{DB: db.Model(User{}), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}} } // GetTableName get sql table name.获取数据库名字 @@ -27,14 +26,14 @@ func (obj *_UserMgr) GetTableName() string { // Get 获取 func (obj *_UserMgr) Get() (result User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error return } // Gets 获取批量结果 func (obj *_UserMgr) Gets() (results []*User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error return } @@ -70,7 +69,7 @@ func (obj *_UserMgr) GetByOption(opts ...Option) (result User, err error) { o.apply(&options) } - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error return } @@ -84,7 +83,7 @@ func (obj *_UserMgr) GetByOptions(opts ...Option) (results []*User, err error) { o.apply(&options) } - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error return } @@ -93,56 +92,56 @@ func (obj *_UserMgr) GetByOptions(opts ...Option) (results []*User, err error) { // GetFromUserID 通过user_id获取内容 func (obj *_UserMgr) GetFromUserID(userID int) (result User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ?", userID).Find(&result).Error return } -// GetBatchFromUserID 批量唯一主键查找 +// GetBatchFromUserID 批量查找 func (obj *_UserMgr) GetBatchFromUserID(userIDs []int) (results []*User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id IN (?)", userIDs).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` IN (?)", userIDs).Find(&results).Error return } // GetFromName 通过name获取内容 func (obj *_UserMgr) GetFromName(name string) (results []*User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name = ?", name).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`name` = ?", name).Find(&results).Error return } -// GetBatchFromName 批量唯一主键查找 +// GetBatchFromName 批量查找 func (obj *_UserMgr) GetBatchFromName(names []string) (results []*User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name IN (?)", names).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`name` IN (?)", names).Find(&results).Error return } // GetFromSex 通过sex获取内容 func (obj *_UserMgr) GetFromSex(sex int) (results []*User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("sex = ?", sex).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`sex` = ?", sex).Find(&results).Error return } -// GetBatchFromSex 批量唯一主键查找 +// GetBatchFromSex 批量查找 func (obj *_UserMgr) GetBatchFromSex(sexs []int) (results []*User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("sex IN (?)", sexs).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`sex` IN (?)", sexs).Find(&results).Error return } // GetFromJob 通过job获取内容 func (obj *_UserMgr) GetFromJob(job int) (results []*User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("job = ?", job).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`job` = ?", job).Find(&results).Error return } -// GetBatchFromJob 批量唯一主键查找 +// GetBatchFromJob 批量查找 func (obj *_UserMgr) GetBatchFromJob(jobs []int) (results []*User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("job IN (?)", jobs).Find(&results).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`job` IN (?)", jobs).Find(&results).Error return } @@ -151,7 +150,7 @@ func (obj *_UserMgr) GetBatchFromJob(jobs []int) (results []*User, err error) { // FetchByPrimaryKey primay or index 获取唯一内容 func (obj *_UserMgr) FetchByPrimaryKey(userID int) (result User, err error) { - err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&result).Error + err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`user_id` = ?", userID).Find(&result).Error return } diff --git a/data/view/genfunc/model/matrix.go b/data/view/genfunc/model/matrix.go index e438f58..f2883ed 100644 --- a/data/view/genfunc/model/matrix.go +++ b/data/view/genfunc/model/matrix.go @@ -2,18 +2,56 @@ package model // Account [...] type Account struct { - ID int `gorm:"primary_key;column:id;type:int;not null" json:"-"` - AccountID int `gorm:"unique_index:account;column:account_id;type:int" json:"account_id"` - UserID int `gorm:"unique_index:account;index:tp;column:user_id;type:int" json:"user_id"` - User User `gorm:"association_foreignkey:user_id;foreignkey:user_id" json:"user_list"` - Type int `gorm:"index:tp;column:type;type:int" json:"type"` + ID int `gorm:"primaryKey;column:id;type:int(11);not null" json:"-"` + AccountID int `gorm:"uniqueIndex:account;column:account_id;type:int(11)" json:"accountId"` + UserID int `gorm:"uniqueIndex:account;index:tp;column:user_id;type:int(11)" json:"userId"` + User User `gorm:"joinForeignKey:user_id;foreignKey:user_id" json:"userList"` + Type int `gorm:"index:tp;column:type;type:int(11)" json:"type"` Name string `gorm:"column:name;type:varchar(255)" json:"name"` } +// TableName get sql table name.获取数据库表名 +func (m *Account) TableName() string { + return "account" +} + +// AccountColumns get sql column name.获取数据库列名 +var AccountColumns = struct { + ID string + AccountID string + UserID string + Type string + Name string +}{ + ID: "id", + AccountID: "account_id", + UserID: "user_id", + Type: "type", + Name: "name", +} + // User [...] type User struct { - UserID int `gorm:"primary_key;column:user_id;type:int;not null" json:"-"` + UserID int `gorm:"primaryKey;column:user_id;type:int(11);not null" json:"-"` Name string `gorm:"column:name;type:varchar(30);not null" json:"name"` - Sex int `gorm:"column:sex;type:int;not null" json:"sex"` - Job int `gorm:"column:job;type:int;not null" json:"job"` + Sex int `gorm:"column:sex;type:int(11);not null" json:"sex"` + Job int `gorm:"column:job;type:int(11);not null" json:"job"` +} + +// TableName get sql table name.获取数据库表名 +func (m *User) TableName() string { + return "user" +} + +// UserColumns get sql column name.获取数据库列名 +var UserColumns = struct { + UserID string + Name string + Sex string + Job string +}{ + UserID: "user_id", + Name: "name", + Sex: "sex", + Job: "job", } diff --git a/data/view/model/model.go b/data/view/model/model.go index c6a65e7..1e28894 100755 --- a/data/view/model/model.go +++ b/data/view/model/model.go @@ -280,7 +280,7 @@ func (m *_Model) getColumnsKeyMulti(tableName, col string) (isMulti bool, isFind // ///////////////////////// func func (m *_Model) generateFunc() (genOut []GenOutInfo) { // getn base - tmpl, err := template.New("gen_base").Parse(genfunc.GetGenBaseTemp()) + tmpl, err := template.New("gen_base").Funcs(template.FuncMap{"GetVV": func() string { return "`%v`" }}).Parse(genfunc.GetGenBaseTemp()) if err != nil { panic(err) } diff --git a/go.mod b/go.mod index 1fa4ea6..8d36e7a 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/leodido/go-urn v1.2.0 // indirect github.com/nicksnyder/go-i18n/v2 v2.0.3 github.com/spf13/cobra v1.0.0 - github.com/xxjwxc/public v0.0.0-20210528122519-9c0267a26e38 + github.com/xxjwxc/public v0.0.0-20210611023445-d5fd9e893ee4 golang.org/x/text v0.3.3 gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/go-playground/validator.v9 v9.30.2 diff --git a/go.sum b/go.sum index 828e8b3..5b1eb55 100644 --- a/go.sum +++ b/go.sum @@ -192,10 +192,8 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/xxjwxc/gowp v0.0.0-20200603130651-4d7368b0e285/go.mod h1:yJ/fY5BorWARfDDsxBU/MyQTHc5MVyNcqBQQYD6MN0k= github.com/xxjwxc/public v0.0.0-20200603115833-341beff27850/go.mod h1:fp3M+FEQrCgWD1fZ/PLwZkCTglf086OEhC9LcydAUnc= -github.com/xxjwxc/public v0.0.0-20210303034518-3d2086731fb9 h1:GAF/PlgWqldmUNmkvAPE21FqkjfPxrlQv8A5Aivo0Q4= -github.com/xxjwxc/public v0.0.0-20210303034518-3d2086731fb9/go.mod h1:eEooPAer8T/WuVbu+gP4Xl2YjFb6v56NpCOb4IJibvc= -github.com/xxjwxc/public v0.0.0-20210528122519-9c0267a26e38 h1:3aLc/rYIu1bFHnsnnXKanqQaG9Z5qfwQslCOx7DtGBU= -github.com/xxjwxc/public v0.0.0-20210528122519-9c0267a26e38/go.mod h1:kNBfZ2iA/e+e3yoD5Zm81BgAHNP5htgZf5XsWiXpCTU= +github.com/xxjwxc/public v0.0.0-20210611023445-d5fd9e893ee4 h1:G35nxudFL8q7thdd83VYs/xum7RGvDkPnA7DVdLx9Mg= +github.com/xxjwxc/public v0.0.0-20210611023445-d5fd9e893ee4/go.mod h1:kNBfZ2iA/e+e3yoD5Zm81BgAHNP5htgZf5XsWiXpCTU= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.opentelemetry.io/otel v0.16.0/go.mod h1:e4GKElweB8W2gWUqbghw0B8t5MCTccc9212eNHnOHwA= go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU= @@ -283,8 +281,8 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/eapache/queue.v1 v1.1.0/go.mod h1:wNtmx1/O7kZSR9zNT1TTOJ7GLpm3Vn7srzlfylFbQwU= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=