2020-02-07 18:45:35 +03:00
|
|
|
package clause_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm/clause"
|
2020-02-07 18:45:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLimit(t *testing.T) {
|
2022-10-07 15:14:14 +03:00
|
|
|
limit0 := 0
|
|
|
|
limit10 := 10
|
|
|
|
limit50 := 50
|
|
|
|
limitNeg10 := -10
|
2020-02-07 18:45:35 +03:00
|
|
|
results := []struct {
|
|
|
|
Clauses []clause.Interface
|
|
|
|
Result string
|
|
|
|
Vars []interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{
|
2022-10-07 15:14:14 +03:00
|
|
|
Limit: &limit10,
|
2020-02-07 18:45:35 +03:00
|
|
|
Offset: 20,
|
|
|
|
}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` LIMIT ? OFFSET ?",
|
|
|
|
[]interface{}{limit10, 20},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
2022-10-07 15:14:14 +03:00
|
|
|
{
|
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Limit: &limit0}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` LIMIT ?",
|
|
|
|
[]interface{}{limit0},
|
2022-10-07 15:14:14 +03:00
|
|
|
},
|
2023-04-11 05:25:47 +03:00
|
|
|
{
|
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Limit: &limit0}, clause.Limit{Offset: 0}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` LIMIT ?",
|
|
|
|
[]interface{}{limit0},
|
2023-04-11 05:25:47 +03:00
|
|
|
},
|
2020-07-02 13:14:33 +03:00
|
|
|
{
|
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Offset: 20}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` OFFSET ?",
|
|
|
|
[]interface{}{20},
|
2020-07-02 13:14:33 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Offset: 20}, clause.Limit{Offset: 30}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` OFFSET ?",
|
|
|
|
[]interface{}{30},
|
2020-07-02 13:14:33 +03:00
|
|
|
},
|
|
|
|
{
|
2022-10-07 15:14:14 +03:00
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Offset: 20}, clause.Limit{Limit: &limit10}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` LIMIT ? OFFSET ?",
|
|
|
|
[]interface{}{limit10, 20},
|
2020-07-02 13:14:33 +03:00
|
|
|
},
|
2020-02-07 18:45:35 +03:00
|
|
|
{
|
2022-10-07 15:14:14 +03:00
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Limit: &limit10, Offset: 20}, clause.Limit{Offset: 30}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` LIMIT ? OFFSET ?",
|
|
|
|
[]interface{}{limit10, 30},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
{
|
2022-10-07 15:14:14 +03:00
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Limit: &limit10, Offset: 20}, clause.Limit{Offset: 30}, clause.Limit{Offset: -10}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` LIMIT ?",
|
|
|
|
[]interface{}{limit10},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
{
|
2022-10-07 15:14:14 +03:00
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Limit: &limit10, Offset: 20}, clause.Limit{Offset: 30}, clause.Limit{Limit: &limitNeg10}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` OFFSET ?",
|
|
|
|
[]interface{}{30},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
{
|
2022-10-07 15:14:14 +03:00
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Limit: &limit10, Offset: 20}, clause.Limit{Offset: 30}, clause.Limit{Limit: &limit50}},
|
2024-02-06 05:54:40 +03:00
|
|
|
"SELECT * FROM `users` LIMIT ? OFFSET ?",
|
|
|
|
[]interface{}{limit50, 30},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, result := range results {
|
|
|
|
t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) {
|
|
|
|
checkBuildClauses(t, result.Clauses, result.Result, result.Vars)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|