Skip to content

Commit 3933b2e

Browse files
committed
Update gq
1 parent 657e312 commit 3933b2e

File tree

5 files changed

+96
-73
lines changed

5 files changed

+96
-73
lines changed

go.sum

Whitespace-only changes.

goconv_test.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package goconv
22

33
import (
4-
"fmt"
54
"testing"
6-
7-
"github.com/baagod/goconv/strmu"
85
)
96

10-
func TestName(t *testing.T) {
11-
for i := 0; i < 10; i++ {
12-
fmt.Println(strmu.Rand(12, true))
13-
}
14-
}
7+
func TestName(t *testing.T) {}

gq/builder.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
package gq
22

3-
import "strings"
3+
import (
4+
"regexp"
5+
"strings"
6+
)
47

58
type Builder interface {
69
SQL() string
710
}
811

912
type WhereBuilder struct {
10-
List []Builder
11-
Sep string // WHERE 条件分隔符
13+
Builders []Builder
1214
}
1315

1416
func (c *WhereBuilder) SQL() (sql string) {
15-
sep := " " + c.Sep + " "
16-
for _, x := range c.List {
17-
if s := x.SQL(); s != "" {
18-
sql += s + sep
17+
re := regexp.MustCompile("^\n? +AND |\n? +AND $")
18+
for _, c := range c.Builders {
19+
s := c.SQL()
20+
if s == "" {
21+
continue
22+
}
23+
24+
if _, ok := c.(*List); ok {
25+
s += "\n"
1926
}
20-
}
2127

22-
if c.Sep == "OR" {
23-
sql = "(" + sql + ")"
28+
if strings.HasSuffix(s, "\n") {
29+
sql = re.ReplaceAllString(sql, "") + "\n AND " + s + " AND "
30+
} else {
31+
sql += s + " AND "
32+
}
2433
}
2534

26-
return strings.TrimSuffix(sql, sep)
35+
return re.ReplaceAllString(sql, "")
2736
}

gq/cond.go

+45-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
)
88

99
type Cond struct {
10-
Name string
11-
Value any
12-
Operator string
13-
isSkip bool
10+
Name string
11+
Value any
12+
Opr string
13+
isSkip bool
1414
}
1515

1616
func (c *Cond) Skip(skip bool) *Cond {
@@ -32,17 +32,17 @@ func (c *Cond) SQL() string {
3232
if c.IsSkip() {
3333
return ""
3434
}
35-
return fmt.Sprintf("%s %s %s", c.Name, c.Operator, c.StrValue())
35+
return fmt.Sprintf("%s %s %s", c.Name, c.Opr, c.StrValue())
3636
}
3737

3838
func (c *Cond) IsSkip() bool {
39-
return c.isSkip || (c.Operator == "LINK" && c.Value == "")
39+
return c.isSkip || (c.Opr == "LINK" && c.Value == "")
4040
}
4141

4242
func (c *Cond) StrValue() string {
4343
s := fmt.Sprintf("%v", c.Value)
4444

45-
switch c.Operator {
45+
switch c.Opr {
4646
case "BETWEEN":
4747
a := c.Value.([2]any)
4848
return fmt.Sprintf("%v BETWEEN %v", a[0], a[1])
@@ -61,3 +61,41 @@ func (c *Cond) StrValue() string {
6161

6262
return s
6363
}
64+
65+
// ----
66+
67+
type List struct {
68+
Builders []Builder
69+
Sep string // WHERE 条件分隔符
70+
}
71+
72+
func (l *List) Append(a ...Builder) {
73+
l.Builders = append(l.Builders, a...)
74+
}
75+
76+
func (l *List) Update(name string, value any) {
77+
for _, x := range l.Builders {
78+
if v, ok := x.(*Cond); ok {
79+
if v.Name == name {
80+
v.Value = value
81+
return
82+
}
83+
}
84+
}
85+
}
86+
87+
func (l *List) SQL() (sql string) {
88+
sep := " " + l.Sep + " "
89+
for _, x := range l.Builders {
90+
if s := x.SQL(); s != "" {
91+
sql += s + sep
92+
}
93+
}
94+
95+
sql = strings.TrimSuffix(sql, sep)
96+
if sql != "" && l.Sep == "OR" {
97+
sql = "(" + sql + ")"
98+
}
99+
100+
return sql
101+
}

gq/gq.go

+30-47
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,75 @@ package gq
22

33
import (
44
"fmt"
5-
"regexp"
65
"strings"
76
)
87

98
// Eq 等于 =
10-
func Eq(name string, v any) *Cond {
11-
return &Cond{Name: name, Value: v, Operator: "="}
9+
func Eq[T any](name string, v T, skip ...func(T) bool) *Cond {
10+
return &Cond{Name: name, Value: v, Opr: "=", isSkip: skip != nil && skip[0](v)}
1211
}
1312

1413
// Gt 大于 >
15-
func Gt(name string, v any) *Cond {
16-
return &Cond{Name: name, Value: v, Operator: ">"}
14+
func Gt[T any](name string, v T, skip ...func(T) bool) *Cond {
15+
return &Cond{Name: name, Value: v, Opr: ">", isSkip: skip != nil && skip[0](v)}
1716
}
1817

1918
// Ge 大于等于 ≥
20-
func Ge(name string, v any) *Cond {
21-
return &Cond{Name: name, Value: v, Operator: ">="}
19+
func Ge[T any](name string, v T, skip ...func(T) bool) *Cond {
20+
return &Cond{Name: name, Value: v, Opr: ">=", isSkip: skip != nil && skip[0](v)}
2221
}
2322

2423
// Lt 小于 <
25-
func Lt(name string, v any) *Cond {
26-
return &Cond{Name: name, Value: v, Operator: "<"}
24+
func Lt[T any](name string, v T, skip ...func(T) bool) *Cond {
25+
return &Cond{Name: name, Value: v, Opr: "<", isSkip: skip != nil && skip[0](v)}
2726
}
2827

2928
// Le 小于等于 ≤
30-
func Le(name string, v any) *Cond {
31-
return &Cond{Name: name, Value: v, Operator: "<="}
29+
func Le[T any](name string, v T, skip ...func(T) bool) *Cond {
30+
return &Cond{Name: name, Value: v, Opr: "<=", isSkip: skip != nil && skip[0](v)}
3231
}
3332

3433
// Ne 不等于 != <>
35-
func Ne(name string, v any) *Cond {
36-
return &Cond{Name: name, Value: v, Operator: "!="}
34+
func Ne[T any](name string, v T, skip ...func(T) bool) *Cond {
35+
return &Cond{Name: name, Value: v, Opr: "!=", isSkip: skip != nil && skip[0](v)}
3736
}
3837

3938
// Between 区间
40-
func Between(name string, first, second any) *Cond {
39+
func Between[T any](name string, first, second T, skip ...func(T, T) bool) *Cond {
4140
return &Cond{
42-
Name: name,
43-
Value: [2]any{first, second},
44-
Operator: "BETWEEN",
41+
Name: name,
42+
Value: [2]any{first, second},
43+
Opr: "BETWEEN",
44+
isSkip: skip != nil && skip[0](first, second),
4545
}
4646
}
4747

4848
// In 范围
49-
func In(name string, a ...any) *Cond {
50-
if len(a) == 1 {
49+
func In[T any](name string, a ...T) *Cond {
50+
if a != nil {
51+
// 是否切片或数组
5152
if strings.HasPrefix(fmt.Sprintf("%T", a[0]), "[") {
52-
return &Cond{Name: name, Value: a[0], Operator: "IN"}
53+
return &Cond{Name: name, Value: a[0], Opr: "IN"}
5354
}
5455
}
55-
return &Cond{Name: name, Value: a, Operator: "IN"}
56+
return &Cond{Name: name, Value: a, Opr: "IN"}
5657
}
5758

5859
// Like 模糊查询
59-
func Like(name string, v string) *Cond {
60-
return &Cond{Name: name, Value: v, Operator: "LIKE"}
60+
func Like(name string, v string, skip ...func(string) bool) *Cond {
61+
return &Cond{Name: name, Value: v, Opr: "LIKE", isSkip: skip != nil && skip[0](v)}
6162
}
6263

6364
// Or 或者条件
64-
func Or(a ...Builder) *WhereBuilder {
65-
return &WhereBuilder{List: a, Sep: "OR"}
65+
func Or(a ...Builder) *List {
66+
return &List{Builders: a, Sep: "OR"}
6667
}
6768

6869
// And 并且条件
69-
func And(a ...Builder) *WhereBuilder {
70-
return &WhereBuilder{List: a, Sep: "AND"}
70+
func And(a ...Builder) *List {
71+
return &List{Builders: a, Sep: "AND"}
7172
}
7273

73-
func Where(a ...Builder) (sql string) {
74-
re := regexp.MustCompile("^\n? +AND |\n? +AND $")
75-
for _, c := range a {
76-
s := c.SQL()
77-
if s == "" {
78-
continue
79-
}
80-
81-
if _, ok := c.(*WhereBuilder); ok {
82-
s += "\n"
83-
}
84-
85-
if strings.HasSuffix(s, "\n") {
86-
sql = re.ReplaceAllString(sql, "") + "\n AND " + s + " AND "
87-
} else {
88-
sql += s + " AND "
89-
}
90-
}
91-
92-
return re.ReplaceAllString(sql, "")
74+
func Where(a ...Builder) *WhereBuilder {
75+
return &WhereBuilder{Builders: a}
9376
}

0 commit comments

Comments
 (0)