From e583dfa196400896932c073d05383fcf6cedeb4f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 11 Sep 2020 11:44:58 +0800 Subject: [PATCH] Allow negative number for limit --- clause/limit.go | 4 +--- tests/go.mod | 2 +- tests/query_test.go | 3 ++- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/clause/limit.go b/clause/limit.go index 1946820d..2082f4d9 100644 --- a/clause/limit.go +++ b/clause/limit.go @@ -33,10 +33,8 @@ func (limit Limit) MergeClause(clause *Clause) { clause.Name = "" if v, ok := clause.Expression.(Limit); ok { - if limit.Limit == 0 && v.Limit > 0 { + if limit.Limit == 0 && v.Limit != 0 { limit.Limit = v.Limit - } else if limit.Limit < 0 { - limit.Limit = 0 } if limit.Offset == 0 && v.Offset > 0 { diff --git a/tests/go.mod b/tests/go.mod index f62365f8..17a3b156 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -8,7 +8,7 @@ require ( github.com/lib/pq v1.6.0 gorm.io/driver/mysql v1.0.1 gorm.io/driver/postgres v1.0.0 - gorm.io/driver/sqlite v1.1.1 + gorm.io/driver/sqlite v1.1.2 gorm.io/driver/sqlserver v1.0.4 gorm.io/gorm v1.20.0 ) diff --git a/tests/query_test.go b/tests/query_test.go index 36229e2c..d3bcbdbe 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -625,6 +625,7 @@ func TestLimit(t *testing.T) { {Name: "LimitUser3", Age: 20}, {Name: "LimitUser4", Age: 10}, {Name: "LimitUser5", Age: 20}, + {Name: "LimitUser6", Age: 20}, } DB.Create(&users) @@ -633,7 +634,7 @@ func TestLimit(t *testing.T) { DB.Order("age desc").Limit(3).Find(&users1).Limit(5).Find(&users2).Limit(-1).Find(&users3) if len(users1) != 3 || len(users2) != 5 || len(users3) <= 5 { - t.Errorf("Limit should works") + t.Errorf("Limit should works, users1 %v users2 %v users3 %v", len(users1), len(users2), len(users3)) } }