opt: 修改AND操作符,适配Ors情况

This commit is contained in:
wangjie 2025-08-02 11:28:51 +08:00
parent bc6f4ae6c4
commit 20316abc31

View File

@ -39,6 +39,7 @@ type Condition struct {
Field string // 字段 Field string // 字段
Operator string // 操作类型 Operator string // 操作类型
Arg interface{} // 值 Arg interface{} // 值
Args []interface{}
} }
// PaginationQuery 查询条件 // PaginationQuery 查询条件
@ -123,8 +124,8 @@ func (q *PaginationQuery) Ors(condition string) {
} }
// And 自定义条件, 如: (length(field) = 4 OR length(field) = 6) // And 自定义条件, 如: (length(field) = 4 OR length(field) = 6)
func (q *PaginationQuery) And(condition string) { func (q *PaginationQuery) And(condition string, args ...interface{}) {
q.Conditions = append(q.Conditions, Condition{Field: condition, Operator: "and"}) q.Conditions = append(q.Conditions, Condition{Field: condition, Operator: "and", Args: args})
} }
// Group GROUP BY // Group GROUP BY
@ -244,7 +245,8 @@ func (q *PaginationQuery) BuildRawWhere() (where string, args []interface{}) {
sb.WriteString(fmt.Sprintf(" OR %s = ?", con.Field)) sb.WriteString(fmt.Sprintf(" OR %s = ?", con.Field))
args = append(args, con.Arg) args = append(args, con.Arg)
case "and": case "and":
sb.WriteString(fmt.Sprintf(" AND %s", con.Field)) // 自定义条件, 如: (length(field) = 4 OR length(field) = 6) sb.WriteString(fmt.Sprintf(" AND (%s)", con.Field)) // 自定义条件, 如: (length(field) = 4 OR length(field) = 6)
args = append(args, con.Args...)
case "group": case "group":
sb.WriteString(fmt.Sprintf(" GROUP BY %s", con.Field)) sb.WriteString(fmt.Sprintf(" GROUP BY %s", con.Field))