GroupBy and Having work now

This commit is contained in:
Aaron L 2016-08-08 00:28:01 -07:00
parent d999c7b4d7
commit f6d113a5ef
3 changed files with 14 additions and 0 deletions

1
boil/_fixtures/07.sql Normal file
View file

@ -0,0 +1 @@
SELECT * FROM "a" GROUP BY id,name HAVING id <> 1,length(name, 'utf8') > 5;

View file

@ -73,6 +73,14 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
where, args := whereClause(q) where, args := whereClause(q)
buf.WriteString(where) buf.WriteString(where)
if len(q.groupBy) != 0 {
fmt.Fprintf(buf, " GROUP BY %s", strings.Join(q.groupBy, ","))
}
if len(q.having) != 0 {
fmt.Fprintf(buf, " HAVING %s", strings.Join(q.having, ","))
}
if len(q.orderBy) != 0 { if len(q.orderBy) != 0 {
buf.WriteString(" ORDER BY ") buf.WriteString(" ORDER BY ")
buf.WriteString(strings.Join(q.orderBy, `,`)) buf.WriteString(strings.Join(q.orderBy, `,`))

View file

@ -39,6 +39,11 @@ func TestBuildQuery(t *testing.T) {
from: []string{"happiness as a"}, from: []string{"happiness as a"},
joins: []join{{clause: "rainbows r on a.id = r.happy_id"}}, joins: []join{{clause: "rainbows r on a.id = r.happy_id"}},
}, nil}, }, nil},
{&Query{
from: []string{"a"},
groupBy: []string{"id", "name"},
having: []string{"id <> 1", "length(name, 'utf8') > 5"},
}, nil},
} }
for i, test := range tests { for i, test := range tests {