diff --git a/orm/db_tables.go b/orm/db_tables.go index afed3c96..ddaabf6a 100644 --- a/orm/db_tables.go +++ b/orm/db_tables.go @@ -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 diff --git a/orm/orm_queryset.go b/orm/orm_queryset.go index 61adaff8..449587ca 100644 --- a/orm/orm_queryset.go +++ b/orm/orm_queryset.go @@ -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(" 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]) } diff --git a/orm/types.go b/orm/types.go index 6a503511..7ed5e3a6 100644 --- a/orm/types.go +++ b/orm/types.go @@ -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 diff --git a/orm/utils.go b/orm/utils.go index e73d50f6..7f45782c 100644 --- a/orm/utils.go +++ b/orm/utils.go @@ -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