This commit is contained in:
xxj
2021-06-13 21:55:36 +08:00
parent 96794856b6
commit e3e175313e
9 changed files with 233 additions and 58 deletions

View File

@@ -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}}

View File

@@ -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()
}

View File

@@ -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{}
}

View File

@@ -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 { //

View File

@@ -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
}

View File

@@ -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",
}

View File

@@ -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)
}