Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

Commit 653620e

Browse files
committed
feat(condition): update
1 parent bf2f553 commit 653620e

File tree

4 files changed

+35
-181
lines changed

4 files changed

+35
-181
lines changed

condition/chain.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package condition
22

33
import (
44
"github.com/eddieowens/opts"
5+
"github.com/huandu/go-sqlbuilder"
56
)
67

78
type Chain struct {
@@ -65,17 +66,6 @@ func (c Chain) addChain(field string, operator Operator, value any, op ...opts.O
6566
return c
6667
}
6768

68-
func (c Chain) addChainWithNestedCondition(operator Operator, nestedConditions []Condition, op ...opts.Opt[ChainOperatorOpts]) Chain {
69-
o := opts.DefaultApply(op...)
70-
c.conditions = append(c.conditions, Condition{
71-
Operator: operator,
72-
NestedCondition: nestedConditions,
73-
Skip: o.Skip,
74-
SkipFunc: o.SkipFunc,
75-
})
76-
return c
77-
}
78-
7969
func (c Chain) Equal(field string, value any, op ...opts.Opt[ChainOperatorOpts]) Chain {
8070
return c.addChain(field, Equal, value, op...)
8171
}
@@ -154,9 +144,16 @@ func (c Chain) GroupBy(value any, op ...opts.Opt[ChainOperatorOpts]) Chain {
154144
return c.addChain("", GroupBy, value, op...)
155145
}
156146

157-
// Having construct Having clause with own conditions
158-
func (c Chain) Having(conditions []Condition, op ...opts.Opt[ChainOperatorOpts]) Chain {
159-
return c.addChainWithNestedCondition(Having, conditions, op...)
147+
func (c Chain) Join(option sqlbuilder.JoinOption, table string, onExpr ...string) Chain {
148+
c.conditions = append(c.conditions, Condition{
149+
Operator: Join,
150+
JoinCondition: JoinCondition{
151+
Table: table,
152+
OnExpr: onExpr,
153+
Option: option,
154+
},
155+
})
156+
return c
160157
}
161158

162159
func (c Chain) Build() []Condition {

condition/chain_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,16 @@ func TestChain2(t *testing.T) {
3838
fmt.Println(args)
3939
fmt.Println(builder.String())
4040
}
41+
42+
func TestChainJoin(t *testing.T) {
43+
sb := sqlbuilder.NewSelectBuilder().Select("user.name", "user.age").From("user")
44+
chain := NewChain()
45+
conds := chain.
46+
Equal("user.field", "value2").
47+
Join(sqlbuilder.InnerJoin, "user_info", "user.id = user_info.user_id").
48+
Build()
49+
builder := Select(*sb, conds...)
50+
sql, args := builder.Build()
51+
fmt.Println(sql)
52+
fmt.Println(args)
53+
}

condition/condition.go

Lines changed: 11 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const (
3232
NotBetween Operator = "NOT BETWEEN"
3333
OrderBy Operator = "ORDER BY"
3434
GroupBy Operator = "GROUP BY"
35-
Having Operator = "HAVING"
35+
Join Operator = "JOIN"
3636
)
3737

3838
type Condition struct {
@@ -59,8 +59,14 @@ type Condition struct {
5959
// ValueFunc The priority is higher than Value.
6060
ValueFunc func() any
6161

62-
// NestedCondition Only support for having clause
63-
NestedCondition []Condition
62+
// JoinCondition
63+
JoinCondition
64+
}
65+
66+
type JoinCondition struct {
67+
Option sqlbuilder.JoinOption
68+
Table string
69+
OnExpr []string
6470
}
6571

6672
func New(conditions ...Condition) []Condition {
@@ -99,70 +105,6 @@ func buildExpr(cond *sqlbuilder.Cond, field string, operator Operator, value any
99105
return ""
100106
}
101107

102-
func buildExprForSelectBuilder(sb *sqlbuilder.SelectBuilder, field string, operator Operator, value any) string {
103-
switch operator {
104-
case Equal:
105-
return sb.Equal(field, value)
106-
case NotEqual:
107-
return sb.NotEqual(field, value)
108-
case GreaterThan:
109-
return sb.GreaterThan(field, value)
110-
case LessThan:
111-
return sb.LessThan(field, value)
112-
case GreaterEqualThan:
113-
return sb.GreaterEqualThan(field, value)
114-
case LessEqualThan:
115-
return sb.LessEqualThan(field, value)
116-
case In:
117-
return sb.In(field, castx.ToSlice(value)...)
118-
case NotIn:
119-
return sb.NotIn(field, castx.ToSlice(value)...)
120-
case Like:
121-
return sb.Like(field, value)
122-
case NotLike:
123-
return sb.NotLike(field, value)
124-
case Between:
125-
v := castx.ToSlice(value)
126-
return sb.Between(field, v[0], v[1])
127-
case NotBetween:
128-
v := castx.ToSlice(value)
129-
return sb.NotBetween(field, v[0], v[1])
130-
}
131-
return ""
132-
}
133-
134-
func havingClause(sb *sqlbuilder.SelectBuilder, conditions ...Condition) []string {
135-
var clauses []string
136-
for _, c := range conditions {
137-
if c.SkipFunc != nil {
138-
c.Skip = c.SkipFunc()
139-
}
140-
if c.Skip {
141-
continue
142-
}
143-
if c.Or {
144-
if c.OrValuesFunc != nil {
145-
c.OrValues = c.OrValuesFunc()
146-
}
147-
var expr []string
148-
for i, field := range c.OrFields {
149-
if or := buildExprForSelectBuilder(sb, field, c.OrOperators[i], c.OrValues[i]); or != "" {
150-
expr = append(expr, or)
151-
}
152-
}
153-
if len(expr) > 0 {
154-
clauses = append(clauses, sb.Or(expr...))
155-
}
156-
} else {
157-
if c.ValueFunc != nil {
158-
c.Value = c.ValueFunc()
159-
}
160-
clauses = append(clauses, buildExprForSelectBuilder(sb, c.Field, c.Operator, c.Value))
161-
}
162-
}
163-
return clauses
164-
}
165-
166108
func whereClause(conditions ...Condition) *sqlbuilder.WhereClause {
167109
clause := sqlbuilder.NewWhereClause()
168110
cond := sqlbuilder.NewCond()
@@ -220,11 +162,8 @@ func Select(sb sqlbuilder.SelectBuilder, conditions ...Condition) sqlbuilder.Sel
220162
sb.OrderBy(cast.ToStringSlice(castx.ToSlice(c.Value))...)
221163
case GroupBy:
222164
sb.GroupBy(cast.ToStringSlice(castx.ToSlice(c.Value))...)
223-
case Having:
224-
subClause := havingClause(&sb, c.NestedCondition...)
225-
if len(subClause) > 0 {
226-
sb.Having(subClause...)
227-
}
165+
case Join:
166+
sb.JoinWithOption(c.JoinCondition.Option, c.JoinCondition.Table, cast.ToStringSlice(castx.ToSlice(c.JoinCondition.OnExpr))...)
228167
}
229168
}
230169
if clause != nil {

condition/condition_test.go

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7-
"github.com/huandu/go-assert"
87
"github.com/huandu/go-sqlbuilder"
9-
"github.com/zeromicro/go-zero/core/stores/sqlx"
108
)
119

1210
func TestSelectWithCondition(t *testing.T) {
@@ -137,96 +135,3 @@ func TestWhereClause(t *testing.T) {
137135
fmt.Println(statement)
138136
fmt.Println(args)
139137
}
140-
141-
func TestGroupBySelect(t *testing.T) {
142-
sqlbuilder.DefaultFlavor = sqlbuilder.MySQL
143-
144-
var values []any
145-
values = append(values, []int{24, 48}, []int{170, 175})
146-
147-
cds := New(Condition{
148-
Field: "name",
149-
Operator: Equal,
150-
Value: "jaronnie",
151-
}, Condition{
152-
Field: "money",
153-
Operator: GreaterEqualThan,
154-
Value: 100000,
155-
}, Condition{
156-
Or: true,
157-
OrFields: []string{"age", "height"},
158-
OrOperators: []Operator{Between, Between},
159-
OrValues: values,
160-
}, Condition{
161-
Operator: GroupBy,
162-
Value: []string{"class", "subject"},
163-
}, Condition{
164-
Operator: Having,
165-
NestedCondition: []Condition{
166-
{
167-
Field: "classNum",
168-
Operator: GreaterThan,
169-
Value: 1,
170-
},
171-
{
172-
Or: true,
173-
OrFields: []string{"subjectNum", "subjectNum"},
174-
OrOperators: []Operator{LessThan, GreaterThan},
175-
OrValues: []any{10, 20},
176-
},
177-
},
178-
})
179-
180-
sb := sqlbuilder.NewSelectBuilder().Select("name", "age", "height", "COUNT(class) as classNum", "COUNT(subject) as subjectNum").From("user")
181-
builder := Select(*sb, cds...)
182-
183-
sql, args := builder.Build()
184-
fmt.Println(sql)
185-
fmt.Println(args)
186-
assert.Equal(t, len(args), 9)
187-
}
188-
189-
func TestGroupBySelectInAdmin(t *testing.T) {
190-
conn := sqlx.NewMysql("root:123456@tcp(localhost:3306)/jzeroadmin?charset=utf8mb4&parseTime=True&loc=Local")
191-
sqlbuilder.DefaultFlavor = sqlbuilder.MySQL
192-
193-
cds := New(Condition{
194-
Field: "menu_id",
195-
Operator: GreaterThan,
196-
Value: 5,
197-
}, Condition{
198-
Operator: GroupBy,
199-
Value: []string{"role_id"},
200-
}, Condition{
201-
Operator: Having,
202-
NestedCondition: []Condition{
203-
{
204-
Or: true,
205-
OrFields: []string{"menuNum", "menuNum"},
206-
OrOperators: []Operator{LessThan, GreaterThan},
207-
OrValues: []any{10, 20},
208-
},
209-
},
210-
})
211-
212-
sb := sqlbuilder.NewSelectBuilder().Select("role_id", "COUNT(id) as menuNum").From("manage_role_menu")
213-
builder := Select(*sb, cds...)
214-
215-
sql, args := builder.Build()
216-
assert.Equal(t, len(args), 3)
217-
fmt.Println(sql)
218-
fmt.Println(args)
219-
220-
type Res struct {
221-
A int `db:"role_id"`
222-
B int `db:"menuNum"`
223-
}
224-
225-
var res []Res
226-
err := conn.QueryRowsPartial(&res, sql, args...)
227-
if err != nil {
228-
fmt.Println(err)
229-
return
230-
}
231-
fmt.Println(res)
232-
}

0 commit comments

Comments
 (0)