generate func at early days
This commit is contained in:
49
data/view/genfunc/def.go
Normal file
49
data/view/genfunc/def.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package genfunc
|
||||
|
||||
const (
|
||||
genBase = `
|
||||
package {{.PackageName}}
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
// prepare for outher
|
||||
type _BaseMgr struct {
|
||||
*gorm.DB
|
||||
ctx *context.Context
|
||||
isRelated bool
|
||||
}
|
||||
|
||||
// SetCtx set context
|
||||
func (obj *_BaseMgr) SetCtx(c *context.Context) {
|
||||
obj.ctx = c
|
||||
}
|
||||
|
||||
// GetDB get gorm.DB info
|
||||
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{}
|
||||
}
|
||||
|
||||
// Option overrides behavior of Connect.
|
||||
type Option interface {
|
||||
apply(*options)
|
||||
}
|
||||
|
||||
type optionFunc func(*options)
|
||||
|
||||
func (f optionFunc) apply(o *options) {
|
||||
f(o)
|
||||
}
|
||||
`
|
||||
)
|
||||
@@ -33,10 +33,15 @@ func ExampleMgr(db *gorm.DB) *_ExampleMgr {
|
||||
return &_ExampleMgr{_BaseMgr: &_BaseMgr{DB: db}}
|
||||
}
|
||||
|
||||
// GetTableName get sql table name.获取数据库名字
|
||||
func (obj *_ExampleMgr) GetTableName() string {
|
||||
return "example"
|
||||
}
|
||||
|
||||
// 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 {
|
||||
err = obj.DB.Table(obj.GetTableName()).Where("id = ?", id).Find(&results).Error
|
||||
if err == nil && obj.isRelated {
|
||||
for i := 0; i < len(results); i++ {
|
||||
var userList []User
|
||||
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||
@@ -51,8 +56,8 @@ func (obj *_ExampleMgr) GetFromID(id int) (results []*Example, err error) {
|
||||
|
||||
// GetByPrimaryKey 唯一主键查找
|
||||
func (obj *_ExampleMgr) GetByPrimaryKey(id int64) (result Example, err error) {
|
||||
err = obj.DB.Table("example").Where("id = ?", id).Find(&result).Error
|
||||
if err == nil {
|
||||
err = obj.DB.Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
|
||||
if err == nil && obj.isRelated {
|
||||
var info []User
|
||||
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
|
||||
if err != nil {
|
||||
@@ -65,8 +70,8 @@ func (obj *_ExampleMgr) GetByPrimaryKey(id int64) (result Example, err error) {
|
||||
|
||||
// GetByPrimaryKey 批量唯一主键查找
|
||||
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 {
|
||||
err = obj.DB.Table(obj.GetTableName()).Where("id IN (?)", ids).Find(&results).Error
|
||||
if err == nil && obj.isRelated {
|
||||
for i := 0; i < len(results); i++ {
|
||||
var userList []User
|
||||
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||
@@ -90,8 +95,8 @@ func (obj *_ExampleMgr) GetByOption(opts ...Option) (result Example, err error)
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
err = obj.DB.Table("example").Where(options.query).Find(&result).Error
|
||||
if err == nil {
|
||||
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
||||
if err == nil && obj.isRelated {
|
||||
var info []User
|
||||
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
|
||||
if err != nil {
|
||||
@@ -111,8 +116,8 @@ func (obj *_ExampleMgr) GetByOptions(opts ...Option) (results []*Example, err er
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
err = obj.DB.Table("example").Where(options.query).Find(&results).Error
|
||||
if err == nil {
|
||||
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
||||
if err == nil && obj.isRelated {
|
||||
for i := 0; i < len(results); i++ {
|
||||
var userList []User
|
||||
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
|
||||
|
||||
@@ -1 +1,11 @@
|
||||
package genfunc
|
||||
|
||||
// GetGenBaseTemp get gen base template str
|
||||
func GetGenBaseTemp() string {
|
||||
return genBase
|
||||
}
|
||||
|
||||
// GetGenLogicTemp get gen logic template str
|
||||
func GetGenLogicTemp() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user