func export support

支持快捷函数导出。
This commit is contained in:
谢小军
2020-01-09 20:04:33 +08:00
parent 204164e8b9
commit 1ff17da96f
15 changed files with 932 additions and 44 deletions

View File

@@ -0,0 +1,44 @@
package model
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)
}

View File

@@ -0,0 +1,165 @@
package model
import (
"fmt"
"github.com/jinzhu/gorm"
)
type _ExampleMgr struct {
*_BaseMgr
}
// ExampleMgr open func
func ExampleMgr(db *gorm.DB) *_ExampleMgr {
if db == nil {
panic(fmt.Errorf("ExampleMgr need init by db"))
}
return &_ExampleMgr{_BaseMgr: &_BaseMgr{DB: db}}
}
// GetTableName get sql table name.获取数据库名字
func (obj *_ExampleMgr) GetTableName() string {
return "example"
}
// Get 获取
func (obj *_ExampleMgr) Get() (result Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Find(&result).Error
return
}
// Gets 获取批量结果
func (obj *_ExampleMgr) Gets() (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Find(&results).Error
return
}
//////////////////////////option case ////////////////////////////////////////////
// WithUserID user_id获取
func (obj *_ExampleMgr) WithUserID(UserID int) Option {
return optionFunc(func(o *options) { o.query["user_id"] = UserID })
}
// WithName name获取
func (obj *_ExampleMgr) WithName(Name string) Option {
return optionFunc(func(o *options) { o.query["name"] = Name })
}
// WithSex sex获取
func (obj *_ExampleMgr) WithSex(Sex int) Option {
return optionFunc(func(o *options) { o.query["sex"] = Sex })
}
// WithJob job获取
func (obj *_ExampleMgr) WithJob(Job int) Option {
return optionFunc(func(o *options) { o.query["job"] = Job })
}
// WithID id获取
func (obj *_ExampleMgr) WithID(ID int) Option {
return optionFunc(func(o *options) { o.query["id"] = ID })
}
// GetByOption 功能选项模式获取
func (obj *_ExampleMgr) GetByOption(opts ...Option) (result Example, err error) {
options := options{
query: make(map[string]interface{}, len(opts)),
}
for _, o := range opts {
o.apply(&options)
}
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&result).Error
return
}
// GetByOptions 批量功能选项模式获取
func (obj *_ExampleMgr) GetByOptions(opts ...Option) (results []*Example, err error) {
options := options{
query: make(map[string]interface{}, len(opts)),
}
for _, o := range opts {
o.apply(&options)
}
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error
return
}
//////////////////////////enume case ////////////////////////////////////////////
// GetFromUserID 通过user_id获取内容
func (obj *_ExampleMgr) GetFromUserID(UserID int) (result Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("user_id = ?", UserID).Find(&result).Error
return
}
// GetsBatchFromUserID 批量唯一主键查找
func (obj *_ExampleMgr) GetsBatchFromUserID(UserIDs []int) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("user_id IN (?)", UserIDs).Find(&results).Error
return
}
// GetFromName 通过name获取内容
func (obj *_ExampleMgr) GetFromName(Name string) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("name = ?", Name).Find(&results).Error
return
}
// GetsBatchFromName 批量唯一主键查找
func (obj *_ExampleMgr) GetsBatchFromName(Names []string) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("name IN (?)", Names).Find(&results).Error
return
}
// GetFromSex 通过sex获取内容
func (obj *_ExampleMgr) GetFromSex(Sex int) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("sex = ?", Sex).Find(&results).Error
return
}
// GetsBatchFromSex 批量唯一主键查找
func (obj *_ExampleMgr) GetsBatchFromSex(Sexs []int) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("sex IN (?)", Sexs).Find(&results).Error
return
}
// GetFromJob 通过job获取内容
func (obj *_ExampleMgr) GetFromJob(Job int) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("job = ?", Job).Find(&results).Error
return
}
// GetsBatchFromJob 批量唯一主键查找
func (obj *_ExampleMgr) GetsBatchFromJob(Jobs []int) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("job IN (?)", Jobs).Find(&results).Error
return
}
// GetFromID 通过id获取内容
func (obj *_ExampleMgr) GetFromID(ID int) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("id = ?", ID).Find(&results).Error
return
}
// GetsBatchFromID 批量唯一主键查找
func (obj *_ExampleMgr) GetsBatchFromID(IDs []int) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("id IN (?)", IDs).Find(&results).Error
return
}

View File

@@ -0,0 +1,276 @@
package model
import (
"fmt"
"github.com/jinzhu/gorm"
)
type _OrganMgr struct {
*_BaseMgr
}
// OrganMgr open func
func OrganMgr(db *gorm.DB) *_OrganMgr {
if db == nil {
panic(fmt.Errorf("OrganMgr need init by db"))
}
return &_OrganMgr{_BaseMgr: &_BaseMgr{DB: db}}
}
// GetTableName get sql table name.获取数据库名字
func (obj *_OrganMgr) GetTableName() string {
return "organ"
}
// Get 获取
func (obj *_OrganMgr) Get() (result Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Find(&result).Error
if err == nil && obj.isRelated {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", result.UserID).Find(&info).Error
if err != nil {
return
}
result.UserList = info
}
}
return
}
// Gets 获取批量结果
func (obj *_OrganMgr) Gets() (results []*Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}
//////////////////////////option case ////////////////////////////////////////////
// WithID id获取
func (obj *_OrganMgr) WithID(ID int) Option {
return optionFunc(func(o *options) { o.query["id"] = ID })
}
// WithUserID user_id获取
func (obj *_OrganMgr) WithUserID(UserID int) Option {
return optionFunc(func(o *options) { o.query["user_id"] = UserID })
}
// WithType type获取
func (obj *_OrganMgr) WithType(Type int) Option {
return optionFunc(func(o *options) { o.query["type"] = Type })
}
// WithScore score获取
func (obj *_OrganMgr) WithScore(Score int) Option {
return optionFunc(func(o *options) { o.query["score"] = Score })
}
// GetByOption 功能选项模式获取
func (obj *_OrganMgr) GetByOption(opts ...Option) (result Organ, err error) {
options := options{
query: make(map[string]interface{}, len(opts)),
}
for _, o := range opts {
o.apply(&options)
}
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&result).Error
if err == nil && obj.isRelated {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", result.UserID).Find(&info).Error
if err != nil {
return
}
result.UserList = info
}
}
return
}
// GetByOptions 批量功能选项模式获取
func (obj *_OrganMgr) GetByOptions(opts ...Option) (results []*Organ, err error) {
options := options{
query: make(map[string]interface{}, len(opts)),
}
for _, o := range opts {
o.apply(&options)
}
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 info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}
//////////////////////////enume case ////////////////////////////////////////////
// GetFromID 通过id获取内容
func (obj *_OrganMgr) GetFromID(ID int) (result Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("id = ?", ID).Find(&result).Error
if err == nil && obj.isRelated {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", result.UserID).Find(&info).Error
if err != nil {
return
}
result.UserList = info
}
}
return
}
// GetsBatchFromID 批量唯一主键查找
func (obj *_OrganMgr) GetsBatchFromID(IDs []int) (results []*Organ, err error) {
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 info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}
// GetFromUserID 通过user_id获取内容
func (obj *_OrganMgr) GetFromUserID(UserID int) (results []*Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("user_id = ?", UserID).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}
// GetsBatchFromUserID 批量唯一主键查找
func (obj *_OrganMgr) GetsBatchFromUserID(UserIDs []int) (results []*Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("user_id IN (?)", UserIDs).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}
// GetFromType 通过type获取内容
func (obj *_OrganMgr) GetFromType(Type int) (results []*Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("type = ?", Type).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}
// GetsBatchFromType 批量唯一主键查找
func (obj *_OrganMgr) GetsBatchFromType(Types []int) (results []*Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("type IN (?)", Types).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}
// GetFromScore 通过score获取内容
func (obj *_OrganMgr) GetFromScore(Score int) (results []*Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("score = ?", Score).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}
// GetsBatchFromScore 批量唯一主键查找
func (obj *_OrganMgr) GetsBatchFromScore(Scores []int) (results []*Organ, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("score IN (?)", Scores).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
{
var info []User //
err = obj.DB.Table("user").Where("sex = ?", results[i].UserID).Find(&info).Error
if err != nil {
return
}
results[i].UserList = info
}
}
}
return
}

View File

@@ -0,0 +1,146 @@
package model
import (
"fmt"
"github.com/jinzhu/gorm"
)
type _UserMgr struct {
*_BaseMgr
}
// UserMgr open func
func UserMgr(db *gorm.DB) *_UserMgr {
if db == nil {
panic(fmt.Errorf("UserMgr need init by db"))
}
return &_UserMgr{_BaseMgr: &_BaseMgr{DB: db}}
}
// GetTableName get sql table name.获取数据库名字
func (obj *_UserMgr) GetTableName() string {
return "user"
}
// Get 获取
func (obj *_UserMgr) Get() (result User, err error) {
err = obj.DB.Table(obj.GetTableName()).Find(&result).Error
return
}
// Gets 获取批量结果
func (obj *_UserMgr) Gets() (results []*User, err error) {
err = obj.DB.Table(obj.GetTableName()).Find(&results).Error
return
}
//////////////////////////option case ////////////////////////////////////////////
// WithUserID userId获取
func (obj *_UserMgr) WithUserID(UserID int) Option {
return optionFunc(func(o *options) { o.query["userId"] = UserID })
}
// WithName name获取
func (obj *_UserMgr) WithName(Name string) Option {
return optionFunc(func(o *options) { o.query["name"] = Name })
}
// WithSex sex获取
func (obj *_UserMgr) WithSex(Sex int) Option {
return optionFunc(func(o *options) { o.query["sex"] = Sex })
}
// WithJob job获取
func (obj *_UserMgr) WithJob(Job int) Option {
return optionFunc(func(o *options) { o.query["job"] = Job })
}
// GetByOption 功能选项模式获取
func (obj *_UserMgr) GetByOption(opts ...Option) (result User, err error) {
options := options{
query: make(map[string]interface{}, len(opts)),
}
for _, o := range opts {
o.apply(&options)
}
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&result).Error
return
}
// GetByOptions 批量功能选项模式获取
func (obj *_UserMgr) GetByOptions(opts ...Option) (results []*User, err error) {
options := options{
query: make(map[string]interface{}, len(opts)),
}
for _, o := range opts {
o.apply(&options)
}
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error
return
}
//////////////////////////enume case ////////////////////////////////////////////
// GetFromUserID 通过userId获取内容
func (obj *_UserMgr) GetFromUserID(UserID int) (result User, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("userId = ?", UserID).Find(&result).Error
return
}
// GetsBatchFromUserID 批量唯一主键查找
func (obj *_UserMgr) GetsBatchFromUserID(UserIDs []int) (results []*User, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("userId IN (?)", UserIDs).Find(&results).Error
return
}
// GetFromName 通过name获取内容
func (obj *_UserMgr) GetFromName(Name string) (results []*User, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("name = ?", Name).Find(&results).Error
return
}
// GetsBatchFromName 批量唯一主键查找
func (obj *_UserMgr) GetsBatchFromName(Names []string) (results []*User, err error) {
err = obj.DB.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.DB.Table(obj.GetTableName()).Where("sex = ?", Sex).Find(&results).Error
return
}
// GetsBatchFromSex 批量唯一主键查找
func (obj *_UserMgr) GetsBatchFromSex(Sexs []int) (results []*User, err error) {
err = obj.DB.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.DB.Table(obj.GetTableName()).Where("job = ?", Job).Find(&results).Error
return
}
// GetsBatchFromJob 批量唯一主键查找
func (obj *_UserMgr) GetsBatchFromJob(Jobs []int) (results []*User, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("job IN (?)", Jobs).Find(&results).Error
return
}

View File

@@ -0,0 +1,27 @@
package model
// Example [...]
type Example struct {
UserID int `gorm:"primary_key" json:"user_id"`
Name string `json:"name"`
Sex int `gorm:"index" json:"sex"`
Job int `json:"job"`
ID int `json:"-"`
}
// Organ [...]
type Organ struct {
ID int `gorm:"primary_key" json:"-"`
UserID int `gorm:"index" json:"user_id"`
UserList []User `gorm:"association_foreignkey:user_id;foreignkey:sex" json:"user_list"`
Type int `json:"type"`
Score int `json:"score"`
}
// User [...]
type User struct {
UserID int `gorm:"primary_key" json:"user_id"`
Name string `json:"name"`
Sex int `gorm:"index" json:"sex"`
Job int `json:"job"`
}