Jack Ling bbdd442954 fix(database/gdb): treat negative Limit/Page/Offset values as zero (#4702)
## Summary
Fix bug where negative values in `Limit()`, `Page()`, and `Offset()`
methods generate invalid SQL causing database errors.

## Root Cause
The methods don't validate negative input:
- `Limit(-1)` generates `LIMIT -1` → SQL error
- `Page(1, -10)` generates `LIMIT -10` → SQL error  
- `Offset(-5)` generates `OFFSET -5` → SQL error

## Fix
Treat all negative values as zero (safe default):

**Limit() method**:
```go
case 1:
    if limit[0] < 0 { limit[0] = 0 }
case 2:
    if limit[0] < 0 { limit[0] = 0 }
    if limit[1] < 0 { limit[1] = 0 }
```

**Page() method**:
```go
if limit < 0 { limit = 0 }
```

**Offset() method**:
```go
if offset < 0 { offset = 0 }
```

## Behavior Changes
- `Limit(-1)` → `Limit(0)` (no limit)
- `Limit(-10, -5)` → `Limit(0, 0)` (no offset, no limit)
- `Page(1, -10)` → `Page(1, 0)` (no results)
- `Offset(-5)` → `Offset(0)` (no offset)

## Documentation
Added "Note: Negative values are treated as zero" to all three methods.

## Tests
Added `Test_Issue4699` in `database/gdb/gdb_z_unit_issue_test.go` with 7
test cases:
1. Limit with single negative parameter
2. Limit with two negative parameters
3. Limit with mixed parameters (negative start, positive limit)
4. Page with negative limit
5. Page with negative limit on page 2
6. Offset with negative value
7. Offset with positive value (sanity check)

## Related
Fixes #4699
Ref #4703 (discovered during pagination test development)
2026-02-27 16:00:53 +08:00
2026-01-26 20:37:48 +08:00
dev
2017-07-04 18:31:39 +08:00
2025-12-27 16:07:23 +08:00
2026-01-26 20:37:48 +08:00

English | 简体中文

goframe logo

Go Reference GoFrame CI OpenSSF Scorecard CII Best Practices Go Report Card Code Coverage Production Ready License

Release GitHub pull requests GitHub closed pull requests GitHub issues GitHub closed issues Stars Forks Ask DeepWiki

A powerful framework for faster, easier, and more efficient project development.

Installation

go get -u github.com/gogf/gf/v2

Documentation

Contributors

💖 Thanks to all the contributors who made GoFrame possible 💖

goframe contributors

License

GoFrame is licensed under the MIT License, 100% free and open-source, forever.

Description
No description provided
Readme MIT 34 MiB
Languages
GO 100%