39 lines
1021 B
Go
Raw Normal View History

2024-10-14 19:27:46 +08:00
package gorm_v2
2024-11-08 18:57:08 +08:00
import (
"regexp"
"gorm.io/gorm"
)
2024-10-14 19:27:46 +08:00
/**
* @description: 通过检查字段的方式构建 Where 函数
* @param {*gorm.DB} db
* @param {map[string][]uint8} conditions
* @return {*}
*/
2024-10-16 11:33:32 +08:00
// INFO 特性,源于 MySQL 键值 index from 1
// 同时 go 在解析参数之时,对于 Query 为空的情况会得到 [0] 的结果,
// 所以就可以用这种方式简单的过滤掉。
2024-10-14 19:27:46 +08:00
func BuildWhere(db *gorm.DB, conditions map[string][]uint8) *gorm.DB {
for field, values := range conditions {
if len(values) == 0 || len(values) == 1 && values[0] == 0 {
continue
}
db = db.Where(field+" in (?)", values)
}
return db
}
2024-11-08 18:57:08 +08:00
// isLikePatternMatch 检查字符串是否匹配 LIKE '%name%' 模式
func IsLikePatternMatch(input, pattern string) bool {
// 构建正则表达式
regexPattern := ".*" + regexp.QuoteMeta(pattern) + ".*"
// 编译正则表达式
re := regexp.MustCompile(regexPattern)
// 检查输入字符串是否匹配正则表达式
return re.MatchString(input)
}