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
}
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 {
limit = DefaultRowsLimit
limit = int64(DefaultRowsLimit)
}
if limit < 0 {
// no limit

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package orm
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
@ -133,6 +134,19 @@ func ToStr(value interface{}, args ...int) (s string) {
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 {
data := make([]byte, 0, len(s)*2)
j := false