Skip to content

Commit 21d3cb6

Browse files
committed
添加Group 和 Having支持
1 parent 2f55152 commit 21d3cb6

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

sqlbuilder/builder.go

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ func NewQuery(querySQL string) *Query {
1717

1818
// Query 查询mysql数据库
1919
type Query struct {
20-
query string
21-
whereStmt []string
22-
whereArgs []interface{}
23-
limitStmt string
24-
limitArgs []interface{}
25-
order string
20+
query string
21+
whereStmt []string
22+
whereArgs []interface{}
23+
limitStmt string
24+
limitArgs []interface{}
25+
order string
26+
groupBy string
27+
havingStmt []string
28+
havingArgs []interface{}
2629
}
2730

2831
// Where 添加参数
@@ -39,7 +42,21 @@ func (q *Query) WithWhere(stmts []string, args []interface{}) *Query {
3942
return q
4043
}
4144

42-
// Limit 这周Limit
45+
// Having 添加参数
46+
func (q *Query) Having(stmt string, v ...interface{}) *Query {
47+
q.havingStmt = append(q.havingStmt, stmt)
48+
q.havingArgs = append(q.havingArgs, v...)
49+
return q
50+
}
51+
52+
// WithHaving 携带条件
53+
func (q *Query) WithHaving(stmts []string, args []interface{}) *Query {
54+
q.havingStmt = append(q.havingStmt, stmts...)
55+
q.havingArgs = append(q.havingArgs, args...)
56+
return q
57+
}
58+
59+
// Limit Limit
4360
func (q *Query) Limit(offset int64, limit uint) *Query {
4461
q.limitStmt = "LIMIT ?,? "
4562
q.limitArgs = append(q.limitArgs, offset, limit)
@@ -58,7 +75,17 @@ func (q *Query) Desc() *Query {
5875
return q
5976
}
6077

78+
// GroupBy todo
79+
func (q *Query) GroupBy(d string) *Query {
80+
q.groupBy = fmt.Sprintf("GROUP BY %s ", strings.TrimSpace(d))
81+
return q
82+
}
83+
6184
func (q *Query) whereBuild() string {
85+
if len(q.whereStmt) == 0 {
86+
return ""
87+
}
88+
6289
return "WHERE " + strings.Join(q.whereStmt, " AND ") + " "
6390
}
6491

@@ -72,9 +99,27 @@ func (q *Query) WhereStmt() []string {
7299
return q.whereStmt
73100
}
74101

102+
func (q *Query) havingBuild() string {
103+
if len(q.havingStmt) == 0 {
104+
return ""
105+
}
106+
107+
return "HAVING " + strings.Join(q.havingStmt, " AND ") + " "
108+
}
109+
110+
// HavingArgs where 语句的参数
111+
func (q *Query) HavingArgs() []interface{} {
112+
return q.havingArgs
113+
}
114+
115+
// HavingStmt where条件列表
116+
func (q *Query) HavingStmt() []string {
117+
return q.havingStmt
118+
}
119+
75120
// Build 组件SQL
76121
func (q *Query) Build() (stmt string, args []interface{}) {
77-
stmt = q.query + " " + q.whereBuild() + q.order + q.limitStmt + ";"
122+
stmt = q.query + " " + q.whereBuild() + q.groupBy + q.havingBuild() + q.order + q.limitStmt + ";"
78123

79124
args = append(args, q.whereArgs...)
80125
args = append(args, q.limitArgs...)

sqlbuilder/builder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func TestQueryBuild(t *testing.T) {
1111
should := assert.New(t)
1212
q := sqlbuilder.NewQuery("SELECT * FROM t")
13-
qstmt, args := q.Where("t.a = ? AND t.c = ? AND t.d LIKE ?", "one", "two", "three").Order("t.create_at").Desc().Limit(0, 20).Build()
14-
should.Equal(qstmt, "SELECT * FROM t WHERE t.a = ? AND t.c = ? AND t.d LIKE ? ORDER BY t.create_at DESC LIMIT ?,? ;")
13+
qstmt, args := q.Where("t.a = ? AND t.c = ? AND t.d LIKE ?", "one", "two", "three").GroupBy("t.group").Having("MAX(t.salary) > ?", 10).Order("t.create_at").Desc().Limit(0, 20).Build()
14+
should.Equal(qstmt, "SELECT * FROM t WHERE t.a = ? AND t.c = ? AND t.d LIKE ? GROUP BY t.group HAVING MAX(t.salary) > ? ORDER BY t.create_at DESC LIMIT ?,? ;")
1515
should.Equal(args, []interface{}{"one", "two", "three", int64(0), uint(20)})
1616
should.Equal(q.WhereStmt(), []string{"t.a = ? AND t.c = ? AND t.d LIKE ?"})
1717
should.Equal(q.WhereArgs(), []interface{}{"one", "two", "three"})

0 commit comments

Comments
 (0)