Skip to content

Commit

Permalink
Don't include Order for counting sql
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Aug 5, 2015
1 parent 6b8f2fc commit d21eed4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
20 changes: 12 additions & 8 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,19 @@ func (scope *Scope) whereSql() (sql string) {
return
}

var hasCountRegexp = regexp.MustCompile(`(?i)count(.+)`)

func (scope *Scope) selectSql() string {
if len(scope.Search.selects) == 0 {
return "*"
}
sql := scope.buildSelectQuery(scope.Search.selects)
scope.Search.countingQuery = hasCountRegexp.MatchString(sql)
return scope.buildSelectQuery(scope.Search.selects)
}

func (scope *Scope) orderSql() string {
if len(scope.Search.orders) == 0 {
if len(scope.Search.orders) == 0 || scope.Search.countingQuery {
return ""
}
return " ORDER BY " + strings.Join(scope.Search.orders, ",")
Expand Down Expand Up @@ -466,14 +470,14 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
}

/**
Return the table options string or an empty string if the table options does not exist
Return the table options string or an empty string if the table options does not exist
*/
func (scope *Scope) getTableOptions() string{
tableOptions, ok := scope.Get("gorm:table_options")
if !ok {
return ""
}
return tableOptions.(string)
func (scope *Scope) getTableOptions() string {
tableOptions, ok := scope.Get("gorm:table_options")
if !ok {
return ""
}
return tableOptions.(string)
}

func (scope *Scope) createJoinTable(field *StructField) {
Expand Down
1 change: 1 addition & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type search struct {
tableName string
raw bool
Unscoped bool
countingQuery bool
}

type searchPreload struct {
Expand Down

0 comments on commit d21eed4

Please sign in to comment.