优化 where解析

This commit is contained in:
xing 2023-02-22 20:07:57 +08:00
parent 00f788fad5
commit 9ee402af4b
2 changed files with 35 additions and 36 deletions

View File

@ -1,40 +1,28 @@
package model
import (
"fmt"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"strconv"
"strings"
)
func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
func (w SqlBuilder) parseField(ss []string, s *str.Builder) {
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
s.WriteString("`")
sx := strings.Split(ss[0], ".")
s.WriteString(sx[0])
s.WriteString("`.`")
s.WriteString(sx[1])
s.WriteString("`")
s.Sprintf("`%s`.`%s`", sx[0], sx[1])
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
s.WriteString("`")
s.WriteString(ss[0])
s.WriteString("`")
s.Sprintf("`%s`", ss[0])
} else {
s.WriteString(ss[0])
}
}
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in *[][]any) (t bool) {
func (w SqlBuilder) parseIn(ss []string, s *str.Builder, c *int, args *[]any, in *[][]any) (t bool) {
if slice.IsContained(ss[1], []string{"in", "not 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(")")
sss := strings.Repeat("?,", len((*in)[*c]))
s.Sprintf("(%s)", strings.TrimRight(sss, ","))
*args = append(*args, (*in)[*c]...)
*c++
t = true
}
@ -78,18 +66,18 @@ func (w SqlBuilder) parseType(ss []string, args *[]any) error {
//
// {{"and","field","=","num1","int","or","field","=","num2","int"}} => where (field = num1 or field = num2')
func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
var s strings.Builder
var s = str.NewBuilder()
args := make([]any, 0, len(w))
c := 0
for _, ss := range w {
if len(ss) == 2 {
w.parseField(ss, &s)
w.parseField(ss, s)
s.WriteString("=? and ")
args = append(args, ss[1])
} else if len(ss) >= 3 && len(ss) < 5 {
w.parseField(ss, &s)
w.parseField(ss, s)
s.WriteString(ss[1])
if w.parseIn(ss, &s, &c, &args, in) {
if w.parseIn(ss, s, &c, &args, in) {
s.WriteString(" and ")
continue
}
@ -107,15 +95,14 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
if strings.Contains(st, "and ") && ss[start] == "or" {
st = strings.TrimRight(st, "and ")
s.Reset()
s.WriteString(st)
s.WriteString(fmt.Sprintf(" %s ", ss[start]))
s.Sprintf("%s %s ", st, ss[start])
}
if i == 0 {
s.WriteString("( ")
}
w.parseField(ss[start+1:end], &s)
w.parseField(ss[start+1:end], s)
s.WriteString(ss[start+2])
if w.parseIn(ss[start+1:end], &s, &c, &args, in) {
if w.parseIn(ss[start+1:end], s, &c, &args, in) {
s.WriteString(" and ")
continue
}
@ -128,15 +115,13 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
st := s.String()
st = strings.TrimRight(st, "and ")
s.Reset()
s.WriteString(st)
s.WriteString(") and ")
s.Sprintf("%s ) and ", st)
}
}
ss := strings.TrimRight(s.String(), "and ")
if ss != "" {
s.Reset()
s.WriteString(" where ")
s.WriteString(ss)
s.Sprintf(" where %s", ss)
ss = s.String()
}
if len(*in) > c {

View File

@ -76,7 +76,10 @@ func Test_selects(t *testing.T) {
func BenchmarkSelectXX(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := finds[options](anyDb[options](), ctx, Conditions())
_, err := finds[options](anyDb[options](), ctx, Conditions(
Where(SqlBuilder{{"option_id", "<", "50", "int"}}),
//In(slice.ToAnySlice(number.Range[uint64](1, 50, 1))),
))
if err != nil {
panic(err)
}
@ -84,7 +87,10 @@ func BenchmarkSelectXX(b *testing.B) {
}
func BenchmarkScannerXX(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := scanners[options](anyDb[options](), ctx, Conditions())
_, err := scanners[options](anyDb[options](), ctx, Conditions(
Where(SqlBuilder{{"option_id", "<", "50", "int"}}),
//In(slice.ToAnySlice(number.Range[uint64](1, 50, 1))),
))
if err != nil {
panic(err)
}
@ -92,9 +98,17 @@ func BenchmarkScannerXX(b *testing.B) {
}
func BenchmarkSqlXQueryXX(b *testing.B) {
for i := 0; i < b.N; i++ {
var r []options
err := sq.Select(&r, "select * from wp_options")
/*var r []options
x := number.Range[uint64](1, 50, 1)
j := strings.TrimRight(strings.Repeat("?,", 50), ",")
s := str.NewBuilder()
s.Sprintf("select * from wp_options where option_id in (%s)", j)
ss := s.String()
a := slice.ToAnySlice(x)*/
ss := "select * from wp_options where option_id < ?"
for i := 0; i < b.N; i++ {
err := sq.Select(&r, ss, 50)
if err != nil {
panic(err)
}