1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-18 11:34:13 +00:00

orm make offset simple, can use any numeric type

This commit is contained in:
slene 2013-08-16 20:01:18 +08:00
parent 0372b817d1
commit f02b286ad4
2 changed files with 19 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package orm
import (
"fmt"
"reflect"
)
type querySet struct {
@ -33,16 +34,28 @@ func (o querySet) Exclude(expr string, args ...interface{}) QuerySeter {
return &o
}
func (o querySet) Limit(limit int, args ...int64) QuerySeter {
func (o *querySet) setOffset(num interface{}) {
val := reflect.ValueOf(num)
switch num.(type) {
case int, int8, int16, int32, int64:
o.offset = val.Int()
case uint, uint8, uint16, uint32, uint64:
o.offset = int64(val.Uint())
default:
panic(fmt.Errorf("<QuerySeter> offset value need numeric not `%T`", num))
}
}
func (o querySet) Limit(limit int, args ...interface{}) QuerySeter {
o.limit = limit
if len(args) > 0 {
o.offset = args[0]
o.setOffset(args[0])
}
return &o
}
func (o querySet) Offset(offset int64) QuerySeter {
o.offset = offset
func (o querySet) Offset(offset interface{}) QuerySeter {
o.setOffset(offset)
return &o
}

View File

@ -45,8 +45,8 @@ type QuerySeter interface {
Filter(string, ...interface{}) QuerySeter
Exclude(string, ...interface{}) QuerySeter
SetCond(*Condition) QuerySeter
Limit(int, ...int64) QuerySeter
Offset(int64) QuerySeter
Limit(int, ...interface{}) QuerySeter
Offset(interface{}) QuerySeter
OrderBy(...string) QuerySeter
RelatedSel(...interface{}) QuerySeter
Count() (int64, error)