1596aa7a by slene

orm support use any numeric type set QuerySeter.Limit value

1 parent 8e8d39d3
...@@ -360,9 +360,9 @@ func (d *dbTables) getOrderSql(orders []string) (orderSql string) { ...@@ -360,9 +360,9 @@ func (d *dbTables) getOrderSql(orders []string) (orderSql string) {
360 return 360 return
361 } 361 }
362 362
363 func (d *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int) (limits string) { 363 func (d *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits string) {
364 if limit == 0 { 364 if limit == 0 {
365 limit = DefaultRowsLimit 365 limit = int64(DefaultRowsLimit)
366 } 366 }
367 if limit < 0 { 367 if limit < 0 {
368 // no limit 368 // no limit
......
...@@ -2,7 +2,6 @@ package orm ...@@ -2,7 +2,6 @@ package orm
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "reflect"
6 ) 5 )
7 6
8 type querySet struct { 7 type querySet struct {
...@@ -10,7 +9,7 @@ type querySet struct { ...@@ -10,7 +9,7 @@ type querySet struct {
10 cond *Condition 9 cond *Condition
11 related []string 10 related []string
12 relDepth int 11 relDepth int
13 limit int 12 limit int64
14 offset int64 13 offset int64
15 orders []string 14 orders []string
16 orm *orm 15 orm *orm
...@@ -35,19 +34,11 @@ func (o querySet) Exclude(expr string, args ...interface{}) QuerySeter { ...@@ -35,19 +34,11 @@ func (o querySet) Exclude(expr string, args ...interface{}) QuerySeter {
35 } 34 }
36 35
37 func (o *querySet) setOffset(num interface{}) { 36 func (o *querySet) setOffset(num interface{}) {
38 val := reflect.ValueOf(num) 37 o.offset = ToInt64(num)
39 switch num.(type) {
40 case int, int8, int16, int32, int64:
41 o.offset = val.Int()
42 case uint, uint8, uint16, uint32, uint64:
43 o.offset = int64(val.Uint())
44 default:
45 panic(fmt.Errorf("<QuerySeter> offset value need numeric not `%T`", num))
46 }
47 } 38 }
48 39
49 func (o querySet) Limit(limit int, args ...interface{}) QuerySeter { 40 func (o querySet) Limit(limit interface{}, args ...interface{}) QuerySeter {
50 o.limit = limit 41 o.limit = ToInt64(limit)
51 if len(args) > 0 { 42 if len(args) > 0 {
52 o.setOffset(args[0]) 43 o.setOffset(args[0])
53 } 44 }
......
...@@ -45,7 +45,7 @@ type QuerySeter interface { ...@@ -45,7 +45,7 @@ type QuerySeter interface {
45 Filter(string, ...interface{}) QuerySeter 45 Filter(string, ...interface{}) QuerySeter
46 Exclude(string, ...interface{}) QuerySeter 46 Exclude(string, ...interface{}) QuerySeter
47 SetCond(*Condition) QuerySeter 47 SetCond(*Condition) QuerySeter
48 Limit(int, ...interface{}) QuerySeter 48 Limit(interface{}, ...interface{}) QuerySeter
49 Offset(interface{}) QuerySeter 49 Offset(interface{}) QuerySeter
50 OrderBy(...string) QuerySeter 50 OrderBy(...string) QuerySeter
51 RelatedSel(...interface{}) QuerySeter 51 RelatedSel(...interface{}) QuerySeter
......
...@@ -2,6 +2,7 @@ package orm ...@@ -2,6 +2,7 @@ package orm
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "reflect"
5 "strconv" 6 "strconv"
6 "strings" 7 "strings"
7 "time" 8 "time"
...@@ -133,6 +134,19 @@ func ToStr(value interface{}, args ...int) (s string) { ...@@ -133,6 +134,19 @@ func ToStr(value interface{}, args ...int) (s string) {
133 return s 134 return s
134 } 135 }
135 136
137 func ToInt64(value interface{}) (d int64) {
138 val := reflect.ValueOf(value)
139 switch value.(type) {
140 case int, int8, int16, int32, int64:
141 d = val.Int()
142 case uint, uint8, uint16, uint32, uint64:
143 d = int64(val.Uint())
144 default:
145 panic(fmt.Errorf("ToInt64 need numeric not `%T`", value))
146 }
147 return
148 }
149
136 func snakeString(s string) string { 150 func snakeString(s string) string {
137 data := make([]byte, 0, len(s)*2) 151 data := make([]byte, 0, len(s)*2)
138 j := false 152 j := false
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!