From 7442919f5ace987ea5cc2970ca82905698662290 Mon Sep 17 00:00:00 2001 From: AllenX2018 Date: Fri, 14 Aug 2020 10:57:25 +0800 Subject: [PATCH] fix issue #3776 --- pkg/orm/models_test.go | 20 ++++++++++++++------ pkg/orm/orm_raw.go | 21 +++++++++++++++++++-- pkg/orm/orm_test.go | 15 ++++++++++++++- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/pkg/orm/models_test.go b/pkg/orm/models_test.go index b53da05b..7fba89b1 100644 --- a/pkg/orm/models_test.go +++ b/pkg/orm/models_test.go @@ -54,18 +54,24 @@ func (e *SliceStringField) FieldType() int { } func (e *SliceStringField) SetRaw(value interface{}) error { - switch d := value.(type) { - case []string: - e.Set(d) - case string: - if len(d) > 0 { - parts := strings.Split(d, ",") + f := func(str string) { + if len(str) > 0 { + parts := strings.Split(str, ",") v := make([]string, 0, len(parts)) for _, p := range parts { v = append(v, strings.TrimSpace(p)) } e.Set(v) } + } + + switch d := value.(type) { + case []string: + e.Set(d) + case string: + f(d) + case []byte: + f(string(d)) default: return fmt.Errorf(" unknown value `%v`", value) } @@ -97,6 +103,8 @@ func (e *JSONFieldTest) SetRaw(value interface{}) error { switch d := value.(type) { case string: return json.Unmarshal([]byte(d), e) + case []byte: + return json.Unmarshal(d, e) default: return fmt.Errorf(" unknown value `%v`", value) } diff --git a/pkg/orm/orm_raw.go b/pkg/orm/orm_raw.go index 2f214f93..687f7099 100644 --- a/pkg/orm/orm_raw.go +++ b/pkg/orm/orm_raw.go @@ -17,6 +17,7 @@ package orm import ( "database/sql" "fmt" + "github.com/pkg/errors" "reflect" "time" ) @@ -369,7 +370,15 @@ func (o *rawSet) QueryRow(containers ...interface{}) error { field.Set(mf) field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex) } - o.setFieldValue(field, value) + if fi.isFielder { + fd := field.Addr().Interface().(Fielder) + err := fd.SetRaw(value) + if err != nil { + return errors.Errorf("set raw error:%s", err) + } + } else { + o.setFieldValue(field, value) + } } } } else { @@ -510,7 +519,15 @@ func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) { field.Set(mf) field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex) } - o.setFieldValue(field, value) + if fi.isFielder { + fd := field.Addr().Interface().(Fielder) + err := fd.SetRaw(value) + if err != nil { + return 0, errors.Errorf("set raw error:%s", err) + } + } else { + o.setFieldValue(field, value) + } } } } else { diff --git a/pkg/orm/orm_test.go b/pkg/orm/orm_test.go index fd752a94..6e65ad7a 100644 --- a/pkg/orm/orm_test.go +++ b/pkg/orm/orm_test.go @@ -777,6 +777,20 @@ func TestCustomField(t *testing.T) { throwFailNow(t, AssertIs(user.Extra.Name, "beego")) throwFailNow(t, AssertIs(user.Extra.Data, "orm")) + + var users []User + Q := dDbBaser.TableQuote() + n, err := dORM.Raw(fmt.Sprintf("SELECT * FROM %suser%s where id=?", Q, Q), 2).QueryRows(&users) + throwFailNow(t, err) + throwFailNow(t, AssertIs(n, 1)) + throwFailNow(t, AssertIs(users[0].Extra.Name, "beego")) + throwFailNow(t, AssertIs(users[0].Extra.Data, "orm")) + + user = User{} + err = dORM.Raw(fmt.Sprintf("SELECT * FROM %suser%s where id=?", Q, Q), 2).QueryRow(&user) + throwFailNow(t, err) + throwFailNow(t, AssertIs(user.Extra.Name, "beego")) + throwFailNow(t, AssertIs(user.Extra.Data, "orm")) } func TestExpr(t *testing.T) { @@ -2543,4 +2557,3 @@ func TestStrPkInsert(t *testing.T) { throwFailNow(t, AssertIs(err, nil)) throwFailNow(t, AssertIs(vForTesting.Value, value)) } -