1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 17:10:54 +00:00

Merge pull request #4173 from AllenX2018/fix-bug-queryRow

Fix issue 3866
This commit is contained in:
Ming Deng 2020-08-20 22:25:37 +08:00 committed by GitHub
commit f3be6dd2e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 11 deletions

View File

@ -383,19 +383,33 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
} }
} }
} else { } else {
for i := 0; i < ind.NumField(); i++ { // define recursive function
f := ind.Field(i) var recursiveSetField func(rv reflect.Value)
fe := ind.Type().Field(i) recursiveSetField = func(rv reflect.Value) {
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName)) for i := 0; i < rv.NumField(); i++ {
var col string f := rv.Field(i)
if col = tags["column"]; col == "" { fe := rv.Type().Field(i)
col = nameStrategyMap[nameStrategy](fe.Name)
} // check if the field is a Struct
if v, ok := columnsMp[col]; ok { // recursive the Struct type
value := reflect.ValueOf(v).Elem().Interface() if fe.Type.Kind() == reflect.Struct {
o.setFieldValue(f, value) recursiveSetField(f)
}
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
var col string
if col = tags["column"]; col == "" {
col = nameStrategyMap[nameStrategy](fe.Name)
}
if v, ok := columnsMp[col]; ok {
value := reflect.ValueOf(v).Elem().Interface()
o.setFieldValue(f, value)
}
} }
} }
// init call the recursive function
recursiveSetField(ind)
} }
} else { } else {

View File

@ -1742,6 +1742,24 @@ func TestRawQueryRow(t *testing.T) {
throwFail(t, AssertIs(*status, 3)) throwFail(t, AssertIs(*status, 3))
throwFail(t, AssertIs(pid, nil)) throwFail(t, AssertIs(pid, nil))
type Embeded struct {
Email string
}
type queryRowNoModelTest struct {
Id int
EmbedField Embeded
}
cols = []string{
"id", "email",
}
var row queryRowNoModelTest
query = fmt.Sprintf("SELECT %s%s%s FROM %suser%s WHERE id = ?", Q, strings.Join(cols, sep), Q, Q, Q)
err = dORM.Raw(query, 4).QueryRow(&row)
throwFail(t, err)
throwFail(t, AssertIs(row.Id, 4))
throwFail(t, AssertIs(row.EmbedField.Email, "nobody@gmail.com"))
// test for sql.Null* fields // test for sql.Null* fields
nData := &DataNull{ nData := &DataNull{
NullString: sql.NullString{String: "test sql.null", Valid: true}, NullString: sql.NullString{String: "test sql.null", Valid: true},