From 48f9924d13dd216e8ad104d511ecc9f58cd03f36 Mon Sep 17 00:00:00 2001 From: aydinomer00 <109145643+aydinomer00@users.noreply.github.com> Date: Sat, 4 Jan 2025 21:11:05 +0300 Subject: [PATCH] feat(binding): add CustomDecimal type for parsing decimal numbers with leading dots --- clause/limit.go | 18 ++++++++++++++---- clause/limit_test.go | 12 ++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/clause/limit.go b/clause/limit.go index 3edde434..99e51cf5 100644 --- a/clause/limit.go +++ b/clause/limit.go @@ -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 = "" diff --git a/clause/limit_test.go b/clause/limit_test.go index 96a7e7e6..36b89b8d 100644 --- a/clause/limit_test.go +++ b/clause/limit_test.go @@ -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 {