Skip to content

Commit a0344c7

Browse files
alexsantewebRat
authored andcommitted
Add support for Group By statements (#2)
Thanks!
1 parent cabef04 commit a0344c7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

select.go

+16
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type QueryStatement struct {
3939
hasRowcount bool
4040
rowcount int
4141
useRowcount bool
42+
hasGroupBy bool
43+
groupBy string
4244
}
4345

4446
// NewQueryStatement returns a new instance of the select statement
@@ -110,6 +112,15 @@ func (q *QueryStatement) Where(where string) *QueryStatement {
110112
return q
111113
}
112114

115+
// GroupBy will append a group by statement to the query
116+
func (q *QueryStatement) GroupBy(groupBy string) *QueryStatement {
117+
if len(groupBy) > 0 {
118+
q.groupBy = groupBy
119+
q.hasGroupBy = true
120+
}
121+
return q
122+
}
123+
113124
// And continues to add to the where statement
114125
func (q *QueryStatement) And(param string) *QueryStatement {
115126
if len(param) > 0 {
@@ -241,6 +252,11 @@ func (q *QueryStatement) Assemble() (string, error) {
241252
}
242253
}
243254

255+
if q.hasGroupBy {
256+
sql.WriteString(" GROUP BY ")
257+
sql.WriteString(q.groupBy)
258+
}
259+
244260
if q.hasOrderBy {
245261
sql.WriteString(" ORDER BY ")
246262
sql.WriteString(q.orderby)

0 commit comments

Comments
 (0)