Skip to content

Commit e0c032d

Browse files
authored
fix(database/gdb): handle empty string in Fields() gracefully (#4700)
## Summary Fix bug where `Fields("")` with empty string generates invalid SQL `SELECT FROM table`. ## Root Cause `mappingAndFilterToTableFields` method doesn't skip empty strings when processing fields: - `gstr.SplitAndTrim("", ",")` returns empty array - No fields added to query - Results in invalid SQL: `SELECT FROM table` ## Fix Skip empty string fields in `mappingAndFilterToTableFields` (line 97-100): ```go // Skip empty string fields if fieldStr == "" { continue } ``` ## Behavior Changes - `Fields("")` → SELECT * FROM table (uses default) - `Fields("", "id")` → SELECT id FROM table (ignores empty string) - `Fields("id", "", "nickname")` → SELECT id, nickname FROM table ## Tests Added `Test_Issue4697` with 3 scenarios covering all cases above. ## Related Fixes #4697 Ref #4703 (discovered during pagination test development)
1 parent 063264e commit e0c032d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

contrib/drivers/mysql/mysql_z_unit_issue_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,3 +1949,37 @@ func Test_Issue4500(t *testing.T) {
19491949
t.Assert(len(all), 3)
19501950
})
19511951
}
1952+
1953+
// https://github.com/gogf/gf/issues/4697
1954+
func Test_Issue4697(t *testing.T) {
1955+
table := createInitTable()
1956+
defer dropTable(table)
1957+
1958+
gtest.C(t, func(t *gtest.T) {
1959+
// Fields("") should be treated as Fields() and select all fields
1960+
result, err := db.Model(table).Fields("").Limit(1).All()
1961+
t.AssertNil(err)
1962+
t.AssertGT(len(result), 0)
1963+
// Should have all fields (id, passport, password, nickname, create_time, create_date)
1964+
t.Assert(len(result[0]), 6)
1965+
})
1966+
1967+
gtest.C(t, func(t *gtest.T) {
1968+
// Fields("", "id") should ignore empty string and only select "id"
1969+
result, err := db.Model(table).Fields("", "id").Limit(1).All()
1970+
t.AssertNil(err)
1971+
t.AssertGT(len(result), 0)
1972+
t.Assert(len(result[0]), 1)
1973+
t.AssertNE(result[0]["id"], nil)
1974+
})
1975+
1976+
gtest.C(t, func(t *gtest.T) {
1977+
// Fields("id", "", "nickname") should ignore empty string
1978+
result, err := db.Model(table).Fields("id", "", "nickname").Limit(1).All()
1979+
t.AssertNil(err)
1980+
t.AssertGT(len(result), 0)
1981+
t.Assert(len(result[0]), 2)
1982+
t.AssertNE(result[0]["id"], nil)
1983+
t.AssertNE(result[0]["nickname"], nil)
1984+
})
1985+
}

database/gdb/gdb_model_utility.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func (m *Model) mappingAndFilterToTableFields(table string, fields []any, filter
9494
fieldStr = gconv.String(field)
9595
inputFieldsArray []string
9696
)
97+
// Skip empty string fields.
98+
if fieldStr == "" {
99+
continue
100+
}
97101
switch {
98102
case gregex.IsMatchString(regularFieldNameWithoutDotRegPattern, fieldStr):
99103
inputFieldsArray = append(inputFieldsArray, fieldStr)

0 commit comments

Comments
 (0)