func base template update
This commit is contained in:
@@ -9,7 +9,8 @@ import (
|
|||||||
// prepare for outher
|
// prepare for outher
|
||||||
type _BaseMgr struct {
|
type _BaseMgr struct {
|
||||||
*gorm.DB
|
*gorm.DB
|
||||||
ctx *context.Context
|
ctx *context.Context
|
||||||
|
isRelated bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCtx set context
|
// SetCtx set context
|
||||||
@@ -22,6 +23,11 @@ func (obj *_BaseMgr) GetDB() *gorm.DB {
|
|||||||
return obj.DB
|
return obj.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsRelated Query foreign key Association.是否查询外键关联(gorm.Related)
|
||||||
|
func (obj *_BaseMgr) IsRelated(b bool) {
|
||||||
|
obj.isRelated = b
|
||||||
|
}
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
query map[string]interface{}
|
query map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,30 +36,53 @@ func ExampleMgr(db *gorm.DB) *_ExampleMgr {
|
|||||||
// GetFromID 通过id获取内容
|
// GetFromID 通过id获取内容
|
||||||
func (obj *_ExampleMgr) GetFromID(id int) (results []*Example, err error) {
|
func (obj *_ExampleMgr) GetFromID(id int) (results []*Example, err error) {
|
||||||
err = obj.DB.Table("example").Where("id = ?", id).Find(&results).Error
|
err = obj.DB.Table("example").Where("id = ?", id).Find(&results).Error
|
||||||
if err != nil {
|
if err == nil {
|
||||||
|
for i := 0; i < len(results); i++ {
|
||||||
|
var userList []User
|
||||||
|
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
results[i].UserList = userList
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByPrimaryKey 唯一主键查找
|
// GetByPrimaryKey 唯一主键查找
|
||||||
func (obj *_ExampleMgr) GetByPrimaryKey(id int64) (Example, error) {
|
func (obj *_ExampleMgr) GetByPrimaryKey(id int64) (result Example, err error) {
|
||||||
var tmp Example
|
err = obj.DB.Table("example").Where("id = ?", id).Find(&result).Error
|
||||||
err := obj.DB.Table("example").Where("id = ?", id).Find(&tmp).Error
|
if err == nil {
|
||||||
return tmp, err
|
var info []User
|
||||||
|
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result.UserList = info
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByPrimaryKey 批量唯一主键查找
|
// GetByPrimaryKey 批量唯一主键查找
|
||||||
func (obj *_ExampleMgr) GetByPrimaryKeys(ids []int64) ([]*Example, error) {
|
func (obj *_ExampleMgr) GetByPrimaryKeys(ids []int64) (results []*Example, err error) {
|
||||||
var tmp []*Example
|
err = obj.DB.Table("example").Where("id IN (?)", ids).Find(&results).Error
|
||||||
err := obj.DB.Table("example").Where("id", ids).Find(&tmp).Error
|
if err == nil {
|
||||||
return tmp, err
|
for i := 0; i < len(results); i++ {
|
||||||
|
var userList []User
|
||||||
|
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
results[i].UserList = userList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////option case ////////////////////////////////////////////
|
//////////////////////////option case ////////////////////////////////////////////
|
||||||
|
|
||||||
// GetByPrimaryKey 功能选项模式获取
|
// GetByPrimaryKey 功能选项模式获取
|
||||||
func (obj *_ExampleMgr) GetByOption(opts ...Option) (Example, error) {
|
func (obj *_ExampleMgr) GetByOption(opts ...Option) (result Example, err error) {
|
||||||
options := options{
|
options := options{
|
||||||
query: make(map[string]interface{}, len(opts)),
|
query: make(map[string]interface{}, len(opts)),
|
||||||
}
|
}
|
||||||
@@ -67,13 +90,20 @@ func (obj *_ExampleMgr) GetByOption(opts ...Option) (Example, error) {
|
|||||||
o.apply(&options)
|
o.apply(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmp Example
|
err = obj.DB.Table("example").Where(options.query).Find(&result).Error
|
||||||
err := obj.DB.Table("example").Where(options.query).Find(&tmp).Error
|
if err == nil {
|
||||||
return tmp, err
|
var info []User
|
||||||
|
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result.UserList = info
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByPrimaryKey 批量功能选项模式获取
|
// GetByPrimaryKey 批量功能选项模式获取
|
||||||
func (obj *_ExampleMgr) GetByOptions(opts ...Option) ([]*Example, error) {
|
func (obj *_ExampleMgr) GetByOptions(opts ...Option) (results []*Example, err error) {
|
||||||
options := options{
|
options := options{
|
||||||
query: make(map[string]interface{}, len(opts)),
|
query: make(map[string]interface{}, len(opts)),
|
||||||
}
|
}
|
||||||
@@ -81,16 +111,31 @@ func (obj *_ExampleMgr) GetByOptions(opts ...Option) ([]*Example, error) {
|
|||||||
o.apply(&options)
|
o.apply(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmp []*Example
|
err = obj.DB.Table("example").Where(options.query).Find(&results).Error
|
||||||
err := obj.DB.Table("example").Where(options.query).Find(&tmp).Error
|
if err == nil {
|
||||||
return tmp, err
|
for i := 0; i < len(results); i++ {
|
||||||
|
var userList []User
|
||||||
|
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
results[i].UserList = userList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithID id获取
|
// WithID id获取
|
||||||
func (obj *_ExampleMgr) WithID(id string) Option {
|
func (obj *_ExampleMgr) WithID(id int64) Option {
|
||||||
return optionFunc(func(o *options) {
|
return optionFunc(func(o *options) {
|
||||||
o.query["id"] = id
|
o.query["id"] = id
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (obj *_ExampleMgr) WithUserID(id int64) Option {
|
||||||
|
return optionFunc(func(o *options) {
|
||||||
|
o.query["user_id"] = id
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////
|
///////////////////////////////////////////////////
|
||||||
|
|||||||
29
data/view/genfunc/genfunc_test.go
Normal file
29
data/view/genfunc/genfunc_test.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package genfunc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/xxjwxc/public/mysqldb"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFunc(t *testing.T) {
|
||||||
|
orm := mysqldb.OnInitDBOrm("root:qwer@tcp(127.0.0.1:3306)/matrix?charset=utf8&parseTime=True&loc=Local")
|
||||||
|
defer orm.OnDestoryDB()
|
||||||
|
|
||||||
|
mgr := ExampleMgr(orm.DB)
|
||||||
|
obj, err := mgr.GetFromID(1)
|
||||||
|
fmt.Println(obj, err)
|
||||||
|
|
||||||
|
obj1, err := mgr.GetByPrimaryKey(1)
|
||||||
|
fmt.Println(obj1, err)
|
||||||
|
|
||||||
|
obj2, err := mgr.GetByPrimaryKeys([]int64{1, 2})
|
||||||
|
fmt.Println(obj2, err)
|
||||||
|
|
||||||
|
obj3, err := mgr.GetByOptions(mgr.WithID(1), mgr.WithUserID(1))
|
||||||
|
fmt.Println(obj3, err)
|
||||||
|
|
||||||
|
obj4, err := mgr.GetByOption(mgr.WithID(1), mgr.WithUserID(1))
|
||||||
|
fmt.Println(obj4, err)
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@ package model
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/xxjwxc/public/mybigcamel"
|
||||||
|
|
||||||
"github.com/xxjwxc/gormt/data/config"
|
"github.com/xxjwxc/gormt/data/config"
|
||||||
"github.com/xxjwxc/gormt/data/view/genstruct"
|
"github.com/xxjwxc/gormt/data/view/genstruct"
|
||||||
)
|
)
|
||||||
@@ -91,7 +93,7 @@ func (m *_Model) genTableElement(cols []ColumusInfo) (el []genstruct.GenElement)
|
|||||||
if strings.EqualFold(v.Name, "id") {
|
if strings.EqualFold(v.Name, "id") {
|
||||||
tmp.AddTag(_tagJSON, "-")
|
tmp.AddTag(_tagJSON, "-")
|
||||||
} else if len(v.Name) > 0 {
|
} else if len(v.Name) > 0 {
|
||||||
tmp.AddTag(_tagJSON, v.Name)
|
tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
el = append(el, tmp)
|
el = append(el, tmp)
|
||||||
@@ -129,7 +131,7 @@ func (m *_Model) genForeignKey(col ColumusInfo) (fklist []genstruct.GenElement)
|
|||||||
|
|
||||||
// json tag
|
// json tag
|
||||||
if config.GetIsJSONTag() {
|
if config.GetIsJSONTag() {
|
||||||
tmp.AddTag(_tagJSON, v.TableName+"_list")
|
tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.TableName)+"_list")
|
||||||
}
|
}
|
||||||
|
|
||||||
fklist = append(fklist, tmp)
|
fklist = append(fklist, tmp)
|
||||||
|
|||||||
Reference in New Issue
Block a user