From 553078d956aa97bd5e4c40303e93bba9a5624ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?maxin=5B=E9=A9=AC=E9=91=AB=5D?= Date: Mon, 11 Apr 2016 09:02:22 +0530 Subject: [PATCH 1/5] change limit 1000 to 1,reduce the amount the data --- orm/orm_queryset.go | 1 + 1 file changed, 1 insertion(+) diff --git a/orm/orm_queryset.go b/orm/orm_queryset.go index 802a1fe0..9c2e68aa 100644 --- a/orm/orm_queryset.go +++ b/orm/orm_queryset.go @@ -192,6 +192,7 @@ func (o *querySet) All(container interface{}, cols ...string) (int64, error) { // query one row data and map to containers. // cols means the columns when querying. func (o *querySet) One(container interface{}, cols ...string) error { + o.limit = 1 num, err := o.orm.alias.DbBaser.ReadBatch(o.orm.db, o, o.mi, o.cond, container, o.orm.alias.TZ, cols) if err != nil { return err From 9679f5e22a704569289c488e8b1e7573627c400a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?maxin=5B=E9=A9=AC=E9=91=AB=5D?= Date: Tue, 12 Apr 2016 21:28:29 +0800 Subject: [PATCH 2/5] reduce the data transmission --- orm/orm_queryset.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/orm/orm_queryset.go b/orm/orm_queryset.go index 9c2e68aa..87229cfb 100644 --- a/orm/orm_queryset.go +++ b/orm/orm_queryset.go @@ -192,7 +192,9 @@ func (o *querySet) All(container interface{}, cols ...string) (int64, error) { // query one row data and map to containers. // cols means the columns when querying. func (o *querySet) One(container interface{}, cols ...string) error { - o.limit = 1 + if o.limit != 1 { + o.limit = 2 + } num, err := o.orm.alias.DbBaser.ReadBatch(o.orm.db, o, o.mi, o.cond, container, o.orm.alias.TZ, cols) if err != nil { return err From 81c6c898cf8270483748a9de0951ec88dc1f9775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?maxin=5B=E9=A9=AC=E9=91=AB=5D?= Date: Wed, 13 Apr 2016 10:36:12 +0800 Subject: [PATCH 3/5] remove orm one function thorw ErrMultiRows error --- orm/orm_queryset.go | 7 +------ orm/orm_test.go | 8 +++++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/orm/orm_queryset.go b/orm/orm_queryset.go index 87229cfb..f1aa45c8 100644 --- a/orm/orm_queryset.go +++ b/orm/orm_queryset.go @@ -192,16 +192,11 @@ func (o *querySet) All(container interface{}, cols ...string) (int64, error) { // query one row data and map to containers. // cols means the columns when querying. func (o *querySet) One(container interface{}, cols ...string) error { - if o.limit != 1 { - o.limit = 2 - } + o.limit = 1 num, err := o.orm.alias.DbBaser.ReadBatch(o.orm.db, o, o.mi, o.cond, container, o.orm.alias.TZ, cols) if err != nil { return err } - if num > 1 { - return ErrMultiRows - } if num == 0 { return ErrNoRows } diff --git a/orm/orm_test.go b/orm/orm_test.go index ec0f0d3a..60f4e75a 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -969,13 +969,19 @@ func TestOne(t *testing.T) { var user User qs := dORM.QueryTable("user") err := qs.One(&user) - throwFail(t, AssertIs(err, ErrMultiRows)) + throwFail(t, err) + throwFail(t, AssertIs(user.UserName, "slene")) user = User{} err = qs.OrderBy("Id").Limit(1).One(&user) throwFailNow(t, err) throwFail(t, AssertIs(user.UserName, "slene")) + user = User{} + err = qs.OrderBy("-Id").Limit(100).One(&user) + throwFailNow(t, err) + throwFail(t, AssertIs(user.UserName, "nobody")) + err = qs.Filter("user_name", "nothing").One(&user) throwFail(t, AssertIs(err, ErrNoRows)) From 9ce6dc4cdfa094fe38808d474cc67531d1c4d235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?maxin=5B=E9=A9=AC=E9=91=AB=5D?= Date: Wed, 13 Apr 2016 21:04:46 +0800 Subject: [PATCH 4/5] remove test ; because rows are returned in an unspecified order --- orm/orm_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/orm/orm_test.go b/orm/orm_test.go index 60f4e75a..ff7d2eae 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -970,7 +970,6 @@ func TestOne(t *testing.T) { qs := dORM.QueryTable("user") err := qs.One(&user) throwFail(t, err) - throwFail(t, AssertIs(user.UserName, "slene")) user = User{} err = qs.OrderBy("Id").Limit(1).One(&user) From 56dc9bf6220381efb05864c544d252a646677a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?maxin=5B=E9=A9=AC=E9=91=AB=5D?= Date: Mon, 25 Apr 2016 15:51:12 +0800 Subject: [PATCH 5/5] add return ErrMultiRows --- orm/orm_queryset.go | 4 ++++ orm/orm_test.go | 2 ++ 2 files changed, 6 insertions(+) diff --git a/orm/orm_queryset.go b/orm/orm_queryset.go index f1aa45c8..575f62ae 100644 --- a/orm/orm_queryset.go +++ b/orm/orm_queryset.go @@ -200,6 +200,10 @@ func (o *querySet) One(container interface{}, cols ...string) error { if num == 0 { return ErrNoRows } + + if num > 1 { + return ErrMultiRows + } return nil } diff --git a/orm/orm_test.go b/orm/orm_test.go index ff7d2eae..d9fd7d51 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -975,11 +975,13 @@ func TestOne(t *testing.T) { err = qs.OrderBy("Id").Limit(1).One(&user) throwFailNow(t, err) throwFail(t, AssertIs(user.UserName, "slene")) + throwFail(t, AssertNot(err, ErrMultiRows)) user = User{} err = qs.OrderBy("-Id").Limit(100).One(&user) throwFailNow(t, err) throwFail(t, AssertIs(user.UserName, "nobody")) + throwFail(t, AssertNot(err, ErrMultiRows)) err = qs.Filter("user_name", "nothing").One(&user) throwFail(t, AssertIs(err, ErrNoRows))