优化 where解析
This commit is contained in:
parent
00f788fad5
commit
9ee402af4b
|
@ -1,40 +1,28 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"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 *str.Builder) {
|
||||||
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
s.WriteString("`")
|
|
||||||
sx := strings.Split(ss[0], ".")
|
sx := strings.Split(ss[0], ".")
|
||||||
s.WriteString(sx[0])
|
s.Sprintf("`%s`.`%s`", sx[0], sx[1])
|
||||||
s.WriteString("`.`")
|
|
||||||
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.Sprintf("`%s`", ss[0])
|
||||||
s.WriteString(ss[0])
|
|
||||||
s.WriteString("`")
|
|
||||||
} else {
|
} else {
|
||||||
s.WriteString(ss[0])
|
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 {
|
if slice.IsContained(ss[1], []string{"in", "not in"}) && len(*in) > 0 {
|
||||||
s.WriteString(" (")
|
sss := strings.Repeat("?,", len((*in)[*c]))
|
||||||
for _, p := range (*in)[*c] {
|
s.Sprintf("(%s)", strings.TrimRight(sss, ","))
|
||||||
s.WriteString("?,")
|
*args = append(*args, (*in)[*c]...)
|
||||||
*args = append(*args, p)
|
|
||||||
}
|
|
||||||
sx := s.String()
|
|
||||||
s.Reset()
|
|
||||||
s.WriteString(strings.TrimRight(sx, ","))
|
|
||||||
s.WriteString(")")
|
|
||||||
*c++
|
*c++
|
||||||
t = true
|
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')
|
// {{"and","field","=","num1","int","or","field","=","num2","int"}} => where (field = num1 or field = num2')
|
||||||
func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
var s strings.Builder
|
var s = str.NewBuilder()
|
||||||
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 {
|
if len(ss) == 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 {
|
} else if len(ss) >= 3 && len(ss) < 5 {
|
||||||
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) {
|
||||||
s.WriteString(" and ")
|
s.WriteString(" and ")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -107,15 +95,14 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
if strings.Contains(st, "and ") && ss[start] == "or" {
|
if strings.Contains(st, "and ") && ss[start] == "or" {
|
||||||
st = strings.TrimRight(st, "and ")
|
st = strings.TrimRight(st, "and ")
|
||||||
s.Reset()
|
s.Reset()
|
||||||
s.WriteString(st)
|
s.Sprintf("%s %s ", st, ss[start])
|
||||||
s.WriteString(fmt.Sprintf(" %s ", ss[start]))
|
|
||||||
}
|
}
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
s.WriteString("( ")
|
s.WriteString("( ")
|
||||||
}
|
}
|
||||||
w.parseField(ss[start+1:end], &s)
|
w.parseField(ss[start+1:end], s)
|
||||||
s.WriteString(ss[start+2])
|
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 ")
|
s.WriteString(" and ")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -128,15 +115,13 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
st := s.String()
|
st := s.String()
|
||||||
st = strings.TrimRight(st, "and ")
|
st = strings.TrimRight(st, "and ")
|
||||||
s.Reset()
|
s.Reset()
|
||||||
s.WriteString(st)
|
s.Sprintf("%s ) and ", st)
|
||||||
s.WriteString(") and ")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ss := strings.TrimRight(s.String(), "and ")
|
ss := strings.TrimRight(s.String(), "and ")
|
||||||
if ss != "" {
|
if ss != "" {
|
||||||
s.Reset()
|
s.Reset()
|
||||||
s.WriteString(" where ")
|
s.Sprintf(" where %s", ss)
|
||||||
s.WriteString(ss)
|
|
||||||
ss = s.String()
|
ss = s.String()
|
||||||
}
|
}
|
||||||
if len(*in) > c {
|
if len(*in) > c {
|
||||||
|
|
|
@ -76,7 +76,10 @@ func Test_selects(t *testing.T) {
|
||||||
|
|
||||||
func BenchmarkSelectXX(b *testing.B) {
|
func BenchmarkSelectXX(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -84,7 +87,10 @@ func BenchmarkSelectXX(b *testing.B) {
|
||||||
}
|
}
|
||||||
func BenchmarkScannerXX(b *testing.B) {
|
func BenchmarkScannerXX(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -92,9 +98,17 @@ func BenchmarkScannerXX(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSqlXQueryXX(b *testing.B) {
|
func BenchmarkSqlXQueryXX(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
var r []options
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user