mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
## 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)