2023-04-18 18:25:39 +08:00
|
|
|
// Package database ...
|
2022-05-15 17:35:41 +08:00
|
|
|
//
|
|
|
|
// Description : mysql ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2022-05-15 11:43
|
2023-04-18 18:25:39 +08:00
|
|
|
package database
|
2022-05-15 17:35:41 +08:00
|
|
|
|
2023-10-14 14:57:53 +08:00
|
|
|
import (
|
|
|
|
"git.zhangdeman.cn/zhangdeman/op_type"
|
|
|
|
)
|
2023-04-15 20:26:32 +08:00
|
|
|
|
2022-05-15 17:35:41 +08:00
|
|
|
// SetOption 设置选项
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:46 2022/5/15
|
|
|
|
type SetOption func(o *Option)
|
|
|
|
|
2022-07-23 20:11:51 +08:00
|
|
|
// ORCondition OR 条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:07 2022/7/23
|
|
|
|
type ORCondition struct {
|
2022-07-23 20:14:56 +08:00
|
|
|
Express string `json:"express"` // 表达式
|
|
|
|
Value interface{} `json:"value"` // 绑定值
|
2022-07-23 20:11:51 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 17:35:41 +08:00
|
|
|
// Option 扩展选项
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:05 下午 2021/8/8
|
|
|
|
type Option struct {
|
2024-01-15 15:20:27 +08:00
|
|
|
Model interface{} `json:"-"` // 操作model
|
2023-03-22 21:02:24 +08:00
|
|
|
Table string `json:"table"` // 查询的数据表
|
2022-05-15 17:35:41 +08:00
|
|
|
Limit int `json:"limit"` // 限制数量
|
|
|
|
Offset int `json:"offset"` // 偏移量
|
|
|
|
In map[string]interface{} `json:"in"` // in语句
|
|
|
|
NotIn map[string]interface{} `json:"not_in"` // not in语句
|
|
|
|
Where map[string]interface{} `json:"where"` // where 条件
|
|
|
|
Start map[string]interface{} `json:"start"` // >= 条件
|
|
|
|
End map[string]interface{} `json:"end"` // < 条件
|
|
|
|
Like map[string]string `json:"like"` // like 语句
|
2022-05-15 17:58:42 +08:00
|
|
|
NotLike map[string]string `json:"not_like"` // not like 语句
|
2022-05-15 17:35:41 +08:00
|
|
|
NotEqual map[string]interface{} `json:"not_equal"` // != 语句
|
2022-10-20 20:22:23 +08:00
|
|
|
Order []string `json:"order"` // 排序规则
|
2022-07-23 20:11:51 +08:00
|
|
|
OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
|
2022-05-15 17:35:41 +08:00
|
|
|
}
|
|
|
|
|
2024-01-15 15:20:27 +08:00
|
|
|
// WithModel ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:18 2024/1/15
|
|
|
|
func WithModel(model interface{}) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.Model = model
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 21:02:24 +08:00
|
|
|
// WithTable 设置查询的表名
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:56 2023/3/22
|
|
|
|
func WithTable(tableName string) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.Table = tableName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 17:35:41 +08:00
|
|
|
// WithWhere 设置where条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:17 2022/5/15
|
2023-10-14 14:57:53 +08:00
|
|
|
func WithWhere[T op_type.BaseType](where map[string]T) SetOption {
|
2022-05-15 17:35:41 +08:00
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.Where {
|
|
|
|
o.Where = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
for field, value := range where {
|
|
|
|
o.Where[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLimit 设置limit条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:00 2022/5/15
|
2023-04-15 20:26:32 +08:00
|
|
|
func WithLimit[T op_type.Int](limit T, offset T) SetOption {
|
2022-05-15 17:35:41 +08:00
|
|
|
return func(o *Option) {
|
2023-04-15 20:26:32 +08:00
|
|
|
o.Limit = int(limit)
|
|
|
|
o.Offset = int(offset)
|
2022-05-15 17:35:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 21:47:04 +08:00
|
|
|
// WithLimitByPageAndSize 通过page和size构建条件
|
|
|
|
//
|
|
|
|
// 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) {
|
|
|
|
if size > 0 {
|
|
|
|
o.Limit = int(size)
|
|
|
|
}
|
|
|
|
if page <= 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
o.Offset = int((page - 1) * size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 17:35:41 +08:00
|
|
|
// WithIn 设置in条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:23 2022/5/15
|
2023-10-14 14:57:53 +08:00
|
|
|
func WithIn[T op_type.Array](field string, value T) SetOption {
|
2022-05-15 17:35:41 +08:00
|
|
|
return func(o *Option) {
|
2023-10-14 14:57:53 +08:00
|
|
|
if nil == value {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-05-15 17:35:41 +08:00
|
|
|
if nil == o.In {
|
|
|
|
o.In = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
o.In[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBatchIn ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:24 2022/5/15
|
2023-10-14 14:57:53 +08:00
|
|
|
func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption {
|
2022-05-15 17:35:41 +08:00
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.In {
|
|
|
|
o.In = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
for field, value := range batchIn {
|
2024-01-22 18:26:03 +08:00
|
|
|
WithIn(field, value)
|
2022-05-15 17:35:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNotIn 设置 notin 条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:18 2022/5/15
|
2023-10-14 14:57:53 +08:00
|
|
|
func WithNotIn[T op_type.Array](field string, value T) SetOption {
|
2022-05-15 17:35:41 +08:00
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.NotIn {
|
|
|
|
o.NotIn = make(map[string]interface{})
|
|
|
|
}
|
2023-10-14 14:57:53 +08:00
|
|
|
if value == nil || len(value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-05-15 17:35:41 +08:00
|
|
|
o.NotIn[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBatchNotIn 批量设置 NOT IN
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:23 2022/5/15
|
2023-10-14 14:57:53 +08:00
|
|
|
func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption {
|
2022-05-15 17:35:41 +08:00
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.NotIn {
|
|
|
|
o.NotIn = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
for field, value := range data {
|
2023-10-14 14:57:53 +08:00
|
|
|
if value == nil || len(value) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-15 17:35:41 +08:00
|
|
|
o.NotIn[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStart >= 条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:01 2022/5/15
|
|
|
|
func WithStart(field string, value interface{}) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.Start {
|
|
|
|
o.Start = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
o.Start[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBatchStart 批量设置起始条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:03 2022/5/15
|
|
|
|
func WithBatchStart(data map[string]interface{}) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.Start {
|
|
|
|
o.Start = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
for field, value := range data {
|
|
|
|
o.Start[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithEnd 设置 < 条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:07 2022/5/15
|
|
|
|
func WithEnd(field string, value interface{}) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.End {
|
|
|
|
o.End = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
o.End[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBatchEnd 批量设置 < 条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:10 2022/5/15
|
|
|
|
func WithBatchEnd(data map[string]interface{}) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.End {
|
|
|
|
o.End = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
for field, value := range data {
|
|
|
|
o.End[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLike 设置 like 查询条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:16 2022/5/15
|
|
|
|
func WithLike(field string, value string) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.Like {
|
|
|
|
o.Like = make(map[string]string)
|
|
|
|
}
|
2023-10-14 14:57:53 +08:00
|
|
|
if len(value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-05-15 17:35:41 +08:00
|
|
|
o.Like[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBatchLike 批量设置like条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:19 2022/5/15
|
|
|
|
func WithBatchLike(data map[string]string) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.Like {
|
|
|
|
o.Like = make(map[string]string)
|
|
|
|
}
|
|
|
|
for field, value := range data {
|
2023-10-14 14:57:53 +08:00
|
|
|
if len(value) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-15 17:35:41 +08:00
|
|
|
o.Like[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 17:58:42 +08:00
|
|
|
// WithNotLike NOT LIKE 语句
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:50 2022/5/15
|
|
|
|
func WithNotLike(field string, value string) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.NotLike {
|
|
|
|
o.NotLike = make(map[string]string)
|
|
|
|
}
|
2023-10-14 14:57:53 +08:00
|
|
|
if len(value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-05-15 17:58:42 +08:00
|
|
|
o.NotLike[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBatchNotLike ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:52 2022/5/15
|
|
|
|
func WithBatchNotLike(data map[string]string) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.NotLike {
|
|
|
|
o.NotLike = make(map[string]string)
|
|
|
|
}
|
|
|
|
for field, value := range data {
|
2023-10-14 14:57:53 +08:00
|
|
|
if len(value) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-15 17:58:42 +08:00
|
|
|
o.NotLike[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 17:35:41 +08:00
|
|
|
// WithNotEqual 设置不等于语句
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:31 2022/5/15
|
2023-10-14 14:57:53 +08:00
|
|
|
func WithNotEqual[T op_type.BaseType](field string, value T) SetOption {
|
2022-05-15 17:35:41 +08:00
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.NotEqual {
|
|
|
|
o.NotEqual = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
o.NotEqual[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBatchNotEqual 批量设置不等于条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:33 2022/5/15
|
2023-10-14 14:57:53 +08:00
|
|
|
func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption {
|
2022-05-15 17:35:41 +08:00
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.NotEqual {
|
|
|
|
o.NotEqual = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
for field, value := range data {
|
|
|
|
o.NotEqual[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 20:11:51 +08:00
|
|
|
|
|
|
|
// WithOR 设置OR语句
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:03 2022/7/23
|
2022-07-23 20:14:56 +08:00
|
|
|
func WithOR(orConditionList ...*ORCondition) SetOption {
|
2022-07-23 20:11:51 +08:00
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.OR {
|
|
|
|
o.OR = make([]*ORCondition, 0)
|
|
|
|
}
|
|
|
|
o.OR = append(o.OR, orConditionList...)
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 20:22:23 +08:00
|
|
|
|
|
|
|
// WithOrder 设置排序规则
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:15 2022/10/20
|
|
|
|
func WithOrder(orderRuleList ...string) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.Order = orderRuleList
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 15:10:33 +08:00
|
|
|
|
|
|
|
// WithOrderDesc 降序排序
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:09 2023/4/3
|
|
|
|
func WithOrderDesc(field string) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.Order {
|
|
|
|
o.Order = make([]string, 0)
|
|
|
|
}
|
|
|
|
o.Order = append(o.Order, field+" desc")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOrderAsc 升序排序
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:09 2023/4/3
|
|
|
|
func WithOrderAsc(field string) SetOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if nil == o.Order {
|
|
|
|
o.Order = make([]string, 0)
|
|
|
|
}
|
|
|
|
o.Order = append(o.Order, field+" asc")
|
|
|
|
}
|
|
|
|
}
|