优化查询条件解析,where中添加可以不用参数绑定的条件
This commit is contained in:
parent
4aa54b1ca7
commit
efeeb8b675
|
@ -2,18 +2,17 @@ package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
|
func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
|
||||||
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
s.WriteString("`")
|
x := slice.Map(strings.Split(ss[0], "."), func(t string) string {
|
||||||
sx := strings.Split(ss[0], ".")
|
return str.Join("`", t, "`")
|
||||||
s.WriteString(sx[0])
|
})
|
||||||
s.WriteString("`.`")
|
s.WriteString(strings.Join(x, "."))
|
||||||
s.WriteString(sx[1])
|
|
||||||
s.WriteString("`")
|
|
||||||
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
s.WriteString("`")
|
s.WriteString("`")
|
||||||
s.WriteString(ss[0])
|
s.WriteString(ss[0])
|
||||||
|
@ -26,13 +25,9 @@ func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
|
||||||
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in *[][]any) (t bool) {
|
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in *[][]any) (t bool) {
|
||||||
if slice.IsContained(ss[1], []string{"in", "not in"}) && len(*in) > 0 {
|
if slice.IsContained(ss[1], []string{"in", "not in"}) && len(*in) > 0 {
|
||||||
s.WriteString(" (")
|
s.WriteString(" (")
|
||||||
for _, p := range (*in)[*c] {
|
x := strings.TrimRight(strings.Repeat("?,", len((*in)[*c])), ",")
|
||||||
s.WriteString("?,")
|
s.WriteString(x)
|
||||||
*args = append(*args, p)
|
*args = append(*args, (*in)[*c]...)
|
||||||
}
|
|
||||||
sx := s.String()
|
|
||||||
s.Reset()
|
|
||||||
s.WriteString(strings.TrimRight(sx, ","))
|
|
||||||
s.WriteString(")")
|
s.WriteString(")")
|
||||||
*c++
|
*c++
|
||||||
t = true
|
t = true
|
||||||
|
@ -81,11 +76,15 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
args := make([]any, 0, len(w))
|
args := make([]any, 0, len(w))
|
||||||
c := 0
|
c := 0
|
||||||
for _, ss := range w {
|
for _, ss := range w {
|
||||||
if len(ss) == 2 {
|
switch len(ss) {
|
||||||
|
case 1:
|
||||||
|
s.WriteString(ss[0])
|
||||||
|
s.WriteString(" and ")
|
||||||
|
case 2:
|
||||||
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 {
|
case 3, 4:
|
||||||
w.parseField(ss, &s)
|
w.parseField(ss, &s)
|
||||||
s.WriteString(ss[1])
|
s.WriteString(ss[1])
|
||||||
if w.parseIn(ss, &s, &c, &args, in) {
|
if w.parseIn(ss, &s, &c, &args, in) {
|
||||||
|
@ -97,7 +96,9 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
} else if len(ss) >= 5 && len(ss)%5 == 0 {
|
}
|
||||||
|
|
||||||
|
if len(ss) >= 5 && len(ss)%5 == 0 {
|
||||||
j := len(ss) / 5
|
j := len(ss) / 5
|
||||||
for i := 0; i < j; i++ {
|
for i := 0; i < j; i++ {
|
||||||
start := i * 5
|
start := i * 5
|
||||||
|
@ -149,7 +150,7 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
func (w SqlBuilder) parseOrderBy() string {
|
func (w SqlBuilder) parseOrderBy() string {
|
||||||
s := strings.Builder{}
|
s := strings.Builder{}
|
||||||
for _, ss := range w {
|
for _, ss := range w {
|
||||||
if len(ss) == 2 && ss[0] != "" && slice.IsContained(ss[1], []string{"asc", "desc"}) {
|
if len(ss) == 2 && ss[0] != "" && slice.IsContained(strings.ToLower(ss[1]), []string{"asc", "desc"}) {
|
||||||
s.WriteString(" ")
|
s.WriteString(" ")
|
||||||
s.WriteString(ss[0])
|
s.WriteString(ss[0])
|
||||||
s.WriteString(" ")
|
s.WriteString(" ")
|
||||||
|
|
|
@ -510,7 +510,7 @@ func Test_findScanner(t *testing.T) {
|
||||||
func BenchmarkSqlxQueryXX(b *testing.B) {
|
func BenchmarkSqlxQueryXX(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
var r []options
|
var r []options
|
||||||
err := ddb.Select(&r, "select * from wp_options where option_id<100")
|
err := ddb.Select(&r, "select * from wp_options where option_id<100 and option_id>50")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -523,7 +523,7 @@ func BenchmarkScannerXX(b *testing.B) {
|
||||||
err := findScanner[options](glob, ctx, func(t options) {
|
err := findScanner[options](glob, ctx, func(t options) {
|
||||||
r = append(r, t)
|
r = append(r, t)
|
||||||
//fmt.Println(t)
|
//fmt.Println(t)
|
||||||
}, Conditions(Where(SqlBuilder{{"option_id", "<", "100", "int"}})))
|
}, Conditions(Where(SqlBuilder{{"option_id<100"}, {"option_id>50"}})))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -532,7 +532,9 @@ func BenchmarkScannerXX(b *testing.B) {
|
||||||
|
|
||||||
func BenchmarkFindsXX(b *testing.B) {
|
func BenchmarkFindsXX(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_, err := finds[options](glob, ctx, Conditions(Where(SqlBuilder{{"option_id", "<", "100", "int"}})))
|
_, err := finds[options](glob, ctx, Conditions(
|
||||||
|
Where(SqlBuilder{{"option_id<100"}, {"option_id>50"}})),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user