From 9ee402af4bb9de643bc6d24c0a8298a60e43c37c Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 22 Feb 2023 20:07:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20where=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/parse.go | 49 ++++++++++++++++----------------------------- model/query_test.go | 22 ++++++++++++++++---- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/model/parse.go b/model/parse.go index caef825..446bbb4 100644 --- a/model/parse.go +++ b/model/parse.go @@ -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 { diff --git a/model/query_test.go b/model/query_test.go index 3affc63..432d691 100644 --- a/model/query_test.go +++ b/model/query_test.go @@ -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) { + var r []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++ { - var r []options - err := sq.Select(&r, "select * from wp_options") + err := sq.Select(&r, ss, 50) if err != nil { panic(err) }