1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 09:20:55 +00:00
This commit is contained in:
slene 2013-11-08 22:19:01 +08:00
parent 558738ade8
commit 167ad203cb
2 changed files with 37 additions and 6 deletions

View File

@ -705,7 +705,13 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
ind.Set(mind) ind.Set(mind)
} else { } else {
if cnt == 0 { if cnt == 0 {
slice = reflect.New(ind.Type()).Elem() // you can use a empty & caped container list
// orm will not replace it
if ind.Len() != 0 {
// if container is not empty
// create a new one
slice = reflect.New(ind.Type()).Elem()
}
} }
if isPtr { if isPtr {
@ -718,8 +724,16 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
cnt++ cnt++
} }
if one == false && cnt > 0 { if one == false {
ind.Set(slice) if cnt > 0 {
ind.Set(slice)
} else {
// when a result is empty and container is nil
// to set a empty container
if ind.IsNil() {
ind.Set(reflect.MakeSlice(ind.Type(), 0, 0))
}
}
} }
return cnt, nil return cnt, nil
@ -1058,12 +1072,24 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
) )
typ := 0 typ := 0
switch container.(type) { switch v := container.(type) {
case *[]Params: case *[]Params:
d := *v
if len(d) == 0 {
maps = d
}
typ = 1 typ = 1
case *[]ParamsList: case *[]ParamsList:
d := *v
if len(d) == 0 {
lists = d
}
typ = 2 typ = 2
case *ParamsList: case *ParamsList:
d := *v
if len(d) == 0 {
list = d
}
typ = 3 typ = 3
default: default:
panic(fmt.Errorf("unsupport read values type `%T`", container)) panic(fmt.Errorf("unsupport read values type `%T`", container))

View File

@ -696,8 +696,13 @@ func TestAll(t *testing.T) {
qs = dORM.QueryTable("user") qs = dORM.QueryTable("user")
num, err = qs.Filter("user_name", "nothing").All(&users) num, err = qs.Filter("user_name", "nothing").All(&users)
throwFail(t, err) throwFailNow(t, err)
throwFail(t, AssertIs(num, 0)) throwFailNow(t, AssertIs(num, 0))
var users3 []*User
qs = dORM.QueryTable("user")
num, err = qs.Filter("user_name", "nothing").All(&users3)
throwFailNow(t, AssertIs(users3 == nil, false))
} }
func TestOne(t *testing.T) { func TestOne(t *testing.T) {