test(db): 添加 OR 条件处理的单元测试并修复 WHERE 子句逻辑
- 添加了 TestWhereWithORCondition 测试函数验证 OR 条件的括号包裹 - 修复了 where.go 中条件计数变量名称从 normalCondCount 改为 condCount - 实现了 OR/AND 组条件的括号包裹逻辑确保 SQL 语法正确 - 添加了空条件检查避免生成无效的 SQL 片段 - 更新了 .gitignore 文件添加日志文件忽略规则
This commit is contained in:
+18
-10
@@ -70,8 +70,8 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
|
||||
}
|
||||
sort.Strings(testQu)
|
||||
|
||||
// 追踪普通条件数量,用于自动添加 AND
|
||||
normalCondCount := 0
|
||||
// 追踪条件数量,用于自动添加 AND
|
||||
condCount := 0
|
||||
|
||||
for _, k := range testQu {
|
||||
v := data[k]
|
||||
@@ -79,8 +79,16 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
|
||||
// 检查是否是 AND/OR 条件关键字
|
||||
if isConditionKey(k) {
|
||||
tw, ts := that.cond(strings.ToUpper(k), v.(Map))
|
||||
where += tw
|
||||
res = append(res, ts...)
|
||||
if tw != "" && strings.TrimSpace(tw) != "" {
|
||||
// 与前面的条件用 AND 连接
|
||||
if condCount > 0 {
|
||||
where += " AND "
|
||||
}
|
||||
// 用括号包裹 OR/AND 组条件
|
||||
where += "(" + strings.TrimSpace(tw) + ")"
|
||||
condCount++
|
||||
res = append(res, ts...)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -95,11 +103,11 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
|
||||
// 检查是否是 NOT IN(带 [!] 后缀)- NOT IN 空数组永真,跳过即可
|
||||
if !strings.HasSuffix(k, "[!]") {
|
||||
// IN 空数组 -> 生成永假条件
|
||||
if normalCondCount > 0 {
|
||||
if condCount > 0 {
|
||||
where += " AND "
|
||||
}
|
||||
where += "1=0 "
|
||||
normalCondCount++
|
||||
condCount++
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -107,11 +115,11 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
|
||||
// 检查是否是 NOT IN(带 [!] 后缀)- NOT IN 空数组永真,跳过即可
|
||||
if !strings.HasSuffix(k, "[!]") {
|
||||
// IN 空数组 -> 生成永假条件
|
||||
if normalCondCount > 0 {
|
||||
if condCount > 0 {
|
||||
where += " AND "
|
||||
}
|
||||
where += "1=0 "
|
||||
normalCondCount++
|
||||
condCount++
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -119,11 +127,11 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
|
||||
tv, vv := that.varCond(k, v)
|
||||
if tv != "" {
|
||||
// 自动添加 AND 连接符
|
||||
if normalCondCount > 0 {
|
||||
if condCount > 0 {
|
||||
where += " AND "
|
||||
}
|
||||
where += tv
|
||||
normalCondCount++
|
||||
condCount++
|
||||
res = append(res, vv...)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user