mirror of
https://github.com/astaxie/beego.git
synced 2024-11-21 23:10:55 +00:00
Merge pull request #4173 from AllenX2018/fix-bug-queryRow
Fix issue 3866
This commit is contained in:
commit
f3be6dd2e9
@ -383,9 +383,19 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < ind.NumField(); i++ {
|
||||
f := ind.Field(i)
|
||||
fe := ind.Type().Field(i)
|
||||
// define recursive function
|
||||
var recursiveSetField func(rv reflect.Value)
|
||||
recursiveSetField = func(rv reflect.Value) {
|
||||
for i := 0; i < rv.NumField(); i++ {
|
||||
f := rv.Field(i)
|
||||
fe := rv.Type().Field(i)
|
||||
|
||||
// check if the field is a Struct
|
||||
// recursive the Struct type
|
||||
if fe.Type.Kind() == reflect.Struct {
|
||||
recursiveSetField(f)
|
||||
}
|
||||
|
||||
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
|
||||
var col string
|
||||
if col = tags["column"]; col == "" {
|
||||
@ -398,6 +408,10 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
|
||||
}
|
||||
}
|
||||
|
||||
// init call the recursive function
|
||||
recursiveSetField(ind)
|
||||
}
|
||||
|
||||
} else {
|
||||
if err := rows.Scan(refs...); err != nil {
|
||||
return err
|
||||
|
@ -1742,6 +1742,24 @@ func TestRawQueryRow(t *testing.T) {
|
||||
throwFail(t, AssertIs(*status, 3))
|
||||
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
|
||||
nData := &DataNull{
|
||||
NullString: sql.NullString{String: "test sql.null", Valid: true},
|
||||
|
Loading…
Reference in New Issue
Block a user