feature: using default limite and offset query for count
This commit is contained in:
parent
b4166d9515
commit
c052d15bda
@ -6,6 +6,7 @@ import "strconv"
|
|||||||
type Limit struct {
|
type Limit struct {
|
||||||
Limit int
|
Limit int
|
||||||
Offset int
|
Offset int
|
||||||
|
Ignore bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name where clause name
|
// Name where clause name
|
||||||
@ -15,6 +16,9 @@ func (limit Limit) Name() string {
|
|||||||
|
|
||||||
// Build build where clause
|
// Build build where clause
|
||||||
func (limit Limit) Build(builder Builder) {
|
func (limit Limit) Build(builder Builder) {
|
||||||
|
if limit.Ignore {
|
||||||
|
return
|
||||||
|
}
|
||||||
if limit.Limit > 0 {
|
if limit.Limit > 0 {
|
||||||
builder.WriteString("LIMIT ")
|
builder.WriteString("LIMIT ")
|
||||||
builder.WriteString(strconv.Itoa(limit.Limit))
|
builder.WriteString(strconv.Itoa(limit.Limit))
|
||||||
|
@ -322,6 +322,9 @@ func (db *DB) Count(count *int64) (tx *DB) {
|
|||||||
defer tx.Statement.AddClause(clause.Select{})
|
defer tx.Statement.AddClause(clause.Select{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tx.Statement.AddClause(clause.Limit{Ignore: true})
|
||||||
|
defer tx.Statement.AddClause(clause.Limit{Ignore: false})
|
||||||
|
|
||||||
tx.Statement.Dest = count
|
tx.Statement.Dest = count
|
||||||
tx.callbacks.Query().Execute(tx)
|
tx.callbacks.Query().Execute(tx)
|
||||||
if tx.RowsAffected != 1 {
|
if tx.RowsAffected != 1 {
|
||||||
|
@ -72,4 +72,10 @@ func TestCount(t *testing.T) {
|
|||||||
if err := DB.Debug().Table("users").Joins("LEFT JOIN companies on companies.name = users.name").Where("users.name = ?", user1.Name).Count(&count4).Error; err != nil || count4 != 1 {
|
if err := DB.Debug().Table("users").Joins("LEFT JOIN companies on companies.name = users.name").Where("users.name = ?", user1.Name).Count(&count4).Error; err != nil || count4 != 1 {
|
||||||
t.Errorf("count with join, got error: %v, count %v", err, count)
|
t.Errorf("count with join, got error: %v, count %v", err, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var count5 int64
|
||||||
|
DB.Where("name in ?", []string{user2.Name, user3.Name}).Limit(1).Offset(1).Find(&users).Count(&count5)
|
||||||
|
if len(users) != 1 || count5 != 2 {
|
||||||
|
t.Errorf("count for pagination should works")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user