func base template update
This commit is contained in:
@@ -9,7 +9,8 @@ import (
|
||||
// prepare for outher
|
||||
type _BaseMgr struct {
|
||||
*gorm.DB
|
||||
ctx *context.Context
|
||||
ctx *context.Context
|
||||
isRelated bool
|
||||
}
|
||||
|
||||
// SetCtx set context
|
||||
@@ -22,6 +23,11 @@ func (obj *_BaseMgr) GetDB() *gorm.DB {
|
||||
return obj.DB
|
||||
}
|
||||
|
||||
// IsRelated Query foreign key Association.是否查询外键关联(gorm.Related)
|
||||
func (obj *_BaseMgr) IsRelated(b bool) {
|
||||
obj.isRelated = b
|
||||
}
|
||||
|
||||
type options struct {
|
||||
query map[string]interface{}
|
||||
}
|
||||
|
||||
@@ -36,30 +36,53 @@ func ExampleMgr(db *gorm.DB) *_ExampleMgr {
|
||||
// GetFromID 通过id获取内容
|
||||
func (obj *_ExampleMgr) GetFromID(id int) (results []*Example, err 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
|
||||
}
|
||||
|
||||
// GetByPrimaryKey 唯一主键查找
|
||||
func (obj *_ExampleMgr) GetByPrimaryKey(id int64) (Example, error) {
|
||||
var tmp Example
|
||||
err := obj.DB.Table("example").Where("id = ?", id).Find(&tmp).Error
|
||||
return tmp, err
|
||||
func (obj *_ExampleMgr) GetByPrimaryKey(id int64) (result Example, err error) {
|
||||
err = obj.DB.Table("example").Where("id = ?", id).Find(&result).Error
|
||||
if err == nil {
|
||||
var info []User
|
||||
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result.UserList = info
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByPrimaryKey 批量唯一主键查找
|
||||
func (obj *_ExampleMgr) GetByPrimaryKeys(ids []int64) ([]*Example, error) {
|
||||
var tmp []*Example
|
||||
err := obj.DB.Table("example").Where("id", ids).Find(&tmp).Error
|
||||
return tmp, err
|
||||
func (obj *_ExampleMgr) GetByPrimaryKeys(ids []int64) (results []*Example, err error) {
|
||||
err = obj.DB.Table("example").Where("id IN (?)", ids).Find(&results).Error
|
||||
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
|
||||
}
|
||||
|
||||
//////////////////////////option case ////////////////////////////////////////////
|
||||
|
||||
// GetByPrimaryKey 功能选项模式获取
|
||||
func (obj *_ExampleMgr) GetByOption(opts ...Option) (Example, error) {
|
||||
func (obj *_ExampleMgr) GetByOption(opts ...Option) (result Example, err error) {
|
||||
options := options{
|
||||
query: make(map[string]interface{}, len(opts)),
|
||||
}
|
||||
@@ -67,13 +90,20 @@ func (obj *_ExampleMgr) GetByOption(opts ...Option) (Example, error) {
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
var tmp Example
|
||||
err := obj.DB.Table("example").Where(options.query).Find(&tmp).Error
|
||||
return tmp, err
|
||||
err = obj.DB.Table("example").Where(options.query).Find(&result).Error
|
||||
if err == nil {
|
||||
var info []User
|
||||
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result.UserList = info
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByPrimaryKey 批量功能选项模式获取
|
||||
func (obj *_ExampleMgr) GetByOptions(opts ...Option) ([]*Example, error) {
|
||||
func (obj *_ExampleMgr) GetByOptions(opts ...Option) (results []*Example, err error) {
|
||||
options := options{
|
||||
query: make(map[string]interface{}, len(opts)),
|
||||
}
|
||||
@@ -81,16 +111,31 @@ func (obj *_ExampleMgr) GetByOptions(opts ...Option) ([]*Example, error) {
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
var tmp []*Example
|
||||
err := obj.DB.Table("example").Where(options.query).Find(&tmp).Error
|
||||
return tmp, err
|
||||
err = obj.DB.Table("example").Where(options.query).Find(&results).Error
|
||||
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
|
||||
}
|
||||
|
||||
// WithID id获取
|
||||
func (obj *_ExampleMgr) WithID(id string) Option {
|
||||
func (obj *_ExampleMgr) WithID(id int64) Option {
|
||||
return optionFunc(func(o *options) {
|
||||
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 (
|
||||
"strings"
|
||||
|
||||
"github.com/xxjwxc/public/mybigcamel"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
"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") {
|
||||
tmp.AddTag(_tagJSON, "-")
|
||||
} else if len(v.Name) > 0 {
|
||||
tmp.AddTag(_tagJSON, v.Name)
|
||||
tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.Name))
|
||||
}
|
||||
}
|
||||
el = append(el, tmp)
|
||||
@@ -129,7 +131,7 @@ func (m *_Model) genForeignKey(col ColumusInfo) (fklist []genstruct.GenElement)
|
||||
|
||||
// json tag
|
||||
if config.GetIsJSONTag() {
|
||||
tmp.AddTag(_tagJSON, v.TableName+"_list")
|
||||
tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.TableName)+"_list")
|
||||
}
|
||||
|
||||
fklist = append(fklist, tmp)
|
||||
|
||||
Reference in New Issue
Block a user