orm make offset simple, can use any numeric type
Showing
2 changed files
with
19 additions
and
6 deletions
| ... | @@ -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 | ) | 6 | ) |
| 6 | 7 | ||
| 7 | type querySet struct { | 8 | type querySet struct { |
| ... | @@ -33,16 +34,28 @@ func (o querySet) Exclude(expr string, args ...interface{}) QuerySeter { | ... | @@ -33,16 +34,28 @@ func (o querySet) Exclude(expr string, args ...interface{}) QuerySeter { |
| 33 | return &o | 34 | return &o |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | func (o querySet) Limit(limit int, args ...int64) QuerySeter { | 37 | func (o *querySet) setOffset(num interface{}) { |
| 38 | val := reflect.ValueOf(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 | } | ||
| 48 | |||
| 49 | func (o querySet) Limit(limit int, args ...interface{}) QuerySeter { | ||
| 37 | o.limit = limit | 50 | o.limit = limit |
| 38 | if len(args) > 0 { | 51 | if len(args) > 0 { |
| 39 | o.offset = args[0] | 52 | o.setOffset(args[0]) |
| 40 | } | 53 | } |
| 41 | return &o | 54 | return &o |
| 42 | } | 55 | } |
| 43 | 56 | ||
| 44 | func (o querySet) Offset(offset int64) QuerySeter { | 57 | func (o querySet) Offset(offset interface{}) QuerySeter { |
| 45 | o.offset = offset | 58 | o.setOffset(offset) |
| 46 | return &o | 59 | return &o |
| 47 | } | 60 | } |
| 48 | 61 | ... | ... |
| ... | @@ -45,8 +45,8 @@ type QuerySeter interface { | ... | @@ -45,8 +45,8 @@ 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, ...int64) QuerySeter | 48 | Limit(int, ...interface{}) QuerySeter |
| 49 | Offset(int64) QuerySeter | 49 | Offset(interface{}) QuerySeter |
| 50 | OrderBy(...string) QuerySeter | 50 | OrderBy(...string) QuerySeter |
| 51 | RelatedSel(...interface{}) QuerySeter | 51 | RelatedSel(...interface{}) QuerySeter |
| 52 | Count() (int64, error) | 52 | Count() (int64, error) | ... | ... |
-
Please register or sign in to post a comment