feat(binding): add CustomDecimal type for parsing decimal numbers with leading dots
This commit is contained in:
parent
f482f25c71
commit
48f9924d13
@ -1,18 +1,28 @@
|
||||
package clause
|
||||
|
||||
// Limit limit clause
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Limit represents a limit clause
|
||||
type Limit struct {
|
||||
Limit *int
|
||||
Offset int
|
||||
}
|
||||
|
||||
// Name where clause name
|
||||
// Name returns the name of the clause ("LIMIT")
|
||||
func (limit Limit) Name() string {
|
||||
return "LIMIT"
|
||||
}
|
||||
|
||||
// Build build where clause
|
||||
// Build constructs the LIMIT clause
|
||||
func (limit Limit) Build(builder Builder) {
|
||||
// If only offset is defined and limit is nil, set limit to math.MaxInt
|
||||
if limit.Limit == nil && limit.Offset > 0 {
|
||||
maxInt := math.MaxInt
|
||||
limit.Limit = &maxInt
|
||||
}
|
||||
|
||||
if limit.Limit != nil && *limit.Limit >= 0 {
|
||||
builder.WriteString("LIMIT ")
|
||||
builder.AddVar(builder, *limit.Limit)
|
||||
@ -26,7 +36,7 @@ func (limit Limit) Build(builder Builder) {
|
||||
}
|
||||
}
|
||||
|
||||
// MergeClause merge order by clauses
|
||||
// MergeClause merges two limit clauses
|
||||
func (limit Limit) MergeClause(clause *Clause) {
|
||||
clause.Name = ""
|
||||
|
||||
|
@ -2,6 +2,7 @@ package clause_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
@ -36,9 +37,10 @@ func TestLimit(t *testing.T) {
|
||||
[]interface{}{limit0},
|
||||
},
|
||||
{
|
||||
// Updated test case: only Offset is given, so now we expect math.MaxInt as LIMIT
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Offset: 20}},
|
||||
"SELECT * FROM `users` OFFSET ?",
|
||||
[]interface{}{20},
|
||||
"SELECT * FROM `users` LIMIT ? OFFSET ?",
|
||||
[]interface{}{math.MaxInt, 20},
|
||||
},
|
||||
{
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Offset: 20}, clause.Limit{Offset: 30}},
|
||||
@ -70,6 +72,12 @@ func TestLimit(t *testing.T) {
|
||||
"SELECT * FROM `users` LIMIT ? OFFSET ?",
|
||||
[]interface{}{limit50, 30},
|
||||
},
|
||||
// New test example: if only Offset is defined, Limit should become math.MaxInt
|
||||
{
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Offset: 100}},
|
||||
"SELECT * FROM `users` LIMIT ? OFFSET ?",
|
||||
[]interface{}{math.MaxInt, 100},
|
||||
},
|
||||
}
|
||||
|
||||
for idx, result := range results {
|
||||
|
Loading…
x
Reference in New Issue
Block a user