Merge pull request #576 from kiwih/master

Add basic support for multiple HAVING clauses.
This commit is contained in:
Jinzhu 2015-08-01 10:00:05 +08:00
commit bee1c8d119
2 changed files with 35 additions and 21 deletions

View File

@ -265,10 +265,24 @@ func (scope *Scope) groupSql() string {
} }
func (scope *Scope) havingSql() string { func (scope *Scope) havingSql() string {
if scope.Search.havingCondition == nil { if scope.Search.havingConditions == nil {
return "" return ""
} }
return " HAVING " + scope.buildWhereCondition(scope.Search.havingCondition)
var andConditions []string
for _, clause := range scope.Search.havingConditions {
if sql := scope.buildWhereCondition(clause); sql != "" {
andConditions = append(andConditions, sql)
}
}
combinedSql := strings.Join(andConditions, " AND ")
if len(combinedSql) == 0 {
return ""
}
return " HAVING " + combinedSql
} }
func (scope *Scope) joinsSql() string { func (scope *Scope) joinsSql() string {

View File

@ -3,24 +3,24 @@ package gorm
import "fmt" import "fmt"
type search struct { type search struct {
db *DB db *DB
whereConditions []map[string]interface{} whereConditions []map[string]interface{}
orConditions []map[string]interface{} orConditions []map[string]interface{}
notConditions []map[string]interface{} notConditions []map[string]interface{}
havingCondition map[string]interface{} havingConditions []map[string]interface{}
initAttrs []interface{} initAttrs []interface{}
assignAttrs []interface{} assignAttrs []interface{}
selects map[string]interface{} selects map[string]interface{}
omits []string omits []string
orders []string orders []string
joins string joins string
preload []searchPreload preload []searchPreload
offset string offset string
limit string limit string
group string group string
tableName string tableName string
raw bool raw bool
Unscoped bool Unscoped bool
} }
type searchPreload struct { type searchPreload struct {
@ -93,7 +93,7 @@ func (s *search) Group(query string) *search {
} }
func (s *search) Having(query string, values ...interface{}) *search { func (s *search) Having(query string, values ...interface{}) *search {
s.havingCondition = map[string]interface{}{"query": query, "args": values} s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": query, "args": values})
return s return s
} }