feat(binding): add CustomDecimal type for parsing decimal numbers with leading dots

This commit is contained in:
aydinomer00 2025-01-04 21:11:05 +03:00
parent f482f25c71
commit 48f9924d13
2 changed files with 24 additions and 6 deletions

View File

@ -1,18 +1,28 @@
package clause package clause
// Limit limit clause import (
"math"
)
// Limit represents a limit clause
type Limit struct { type Limit struct {
Limit *int Limit *int
Offset int Offset int
} }
// Name where clause name // Name returns the name of the clause ("LIMIT")
func (limit Limit) Name() string { func (limit Limit) Name() string {
return "LIMIT" return "LIMIT"
} }
// Build build where clause // Build constructs the LIMIT clause
func (limit Limit) Build(builder Builder) { 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 { if limit.Limit != nil && *limit.Limit >= 0 {
builder.WriteString("LIMIT ") builder.WriteString("LIMIT ")
builder.AddVar(builder, *limit.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) { func (limit Limit) MergeClause(clause *Clause) {
clause.Name = "" clause.Name = ""

View File

@ -2,6 +2,7 @@ package clause_test
import ( import (
"fmt" "fmt"
"math"
"testing" "testing"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
@ -36,9 +37,10 @@ func TestLimit(t *testing.T) {
[]interface{}{limit0}, []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}}, []clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Offset: 20}},
"SELECT * FROM `users` OFFSET ?", "SELECT * FROM `users` LIMIT ? OFFSET ?",
[]interface{}{20}, []interface{}{math.MaxInt, 20},
}, },
{ {
[]clause.Interface{clause.Select{}, clause.From{}, clause.Limit{Offset: 20}, clause.Limit{Offset: 30}}, []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 ?", "SELECT * FROM `users` LIMIT ? OFFSET ?",
[]interface{}{limit50, 30}, []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 { for idx, result := range results {