-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
Description
Description
功能需求:增强 WhereLike 方法对特殊字符的自动转义支持
问题描述
当前在使用 gf v2.9.5 的 WhereLike 方法进行模糊查询时,如果用户输入中包含 %、_、\ 等 SQL LIKE 操作符中的特殊字符,会导致查询结果不符合预期或查询失效。
例如,用户输入 "%",期望匹配包含 "%" 的字符串,但实际执行的是 LIKE '%%%',从而模糊查询失效,查询除了所有数据
目前需要开发者手动调用类似 EscapeLikeString 的函数对输入进行转义,增加了使用复杂度和重复代码。
现有解决方案(手动转义)
func EscapeLikeString(s string) string {
s = strings.ReplaceAll(s, "\\", "\\\\")
s = strings.ReplaceAll(s, "%", "\\%")
s = strings.ReplaceAll(s, "_", "\\_")
return s
}
// 使用示例
if req.DeviceName != "" {
deviceName := EscapeLikeString(req.DeviceName)
model = model.WhereLike(dao.NfcLockOpenLog.Columns().DeviceName, "%"+deviceName+"%")
}期望行为
希望框架能在 WhereLike 方法内部自动对传入的字符串进行 LIKE 特殊字符的转义,开发者无需手动处理,提高开发效率与安全性。
相关环境
GoFrame 版本:v2.9.5
Go 版本:go1.25.4
操作系统:windows/amd64
数据库:达梦数据库
Additional
No response