Added COUNT function to select query builder

This commit is contained in:
Patrick O'brien 2016-06-10 09:43:45 +10:00
parent 7aba7104a5
commit 1e412cec74
2 changed files with 13 additions and 0 deletions

View file

@ -57,11 +57,18 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
buf.WriteString("SELECT ")
if q.count {
buf.WriteString("COUNT(")
}
if len(q.selectCols) > 0 {
buf.WriteString(strings.Join(q.selectCols, ","))
} else {
buf.WriteByte('*')
}
// close sql COUNT function
if q.count {
buf.WriteString(")")
}
buf.WriteString(" FROM ")
fmt.Fprintf(buf, `"%s"`, q.table)

View file

@ -51,6 +51,12 @@ func Test{{$tableNamePlural}}All(t *testing.T) {
}
}
// Ensure Count is valid
c, err = {{$tableNamePlural}}().Count()
if c != 3 {
t.Errorf("Expected {{.Table.Name}} table to have 3 rows, but got %d", c)
}
// Attempt to retrieve all objects
res, err := {{$tableNamePlural}}().All()
if err != nil {