forked from mirror/gorm
Allow negative number for limit
This commit is contained in:
parent
b8a74a80d7
commit
e583dfa196
|
@ -33,10 +33,8 @@ func (limit Limit) MergeClause(clause *Clause) {
|
||||||
clause.Name = ""
|
clause.Name = ""
|
||||||
|
|
||||||
if v, ok := clause.Expression.(Limit); ok {
|
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
|
limit.Limit = v.Limit
|
||||||
} else if limit.Limit < 0 {
|
|
||||||
limit.Limit = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if limit.Offset == 0 && v.Offset > 0 {
|
if limit.Offset == 0 && v.Offset > 0 {
|
||||||
|
|
|
@ -8,7 +8,7 @@ require (
|
||||||
github.com/lib/pq v1.6.0
|
github.com/lib/pq v1.6.0
|
||||||
gorm.io/driver/mysql v1.0.1
|
gorm.io/driver/mysql v1.0.1
|
||||||
gorm.io/driver/postgres v1.0.0
|
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/driver/sqlserver v1.0.4
|
||||||
gorm.io/gorm v1.20.0
|
gorm.io/gorm v1.20.0
|
||||||
)
|
)
|
||||||
|
|
|
@ -625,6 +625,7 @@ func TestLimit(t *testing.T) {
|
||||||
{Name: "LimitUser3", Age: 20},
|
{Name: "LimitUser3", Age: 20},
|
||||||
{Name: "LimitUser4", Age: 10},
|
{Name: "LimitUser4", Age: 10},
|
||||||
{Name: "LimitUser5", Age: 20},
|
{Name: "LimitUser5", Age: 20},
|
||||||
|
{Name: "LimitUser6", Age: 20},
|
||||||
}
|
}
|
||||||
|
|
||||||
DB.Create(&users)
|
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)
|
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 {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue