查询添加or条件

This commit is contained in:
xing 2022-09-16 22:32:05 +08:00
parent 0c41cd5bcf
commit 3272dd4aea
2 changed files with 79 additions and 23 deletions

View File

@ -38,6 +38,35 @@ func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
} }
} }
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]interface{}, in [][]interface{}) (t bool) {
if ss[1] == "in" && len(in) > 0 {
s.WriteString(" (")
for _, p := range in[*c] {
s.WriteString("?,")
*args = append(*args, p)
}
sx := s.String()
s.Reset()
s.WriteString(strings.TrimRight(sx, ","))
s.WriteString(")")
*c++
t = true
}
return t
}
func (w SqlBuilder) parseType(ss []string, s *strings.Builder, args *[]interface{}) {
if len(ss) == 4 && ss[3] == "int" {
i, _ := strconv.Atoi(ss[2])
*args = append(*args, i)
} else if len(ss) == 4 && ss[3] == "float" {
i, _ := strconv.ParseFloat(ss[2], 64)
*args = append(*args, i)
} else {
*args = append(*args, ss[2])
}
}
func (w SqlBuilder) ParseWhere(in ...[]interface{}) (string, []interface{}) { func (w SqlBuilder) ParseWhere(in ...[]interface{}) (string, []interface{}) {
var s strings.Builder var s strings.Builder
args := make([]interface{}, 0, len(w)) args := make([]interface{}, 0, len(w))
@ -47,33 +76,60 @@ func (w SqlBuilder) ParseWhere(in ...[]interface{}) (string, []interface{}) {
w.parseField(ss, &s) w.parseField(ss, &s)
s.WriteString("=? and ") s.WriteString("=? and ")
args = append(args, ss[1]) args = append(args, ss[1])
} } else if len(ss) >= 3 && len(ss) < 5 {
if len(ss) >= 3 {
w.parseField(ss, &s) w.parseField(ss, &s)
s.WriteString(ss[1]) s.WriteString(ss[1])
if ss[1] == "in" && len(in) > 0 { if w.parseIn(ss, &s, &c, &args, in) {
s.WriteString(" (")
for _, p := range in[c] {
s.WriteString("?,")
args = append(args, p)
}
sx := s.String()
s.Reset()
s.WriteString(strings.TrimRight(sx, ","))
s.WriteString(")")
c++
s.WriteString(" and ") s.WriteString(" and ")
continue continue
} }
s.WriteString(" ? and ") s.WriteString(" ? and ")
if len(ss) == 4 && ss[3] == "int" { w.parseType(ss, &s, &args)
i, _ := strconv.Atoi(ss[2]) } else if len(ss) >= 5 && len(ss)%5 == 0 {
args = append(args, i) j := len(ss) / 5
} else if len(ss) == 4 && ss[3] == "float" { fl := false
i, _ := strconv.ParseFloat(ss[2], 64) for i := 0; i < j; i++ {
args = append(args, i) start := i * 5
end := start + 5
if ss[start] == "or" {
st := s.String()
if strings.Contains(st, "and ") {
st = strings.TrimRight(st, "and ")
s.Reset()
s.WriteString(st)
s.WriteString(" or ")
}
if i == 0 {
s.WriteString("( ")
fl = true
}
w.parseField(ss[start+1:end], &s)
if w.parseIn(ss[start+1:end], &s, &c, &args, in) {
s.WriteString(" and ")
continue
}
s.WriteString(ss[start+2])
s.WriteString(" ? and ")
w.parseType(ss[start+1:end], &s, &args)
} else { } else {
args = append(args, ss[2]) w.parseField(ss[start+1:end], &s)
if w.parseIn(ss[start+1:end], &s, &c, &args, in) {
s.WriteString(" and ")
continue
}
s.WriteString(ss[start+2])
s.WriteString(" ? and ")
w.parseType(ss[start+1:start+4], &s, &args)
}
if i == j-1 && fl {
st := s.String()
st = strings.TrimRight(st, "and ")
s.Reset()
s.WriteString(st)
s.WriteString(") and ")
}
} }
} }
} }

View File

@ -155,7 +155,7 @@ func index(c *gin.Context) {
"categories": categoryItems, "categories": categoryItems,
"totalPage": totalPage, "totalPage": totalPage,
"queryRaw": q, "queryRaw": q,
"pagination": pagination(page, totalPage, 2, c.Request.URL.Path, q), "pagination": pagination(page, totalPage, 1, c.Request.URL.Path, q),
}) })
} }
@ -211,7 +211,7 @@ func pagination(currentPage, totalPage, step int, path, query string) (html stri
start := currentPage - step start := currentPage - step
end := currentPage + step end := currentPage + step
if start < 1 { if start < 1 {
start = currentPage start = 1
} }
if currentPage > 1 { if currentPage > 1 {
pp := "" pp := ""