feat: sql 构建支持 FOR UPDATE

This commit is contained in:
2025-10-11 18:10:29 +08:00
parent 4acb528533
commit cb3d32c360
3 changed files with 19 additions and 120 deletions

View File

@ -7,9 +7,11 @@ package database
import ( import (
"errors" "errors"
"reflect"
"git.zhangdeman.cn/zhangdeman/database/define" "git.zhangdeman.cn/zhangdeman/database/define"
"gorm.io/gorm" "gorm.io/gorm"
"reflect" "gorm.io/gorm/clause"
) )
// BaseDao 基础dao层 // BaseDao 基础dao层
@ -283,6 +285,10 @@ func (b *BaseDao) setTxCondition(inputTx *gorm.DB, optionFuncList ...define.SetO
tx = tx.Where(o.Where) tx = tx.Where(o.Where)
} }
if o.ForUpdate {
tx = tx.Clauses(clause.Locking{Strength: "UPDATE"})
}
// between // between
for field, betweenVal := range o.Between { for field, betweenVal := range o.Between {
tx = tx.Where("`"+field+"` BETWEEN ? AND ?", betweenVal[0], betweenVal[1]) tx = tx.Where("`"+field+"` BETWEEN ? AND ?", betweenVal[0], betweenVal[1])

View File

@ -34,6 +34,7 @@ type Option struct {
Like map[string]string `json:"like"` // like 语句 Like map[string]string `json:"like"` // like 语句
NotLike map[string]string `json:"not_like"` // not like 语句 NotLike map[string]string `json:"not_like"` // not like 语句
NotEqual map[string]any `json:"not_equal"` // != 语句 NotEqual map[string]any `json:"not_equal"` // != 语句
ForUpdate bool `json:"for_update"` // 加互斥锁
Order []string `json:"order"` // 排序规则 Order []string `json:"order"` // 排序规则
OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
} }

130
option.go
View File

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