feat(core): 下沉查询密码工具与配置快照解码
Verify / verify (push) Canceled after 0s

This commit is contained in:
zhangdeman committed 2026-11-28 03:10:18 -08:00
1 parent bedcf4dd83
commit d2a9033695
23 files changed
+1544 -6

No files matched your search

+367
View File
@@ -0,0 +1,367 @@
package query
import (
"strings"
"gorm.io/gorm/clause"
)
// WithSelect 设置可信查询投影,避免上游会话的投影被 DAO 隔离清空。
//
// 入参:
// - columns:从对应模型 Columns 取得的字段。
//
// 返回:
// - 查询选项;不接受客户端提供的 SQL。
func WithSelect(columns ...string) SetOption {
return func(o *Option) {
o.Select = columns
}
}
// WithBatchSize 限制内部单批扫描数量,不生成偏移分页。
//
// 入参:
// - size:可信入口限制后的批次大小。
//
// 返回:
// - 仅限制数量的查询选项。
func WithBatchSize(size int) SetOption {
return func(o *Option) {
o.Limit = size
o.Offset = -1
}
}
// WithForUpdate 查询语句增加互斥锁
//
// 入参:
// - forUpdate:true 时增加互斥锁,false 时不增加锁;已显式设置的锁强度保持不变。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithForUpdate(forUpdate bool) SetOption {
return func(o *Option) {
if !forUpdate || len(o.LockingStrength) > 0 {
return
}
WithLockingStrength(clause.LockingStrengthUpdate)(o)
}
}
// WithModel 构造当前操作使用的可信查询选项。
//
// 入参:
// - model:当前操作的model参数。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithModel(model any) SetOption {
return func(o *Option) {
o.Model = model
}
}
// WithWhere 设置where条件
//
// 入参:
// - where:字段等值与集合条件。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithWhere[T any](where map[string]T) SetOption {
return func(o *Option) {
if nil == o.Where {
o.Where = make(map[string]any)
}
for field, value := range where {
o.Where[field] = value
}
}
}
// WithLimit 设置limit条件
//
// 入参:
// - limit:返回记录数量上限。
// - offset:既有列表契约中的偏移值。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithLimit[T Integer](limit T, offset T) SetOption {
return func(o *Option) {
o.Limit = int(limit)
o.Offset = int(offset)
}
}
// WithClearLimit 处理当前查询或队列协议步骤。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithClearLimit() SetOption {
return func(o *Option) {
o.Limit = 0
o.Offset = 0
}
}
// WithIn 设置in条件
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
// - value:需要绑定的真实字段值。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithIn[T ~[]E, E any](field string, value T) SetOption {
return func(o *Option) {
if nil == value {
return
}
if len(value) == 0 {
return
}
if nil == o.In {
o.In = make(map[string]any)
}
o.In[field] = value
}
}
// WithNotIn 设置 notin 条件
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
// - value:需要绑定的真实字段值。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithNotIn[T ~[]E, E any](field string, value T) SetOption {
return func(o *Option) {
if nil == o.NotIn {
o.NotIn = make(map[string]any)
}
if len(value) == 0 {
return
}
o.NotIn[field] = value
}
}
// WithBatchNotIn 批量设置 NOT IN
//
// 入参:
// - data:需要持久化或投递的当前数据。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithBatchNotIn[T ~[]E, E any](data map[string]T) SetOption {
return func(o *Option) {
if nil == o.NotIn {
o.NotIn = make(map[string]any)
}
for field, value := range data {
if len(value) == 0 {
continue
}
o.NotIn[field] = value
}
}
}
// WithStart >= 条件
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
// - value:需要绑定的真实字段值。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithStart(field string, value any) SetOption {
return func(o *Option) {
if nil == o.Start {
o.Start = make(map[string]any)
}
o.Start[field] = value
}
}
// WithEnd 设置 < 条件
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
// - value:需要绑定的真实字段值。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithEnd(field string, value any) SetOption {
return func(o *Option) {
if nil == o.End {
o.End = make(map[string]any)
}
o.End[field] = value
}
}
// WithLike 设置 like 查询条件
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
// - value:需要绑定的真实字段值。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithLike(field string, value string) SetOption {
return func(o *Option) {
if nil == o.Like {
o.Like = make(map[string]string)
}
if len(value) == 0 {
return
}
o.Like[field] = value
}
}
// WithBatchLike 批量设置like条件
//
// 入参:
// - data:需要持久化或投递的当前数据。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
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 {
if len(value) == 0 {
continue
}
o.Like[field] = value
}
}
}
// WithNotEqual 设置不等于语句
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
// - value:需要绑定的真实字段值。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithNotEqual[T any](field string, value T) SetOption {
return func(o *Option) {
if nil == o.NotEqual {
o.NotEqual = make(map[string]any)
}
o.NotEqual[field] = value
}
}
// WithOR 设置OR语句
//
// 入参:
// - orConditionList:需要在同一括号内做 OR 的可信条件。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithOR(orConditionList ...SetOption) SetOption {
return func(o *Option) {
if nil == o.OR {
o.OR = make([][]SetOption, 0)
}
o.OR = append(o.OR, orConditionList)
}
}
// WithOrder 设置排序规则
//
// 入参:
// - orderRuleList:已由调用方校验的排序字段和方向。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithOrder(orderRuleList ...string) SetOption {
return func(o *Option) {
if len(orderRuleList) != 2 {
return
}
if len(orderRuleList[0]) == 0 {
return
}
if len(orderRuleList[1]) == 0 {
orderRuleList[1] = "DESC"
}
o.Order = orderRuleList
}
}
// WithOrderDesc 降序排序
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithOrderDesc(field string) SetOption {
return WithOrder(field, "DESC")
}
// WithOrderAsc 升序排序
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithOrderAsc(field string) SetOption {
return WithOrder(field, "ASC")
}
// WithBetween between 语句
//
// 入参:
// - field:从对应 Model.Columns 取得的字段。
// - left:当前操作的left参数。
// - right:当前操作的right参数。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithBetween(field string, left any, right any) SetOption {
return func(o *Option) {
if nil == o.Between {
o.Between = map[string][2]any{}
}
o.Between[field] = [2]any{left, right}
}
}
// WithLockingStrength 给sql语句加锁
//
// 入参:
// - lockingStrength:当前操作需要的锁强度。
//
// 返回:
// - 返回值 1:当前查询或协议操作的结果。
func WithLockingStrength(lockingStrength string) SetOption {
return func(o *Option) {
lockingStrength = strings.ToUpper(lockingStrength)
if lockingStrength != clause.LockingStrengthUpdate && lockingStrength != clause.LockingStrengthShare && lockingStrength != clause.LockingOptionsSkipLocked && lockingStrength != clause.LockingOptionsNoWait {
return
}
if o.ForUpdate {
o.ForUpdate = false
}
o.LockingStrength = lockingStrength
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.