feat: sql构建支持 FOR UPDATE/迁移日志实现 #12
							
								
								
									
										8
									
								
								base.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								base.go
									
									
									
									
									
								
							@ -7,9 +7,11 @@ package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"reflect"
 | 
			
		||||
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/database/define"
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"gorm.io/gorm/clause"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// BaseDao 基础dao层
 | 
			
		||||
@ -283,6 +285,10 @@ func (b *BaseDao) setTxCondition(inputTx *gorm.DB, optionFuncList ...define.SetO
 | 
			
		||||
		tx = tx.Where(o.Where)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if o.ForUpdate {
 | 
			
		||||
		tx = tx.Clauses(clause.Locking{Strength: "UPDATE"})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// between
 | 
			
		||||
	for field, betweenVal := range o.Between {
 | 
			
		||||
		tx = tx.Where("`"+field+"` BETWEEN ? AND ?", betweenVal[0], betweenVal[1])
 | 
			
		||||
 | 
			
		||||
@ -34,6 +34,7 @@ type Option struct {
 | 
			
		||||
	Like       map[string]string `json:"like"`        // like 语句
 | 
			
		||||
	NotLike    map[string]string `json:"not_like"`    // not like 语句
 | 
			
		||||
	NotEqual   map[string]any    `json:"not_equal"`   // != 语句
 | 
			
		||||
	ForUpdate  bool              `json:"for_update"`  // 加互斥锁
 | 
			
		||||
	Order      []string          `json:"order"`       // 排序规则
 | 
			
		||||
	OR         [][]SetOption     `json:"or"`          // or 语句, or 和其他属性 and 关系, 内部 OR 关系
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										130
									
								
								option.go
									
									
									
									
									
								
							
							
						
						
									
										130
									
								
								option.go
									
									
									
									
									
								
							@ -11,19 +11,23 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/consts"
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/database/define"
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/op_type"
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/serialize"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// WithForUpdate 查询语句增加互斥锁
 | 
			
		||||
func WithForUpdate(forUpdate bool) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		o.ForUpdate = forUpdate
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithModel ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 15:18 2024/1/15
 | 
			
		||||
func WithModel(model any) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		o.Model = model
 | 
			
		||||
@ -31,10 +35,6 @@ func WithModel(model any) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithTable 设置查询的表名
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 20:56 2023/3/22
 | 
			
		||||
func WithTable(tableName string) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		o.Table = tableName
 | 
			
		||||
@ -42,10 +42,6 @@ func WithTable(tableName string) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithWhere 设置where条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 12:17 2022/5/15
 | 
			
		||||
func WithWhere[T op_type.BaseType](where map[string]T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.Where {
 | 
			
		||||
@ -58,10 +54,6 @@ func WithWhere[T op_type.BaseType](where map[string]T) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithLimit 设置limit条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 12:00 2022/5/15
 | 
			
		||||
func WithLimit[T op_type.Int](limit T, offset T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		o.Limit = int(limit)
 | 
			
		||||
@ -77,10 +69,6 @@ func WithClearLimit() define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithLimitByPageAndSize 通过page和size构建条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 21:42 2023/10/14
 | 
			
		||||
func WithLimitByPageAndSize[T op_type.Int](page T, size T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if size > 0 {
 | 
			
		||||
@ -94,10 +82,6 @@ func WithLimitByPageAndSize[T op_type.Int](page T, size T) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithIn 设置in条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 12:23 2022/5/15
 | 
			
		||||
func WithIn[T op_type.Array](field string, value T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == value {
 | 
			
		||||
@ -113,11 +97,7 @@ func WithIn[T op_type.Array](field string, value T) define.SetOption {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithBatchIn ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 12:24 2022/5/15
 | 
			
		||||
// WithBatchIn 批量设置IN条件
 | 
			
		||||
func WithBatchIn[T op_type.Array](batchIn map[string]T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.In {
 | 
			
		||||
@ -130,10 +110,6 @@ func WithBatchIn[T op_type.Array](batchIn map[string]T) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithNotIn 设置 notin 条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 16:18 2022/5/15
 | 
			
		||||
func WithNotIn[T op_type.Array](field string, value T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.NotIn {
 | 
			
		||||
@ -147,10 +123,6 @@ func WithNotIn[T op_type.Array](field string, value T) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithBatchNotIn 批量设置 NOT IN
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 16:23 2022/5/15
 | 
			
		||||
func WithBatchNotIn[T op_type.Array](data map[string]T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.NotIn {
 | 
			
		||||
@ -166,10 +138,6 @@ func WithBatchNotIn[T op_type.Array](data map[string]T) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithStart >= 条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:01 2022/5/15
 | 
			
		||||
func WithStart(field string, value any) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.Start {
 | 
			
		||||
@ -180,10 +148,6 @@ func WithStart(field string, value any) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithBatchStart 批量设置起始条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:03 2022/5/15
 | 
			
		||||
func WithBatchStart(data map[string]any) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.Start {
 | 
			
		||||
@ -196,10 +160,6 @@ func WithBatchStart(data map[string]any) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithEnd 设置 < 条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:07 2022/5/15
 | 
			
		||||
func WithEnd(field string, value any) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.End {
 | 
			
		||||
@ -210,10 +170,6 @@ func WithEnd(field string, value any) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithBatchEnd 批量设置 < 条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:10 2022/5/15
 | 
			
		||||
func WithBatchEnd(data map[string]any) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.End {
 | 
			
		||||
@ -226,10 +182,6 @@ func WithBatchEnd(data map[string]any) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithLike 设置 like 查询条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:16 2022/5/15
 | 
			
		||||
func WithLike(field string, value string) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.Like {
 | 
			
		||||
@ -243,10 +195,6 @@ func WithLike(field string, value string) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithBatchLike 批量设置like条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:19 2022/5/15
 | 
			
		||||
func WithBatchLike(data map[string]string) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.Like {
 | 
			
		||||
@ -262,10 +210,6 @@ func WithBatchLike(data map[string]string) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithNotLike NOT LIKE 语句
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:50 2022/5/15
 | 
			
		||||
func WithNotLike(field string, value string) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.NotLike {
 | 
			
		||||
@ -279,10 +223,6 @@ func WithNotLike(field string, value string) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithBatchNotLike ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:52 2022/5/15
 | 
			
		||||
func WithBatchNotLike(data map[string]string) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.NotLike {
 | 
			
		||||
@ -298,10 +238,6 @@ func WithBatchNotLike(data map[string]string) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithNotEqual 设置不等于语句
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:31 2022/5/15
 | 
			
		||||
func WithNotEqual[T op_type.BaseType](field string, value T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.NotEqual {
 | 
			
		||||
@ -312,10 +248,6 @@ func WithNotEqual[T op_type.BaseType](field string, value T) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithBatchNotEqual 批量设置不等于条件
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:33 2022/5/15
 | 
			
		||||
func WithBatchNotEqual[T op_type.BaseType](data map[string]T) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.NotEqual {
 | 
			
		||||
@ -328,10 +260,6 @@ func WithBatchNotEqual[T op_type.BaseType](data map[string]T) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithOR 设置OR语句
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 20:03 2022/7/23
 | 
			
		||||
func WithOR(orConditionList ...define.SetOption) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.OR {
 | 
			
		||||
@ -342,10 +270,6 @@ func WithOR(orConditionList ...define.SetOption) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithOrder 设置排序规则
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 20:15 2022/10/20
 | 
			
		||||
func WithOrder(orderRuleList ...string) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if len(orderRuleList) != 2 {
 | 
			
		||||
@ -364,28 +288,16 @@ func WithOrder(orderRuleList ...string) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithOrderDesc 降序排序
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 15:09 2023/4/3
 | 
			
		||||
func WithOrderDesc(field string) define.SetOption {
 | 
			
		||||
	return WithOrder(field, "DESC")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithOrderAsc 升序排序
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 15:09 2023/4/3
 | 
			
		||||
func WithOrderAsc(field string) define.SetOption {
 | 
			
		||||
	return WithOrder(field, "ASC")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// newOption 生成新的option
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:46 2024/8/9
 | 
			
		||||
func newOption(setOptionList ...define.SetOption) *define.Option {
 | 
			
		||||
	o := &define.Option{}
 | 
			
		||||
	for _, item := range setOptionList {
 | 
			
		||||
@ -395,10 +307,6 @@ func newOption(setOptionList ...define.SetOption) *define.Option {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// optionToSql 基于 option 配置生成sql
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:46 2024/8/9
 | 
			
		||||
func optionToSql(o *define.Option) (sqlBuildResult string, bindValue []any) {
 | 
			
		||||
	bindValue = make([]any, 0)
 | 
			
		||||
	sqlBuildResultBlockList := make([]string, 0)
 | 
			
		||||
@ -486,10 +394,6 @@ func optionToSql(o *define.Option) (sqlBuildResult string, bindValue []any) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// parseInSql 解析in语句需要绑定占位符以及数据
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 22:07 2024/8/9
 | 
			
		||||
func parseInSql(fieldValue any) (string, []any) {
 | 
			
		||||
	byteData, _ := json.Marshal(fieldValue)
 | 
			
		||||
	var dataList []any
 | 
			
		||||
@ -505,10 +409,6 @@ func parseInSql(fieldValue any) (string, []any) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithBetween between 语句
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 15:52 2024/8/23
 | 
			
		||||
func WithBetween(field string, left any, right any) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.Between {
 | 
			
		||||
@ -519,10 +419,6 @@ func WithBetween(field string, left any, right any) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithNotBetween not between 语句
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 15:52 2024/8/23
 | 
			
		||||
func WithNotBetween(field string, left any, right any) define.SetOption {
 | 
			
		||||
	return func(o *define.Option) {
 | 
			
		||||
		if nil == o.NotBetween {
 | 
			
		||||
@ -533,10 +429,6 @@ func WithNotBetween(field string, left any, right any) define.SetOption {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WithAnyCondition 设置任意查询条件, 仅 where 子句 in / not in / like / not like / == / !=
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 14:48 2024/8/23
 | 
			
		||||
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