Merge f159d05209d0018e492b9273e508c22a75523331 into 9acaa33324bbcc78239a1c913d4f1292c12177b9

This commit is contained in:
0x6875790d0a 2017-05-15 05:31:13 +00:00 committed by GitHub
commit 13a18a218e
2 changed files with 12 additions and 0 deletions

View File

@ -326,6 +326,11 @@ func (s *DB) Count(value interface{}) *DB {
return s.NewScope(s.Value).count(value).db
}
// CountDistinct get how many distinct records for a model
func (s *DB) CountDistinct(value interface{}, columnName string) *DB {
return s.NewScope(s.Value).countDistinct(value, columnName).db
}
// Related get related associations
func (s *DB) Related(value interface{}, foreignKeys ...string) *DB {
return s.clone().NewScope(s.Value).related(value, foreignKeys...).db

View File

@ -947,6 +947,13 @@ func (scope *Scope) count(value interface{}) *Scope {
return scope
}
func (scope *Scope) countDistinct(value interface{}, column string) *Scope {
scope.Search.Select(fmt.Sprintf("count(%s)", column))
scope.Search.countingQuery = true
scope.Err(scope.row().Scan(value))
return scope
}
func (scope *Scope) typeName() string {
typ := scope.IndirectValue().Type()