forked from mirror/gorm
Refact First, Last to make it possible to reset table name in Plugin
This commit is contained in:
parent
39ac95adbb
commit
3af077ac46
|
@ -1,6 +1,7 @@
|
||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -21,6 +22,12 @@ func Query(scope *Scope) {
|
||||||
dest = reflect.Indirect(reflect.ValueOf(value))
|
dest = reflect.Indirect(reflect.ValueOf(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if orderBy, ok := scope.InstanceGet("gorm:order_by_primary_key"); ok {
|
||||||
|
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
|
||||||
|
scope.Search = scope.Search.clone().order(fmt.Sprintf("%v.%v %v", scope.TableName(), primaryKey, orderBy))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if dest.Kind() == reflect.Slice {
|
if dest.Kind() == reflect.Slice {
|
||||||
isSlice = true
|
isSlice = true
|
||||||
destType = dest.Type().Elem()
|
destType = dest.Type().Elem()
|
||||||
|
|
18
main.go
18
main.go
|
@ -153,23 +153,13 @@ func (s *DB) Assign(attrs ...interface{}) *DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) First(out interface{}, where ...interface{}) *DB {
|
func (s *DB) First(out interface{}, where ...interface{}) *DB {
|
||||||
scope := s.clone().NewScope(out)
|
return s.clone().Limit(1).NewScope(out).InstanceSet("gorm:order_by_primary_key", "ASC").
|
||||||
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
|
inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
||||||
scope.Search = scope.Search.clone().order(scope.TableName() + "." + primaryKey).limit(1)
|
|
||||||
} else {
|
|
||||||
scope.Search = scope.Search.clone().limit(1)
|
|
||||||
}
|
|
||||||
return scope.inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
|
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
|
||||||
scope := s.clone().NewScope(out)
|
return s.clone().Limit(1).NewScope(out).InstanceSet("gorm:order_by_primary_key", "DESC").
|
||||||
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
|
inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
||||||
scope.Search = scope.Search.clone().order(scope.TableName() + "." + primaryKey + " DESC").limit(1)
|
|
||||||
} else {
|
|
||||||
scope.Search = scope.Search.clone().limit(1)
|
|
||||||
}
|
|
||||||
return scope.inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Find(out interface{}, where ...interface{}) *DB {
|
func (s *DB) Find(out interface{}, where ...interface{}) *DB {
|
||||||
|
|
Loading…
Reference in New Issue