1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:50:55 +00:00

orm support use any numeric type set QuerySeter.Limit value

This commit is contained in:
slene 2013-09-13 18:06:44 +08:00
parent 8e8d39d3cb
commit 1596aa7a34
4 changed files with 21 additions and 16 deletions

View File

@ -360,9 +360,9 @@ func (d *dbTables) getOrderSql(orders []string) (orderSql string) {
return return
} }
func (d *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int) (limits string) { func (d *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits string) {
if limit == 0 { if limit == 0 {
limit = DefaultRowsLimit limit = int64(DefaultRowsLimit)
} }
if limit < 0 { if limit < 0 {
// no limit // no limit

View File

@ -2,7 +2,6 @@ package orm
import ( import (
"fmt" "fmt"
"reflect"
) )
type querySet struct { type querySet struct {
@ -10,7 +9,7 @@ type querySet struct {
cond *Condition cond *Condition
related []string related []string
relDepth int relDepth int
limit int limit int64
offset int64 offset int64
orders []string orders []string
orm *orm orm *orm
@ -35,19 +34,11 @@ func (o querySet) Exclude(expr string, args ...interface{}) QuerySeter {
} }
func (o *querySet) setOffset(num interface{}) { func (o *querySet) setOffset(num interface{}) {
val := reflect.ValueOf(num) o.offset = ToInt64(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 { func (o querySet) Limit(limit interface{}, args ...interface{}) QuerySeter {
o.limit = limit o.limit = ToInt64(limit)
if len(args) > 0 { if len(args) > 0 {
o.setOffset(args[0]) o.setOffset(args[0])
} }

View File

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

View File

@ -2,6 +2,7 @@ package orm
import ( import (
"fmt" "fmt"
"reflect"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -133,6 +134,19 @@ func ToStr(value interface{}, args ...int) (s string) {
return s return s
} }
func ToInt64(value interface{}) (d int64) {
val := reflect.ValueOf(value)
switch value.(type) {
case int, int8, int16, int32, int64:
d = val.Int()
case uint, uint8, uint16, uint32, uint64:
d = int64(val.Uint())
default:
panic(fmt.Errorf("ToInt64 need numeric not `%T`", value))
}
return
}
func snakeString(s string) string { func snakeString(s string) string {
data := make([]byte, 0, len(s)*2) data := make([]byte, 0, len(s)*2)
j := false j := false