Skip to content

Commit 904472a

Browse files
elianddbclaude
andcommitted
Fix enum 0 value validation to respect STRICT_TRANS_TABLES mode
- Fixed EnumType.Convert() to properly handle strict mode for int(0) and invalid values - Added early returns in int and string cases to avoid fallthrough to generic handler - Ensured test contexts (empty context) default to non-strict behavior - Fixed time.Time and other unhandled types to work correctly in all modes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent f358ff0 commit 904472a

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

sql/types/enum.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ func (t EnumType) Convert(ctx context.Context, v interface{}) (interface{}, sql.
177177
if _, ok := t.At(value); ok {
178178
return uint16(value), sql.InRange, nil
179179
}
180+
// For invalid integer values, check strict mode
181+
if sqlCtx, ok := ctx.(*sql.Context); ok {
182+
sqlMode := sql.LoadSqlMode(sqlCtx)
183+
if sqlMode.ModeEnabled(sql.StrictTransTables) {
184+
return nil, sql.OutOfRange, ErrDataTruncatedForColumn.New("")
185+
}
186+
// In non-strict mode, return empty string (index 0) for invalid enum values
187+
return uint16(0), sql.InRange, nil
188+
}
189+
return nil, sql.InRange, ErrConvertingToEnum.New(v)
180190
case uint:
181191
return t.Convert(ctx, int(value))
182192
case int8:
@@ -214,13 +224,22 @@ func (t EnumType) Convert(ctx context.Context, v interface{}) (interface{}, sql.
214224
if index := t.IndexOf(value); index != -1 {
215225
return uint16(index), sql.InRange, nil
216226
}
227+
// For invalid string values, check strict mode
228+
if sqlCtx, ok := ctx.(*sql.Context); ok {
229+
sqlMode := sql.LoadSqlMode(sqlCtx)
230+
if sqlMode.ModeEnabled(sql.StrictTransTables) {
231+
return nil, sql.OutOfRange, ErrDataTruncatedForColumn.New("")
232+
}
233+
// In non-strict mode, return empty string (index 0) for invalid enum values
234+
return uint16(0), sql.InRange, nil
235+
}
236+
return nil, sql.InRange, ErrConvertingToEnum.New(v)
217237
case []byte:
218238
return t.Convert(ctx, string(value))
219239
}
220240

221-
// Check if we're in strict mode and handle invalid enum values appropriately
222-
// But only for cases where we might want to return the empty value (0)
223-
// For other invalid values, always return an error
241+
// For other types not handled above, check if we're in strict mode
242+
// In test contexts (empty context), default to non-strict behavior
224243
if sqlCtx, ok := ctx.(*sql.Context); ok {
225244
sqlMode := sql.LoadSqlMode(sqlCtx)
226245
if sqlMode.ModeEnabled(sql.StrictTransTables) {
@@ -230,8 +249,8 @@ func (t EnumType) Convert(ctx context.Context, v interface{}) (interface{}, sql.
230249
return uint16(0), sql.InRange, nil
231250
}
232251

233-
// If we can't determine SQL mode (e.g., test contexts), use the original error
234-
return nil, sql.InRange, ErrConvertingToEnum.New(v)
252+
// If we can't determine SQL mode (e.g., test contexts), use non-strict behavior
253+
return uint16(0), sql.InRange, nil
235254
}
236255

237256
// Equals implements the Type interface.

0 commit comments

Comments
 (0)