Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
muidea committed Oct 7, 2023
1 parent 47efddb commit fa4def5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
31 changes: 31 additions & 0 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func TestBuilderCommon(t *testing.T) {
return
}

filter, err := localProvider.GetEntityFilter(unit)
if err != nil {
t.Errorf("GetEntityFilter failed, err:%s", err.Error())
return
}

builder := NewBuilder(info, localProvider, "abc")
if builder == nil {
t.Error("new Builder failed")
Expand Down Expand Up @@ -100,6 +106,11 @@ func TestBuilderCommon(t *testing.T) {
return
}

err = filter.Above("value", 12)
if err != nil {
t.Errorf("filter.Above failed, err:%s", err.Error())
return
}
str, err = builder.BuildQuery(nil)
if err != nil {
t.Errorf("build query failed, err:%s", err.Error())
Expand All @@ -110,6 +121,16 @@ func TestBuilderCommon(t *testing.T) {
return
}

str, err = builder.BuildQuery(filter)
if err != nil {
t.Errorf("build query failed, err:%s", err.Error())
return
}
if str != "SELECT `uid`,`name`,`value`,`ts` FROM `abc_Unit` WHERE `value` > 12" {
t.Errorf("build query failed, str:%s", str)
return
}

str, err = builder.BuildCount(nil)
if err != nil {
t.Errorf("build count failed, err:%s", err.Error())
Expand All @@ -120,6 +141,16 @@ func TestBuilderCommon(t *testing.T) {
return
}

str, err = builder.BuildCount(filter)
if err != nil {
t.Errorf("build count failed, err:%s", err.Error())
return
}
if str != "SELECT COUNT(`uid`) FROM `abc_Unit` WHERE `value` > 12" {
t.Errorf("build count failed, str:%s", str)
return
}

return
}

Expand Down
6 changes: 5 additions & 1 deletion provider/local/codec/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import (
)

// encodeFloat get float value str
func (s *impl) encodeFloat(vVal model.Value, vType model.Type) (ret interface{}, err error) {
func (s *impl) encodeFloat(vVal model.Value, _ model.Type) (ret interface{}, err error) {
val := reflect.Indirect(vVal.Get().(reflect.Value))
switch val.Kind() {
case reflect.Float32, reflect.Float64:
ret = val.Float()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ret = val.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
ret = val.Uint()
default:
err = fmt.Errorf("encodeFloat failed, illegal float value, type:%s", val.Type().String())
}
Expand Down
8 changes: 6 additions & 2 deletions provider/local/codec/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"reflect"
)

func (s *impl) encodeInt(vVal model.Value, vType model.Type) (ret interface{}, err error) {
func (s *impl) encodeInt(vVal model.Value, _ model.Type) (ret interface{}, err error) {
val := reflect.Indirect(vVal.Get().(reflect.Value))
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ret = val.Int()
case reflect.Float32, reflect.Float64:
ret = int64(val.Float())
default:
err = fmt.Errorf("encodeInt failed, illegal int value, type:%s", val.Type().String())
}
Expand All @@ -25,11 +27,13 @@ func (s *impl) decodeInt(val interface{}, vType model.Type) (ret model.Value, er
}

// encodeUint get uint value str
func (s *impl) encodeUint(vVal model.Value, vType model.Type) (ret interface{}, err error) {
func (s *impl) encodeUint(vVal model.Value, _ model.Type) (ret interface{}, err error) {
val := reflect.Indirect(vVal.Get().(reflect.Value))
switch val.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
ret = val.Uint()
case reflect.Float32, reflect.Float64:
ret = uint64(val.Float())
default:
err = fmt.Errorf("encodeUint failed, illegal uint value, type:%s", val.Type().String())
}
Expand Down
8 changes: 7 additions & 1 deletion provider/remote/codec/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package codec

import (
"fmt"
"reflect"

"github.com/muidea/magicOrm/model"
)

// encodeFloat get float value str
func (s *impl) encodeFloat(vVal model.Value, vType model.Type) (ret interface{}, err error) {
func (s *impl) encodeFloat(vVal model.Value, _ model.Type) (ret interface{}, err error) {
switch vVal.Get().(type) {
case float32:
ret = vVal.Get().(float32)
case float64:
ret = vVal.Get().(float64)
case int8, int16, int32, int, int64:
ret = reflect.ValueOf(vVal.Get()).Int()
case uint8, uint16, uint32, uint, uint64:
ret = reflect.ValueOf(vVal.Get()).Uint()
default:
err = fmt.Errorf("encodeFloat failed, illegal float value, value:%v", vVal.Get())
}
Expand Down
8 changes: 6 additions & 2 deletions provider/remote/codec/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"reflect"
)

func (s *impl) encodeInt(vVal model.Value, vType model.Type) (ret interface{}, err error) {
func (s *impl) encodeInt(vVal model.Value, _ model.Type) (ret interface{}, err error) {
switch vVal.Get().(type) {
case int8, int16, int32, int, int64:
ret = reflect.ValueOf(vVal.Get()).Int()
case uint8, uint16, uint32, uint, uint64:
ret = reflect.ValueOf(vVal.Get()).Uint()
case float32, float64:
ret = int64(reflect.ValueOf(vVal.Get()).Float())
default:
Expand All @@ -26,10 +28,12 @@ func (s *impl) decodeInt(val interface{}, vType model.Type) (ret model.Value, er
}

// encodeUint get uint value str
func (s *impl) encodeUint(vVal model.Value, vType model.Type) (ret interface{}, err error) {
func (s *impl) encodeUint(vVal model.Value, _ model.Type) (ret interface{}, err error) {
switch vVal.Get().(type) {
case uint8, uint16, uint32, uint, uint64:
ret = reflect.ValueOf(vVal.Get()).Uint()
case int8, int16, int32, int, int64:
ret = reflect.ValueOf(vVal.Get()).Int()
case float32, float64:
ret = uint64(reflect.ValueOf(vVal.Get()).Float())
default:
Expand Down

0 comments on commit fa4def5

Please sign in to comment.