修复代码BUG + 循环引用
This commit is contained in:
142
option.go
142
option.go
@ -12,50 +12,20 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/database/define"
|
||||
"git.zhangdeman.cn/zhangdeman/op_type"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SetOption 设置选项
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:46 2022/5/15
|
||||
type SetOption func(o *Option)
|
||||
|
||||
// Option 扩展选项
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:05 下午 2021/8/8
|
||||
type Option struct {
|
||||
Model any `json:"-"` // 操作model
|
||||
Table string `json:"table"` // 查询的数据表
|
||||
Limit int `json:"limit"` // 限制数量
|
||||
Offset int `json:"offset"` // 偏移量
|
||||
In map[string]any `json:"in"` // in语句
|
||||
NotIn map[string]any `json:"not_in"` // not in语句
|
||||
Where map[string]any `json:"where"` // where 条件
|
||||
Between map[string][2]any `json:"between"` // between 条件
|
||||
NotBetween map[string][2]any `json:"not_between"` // not between 条件
|
||||
Start map[string]any `json:"start"` // >= 条件
|
||||
End map[string]any `json:"end"` // < 条件
|
||||
Like map[string]string `json:"like"` // like 语句
|
||||
NotLike map[string]string `json:"not_like"` // not like 语句
|
||||
NotEqual map[string]any `json:"not_equal"` // != 语句
|
||||
Order []string `json:"order"` // 排序规则
|
||||
OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
|
||||
}
|
||||
|
||||
// WithModel ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:18 2024/1/15
|
||||
func WithModel(model any) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithModel(model any) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
o.Model = model
|
||||
}
|
||||
}
|
||||
@ -65,8 +35,8 @@ func WithModel(model any) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:56 2023/3/22
|
||||
func WithTable(tableName string) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithTable(tableName string) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
o.Table = tableName
|
||||
}
|
||||
}
|
||||
@ -76,8 +46,8 @@ func WithTable(tableName string) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:17 2022/5/15
|
||||
func WithWhere[T op_type.BaseType](where map[string]T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithWhere[T op_type.BaseType](where map[string]T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.Where {
|
||||
o.Where = make(map[string]any)
|
||||
}
|
||||
@ -92,8 +62,8 @@ func WithWhere[T op_type.BaseType](where map[string]T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:00 2022/5/15
|
||||
func WithLimit[T op_type.Int](limit T, offset T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithLimit[T op_type.Int](limit T, offset T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
o.Limit = int(limit)
|
||||
o.Offset = int(offset)
|
||||
}
|
||||
@ -104,8 +74,8 @@ func WithLimit[T op_type.Int](limit T, offset T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:42 2023/10/14
|
||||
func WithLimitByPageAndSize[T op_type.Int](page T, size T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithLimitByPageAndSize[T op_type.Int](page T, size T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if size > 0 {
|
||||
o.Limit = int(size)
|
||||
}
|
||||
@ -121,8 +91,8 @@ func WithLimitByPageAndSize[T op_type.Int](page T, size T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:23 2022/5/15
|
||||
func WithIn[T op_type.Array](field string, value T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithIn[T op_type.Array](field string, value T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == value {
|
||||
return
|
||||
}
|
||||
@ -141,8 +111,8 @@ func WithIn[T op_type.Array](field string, value T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:24 2022/5/15
|
||||
func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithBatchIn[T op_type.Array](batchIn map[string]T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.In {
|
||||
o.In = make(map[string]any)
|
||||
}
|
||||
@ -157,8 +127,8 @@ func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:18 2022/5/15
|
||||
func WithNotIn[T op_type.Array](field string, value T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithNotIn[T op_type.Array](field string, value T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.NotIn {
|
||||
o.NotIn = make(map[string]any)
|
||||
}
|
||||
@ -174,8 +144,8 @@ func WithNotIn[T op_type.Array](field string, value T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:23 2022/5/15
|
||||
func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithBatchNotIn[T op_type.Array](data map[string]T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.NotIn {
|
||||
o.NotIn = make(map[string]any)
|
||||
}
|
||||
@ -193,8 +163,8 @@ func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:01 2022/5/15
|
||||
func WithStart(field string, value any) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithStart(field string, value any) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.Start {
|
||||
o.Start = make(map[string]any)
|
||||
}
|
||||
@ -207,8 +177,8 @@ func WithStart(field string, value any) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:03 2022/5/15
|
||||
func WithBatchStart(data map[string]any) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithBatchStart(data map[string]any) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.Start {
|
||||
o.Start = make(map[string]any)
|
||||
}
|
||||
@ -223,8 +193,8 @@ func WithBatchStart(data map[string]any) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:07 2022/5/15
|
||||
func WithEnd(field string, value any) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithEnd(field string, value any) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.End {
|
||||
o.End = make(map[string]any)
|
||||
}
|
||||
@ -237,8 +207,8 @@ func WithEnd(field string, value any) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:10 2022/5/15
|
||||
func WithBatchEnd(data map[string]any) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithBatchEnd(data map[string]any) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.End {
|
||||
o.End = make(map[string]any)
|
||||
}
|
||||
@ -253,8 +223,8 @@ func WithBatchEnd(data map[string]any) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:16 2022/5/15
|
||||
func WithLike(field string, value string) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithLike(field string, value string) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.Like {
|
||||
o.Like = make(map[string]string)
|
||||
}
|
||||
@ -270,8 +240,8 @@ func WithLike(field string, value string) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:19 2022/5/15
|
||||
func WithBatchLike(data map[string]string) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithBatchLike(data map[string]string) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.Like {
|
||||
o.Like = make(map[string]string)
|
||||
}
|
||||
@ -289,8 +259,8 @@ func WithBatchLike(data map[string]string) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:50 2022/5/15
|
||||
func WithNotLike(field string, value string) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithNotLike(field string, value string) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.NotLike {
|
||||
o.NotLike = make(map[string]string)
|
||||
}
|
||||
@ -306,8 +276,8 @@ func WithNotLike(field string, value string) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:52 2022/5/15
|
||||
func WithBatchNotLike(data map[string]string) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithBatchNotLike(data map[string]string) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.NotLike {
|
||||
o.NotLike = make(map[string]string)
|
||||
}
|
||||
@ -325,8 +295,8 @@ func WithBatchNotLike(data map[string]string) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:31 2022/5/15
|
||||
func WithNotEqual[T op_type.BaseType](field string, value T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithNotEqual[T op_type.BaseType](field string, value T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.NotEqual {
|
||||
o.NotEqual = make(map[string]any)
|
||||
}
|
||||
@ -339,8 +309,8 @@ func WithNotEqual[T op_type.BaseType](field string, value T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:33 2022/5/15
|
||||
func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithBatchNotEqual[T op_type.BaseType](data map[string]T) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.NotEqual {
|
||||
o.NotEqual = make(map[string]any)
|
||||
}
|
||||
@ -355,10 +325,10 @@ func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:03 2022/7/23
|
||||
func WithOR(orConditionList ...SetOption) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithOR(orConditionList ...define.SetOption) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.OR {
|
||||
o.OR = make([][]SetOption, 0)
|
||||
o.OR = make([][]define.SetOption, 0)
|
||||
}
|
||||
o.OR = append(o.OR, orConditionList)
|
||||
}
|
||||
@ -369,8 +339,8 @@ func WithOR(orConditionList ...SetOption) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:15 2022/10/20
|
||||
func WithOrder(orderRuleList ...string) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithOrder(orderRuleList ...string) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
o.Order = orderRuleList
|
||||
}
|
||||
}
|
||||
@ -380,8 +350,8 @@ func WithOrder(orderRuleList ...string) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:09 2023/4/3
|
||||
func WithOrderDesc(field string) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithOrderDesc(field string) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.Order {
|
||||
o.Order = make([]string, 0)
|
||||
}
|
||||
@ -394,8 +364,8 @@ func WithOrderDesc(field string) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:09 2023/4/3
|
||||
func WithOrderAsc(field string) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithOrderAsc(field string) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.Order {
|
||||
o.Order = make([]string, 0)
|
||||
}
|
||||
@ -408,8 +378,8 @@ func WithOrderAsc(field string) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:46 2024/8/9
|
||||
func newOption(setOptionList ...SetOption) *Option {
|
||||
o := &Option{}
|
||||
func newOption(setOptionList ...define.SetOption) *define.Option {
|
||||
o := &define.Option{}
|
||||
for _, item := range setOptionList {
|
||||
item(o)
|
||||
}
|
||||
@ -421,7 +391,7 @@ func newOption(setOptionList ...SetOption) *Option {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:46 2024/8/9
|
||||
func optionToSql(o *Option) (sqlBuildResult string, bindValue []any) {
|
||||
func optionToSql(o *define.Option) (sqlBuildResult string, bindValue []any) {
|
||||
bindValue = make([]any, 0)
|
||||
sqlBuildResultBlockList := make([]string, 0)
|
||||
// 设置where条件
|
||||
@ -531,8 +501,8 @@ func parseInSql(fieldValue any) (string, []any) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:52 2024/8/23
|
||||
func WithBetween(field string, left any, right any) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithBetween(field string, left any, right any) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.Between {
|
||||
o.Between = map[string][2]any{}
|
||||
}
|
||||
@ -545,8 +515,8 @@ func WithBetween(field string, left any, right any) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:52 2024/8/23
|
||||
func WithNotBetween(field string, left any, right any) SetOption {
|
||||
return func(o *Option) {
|
||||
func WithNotBetween(field string, left any, right any) define.SetOption {
|
||||
return func(o *define.Option) {
|
||||
if nil == o.NotBetween {
|
||||
o.NotBetween = map[string][2]any{}
|
||||
}
|
||||
@ -559,7 +529,7 @@ func WithNotBetween(field string, left any, right any) SetOption {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:48 2024/8/23
|
||||
func WithAnyCondition(column string, operate string, value any) (SetOption, error) {
|
||||
func WithAnyCondition(column string, operate string, value any) (define.SetOption, error) {
|
||||
if nil == value {
|
||||
return nil, errors.New("value is nil")
|
||||
}
|
||||
|
Reference in New Issue
Block a user